cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mtsStateData.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 
6  Author(s): Peter Kazanzides
7 
8  (C) Copyright 2007-2008 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 _mtsStateData_h
26 #define _mtsStateData_h
27 
31 #include <cisstMultiTask/mtsTask.h>
33 
34 // PKAZ 3/17/09: this entire class is deprecated and exists just for backward compatibility.
35 //
36 // This class handles the interface to the state table. Note that there are a few things mixed
37 // in here that could be separated:
38 //
39 // 1) The class can be used for variables that just need to be set (write-only). In this case, the
40 // the Id and Table fields are not used, and AddToStateTable() would not be called. It would
41 // only be necessary to call AddWriteCommandToInterface(). This functionality could be moved
42 // to a base class.
43 //
44 // 2) The reading of data from the state table does not need to use the Data field.
45 //
46 // Also, it would be better to have a return value for a null table pointer. In general, we
47 // should rethink whether the underlying command methods (such as Get, GetLatest, Set) should
48 // return void.
49 
50 template <class _elementType>
51 class mtsStateData {
52 protected:
55  typedef _elementType value_type;
59 
60 public: // PKAZ (was protected, but needed access for daVinci example)
61  void Get(const mtsStateIndex & when, value_type & data) const {
62  //if (!Table) return mtsExecutionResult::DEV_NOT_OK;
63  //return Table->ReadFromReader(Id, when, data)?mtsExecutionResult::DEV_OK:mtsExecutionResult::DEV_NOT_OK;
64  //if (Table) Table->ReadFromReader(Id, when, data);
65  if (Accessor) Accessor->Get(when, data);
66  }
67 
68  void GetLatest(value_type & obj) const {
69  //if (!Table) return mtsExecutionResult::DEV_NOT_OK;
70  //return Get(Table->GetIndexReader(), obj);
71  //if (Table) Get(Table->GetIndexReader(), obj);
72  if (Accessor) Accessor->GetLatest(obj);
73  }
74 
75  // Get a vector of data, starting and ending at the specified time indices (inclusive).
76  // For now, set the start index based on the vector size. In the future, we
77  // should define a new parameter type that consists of a pair of mtsStateIndex.
78  void GetHistory(const mtsStateIndex & end, mtsVector<value_type> & data) const {
79  if (data.size() > 0) {
80  //mtsStateIndex start = end;
81  //start -= (data.size()-1);
82  //if (Table) Table->ReadVectorFromReader(Id, start, end, data);
83  if (Accessor) Accessor->GetHistory(end, data);
84  }
85  }
86 
87  void Set(const value_type & obj) {
88  Data = obj;
89  }
90 
91 public:
93 
94  mtsStateData() : Id(-1), Table(0), Accessor(0) {}
96 
100  inline ThisType & operator=(value_type data) {
101  Data = data;
102  return *this;
103  }
104 
108  inline operator value_type & (void) {
109  return Data;
110  }
111 
115  void AddToStateTable(mtsStateTable & table, const std::string & dataName = "") {
116  Table = &table;
117  Id = Table->NewElement(dataName, &Data);
118  Accessor = dynamic_cast<const AccessorType *>(table.GetAccessor(dataName));
119  if (!Accessor)
120  CMN_LOG_INIT_ERROR << "mtsStateData: could not get data accessor for " << dataName << std::endl;
121  }
122 
130  const std::string & commandName) {
131  interfaceProvided->AddCommandRead(&ThisType::GetLatest, this, commandName, this->Data);
132  interfaceProvided->AddCommandQualifiedRead(&ThisType::Get, this, commandName, mtsStateIndex(), this->Data);
133 #ifdef CISST_GETVECTOR
134  // PK: fix the following
135  interfaceProvided->AddCommandQualifiedRead(&ThisType::GetHistory, this, commandName+"History", mtsStateIndex(), mtsVector<value_type>());
136 #endif
137  }
138 
142  const std::string & commandName) {
143  interfaceProvided->AddCommandWrite(&ThisType::Set, this, commandName);
144  }
145 
146 };
147 
148 
149 // overload mtsObjectName to provide the class name
150 template <class _elementType>
152 {
153  return "mtsStateData";
154 }
155 
156 
157 #endif // _mtsStateData_h
158 
Defines the state data table.
void GetHistory(const mtsStateIndex &end, mtsVector< value_type > &data) const
Definition: mtsStateData.h:78
mtsStateTable * Table
Definition: mtsStateData.h:57
std::string mtsObjectName(const mtsStateData< _elementType > *CMN_UNUSED(object))
Definition: mtsStateData.h:151
mtsStateDataId Id
Definition: mtsStateData.h:56
#define CMN_UNUSED(argument)
Definition: cmnPortability.h:479
mtsCommandRead * AddCommandRead(void(__classType::*method)(__argumentType &) const, __classType *classInstantiation, const std::string &commandName, const __argumentType &argumentPrototype)
Definition: mtsInterfaceProvided.h:373
value_type Data
Definition: mtsStateData.h:92
bool Get(const mtsStateIndex &when, value_type &data) const
Definition: mtsStateTable.h:185
mtsCommandQualifiedRead * AddCommandQualifiedRead(void(__classType::*method)(const __argument1Type &, __argument2Type &) const, __classType *classInstantiation, const std::string &commandName, const __argument1Type &argument1Prototype, const __argument2Type &argument2Prototype)
Definition: mtsInterfaceProvided.h:421
#define CMN_LOG_INIT_ERROR
Definition: cmnLogger.h:162
void Get(const mtsStateIndex &when, value_type &data) const
Definition: mtsStateData.h:61
void AddToStateTable(mtsStateTable &table, const std::string &dataName="")
Definition: mtsStateData.h:115
mtsStateTable::Accessor< _elementType > AccessorType
Definition: mtsStateData.h:54
Definition: mtsVector.h:33
ThisType & operator=(value_type data)
Definition: mtsStateData.h:100
size_type size(void) const
Definition: vctDynamicConstVectorBase.h:164
void GetLatest(value_type &obj) const
Definition: mtsStateData.h:68
mtsStateDataId NewElement(const std::string &name="", _elementType *element=0)
Definition: mtsStateTable.h:501
mtsStateData< _elementType > ThisType
Definition: mtsStateData.h:53
Definition: mtsStateTable.h:169
void AddReadCommandToInterface(mtsInterfaceProvided *interfaceProvided, const std::string &commandName)
Definition: mtsStateData.h:129
Definition: mtsStateData.h:51
Definition: mtsStateTable.h:67
~mtsStateData()
Definition: mtsStateData.h:95
Definition: mtsInterfaceProvided.h:96
void AddWriteCommandToInterface(mtsInterfaceProvided *interfaceProvided, const std::string &commandName)
Definition: mtsStateData.h:141
int mtsStateDataId
Definition: mtsStateTable.h:47
mtsStateTable::AccessorBase * GetAccessor(const _elementType &element) const
Defines a periodic task.
Declaration of mtsInterfaceProvided.
mtsCommandWriteBase * AddCommandWrite(void(__classType::*method)(const __argumentType &), __classType *classInstantiation, const std::string &commandName, const __argumentType &argumentPrototype, mtsCommandQueueingPolicy queueingPolicy=MTS_INTERFACE_COMMAND_POLICY)
Definition: mtsInterfaceProvided.h:314
mtsStateData()
Definition: mtsStateData.h:94
Definition: mtsStateIndex.h:51
_elementType value_type
Definition: mtsStateData.h:55
const AccessorType * Accessor
Definition: mtsStateData.h:58
void Set(const value_type &obj)
Definition: mtsStateData.h:87
bool GetLatest(value_type &data) const
Definition: mtsStateTable.h:211