DABC (Data Acquisition Backbone Core)  2.9.9
Parameter.h
Go to the documentation of this file.
1 // $Id: Parameter.h 4472 2020-04-15 13:33:18Z linev $
2 
3 /************************************************************
4  * The Data Acquisition Backbone Core (DABC) *
5  ************************************************************
6  * Copyright (C) 2009 - *
7  * GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
8  * Planckstr. 1, 64291 Darmstadt, Germany *
9  * Contact: http://dabc.gsi.de *
10  ************************************************************
11  * This software can be used under the GPL license *
12  * agreements as stated in LICENSE.txt file *
13  * which is part of the distribution. *
14  ************************************************************/
15 
16 #ifndef DABC_Parameter
17 #define DABC_Parameter
18 
19 #ifndef DABC_Record
20 #include "dabc/Record.h"
21 #endif
22 
23 #ifndef DABC_timing
24 #include "dabc/timing.h"
25 #endif
26 
27 #ifndef DABC_threads
28 #include "dabc/threads.h"
29 #endif
30 
31 #include <cstring>
32 
33 namespace dabc {
34 
35  class Worker;
36  class Command;
37  class Manager;
38  class Parameter;
39 
40  enum EParamEvent {
41  parCreated = 0,
44  parDestroy = 3
45  };
46 
53  friend class Parameter;
54  friend class Worker;
55  friend class Manager;
56 
57  protected:
58 
60 
61  std::string fKind;
63  double fInterval;
64  bool fAsynchron;
67  double fRateValueSum;
68  double fRateTimeSum;
69  double fRateNumSum;
70  bool fMonitored;
71  bool fRecorded;
72  bool fWaitWorker;
75  int fRateWidth;
76  int fRatePrec;
77 
78 
79  virtual std::string DefaultFiledName() const;
80 
81  virtual bool HasField(const std::string &name) const
82  { LockGuard guard(ObjectMutex()); return RecordContainer::HasField(name); }
83 
84  virtual bool RemoveField(const std::string &name)
85  { LockGuard guard(ObjectMutex()); return RecordContainer::RemoveField(name); }
86 
87  virtual unsigned NumFields() const
88  { LockGuard guard(ObjectMutex()); return RecordContainer::NumFields(); }
89 
90  virtual std::string FieldName(unsigned cnt) const
91  { LockGuard guard(ObjectMutex()); return RecordContainer::FieldName(cnt); }
92 
93  virtual RecordField GetField(const std::string &name) const;
94 
95  virtual bool SetField(const std::string &name, const RecordField& v);
96 
101  void ProcessTimeout(double last_dif);
102 
103  ParameterContainer(Reference worker, const std::string &name, const std::string &parkind = "", bool hidden = false);
104  virtual ~ParameterContainer();
105 
106  inline void Modified() { FireParEvent(parModified); }
107 
109  virtual bool _CanChangeField(const std::string&) { return true; }
110 
111  void FireParEvent(int id);
112 
113  virtual void ObjectCleanup();
114 
115  bool _CalcRate(double& value, std::string& svalue);
116 
121  void SetSynchron(bool on, double interval = 1., bool everyevnt = false);
122 
123  Worker* GetWorker() const;
124 
125  int GetDebugLevel() const { return GetField("debug").AsInt(-1); }
126 
127  const std::string GetActualUnits() const;
128 
130  bool IsDeliverAllEvents() const;
131 
134  void FireModified(const std::string &svalue);
135 
137  virtual void BuildFieldsMap(RecordFieldsMap* cont);
138 
141  unsigned ConfirmFromWorker();
142 
143  public:
144 
145  virtual const char* ClassName() const { return "Parameter"; }
146 
147  const std::string &Kind() const;
148  };
149 
150  // _______________________________________________________________________
151 
163  class Parameter : public Record {
164 
165  friend class Worker;
166  friend class Manager;
167 
168  protected:
169 
170  virtual const char* ParReferenceKind() { return nullptr; }
171 
176  // These method is set parameter fields, specified in the command
177  int ExecuteChange(Command cmd);
178 
180  bool TakeAttrModified();
181 
183  void FireConfigured();
184 
186  template<class T>
187  bool verify_object(Object* src, T* &tgt)
188  {
189  // TODO: should we implement this method ???
190  tgt = dynamic_cast<T*>(src);
191  if (!tgt) return false;
192  const char* refkind = ParReferenceKind();
193  if (!refkind) return true;
194  return tgt->Kind() == refkind;
195  }
196 
197  public:
198 
200 
201 
202  RecordField Value() const { return GetField(""); }
203 
205  bool SetValue(const RecordField& v) { return SetField("", v); }
206 
209  bool Dflt(const RecordField& v) { return Value().null() ? SetValue(v) : false; }
210 
212  bool NeedTimeout();
213 
215  bool IsMonitored();
216 
218  Reference GetWorker() const;
219 
221  Parameter &SetSynchron(bool on, double interval = 1., bool everyevnt = false);
222 
225  Parameter &SetRatemeter(bool synchron = false, double interval = 1.0);
226 
228  bool IsRatemeter() const;
229 
232 
236  Parameter &SetAverage(bool synchron = false, double interval = 1.0);
237 
240 
242  bool IsAverage() const;
243 
245  Parameter &SetMonitored(bool on = true);
246 
248  Parameter &SetDebugOutput(bool on = true, int level = 1) { return SetDebugLevel(on ? level : -1); }
249  Parameter &SetDebugLevel(int level = 1) { SetField("debug", level); return *this; }
250  int GetDebugLevel() const;
251 
253  Parameter &SetWidthPrecision(unsigned width, unsigned prec);
254 
256  Parameter &SetUnits(const std::string &unit) { SetField("units", unit); return *this; }
257 
259  const std::string GetUnits() const { return GetField("units").AsStr(); }
260 
262  const std::string GetActualUnits() const;
263 
264  void SetLowerLimit(double low) { SetField("low", low); }
265  double GetLowerLimit() const { return GetField("low").AsDouble(); }
266 
267  void SetUpperLimit(double up) { SetField("up", up); }
268  double GetUpperLimit() const { return GetField("up").AsDouble(); }
269 
270  Parameter& SetLimits(double low, double up) { SetLowerLimit(low); SetUpperLimit(up); return *this; }
271 
272  Parameter &SetFld(const std::string &name, const RecordField &v) { SetField(name, v); return *this; }
273 
274  const std::string Kind() const;
275 
279  void FireModified();
280 
282  {
283  if (GetObject())
284  GetObject()->BuildFieldsMap(cont);
285  }
286 
287  bool SubmitSetValue(const RecordField& v);
288  };
289 
290  // ___________________________________________________________________________________
291 
300  class InfoParameter : public Parameter {
301 
302  protected:
303  // by this we indicate that only parameter specified as info can be referenced
304  virtual const char* ParReferenceKind() { return infokind(); }
305 
306  public:
307 
309 
310  void SetInfo(const std::string &info) { SetValue(info); }
311  std::string GetInfo() const { return Value().AsStr(); }
312 
313  void SetColor(const std::string &name) { SetField("color", name); }
314  std::string GetColor() const { return GetField("color").AsStr("Green"); }
315 
316  void SetVerbosity(int level) { SetField("verbosity", level); }
317  int GetVerbosity() const { return GetField("verbosity").AsInt(1); }
318 
319  static const char* infokind() { return "info"; }
320  };
321 
322  // _______________________________________________________________________________
323 
333  class CommandDefinition : public Record {
334 
335  protected:
336  // by this we indicate that only parameter specified as info can be referenced
337 
338  std::string ArgName(int n) const;
339 
340  public:
341 
343 
344  CommandDefinition& AddArg(const std::string &name, const std::string &kind = "string", bool required = true, const RecordField& dflt = RecordField());
345 
346  CommandDefinition& SetArgMinMax(const std::string &name, const RecordField& min, const RecordField& max);
347 
348  int NumArgs() const;
349 
350  int FindArg(const std::string &name) const;
351 
352  bool HasArg(const std::string &name) const { return FindArg(name) >=0; }
353 
354  bool GetArg(int n, std::string& name, std::string& kind, bool& required, std::string& dflt) const;
355 
358  Command MakeCommand() const;
359  };
360 
361 }
362 
363 #endif
#define DABC_REFERENCE(RefClass, ParentClass, T)
Definition: Reference.h:222
Command definition class.
Definition: Parameter.h:333
int FindArg(const std::string &name) const
Definition: Parameter.cxx:584
CommandDefinition & SetArgMinMax(const std::string &name, const RecordField &min, const RecordField &max)
Definition: Parameter.cxx:618
bool HasArg(const std::string &name) const
Definition: Parameter.h:352
std::string ArgName(int n) const
Definition: Parameter.cxx:579
Command MakeCommand() const
Create command according command definition, all default and required parameters will be specified.
Definition: Parameter.cxx:656
bool GetArg(int n, std::string &name, std::string &kind, bool &required, std::string &dflt) const
Definition: Parameter.cxx:632
CommandDefinition & AddArg(const std::string &name, const std::string &kind="string", bool required=true, const RecordField &dflt=RecordField())
Definition: Parameter.cxx:593
Represents command with its arguments.
Definition: Command.h:99
Special info parameter class.
Definition: Parameter.h:300
void SetVerbosity(int level)
Definition: Parameter.h:316
virtual const char * ParReferenceKind()
Definition: Parameter.h:304
void SetInfo(const std::string &info)
Definition: Parameter.h:310
static const char * infokind()
Definition: Parameter.h:319
std::string GetInfo() const
Definition: Parameter.h:311
void SetColor(const std::string &name)
Definition: Parameter.h:313
int GetVerbosity() const
Definition: Parameter.h:317
std::string GetColor() const
Definition: Parameter.h:314
Lock guard for posix mutex.
Definition: threads.h:127
Manager of everything in DABC
Definition: Manager.h:291
Base class for most of the DABC classes.
Definition: Object.h:116
Mutex * ObjectMutex() const
Returns mutex, used for protection of Object data members.
Definition: Object.h:190
virtual void BuildFieldsMap(RecordFieldsMap *cont)
Fill fields map, which is relevant for the object Objects hierarchy produced from dabc::Manager.
Definition: Object.h:428
Container for parameter object.
Definition: Parameter.h:52
void SetSynchron(bool on, double interval=1., bool everyevnt=false)
Specifies that parameter produce 'modified' events synchronous with changes of parameter.
Definition: Parameter.cxx:283
double fInterval
how often modified events are produced TODO: should we move it in the normal record field?
Definition: Parameter.h:63
virtual void ObjectCleanup()
User method to cleanup object content before it will be destroyed Main motivation is to release any r...
Definition: Parameter.cxx:209
bool fDeliverAllEvents
if true, any modification event will be delivered, default off
Definition: Parameter.h:74
unsigned ConfirmFromWorker()
Get confirmation from worker, which monitor parameters changes.
Definition: Parameter.cxx:179
const std::string GetActualUnits() const
Definition: Parameter.cxx:54
int fRateWidth
display width of rate variable
Definition: Parameter.h:75
double fRateValueSum
sum of values
Definition: Parameter.h:67
virtual void BuildFieldsMap(RecordFieldsMap *cont)
Save parameter attributes into container.
Definition: Parameter.cxx:311
TimeStamp fLastChangeTm
last time when parameter was modified
Definition: Parameter.h:62
int fRatePrec
display precision of rate variable
Definition: Parameter.h:76
void ProcessTimeout(double last_dif)
Method called from manager thread when parameter configured as asynchronous.
Definition: Parameter.cxx:216
virtual std::string FieldName(unsigned cnt) const
Definition: Parameter.h:90
bool fMonitored
if true parameter change event will be delivered to the worker
Definition: Parameter.h:70
virtual ~ParameterContainer()
Definition: Parameter.cxx:46
ParameterContainer(Reference worker, const std::string &name, const std::string &parkind="", bool hidden=false)
Definition: Parameter.cxx:23
Worker * GetWorker() const
Definition: Parameter.cxx:294
bool fAsynchron
indicates if parameter can produce events asynchronous to the modification of parameter itself it is ...
Definition: Parameter.h:64
bool IsDeliverAllEvents() const
If true, all events must be delivered to the consumer.
Definition: Parameter.cxx:67
virtual const char * ClassName() const
Returns class name of the object instance.
Definition: Parameter.h:145
virtual bool HasField(const std::string &name) const
Definition: Parameter.h:81
void FireModified(const std::string &svalue)
Internal method, used to inform system that parameter is modified If configured, also debug output wi...
Definition: Parameter.cxx:191
int GetDebugLevel() const
Definition: Parameter.h:125
double fRateNumSum
sum of accumulated counts
Definition: Parameter.h:69
virtual std::string DefaultFiledName() const
Definition: Parameter.cxx:74
bool fRecorded
if true, parameter changes should be reported to worker where it will be recorded
Definition: Parameter.h:71
bool fWaitWorker
if true, waiting confirmation from worker
Definition: Parameter.h:72
const std::string & Kind() const
Definition: Parameter.cxx:305
virtual RecordField GetField(const std::string &name) const
Definition: Parameter.cxx:80
virtual bool RemoveField(const std::string &name)
Definition: Parameter.h:84
virtual unsigned NumFields() const
Definition: Parameter.h:87
void FireParEvent(int id)
Definition: Parameter.cxx:202
virtual bool SetField(const std::string &name, const RecordField &v)
Definition: Parameter.cxx:92
bool _CalcRate(double &value, std::string &svalue)
Definition: Parameter.cxx:263
bool fAttrModified
indicate if attribute was modified since last parameter event
Definition: Parameter.h:73
virtual bool _CanChangeField(const std::string &)
Method allows in derived classes to block changes of some fields.
Definition: Parameter.h:109
double fRateTimeSum
sum of time
Definition: Parameter.h:68
EStatistic fStatistic
indicates if statistic is calculated: 0 - off, 1 - rate, 2 - average
Definition: Parameter.h:66
std::string fKind
specified kind of parameter (int, double, info, ratemeter), to be used by reference to decide if obje...
Definition: Parameter.h:61
Parameter class
Definition: Parameter.h:163
bool TakeAttrModified()
Returns true if any parameter attribute was modified since last call to this method.
Definition: Parameter.cxx:346
Parameter & SetMonitored(bool on=true)
Specify if parameter event should be delivered to the worker.
Definition: Parameter.cxx:524
Parameter & DisableRatemeter()
Disable ratemeter functionality.
Definition: Parameter.cxx:394
bool Dflt(const RecordField &v)
Set default parameter value.
Definition: Parameter.h:209
void FireConfigured()
Fire parConfigured event for parameter.
Definition: Parameter.cxx:550
bool SetValue(const RecordField &v)
Set parameter value.
Definition: Parameter.h:205
Parameter & SetWidthPrecision(unsigned width, unsigned prec)
Set parameter to convert double values to the string - used for ratemeter.
Definition: Parameter.cxx:420
RecordField Value() const
Returns parameter value.
Definition: Parameter.h:202
double GetUpperLimit() const
Definition: Parameter.h:268
Parameter & SetAverage(bool synchron=false, double interval=1.0)
Converts parameter in statistic variable.
Definition: Parameter.cxx:434
int ExecuteChange(Command cmd)
Specifies that parameter produce 'modified' events synchronous with changes of parameter.
Definition: Parameter.cxx:508
void ScanParamFields(RecordFieldsMap *cont)
Definition: Parameter.h:281
Parameter & SetLimits(double low, double up)
Definition: Parameter.h:270
double GetLowerLimit() const
Definition: Parameter.h:265
bool SubmitSetValue(const RecordField &v)
Definition: Parameter.cxx:561
bool IsAverage() const
Returns true if average calculation is active.
Definition: Parameter.cxx:481
Parameter & SetFld(const std::string &name, const RecordField &v)
Definition: Parameter.h:272
bool IsMonitored()
Returns true when parameter event should be delivered to the worker.
Definition: Parameter.cxx:515
void SetLowerLimit(double low)
Definition: Parameter.h:264
const std::string GetActualUnits() const
Return actual units of parameter value, taking into account rate (1/s) unit when enabled.
Definition: Parameter.cxx:543
bool IsRatemeter() const
Returns true if rate measurement is activated.
Definition: Parameter.cxx:411
bool NeedTimeout()
Returns true if parameter object requires timeout processing.
Definition: Parameter.cxx:339
Parameter & SetUnits(const std::string &unit)
Set units field of parameter.
Definition: Parameter.h:256
const std::string GetUnits() const
Return units of parameter value.
Definition: Parameter.h:259
virtual const char * ParReferenceKind()
Definition: Parameter.h:170
int GetDebugLevel() const
Definition: Parameter.cxx:360
Parameter & SetRatemeter(bool synchron=false, double interval=1.0)
Converts parameter in ratemeter - all values will be summed up and divided on specified interval.
Definition: Parameter.cxx:365
Parameter & SetDebugOutput(bool on=true, int level=1)
Enable/disable debug output when parameter value is changed.
Definition: Parameter.h:248
Parameter & DisableAverage()
Disables averaging functionality.
Definition: Parameter.cxx:462
void SetUpperLimit(double up)
Definition: Parameter.h:267
Reference GetWorker() const
Returns reference on the worker.
Definition: Parameter.cxx:502
const std::string Kind() const
Definition: Parameter.cxx:536
Parameter & SetSynchron(bool on, double interval=1., bool everyevnt=false)
Indicate if parameter is should generate events synchron with code which modified it.
Definition: Parameter.cxx:491
Parameter & SetDebugLevel(int level=1)
Definition: Parameter.h:249
void FireModified()
Can be called by user to signal framework that parameter was modified.
Definition: Parameter.cxx:555
bool verify_object(Object *src, T *&tgt)
Method used in reference constructor/assignments to verify is object is suitable.
Definition: Parameter.h:187
Container for records fields.
Definition: Record.h:440
virtual bool HasField(const std::string &name) const
Definition: Record.h:460
virtual bool RemoveField(const std::string &name)
Definition: Record.h:463
virtual std::string FieldName(unsigned cnt) const
Definition: Record.h:469
virtual unsigned NumFields() const
Definition: Record.h:466
std::string AsStr(const std::string &dflt="") const
Definition: Record.cxx:749
int64_t AsInt(int64_t dflt=0) const
Definition: Record.cxx:501
double AsDouble(double dflt=0.) const
Definition: Record.cxx:549
bool null() const
Definition: Record.h:302
RecordField GetField(const std::string &name) const
Definition: Record.h:510
bool SetField(const std::string &name, const RecordField &v)
Definition: Record.h:516
Reference on the arbitrary object
Definition: Reference.h:73
Object * GetObject() const
Return pointer on the object.
Definition: Reference.h:129
Active object, which is working inside dabc::Thread.
Definition: Worker.h:116
Event manipulation API.
Definition: api.h:23
EParamEvent
Definition: Parameter.h:40
@ parConfigured
event only for manager, used to react on reconfiguration of parameter
Definition: Parameter.h:42
@ parCreated
produced once when parameter is created
Definition: Parameter.h:41
@ parModified
produced when parameter value modified. Either every change or after time interval (default = 1 sec)
Definition: Parameter.h:43
@ parDestroy
produced once when parameter is destroyed
Definition: Parameter.h:44
Class for acquiring and holding timestamps.
Definition: timing.h:40