w11 - cpp 0.794
Backend server for Rlink and w11
Loading...
Searching...
No Matches
RtclStats.cpp
Go to the documentation of this file.
1// $Id: RtclStats.cpp 1186 2019-07-12 17:49:59Z mueller $
2// SPDX-License-Identifier: GPL-3.0-or-later
3// Copyright 2011-2019 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
4//
5// Revision History:
6// Date Rev Version Comment
7// 2019-06-07 1160 1.1 Rename Collect->Exec, not longer const; add -reset
8// 2014-08-22 584 1.0.2 use nullptr
9// 2013-03-06 495 1.0.1 Rename Exec->Collect
10// 2011-02-26 364 1.0 Initial version
11// 2011-02-20 363 0.1 First draft
12// ---------------------------------------------------------------------------
13
18#include <sstream>
19
20#include "RtclStats.hpp"
21#include "RtclNameSet.hpp"
22#include "RtclOPtr.hpp"
23
24using namespace std;
25
31// all method definitions in namespace Retro
32namespace Retro {
33
34//------------------------------------------+-----------------------------------
36
38{
39 static RtclNameSet optset("-lname|-ltext|-lvalue|-lpair|-lall|"
40 "-atext|-avalue|-print|-reset");
41
42 string opt;
43 string varname;
44 string format;
45 int width=0;
46 int prec=0;
47
48 if (args.NextOpt(opt, optset)) {
49 if (opt == "-atext" || opt == "-avalue") {
50 if (!args.GetArg("varName", varname)) return false;
51 } else if (opt == "-print") {
52 if (!args.GetArg("?format", format)) return false;
53 if (!args.GetArg("?width", width, 0, 32)) return false;
54 if (!args.GetArg("?prec", prec, 0, 32)) return false;
55 }
56
57 } else {
58 opt = "-print";
59 width = 12;
60 }
61 if (!args.AllDone()) return false;
62
63 cntx.opt = opt;
64 cntx.varname = varname;
65 cntx.format = format;
66 cntx.width = width;
67 cntx.prec = prec;
68
69 return true;
70}
71
72//------------------------------------------+-----------------------------------
74
75bool RtclStats::Exec(RtclArgs& args, const Context& cntx, Rstats& stats)
76{
77 Tcl_Interp* interp = args.Interp();
78 Tcl_Obj* plist = Tcl_GetObjResult(interp);
79
80 if (cntx.opt == "-lname") { // -lname -------------------------
81 for (size_t i=0; i<stats.Size(); i++) {
82 const string& name(stats.Name(i));
83 RtclOPtr pobj(Tcl_NewStringObj(name.data(), name.length()));
84 if (Tcl_ListObjAppendElement(interp, plist, pobj) != TCL_OK) return false;
85 }
86
87 } else if (cntx.opt == "-ltext") { // -ltext -------------------------
88 for (size_t i=0; i<stats.Size(); i++) {
89 const string& text(stats.Text(i));
90 RtclOPtr pobj(Tcl_NewStringObj(text.data(), text.length()));
91 if (Tcl_ListObjAppendElement(interp, plist, pobj) != TCL_OK) return false;
92 }
93
94 } else if (cntx.opt == "-lvalue") { // -lvalue ------------------------
95 for (size_t i=0; i<stats.Size(); i++) {
96 RtclOPtr pobj(Tcl_NewDoubleObj(stats.Value(i)));
97 if (Tcl_ListObjAppendElement(interp, plist, pobj) != TCL_OK) return false;
98 }
99
100 } else if (cntx.opt == "-lpair" || cntx.opt == "-lall") { // -lpair -lall ---
101 for (size_t i=0; i<stats.Size(); i++) {
102 const string& name(stats.Name(i));
103 RtclOPtr ptup(Tcl_NewListObj(0,nullptr));
104 Tcl_ListObjAppendElement(nullptr, ptup,
105 Tcl_NewDoubleObj(stats.Value(i)));
106 Tcl_ListObjAppendElement(nullptr, ptup,
107 Tcl_NewStringObj(name.data(), name.length()));
108 if (cntx.opt == "-lall") {
109 const string& text(stats.Text(i));
110 Tcl_ListObjAppendElement(nullptr, ptup,
111 Tcl_NewStringObj(text.data(), text.length()));
112 }
113 if (Tcl_ListObjAppendElement(interp, plist, ptup) != TCL_OK) return false;
114 }
115
116 } else if (cntx.opt == "-atext") { // -atext -------------------------
117 for (size_t i=0; i<stats.Size(); i++) {
118 const string& text(stats.Text(i));
119 RtclOPtr pobj(Tcl_NewStringObj(text.data(), text.length()));
120 if (!Tcl_SetVar2Ex(interp, cntx.varname.c_str(), stats.Name(i).c_str(),
121 pobj, TCL_LEAVE_ERR_MSG)) return false;
122 }
123
124 } else if (cntx.opt == "-avalue") { // -avalue ------------------------
125 for (size_t i=0; i<stats.Size(); i++) {
126 RtclOPtr pobj(Tcl_NewDoubleObj(stats.Value(i)));
127 if (!Tcl_SetVar2Ex(interp, cntx.varname.c_str(), stats.Name(i).c_str(),
128 pobj, TCL_LEAVE_ERR_MSG)) return false;
129 }
130
131 } else if (cntx.opt == "-print") { // -print -------------------------
132 ostringstream sos;
133 stats.Print(sos, cntx.format.c_str(), cntx.width, cntx.prec);
134 args.AppendResultLines(sos);
135
136 } else if (cntx.opt == "-reset") { // -reset -------------------------
137 stats.Reset();
138
139 } else {
140 args.AppendResult("-E: BUG! RtclStats::Collect: bad option '",
141 cntx.opt.c_str(), "'", nullptr);
142 return false;
143 }
144
145 return true;
146}
147
148} // end namespace Retro
FIXME_docs.
Definition: Rstats.hpp:28
size_t Size() const
FIXME_docs.
Definition: Rstats.ipp:38
void Print(std::ostream &os, const char *format=0, int width=0, int prec=0) const
FIXME_docs.
Definition: Rstats.cpp:157
const std::string & Name(size_t ind) const
FIXME_docs.
Definition: Rstats.ipp:54
double Value(size_t ind) const
FIXME_docs.
Definition: Rstats.ipp:46
const std::string & Text(size_t ind) const
FIXME_docs.
Definition: Rstats.ipp:62
void Reset()
FIXME_docs.
Definition: Rstats.cpp:105
FIXME_docs.
Definition: RtclArgs.hpp:41
bool NextOpt(std::string &val)
FIXME_docs.
Definition: RtclArgs.cpp:368
void AppendResultLines(const std::string &str)
FIXME_docs.
Definition: RtclArgs.cpp:484
bool GetArg(const char *name, Tcl_Obj *&pval)
FIXME_docs.
Definition: RtclArgs.cpp:114
void AppendResult(const char *str,...)
FIXME_docs.
Definition: RtclArgs.cpp:471
Tcl_Interp * Interp() const
FIXME_docs.
Definition: RtclArgs.ipp:28
bool AllDone()
FIXME_docs.
Definition: RtclArgs.cpp:447
Implemenation (inline) of RtclOPtr.
Definition: RtclOPtr.hpp:23
static bool Exec(RtclArgs &args, const Context &cntx, Rstats &stats)
FIXME_docs.
Definition: RtclStats.cpp:75
static bool GetArgs(RtclArgs &args, Context &cntx)
FIXME_docs.
Definition: RtclStats.cpp:37
Declaration of class ReventLoop.
Definition: ReventLoop.cpp:47