cisst-saw
Loading...
Searching...
No Matches
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-2025 Johns Hopkins University (JHU), All Rights Reserved.
9
10--- begin cisst license - do not edit ---
11
12This software is provided "as is" under an open source license, with
13no warranty. The complete license can be found in license.txt and
14http://www.cisst.org/cisst/license.txt.
15
16--- end cisst license ---
17*/
18
19
24
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
47class osaTimeServer;
48
49
52typedef int mtsStateDataId;
53
68
70
71 friend class mtsCollectorState;
72 friend class mtsComponent;
73 friend class mtsTaskTest;
74 friend class mtsStateTableTest;
76
77 public:
81 {
82 CMN_DECLARE_SERVICES(CMN_DYNAMIC_CREATION, CMN_LOG_ALLOW_DEFAULT);
83 public:
86
87 void ToStreamRaw(std::ostream & outputStream, const char delimiter = ' ',
88 bool headerOnly = false, const std::string & headerPrefix = "") const override;
89 };
90
156
157public:
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 {
170 typedef typename mtsGenericTypes<_elementType>::FinalType value_type;
171 typedef typename mtsGenericTypes<_elementType>::FinalRefType value_ref_type;
172 typedef typename mtsStateTable::Accessor<_elementType> ThisType;
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
234
237
240
243
246
249 size_t Delay;
250
259
261 std::vector<mtsStateArrayBase *> StateVector;
262
267 std::vector<mtsGenericObject *> StateVectorElements;
268
271 std::vector<std::string> StateVectorDataNames;
272
275 std::vector<AccessorBase *> StateVectorAccessors;
276
279 std::vector<mtsStateIndex::TimeTicksType> Ticks;
280
284
287
288public:
289
290 /* The start/end times for the current row of data. */
292
296
299
300 protected:
304
307
309 std::string Name;
310
314
316 bool Write(mtsStateDataId id, const mtsGenericObject & obj);
317
318
319 public:
322 mtsStateTable(size_t size, const std::string & name);
323
326
328 bool SetSize(const size_t size);
329
333
334 inline void GetIndexReader(mtsStateIndex & timeIndex) const {
335 timeIndex = GetIndexReader();
336 }
337
342
344 size_t SetDelay(size_t newDelay);
345
347 inline bool ValidateReadIndex(const mtsStateIndex &timeIndex) const {
348 return (Ticks[timeIndex.Index()] == timeIndex.Ticks());
349 }
350
352 inline const bool & AutomaticAdvance(void) const {
353 return this->AutomaticAdvanceFlag;
354 }
355
357 inline void SetAutomaticAdvance(bool automaticAdvance) {
358 this->AutomaticAdvanceFlag = automaticAdvance;
359 }
360
362 int GetStateVectorID(const std::string & dataName) const;
363
368 template <class _elementType>
369 mtsStateDataId NewElement(const std::string & name = "", _elementType * element = 0);
370
372 template <class _elementType>
373 void AddData(_elementType & element, const std::string & name = "") {
374 NewElement(name, &element);
375 }
376
379 template <class _elementType>
380 _elementType * GetStateDataElement(mtsStateDataId id) const {
381 return StateVectorElements[id]; // WEIRD???
382 }
383
384
386 return StateVectorElements[id];
387 }
388
393 template<class _elementType>
394 mtsStateTable::AccessorBase * GetAccessorByInstance(const _elementType & element) const;
395
401 mtsStateTable::AccessorBase * GetAccessorByName(const std::string & name) const;
404
410
411#ifndef SWIG
412 template<class _elementType>
413 CISST_DEPRECATED mtsStateTable::AccessorBase * GetAccessor(const _elementType & element) const {
414 CMN_LOG_RUN_WARNING << "mtsStateTable::GetAccessor is deprecated, use GetAccessorBy{Instance,Name,Id}" << std::endl;
415 return GetAccessorByInstance(element);
416 }
417 CISST_DEPRECATED mtsStateTable::AccessorBase * GetAccessor(const std::string & name) const {
418 CMN_LOG_RUN_WARNING << "mtsStateTable::GetAccessor is deprecated, use GetAccessorBy{Instance,Name,Id}" << std::endl;
419 return GetAccessorByName(name);
420 }
422 CMN_LOG_RUN_WARNING << "mtsStateTable::GetAccessor is deprecated, use GetAccessorBy{Instance,Name,Id}" << std::endl;
423 return GetAccessorByName(name);
424 }
426 CMN_LOG_RUN_WARNING << "mtsStateTable::GetAccessor is deprecated, use GetAccessorBy{Instance,Name,Id}" << std::endl;
427 return GetAccessorById(id);
428 }
429#endif
430
433
435 void Start(void);
436
439
441 inline bool Started(void) const {
442 return mStarted;
443 }
444
450 void Advance(void);
451
454
458 bool ReplayAdvance(void);
459
461 void Cleanup(void);
462
463 inline double GetTic(void) const {
464 return this->Tic.Data;
465 }
466
467 inline double GetToc(void) const {
468 return this->Toc.Data;
469 }
470
471 inline size_t GetHistoryLength(void) const {
472 return this->HistoryLength;
473 }
474
475 inline size_t GetNumberOfElements(void) const {
476 return this->StateVector.size();
477 }
478
479 inline const std::vector<std::string> & GetDataNames(void) const {
481 }
482
485 inline double GetAveragePeriod(void) const {
486 return AveragePeriod;
487 }
488
490 void ToStream(std::ostream & out) const override;
491
494 void Debug(std::ostream & out, unsigned int * listColumn, unsigned int number) const;
495
502 void CSVWrite(std::ostream & out, bool nonZeroOnly = false);
503 void CSVWrite(std::ostream & out, unsigned int * listColumn, unsigned int number, bool nonZeroOnly = false);
504
505 void CSVWrite(std::ostream & out, mtsGenericObject ** listColumn, unsigned int number, bool nonZeroOnly = false);
506
508 inline const std::string & GetName(void) const { return Name; }
509
511 void DataCollectionEventTriggeringRatio(const mtsDouble & eventTriggeringRatio);
512
515 void DataCollectionStart(const mtsDouble & delay);
516 void DataCollectionStop(const mtsDouble & delay);
518};
519
523
524
525// overload mtsObjectName to provide the class name
526inline std::string mtsObjectName(const mtsStateTable * object)
527{
528 return object->GetName();
529}
530
531// overload mtsObjectName for mtsStateTable::Accessor
532template <class _elementType>
534 return "mtsStateTable::Accessor";
535}
536
537template <class _elementType>
538mtsStateDataId mtsStateTable::NewElement(const std::string & name, _elementType * element) {
541 mtsStateArray<FinalType> * elementHistory =
543 StateVector.push_back(elementHistory);
545 StateVectorElements.push_back(pdata);
546
547 StateVectorDataNames.push_back(name);
548 mtsStateDataId id = static_cast<mtsStateDataId>(StateVector.size() - 1);
549 AccessorBase * accessor = new Accessor<_elementType>(*this, id, elementHistory, pdata);
550 StateVectorAccessors.push_back(accessor);
551 return id;
552}
553
554template <class _elementType>
556{
557 for (size_t i = 0; i < StateVectorElements.size(); i++) {
559 return StateVectorAccessors[i];
560 }
561 return 0;
562}
563
564#endif // _mtsStateTable_h
Base class for high level objects.
Definition cmnGenericObject.h:53
Definition mtsFunctionVoid.h:36
Definition mtsFunctionWrite.h:37
Base class for data object in cisstMultiTask.
Definition mtsGenericObject.h:52
mtsGenericObject(void)
Definition mtsGenericObject.h:77
value_type Data
Definition mtsGenericObjectProxy.h:385
static bool IsEqual(const T &obj1, const mtsGenericObject &obj2)
Definition mtsGenericObjectProxy.h:689
static FinalRefType * ConditionalWrap(T &obj)
Definition mtsGenericObjectProxy.h:688
mtsGenericTypesImpl< T, cmnIsDerivedFrom< T, mtsGenericObject >::IS_DERIVED >::FinalRefType FinalRefType
Definition mtsGenericObjectProxy.h:687
mtsGenericTypesImpl< T, cmnIsDerivedFrom< T, mtsGenericObject >::IS_DERIVED >::FinalType FinalType
Definition mtsGenericObjectProxy.h:686
Definition mtsIntervalStatistics.h:45
Definition mtsStateArray.h:47
mtsStateArray(const value_type &objectExample, size_type size=0)
Definition mtsStateArray.h:62
Definition mtsStateIndex.h:50
TimeTicksType Ticks(void) const
Definition mtsStateIndex.h:98
int Index(void) const
Definition mtsStateIndex.h:93
Definition mtsStateTable.h:158
const mtsStateTable & Table
Definition mtsStateTable.h:160
AccessorBase(const mtsStateTable &table, mtsStateDataId id)
Definition mtsStateTable.h:163
virtual ~AccessorBase()
Definition mtsStateTable.h:164
mtsStateDataId Id
Definition mtsStateTable.h:161
virtual void ToStream(std::ostream &outputStream, const mtsStateIndex &when) const =0
Definition mtsStateTable.h:169
bool GetDelayed(mtsGenericObject &data) const
Definition mtsStateTable.h:221
bool GetDelayed(value_type &data) const
Definition mtsStateTable.h:218
const value_type * GetPointer(const mtsStateIndex &when) const
Definition mtsStateTable.h:192
bool GetLatest(mtsGenericObject &data) const
Definition mtsStateTable.h:214
bool Get(const mtsStateIndex &when, value_type &data) const
Definition mtsStateTable.h:185
bool Get(const mtsStateIndex &when, mtsGenericObject &data) const
Definition mtsStateTable.h:199
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
bool GetLatest(value_type &data) const
Definition mtsStateTable.h:211
void ToStream(std::ostream &outputStream, const mtsStateIndex &when) const
Definition mtsStateTable.h:181
Definition mtsStateTable.h:94
double TimeOfLastProgressEvent
Definition mtsStateTable.h:126
mtsFunctionWrite BatchReady
Definition mtsStateTable.h:131
size_t BatchCounter
Definition mtsStateTable.h:117
bool Collecting
Definition mtsStateTable.h:98
DataCollectionInfo(void)
Definition mtsStateTable.h:145
double StartTime
Definition mtsStateTable.h:103
~DataCollectionInfo()
Definition mtsStateTable.h:154
mtsFunctionVoid CollectionStarted
Definition mtsStateTable.h:135
double StopTime
Definition mtsStateTable.h:108
mtsStateTable::IndexRange BatchRange
Definition mtsStateTable.h:111
double TimeIntervalForProgressEvent
Definition mtsStateTable.h:123
size_t BatchSize
Definition mtsStateTable.h:114
mtsFunctionWrite Progress
Definition mtsStateTable.h:142
mtsFunctionWrite CollectionStopped
Definition mtsStateTable.h:139
size_t CounterForEvent
Definition mtsStateTable.h:120
Definition mtsStateTable.h:81
mtsStateIndex Last
Definition mtsStateTable.h:85
mtsStateIndex First
Definition mtsStateTable.h:84
void ToStreamRaw(std::ostream &outputStream, const char delimiter=' ', bool headerOnly=false, const std::string &headerPrefix="") const override
Definition mtsStateTable.h:67
mtsDouble Toc
Definition mtsStateTable.h:291
void Debug(std::ostream &out, unsigned int *listColumn, unsigned int number) const
void Advance(void)
mtsStateDataId PeriodId
Definition mtsStateTable.h:283
void AddData(_elementType &element, const std::string &name="")
Definition mtsStateTable.h:373
bool SetSize(const size_t size)
void AdvanceIfAutomatic(void)
mtsStateTable::AccessorBase * GetAccessorById(const size_t id) const
CISST_DEPRECATED mtsStateTable::AccessorBase * GetAccessor(const _elementType &element) const
Definition mtsStateTable.h:413
size_t GetNumberOfElements(void) const
Definition mtsStateTable.h:475
size_t IndexDelayed
Definition mtsStateTable.h:245
const std::vector< std::string > & GetDataNames(void) const
Definition mtsStateTable.h:479
mtsStateIndex GetIndexWriter(void) const
mtsStateTable::AccessorBase * GetAccessorByName(const std::string &name) const
size_t IndexReader
Definition mtsStateTable.h:242
std::vector< mtsStateIndex::TimeTicksType > Ticks
Definition mtsStateTable.h:279
bool mStarted
Definition mtsStateTable.h:233
mtsStateIndex GetIndexReader(void) const
const osaTimeServer * TimeServer
Definition mtsStateTable.h:286
mtsGenericObject * GetStateVectorElement(size_t id) const
Definition mtsStateTable.h:385
void SetAutomaticAdvance(bool automaticAdvance)
Definition mtsStateTable.h:357
const bool & AutomaticAdvance(void) const
Definition mtsStateTable.h:352
bool ReplayAdvance(void)
void CSVWrite(std::ostream &out, bool nonZeroOnly=false)
mtsDouble Period
Definition mtsStateTable.h:295
void CSVWrite(std::ostream &out, mtsGenericObject **listColumn, unsigned int number, bool nonZeroOnly=false)
CISST_DEPRECATED mtsStateTable::AccessorBase * GetAccessor(const char *name) const
Definition mtsStateTable.h:421
double AveragePeriod
Definition mtsStateTable.h:306
double SumOfPeriods
Definition mtsStateTable.h:303
std::vector< AccessorBase * > StateVectorAccessors
Definition mtsStateTable.h:275
void StartIfAutomatic(void)
friend class mtsCollectorBaseTest
Definition mtsStateTable.h:75
CISST_DEPRECATED mtsStateTable::AccessorBase * GetAccessor(const size_t id) const
Definition mtsStateTable.h:425
void ToStream(std::ostream &out) const override
friend class mtsTaskTest
Definition mtsStateTable.h:73
size_t SetDelay(size_t newDelay)
std::vector< mtsStateArrayBase * > StateVector
Definition mtsStateTable.h:261
double GetToc(void) const
Definition mtsStateTable.h:467
mtsStateTable::AccessorBase * GetAccessorByName(const char *name) const
void Cleanup(void)
void Start(void)
friend class mtsStateTableTest
Definition mtsStateTable.h:74
size_t HistoryLength
Definition mtsStateTable.h:236
bool Started(void) const
Definition mtsStateTable.h:441
std::vector< mtsGenericObject * > StateVectorElements
Definition mtsStateTable.h:267
bool Write(mtsStateDataId id, const mtsGenericObject &obj)
mtsStateDataId TocId
Definition mtsStateTable.h:282
void DataCollectionEventTriggeringRatio(const mtsDouble &eventTriggeringRatio)
void DataCollectionStop(const mtsDouble &delay)
friend class mtsCollectorState
Definition mtsStateTable.h:71
int GetStateVectorID(const std::string &dataName) const
mtsStateDataId TicId
Definition mtsStateTable.h:282
double GetTic(void) const
Definition mtsStateTable.h:463
bool AutomaticAdvanceFlag
Definition mtsStateTable.h:258
const std::string & GetName(void) const
Definition mtsStateTable.h:508
_elementType * GetStateDataElement(mtsStateDataId id) const
Definition mtsStateTable.h:380
void CSVWrite(std::ostream &out, unsigned int *listColumn, unsigned int number, bool nonZeroOnly=false)
void GetIndexReader(mtsStateIndex &timeIndex) const
Definition mtsStateTable.h:334
void DataCollectionStart(const mtsDouble &delay)
double GetAveragePeriod(void) const
Definition mtsStateTable.h:485
std::string Name
Definition mtsStateTable.h:309
bool ValidateReadIndex(const mtsStateIndex &timeIndex) const
Definition mtsStateTable.h:347
std::vector< std::string > StateVectorDataNames
Definition mtsStateTable.h:271
CISST_DEPRECATED mtsStateTable::AccessorBase * GetAccessor(const std::string &name) const
Definition mtsStateTable.h:417
mtsStateIndex GetIndexDelayed(void) const
DataCollectionInfo DataCollection
Definition mtsStateTable.h:313
friend class mtsComponent
Definition mtsStateTable.h:72
mtsIntervalStatistics PeriodStats
Definition mtsStateTable.h:298
size_t GetHistoryLength(void) const
Definition mtsStateTable.h:471
mtsDouble Tic
Definition mtsStateTable.h:291
mtsStateTable(size_t size, const std::string &name)
size_t IndexWriter
Definition mtsStateTable.h:239
size_t Delay
Definition mtsStateTable.h:249
mtsStateDataId NewElement(const std::string &name="", _elementType *element=0)
Definition mtsStateTable.h:538
mtsStateTable::AccessorBase * GetAccessorByInstance(const _elementType &element) const
Definition mtsStateTable.h:555
Class for relative time.
Definition osaTimeServer.h:74
Class registration macros.
#define CMN_DECLARE_SERVICES_INSTANTIATION(className)
Definition cmnClassRegisterMacros.h:199
const int CMN_NO_DYNAMIC_CREATION
Definition cmnClassRegisterMacros.h:325
const int CMN_DYNAMIC_CREATION
Definition cmnClassRegisterMacros.h:328
#define CISST_EXPORT
Definition cmnExportMacros.h:50
Defines cmnGenericObject.
#define CMN_LOG_ALLOW_DEFAULT
Definition cmnLogLoD.h:76
#define CMN_LOG_RUN_WARNING
Definition cmnLogger.h:167
#define CMN_UNUSED(argument)
Definition cmnPortability.h:497
#define CISST_DEPRECATED
Definition cmnPortability.h:314
std::string mtsObjectName(const mtsComponent *object)
Definition mtsComponent.h:588
Rules of exporting.
Forward declarations and #define for cisstMultiTask.
Defines the command interfaces.
Defines a function object to use a void command (mtsCommandVoid).
Defines the command interfaces.
mtsGenericObjectProxy< double > mtsDouble
Definition mtsGenericObjectProxy.h:747
Statistical analysis for periodic signals.
Defines a state data array used in a state table.
Defines a base class the state data array.
Defines an index used for mtsStateTable.
mtsStateTable::IndexRange mtsStateTableIndexRange
Definition mtsStateTable.h:521
int mtsStateDataId
Definition mtsStateTable.h:52
size_type size(void) const
Definition vctDynamicConstMatrixBase.h:228