cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mtsStateArray.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): Ankur Kapoor
7  Created on: 2004-04-30
8 
9  (C) Copyright 2004-2009 Johns Hopkins University (JHU), All Rights Reserved.
10 
11 --- begin cisst license - do not edit ---
12 
13 This software is provided "as is" under an open source license, with
14 no warranty. The complete license can be found in license.txt and
15 http://www.cisst.org/cisst/license.txt.
16 
17 --- end cisst license ---
18 */
19 
20 
26 #ifndef _mtsStateArray_h
27 #define _mtsStateArray_h
28 
29 #include <cisstCommon/cmnLogger.h>
33 
34 #include <vector>
35 #include <typeinfo>
36 
46 template <class _elementType>
48 {
49 public:
50  typedef _elementType value_type;
51  // PK: why not use vctDynamicVector or mtsVector instead of std::vector?
52  typedef std::vector<value_type> VectorType;
53  typedef typename VectorType::iterator iterator;
54  typedef typename VectorType::const_iterator const_iterator;
55 
56 protected:
60 
61 public:
63  inline mtsStateArray(const value_type & objectExample,
64  size_type size = 0):
65  Data(size, objectExample)
66  {}
67 
69  virtual ~mtsStateArray() {}
70 
71 
72  bool SetDataSize(const size_t size){
73  value_type objectExample = Data[0];
74  //\todo add try catch for alloc exception
75  Data.resize(size,objectExample);
76  return true;
77  }
78 
79 
83  const value_type & Element(index_type index) const { return Data[index]; }
84  value_type & Element(index_type index) { return Data[index]; }
85 
88  inline mtsGenericObject & operator[](index_type index){ return Data[index]; }
89  inline const mtsGenericObject & operator[](index_type index) const { return Data[index]; }
90 
91  /* Create the array of data. This is currently unused. */
92  inline mtsStateArrayBase * Create(const mtsGenericObject * objectExample,
93  size_type size) {
94  const value_type * typedObjectExample = dynamic_cast<const value_type *>(objectExample);
95  if (typedObjectExample) {
96  this->Data.resize(size, *typedObjectExample);
97  this->DataClassServices = objectExample->Services();
99  } else {
100  CMN_LOG_INIT_ERROR << "mtsStateArray: Create used with an object example of the wrong type, received: "
101  << objectExample->Services()->GetName()
102  << " while expecting "
103  << value_type::ClassServices()->GetName()
104  << std::endl;
105  return 0;
106  }
107  return this;
108  }
109 
110 
112  inline void Copy(index_type indexTo, index_type indexFrom) {
113  this->Data[indexTo] = this->Data[indexFrom];
114  }
115 
116 
127  bool Get(index_type index, mtsGenericObject & object) const;
128  bool Set(index_type index, const mtsGenericObject & object);
130 };
131 
132 
133 #include <iostream>
134 
135 template <class _elementType>
137 {
138  // Case 1: The state table entry was derived from mtsGenericObject
139  const _elementType *pdata = dynamic_cast<const _elementType *>(&object);
140  if (pdata) {
141  Data[index] = *pdata;
142  return true;
143  }
144  // Case 2: The state table entry was not derived from mtsGenericObject, so it was wrapped
145  typedef typename mtsGenericTypesUnwrap<_elementType>::RefType RefType;
146  const RefType* pref = dynamic_cast<const RefType*>(&object);
147  if (pref) {
148  Data[index] = *pref;
149  return true;
150  }
151  CMN_LOG_RUN_ERROR << "mtsStateArray::Set -- type mismatch, expected " << typeid(_elementType).name() << std::endl;
152  return false;
153 }
154 
155 // PK: obsolete: use accessor class instead
156 template <class _elementType>
158  _elementType* pdata = dynamic_cast<_elementType*>(&object);
159  if (pdata) {
160  *pdata = Data[index];
161  return true;
162  }
163  CMN_LOG_RUN_ERROR << "mtsStateArray::Get -- type mismatch, expected " << typeid(_elementType).name() << std::endl;
164  return false;
165 }
166 
167 #endif // _mtsStateArray_h
168 
VectorType::iterator iterator
Definition: mtsStateArray.h:53
value_type & Element(index_type index)
Definition: mtsStateArray.h:84
void Copy(index_type indexTo, index_type indexFrom)
Definition: mtsStateArray.h:112
#define CMN_ASSERT(expr)
Definition: cmnAssert.h:90
#define CMN_LOG_RUN_ERROR
Definition: cmnLogger.h:166
const std::string & GetName(void) const
bool SetDataSize(const size_t size)
Definition: mtsStateArray.h:72
Class register definitions and log macros.
#define CMN_LOG_INIT_ERROR
Definition: cmnLogger.h:162
Base class for data object in cisstMultiTask.
Definition: mtsGenericObject.h:56
virtual const cmnClassServicesBase * Services(void) const =0
std::vector< value_type > VectorType
Definition: mtsStateArray.h:52
virtual ~mtsStateArray()
Definition: mtsStateArray.h:69
Declaration of cmnLogger amd macros for human readable logging.
mtsGenericObject & operator[](index_type index)
Definition: mtsStateArray.h:88
Definition: mtsStateArray.h:47
_elementType value_type
Definition: mtsStateArray.h:50
mtsStateArray(const value_type &objectExample, size_type size=0)
Definition: mtsStateArray.h:63
mtsGenericTypesUnwrapImpl< T, cmnIsDerivedFromTemplated< T, mtsGenericObjectProxyBase >::IS_DERIVED >::RefType RefType
Definition: mtsGenericObjectProxy.h:714
Defines a base class the state data array.
size_t index_type
Definition: mtsStateArrayBase.h:51
mtsStateArrayBase * Create(const mtsGenericObject *objectExample, size_type size)
Definition: mtsStateArray.h:92
Definition: mtsGenericObjectProxy.h:45
VectorType Data
Definition: mtsStateArray.h:59
VectorType::const_iterator const_iterator
Definition: mtsStateArray.h:54
bool Get(index_type index, mtsGenericObject &object) const
Definition: mtsStateArray.h:157
const mtsGenericObject & operator[](index_type index) const
Definition: mtsStateArray.h:89
size_t size_type
Definition: mtsStateArrayBase.h:52
Definition: mtsStateArrayBase.h:42
bool Set(index_type index, const mtsGenericObject &object)
Definition: mtsStateArray.h:136
const cmnClassServicesBase * DataClassServices
Definition: mtsStateArrayBase.h:45
const value_type & Element(index_type index) const
Definition: mtsStateArray.h:83