cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mtsStateTable.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */
3 
4 /*
5  Author(s): Ankur Kapoor, Min Yang Jung, Peter Kazanzides
6  Created on: 2004-04-30
7 
8  (C) Copyright 2004-2014 Johns Hopkins University (JHU), All Rights Reserved.
9 
10 --- begin cisst license - do not edit ---
11 
12 This software is provided "as is" under an open source license, with
13 no warranty. The complete license can be found in license.txt and
14 http://www.cisst.org/cisst/license.txt.
15 
16 --- end cisst license ---
17 */
18 
19 
25 #ifndef _mtsStateTable_h
26 #define _mtsStateTable_h
27 
38 
39 
40 #include <vector>
41 #include <iostream>
42 
43 // Always include last
45 
46 // Forward declaration
48 
49 
52 typedef int mtsStateDataId;
53 
68 
70 
71  friend class mtsCollectorState;
72  friend class mtsComponent;
73  friend class mtsTaskTest;
74  friend class mtsStateTableTest;
75  friend class mtsCollectorBaseTest;
76 
77  public:
81  {
83  public:
86 
87  void ToStreamRaw(std::ostream & outputStream, const char delimiter = ' ',
88  bool headerOnly = false, const std::string & headerPrefix = "") const;
89  };
90 
95  public:
98  bool Collecting;
99 
103  double StartTime;
104 
108  double StopTime;
109 
112 
114  size_t BatchSize;
115 
117  size_t BatchCounter;
118 
121 
124 
127 
132 
136 
140 
143 
145  inline DataCollectionInfo(void):
146  Collecting(false),
147  StartTime(0.0),
148  StopTime(0.0),
149  BatchSize(0),
150  BatchCounter(0),
151  CounterForEvent(0)
152  {}
153 
155  };
156 
157 public:
158  class AccessorBase {
159  protected:
161  mtsStateDataId Id; // Not currently used
162  public:
163  AccessorBase(const mtsStateTable &table, mtsStateDataId id): Table(table), Id(id) {}
164  virtual ~AccessorBase() {}
165  virtual void ToStream(std::ostream & outputStream, const mtsStateIndex & when) const = 0;
166  };
167 
168  template <class _elementType>
169  class Accessor : public AccessorBase {
173  const mtsStateArray<value_type> & History;
174  value_ref_type * Current;
175 
176  public:
178  const mtsStateArray<value_type> * history, value_ref_type * data):
179  AccessorBase(table, id), History(*history), Current(data) {}
180 
181  void ToStream(std::ostream & outputStream, const mtsStateIndex & when) const {
182  History.Element(when.Index()).ToStream(outputStream);
183  }
184 
185  bool Get(const mtsStateIndex & when, value_type & data) const {
186  data = History.Element(when.Index());
187  return Table.ValidateReadIndex(when);
188  }
189 
190  //This should be used with caution because
191  //the state table mechanism could override the data that the pointer is pointing to.
192  const value_type * GetPointer(const mtsStateIndex & when) const {
193  if (!Table.ValidateReadIndex(when))
194  return 0;
195  else
196  return &(History.Element(when.Index()));
197  }
198 
199  bool Get(const mtsStateIndex & when, mtsGenericObject & data) const {
200  value_type* pdata = dynamic_cast<value_type*>(&data);
201  if (pdata) {
202  return Get(when, *pdata);
203  }
204  value_ref_type* pref = dynamic_cast<value_ref_type*>(&data);
205  if (pref) {
206  return Get(when, *pref);
207  }
208  return false;
209  }
210 
211  bool GetLatest(value_type & data) const {
212  return Get(Table.GetIndexReader(), data);
213  }
214  bool GetLatest(mtsGenericObject & data) const {
215  return Get(Table.GetIndexReader(), data);
216  }
217 
218  bool GetDelayed(value_type & data) const {
219  return Get(Table.GetIndexDelayed(), data);
220  }
221  bool GetDelayed(mtsGenericObject & data) const {
222  return Get(Table.GetIndexDelayed(), data);
223  }
224  void SetCurrent(const value_type & data) {
225  *Current = data;
226  }
227  };
228 
229  protected:
230 
233 
235  size_t IndexWriter;
236 
238  size_t IndexReader;
239 
241  size_t IndexDelayed;
242 
245  size_t Delay;
246 
255 
257  std::vector<mtsStateArrayBase *> StateVector;
258 
263  std::vector<mtsGenericObject *> StateVectorElements;
264 
267  std::vector<std::string> StateVectorDataNames;
268 
271  std::vector<AccessorBase *> StateVectorAccessors;
272 
275  std::vector<mtsStateIndex::TimeTicksType> Ticks;
276 
280 
283 
284 public:
285 
286  /* The start/end times for the current row of data. */
288 
292 
295 
296  protected:
299  double SumOfPeriods;
300 
303 
305  std::string Name;
306 
310 
312  bool Write(mtsStateDataId id, const mtsGenericObject & obj);
313 
314 
315  public:
318  mtsStateTable(size_t size, const std::string & name);
319 
321  ~mtsStateTable();
322 
324  bool SetSize(const size_t size);
325 
328  mtsStateIndex GetIndexReader(void) const;
329 
330  inline void GetIndexReader(mtsStateIndex & timeIndex) const {
331  timeIndex = GetIndexReader();
332  }
333 
337  mtsStateIndex GetIndexDelayed(void) const;
338 
340  size_t SetDelay(size_t newDelay);
341 
343  inline bool ValidateReadIndex(const mtsStateIndex &timeIndex) const {
344  return (Ticks[timeIndex.Index()] == timeIndex.Ticks());
345  }
346 
348  inline const bool & AutomaticAdvance(void) const {
349  return this->AutomaticAdvanceFlag;
350  }
351 
353  inline void SetAutomaticAdvance(bool automaticAdvance) {
354  this->AutomaticAdvanceFlag = automaticAdvance;
355  }
356 
358  int GetStateVectorID(const std::string & dataName) const;
359 
364  template <class _elementType>
365  mtsStateDataId NewElement(const std::string & name = "", _elementType * element = 0);
366 
368  template <class _elementType>
369  void AddData(_elementType & element, const std::string & name = "") {
370  NewElement(name, &element);
371  }
372 
375  template <class _elementType>
376  _elementType * GetStateDataElement(mtsStateDataId id) const {
377  return StateVectorElements[id]; // WEIRD???
378  }
379 
380 
382  return StateVectorElements[id];
383  }
384 
390  template<class _elementType>
391  mtsStateTable::AccessorBase * GetAccessor(const _elementType & element) const;
392 
398  mtsStateTable::AccessorBase * GetAccessor(const std::string & name) const;
399  mtsStateTable::AccessorBase * GetAccessor(const char * name) const;
400  mtsStateTable::AccessorBase * GetAccessor(const size_t id) const;
401 
402 
404  mtsStateIndex GetIndexWriter(void) const;
405 
407  void Start(void);
408 
410  void StartIfAutomatic(void);
411 
417  void Advance(void);
418 
420  void AdvanceIfAutomatic(void);
421 
425  bool ReplayAdvance(void);
426 
428  void Cleanup(void);
429 
430  inline double GetTic(void) const {
431  return this->Tic.Data;
432  }
433 
434  inline double GetToc(void) const {
435  return this->Toc.Data;
436  }
437 
438  inline size_t GetHistoryLength(void) const {
439  return this->HistoryLength;
440  }
441 
442  inline size_t GetNumberOfElements(void) const {
443  return this->StateVector.size();
444  }
445 
448  inline double GetAveragePeriod(void) const {
449  return AveragePeriod;
450  }
451 
453  void ToStream(std::ostream & out) const;
454 
457  void Debug(std::ostream & out, unsigned int * listColumn, unsigned int number) const;
458 
465  void CSVWrite(std::ostream & out, bool nonZeroOnly = false);
466  void CSVWrite(std::ostream & out, unsigned int * listColumn, unsigned int number, bool nonZeroOnly = false);
467 
468  void CSVWrite(std::ostream & out, mtsGenericObject ** listColumn, unsigned int number, bool nonZeroOnly = false);
469 
471  inline const std::string & GetName(void) const { return Name; }
472 
474  void DataCollectionEventTriggeringRatio(const mtsDouble & eventTriggeringRatio);
475 
478  void DataCollectionStart(const mtsDouble & delay);
479  void DataCollectionStop(const mtsDouble & delay);
481 };
482 
486 
487 
488 // overload mtsObjectName to provide the class name
489 inline std::string mtsObjectName(const mtsStateTable * object)
490 {
491  return object->GetName();
492 }
493 
494 // overload mtsObjectName for mtsStateTable::Accessor
495 template <class _elementType>
496 inline std::string mtsObjectName(const mtsStateTable::Accessor<_elementType> * CMN_UNUSED(accessor)) {
497  return "mtsStateTable::Accessor";
498 }
499 
500 template <class _elementType>
501 mtsStateDataId mtsStateTable::NewElement(const std::string & name, _elementType * element) {
502  typedef typename mtsGenericTypes<_elementType>::FinalType FinalType;
503  typedef typename mtsGenericTypes<_elementType>::FinalRefType FinalRefType;
504  mtsStateArray<FinalType> * elementHistory =
506  StateVector.push_back(elementHistory);
507  FinalRefType *pdata = mtsGenericTypes<_elementType>::ConditionalWrap(*element);
508  StateVectorElements.push_back(pdata);
509 
510  StateVectorDataNames.push_back(name);
511  mtsStateDataId id = static_cast<mtsStateDataId>(StateVector.size() - 1);
512  AccessorBase * accessor = new Accessor<_elementType>(*this, id, elementHistory, pdata);
513  StateVectorAccessors.push_back(accessor);
514  return id;
515 }
516 
517 template <class _elementType>
518 mtsStateTable::AccessorBase *mtsStateTable::GetAccessor(const _elementType & element) const
519 {
520  for (size_t i = 0; i < StateVectorElements.size(); i++) {
522  return StateVectorAccessors[i];
523  }
524  return 0;
525 }
526 
527 #endif // _mtsStateTable_h
528 
Defines the command interfaces.
Defines a function object to use a void command (mtsCommandVoid)
#define CISST_EXPORT
Definition: cmnExportMacros.h:50
mtsFunctionWrite Progress
Definition: mtsStateTable.h:142
mtsStateTable::IndexRange BatchRange
Definition: mtsStateTable.h:111
Defines the command interfaces.
mtsStateIndex Last
Definition: mtsStateTable.h:85
double TimeIntervalForProgressEvent
Definition: mtsStateTable.h:123
std::string Name
Definition: mtsStateTable.h:305
bool GetDelayed(mtsGenericObject &data) const
Definition: mtsStateTable.h:221
CMN_DECLARE_SERVICES_INSTANTIATION(mtsStateTable)
size_t CounterForEvent
Definition: mtsStateTable.h:120
#define CMN_UNUSED(argument)
Definition: cmnPortability.h:479
const value_type * GetPointer(const mtsStateIndex &when) const
Definition: mtsStateTable.h:192
const osaTimeServer * TimeServer
Definition: mtsStateTable.h:282
Class registration macros.
std::vector< AccessorBase * > StateVectorAccessors
Definition: mtsStateTable.h:271
~DataCollectionInfo()
Definition: mtsStateTable.h:154
double StopTime
Definition: mtsStateTable.h:108
size_t BatchCounter
Definition: mtsStateTable.h:117
TimeTicksType Ticks(void) const
Definition: mtsStateIndex.h:99
mtsDouble Toc
Definition: mtsStateTable.h:287
virtual void ToStream(std::ostream &outputStream) const
size_t GetNumberOfElements(void) const
Definition: mtsStateTable.h:442
bool Get(const mtsStateIndex &when, value_type &data) const
Definition: mtsStateTable.h:185
bool ValidateReadIndex(const mtsStateIndex &timeIndex) const
Definition: mtsStateTable.h:343
mtsStateTable::IndexRange mtsStateTableIndexRange
Definition: mtsStateTable.h:484
DataCollectionInfo DataCollection
Definition: mtsStateTable.h:309
Statistical analysis for periodic signals.
double StartTime
Definition: mtsStateTable.h:103
Definition: mtsFunctionVoid.h:36
Base class for high level objects.
Definition: cmnGenericObject.h:51
std::vector< std::string > StateVectorDataNames
Definition: mtsStateTable.h:267
Base class for data object in cisstMultiTask.
Definition: mtsGenericObject.h:56
static FinalRefType * ConditionalWrap(T &obj)
Definition: mtsGenericObjectProxy.h:673
bool Get(const mtsStateIndex &when, mtsGenericObject &data) const
Definition: mtsStateTable.h:199
size_t HistoryLength
Definition: mtsStateTable.h:232
mtsIntervalStatistics PeriodStats
Definition: mtsStateTable.h:294
mtsFunctionVoid CollectionStarted
Definition: mtsStateTable.h:135
Definition: mtsStateTable.h:94
Definition: mtsIntervalStatistics.h:45
const bool & AutomaticAdvance(void) const
Definition: mtsStateTable.h:348
void GetIndexReader(mtsStateIndex &timeIndex) const
Definition: mtsStateTable.h:330
DataCollectionInfo(void)
Definition: mtsStateTable.h:145
mtsFunctionWrite BatchReady
Definition: mtsStateTable.h:131
Definition: mtsGenericObjectProxy.h:47
double AveragePeriod
Definition: mtsStateTable.h:302
Defines a state data array used in a state table.
mtsStateDataId NewElement(const std::string &name="", _elementType *element=0)
Definition: mtsStateTable.h:501
Definition: mtsStateArray.h:47
Definition: mtsStateTable.h:169
Definition: mtsStateTable.h:67
Forward declarations and #define for cisstMultiTask.
const mtsStateTable & Table
Definition: mtsStateTable.h:160
Definition: mtsComponent.h:150
size_t Delay
Definition: mtsStateTable.h:245
double TimeOfLastProgressEvent
Definition: mtsStateTable.h:126
Defines cmnGenericObject.
std::vector< mtsStateIndex::TimeTicksType > Ticks
Definition: mtsStateTable.h:275
bool Collecting
Definition: mtsStateTable.h:98
Definition: mtsStateTable.h:158
mtsStateDataId Id
Definition: mtsStateTable.h:161
#define CMN_DECLARE_SERVICES(hasDynamicCreation, lod)
Definition: cmnClassRegisterMacros.h:116
void AddData(_elementType &element, const std::string &name="")
Definition: mtsStateTable.h:369
Definition: mtsGenericObjectProxy.h:46
void SetAutomaticAdvance(bool automaticAdvance)
Definition: mtsStateTable.h:353
mtsStateIndex First
Definition: mtsStateTable.h:84
double GetToc(void) const
Definition: mtsStateTable.h:434
size_t IndexDelayed
Definition: mtsStateTable.h:241
bool GetDelayed(value_type &data) const
Definition: mtsStateTable.h:218
double SumOfPeriods
Definition: mtsStateTable.h:299
Class for relative time.
Definition: osaTimeServer.h:73
size_t IndexWriter
Definition: mtsStateTable.h:235
int mtsStateDataId
Definition: mtsStateTable.h:47
bool GetLatest(mtsGenericObject &data) const
Definition: mtsStateTable.h:214
mtsStateTable::AccessorBase * GetAccessor(const _elementType &element) const
size_t BatchSize
Definition: mtsStateTable.h:114
std::string mtsObjectName(const mtsStateTable *object)
Definition: mtsStateTable.h:489
_elementType * GetStateDataElement(mtsStateDataId id) const
Definition: mtsStateTable.h:376
size_t GetHistoryLength(void) const
Definition: mtsStateTable.h:438
void ToStream(std::ostream &outputStream, const mtsStateIndex &when) const
Definition: mtsStateTable.h:181
Defines an index used for mtsStateTable.
Rules of exporting.
mtsFunctionWrite CollectionStopped
Definition: mtsStateTable.h:139
Defines a base class the state data array.
bool AutomaticAdvanceFlag
Definition: mtsStateTable.h:254
double GetAveragePeriod(void) const
Definition: mtsStateTable.h:448
double GetTic(void) const
Definition: mtsStateTable.h:430
Definition: mtsCollectorState.h:44
virtual ~AccessorBase()
Definition: mtsStateTable.h:164
Definition: mtsStateIndex.h:51
Definition: mtsFunctionWrite.h:37
Definition: mtsGenericObjectProxy.h:45
mtsStateDataId PeriodId
Definition: mtsStateTable.h:279
const int CMN_NO_DYNAMIC_CREATION
Definition: cmnClassRegisterMacros.h:328
const int CMN_DYNAMIC_CREATION
Definition: cmnClassRegisterMacros.h:331
mtsDouble Period
Definition: mtsStateTable.h:291
size_t IndexReader
Definition: mtsStateTable.h:238
mtsStateDataId TocId
Definition: mtsStateTable.h:278
AccessorBase(const mtsStateTable &table, mtsStateDataId id)
Definition: mtsStateTable.h:163
virtual void ToStreamRaw(std::ostream &outputStream, const char delimiter= ' ', bool headerOnly=false, const std::string &headerPrefix="") const
const std::string & GetName(void) const
Definition: mtsStateTable.h:471
void SetCurrent(const value_type &data)
Definition: mtsStateTable.h:224
Accessor(const mtsStateTable &table, mtsStateDataId id, const mtsStateArray< value_type > *history, value_ref_type *data)
Definition: mtsStateTable.h:177
int Index(void) const
Definition: mtsStateIndex.h:94
Definition: mtsStateTable.h:80
std::vector< mtsStateArrayBase * > StateVector
Definition: mtsStateTable.h:257
#define CMN_LOG_ALLOW_DEFAULT
Definition: cmnLogLoD.h:76
std::vector< mtsGenericObject * > StateVectorElements
Definition: mtsStateTable.h:263
mtsGenericObject * GetStateVectorElement(size_t id) const
Definition: mtsStateTable.h:381
bool GetLatest(value_type &data) const
Definition: mtsStateTable.h:211