cisst-saw
Loading...
Searching...
No Matches
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 Author(s): Ankur Kapoor
6 Created on: 2004-04-30
7
8 (C) Copyright 2004-2019 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 _mtsStateArray_h
26#define _mtsStateArray_h
27
32
33#include <vector>
34#include <typeinfo>
35
44
45template <class _elementType>
47{
48public:
49 typedef _elementType value_type;
50 // PK: why not use vctDynamicVector or mtsVector instead of std::vector?
51 typedef std::vector<value_type> VectorType;
52 typedef typename VectorType::iterator iterator;
53 typedef typename VectorType::const_iterator const_iterator;
54
55protected:
59
60public:
62 inline mtsStateArray(const value_type & objectExample,
63 size_type size = 0):
64 Data(size, objectExample)
65 {}
66
68 virtual ~mtsStateArray() {
69 Data.clear();
70 }
71
72 bool SetDataSize(const size_t size) {
73 if (Data.size() > 0) {
74 value_type objectExample = Data.at(0);
75 //\todo add try catch for alloc exception
76 Data.resize(size, objectExample);
77 } else {
78 Data.resize(size);
79 }
80 return true;
81 }
82
83
87 const value_type & Element(index_type index) const { return Data[index]; }
88 value_type & Element(index_type index) { return Data[index]; }
89
92 inline mtsGenericObject & operator[](index_type index){ return Data[index]; }
93 inline const mtsGenericObject & operator[](index_type index) const { return Data[index]; }
94
95 /* Create the array of data. This is currently unused. */
96 inline mtsStateArrayBase * Create(const mtsGenericObject * objectExample,
98 const value_type * typedObjectExample = dynamic_cast<const value_type *>(objectExample);
99 if (typedObjectExample) {
100 this->Data.resize(size, *typedObjectExample);
101 this->DataClassServices = objectExample->Services();
103 } else {
104 CMN_LOG_INIT_ERROR << "mtsStateArray: Create used with an object example of the wrong type, received: "
105 << objectExample->Services()->GetName()
106 << " while expecting "
108 << std::endl;
109 return 0;
110 }
111 return this;
112 }
113
114
116 inline void Copy(index_type indexTo, index_type indexFrom) {
117 this->Data[indexTo] = this->Data[indexFrom];
118 }
119
120
131 bool Get(index_type index, mtsGenericObject & object) const;
132 bool Set(index_type index, const mtsGenericObject & object);
134};
135
136
137#include <iostream>
138
139template <class _elementType>
141{
142 // Case 1: The state table entry was derived from mtsGenericObject
143 const _elementType *pdata = dynamic_cast<const _elementType *>(&object);
144 if (pdata) {
145 Data[index] = *pdata;
146 return true;
147 }
148 // Case 2: The state table entry was not derived from mtsGenericObject, so it was wrapped
150 const RefType* pref = dynamic_cast<const RefType*>(&object);
151 if (pref) {
152 Data[index] = *pref;
153 return true;
154 }
155 CMN_LOG_RUN_ERROR << "mtsStateArray::Set -- type mismatch, expected " << typeid(_elementType).name() << std::endl;
156 return false;
157}
158
159// PK: obsolete: use accessor class instead
160template <class _elementType>
162 _elementType* pdata = dynamic_cast<_elementType*>(&object);
163 if (pdata) {
164 *pdata = Data[index];
165 return true;
166 }
167 CMN_LOG_RUN_ERROR << "mtsStateArray::Get -- type mismatch, expected " << typeid(_elementType).name() << std::endl;
168 return false;
169}
170
171#endif // _mtsStateArray_h
const std::string & GetName(void) const
virtual const cmnClassServicesBase * Services(void) const =0
Base class for data object in cisstMultiTask.
Definition mtsGenericObject.h:52
static cmnClassServicesBase * ClassServices(void)
Definition mtsGenericObjectProxy.h:337
mtsGenericTypesUnwrapImpl< T, cmnIsDerivedFromTemplated< T, mtsGenericObjectProxyBase >::IS_DERIVED >::RefType RefType
Definition mtsGenericObjectProxy.h:731
size_t index_type
Definition mtsStateArrayBase.h:51
size_t size_type
Definition mtsStateArrayBase.h:52
mtsStateArrayBase(void)
Definition mtsStateArrayBase.h:45
const cmnClassServicesBase * DataClassServices
Definition mtsStateArrayBase.h:48
bool Set(index_type index, const mtsGenericObject &object)
Definition mtsStateArray.h:140
mtsStateArrayBase * Create(const mtsGenericObject *objectExample, size_type size)
Definition mtsStateArray.h:96
mtsGenericObject & operator[](index_type index)
Definition mtsStateArray.h:92
_elementType value_type
Definition mtsStateArray.h:49
mtsStateArray(const value_type &objectExample, size_type size=0)
Definition mtsStateArray.h:62
virtual ~mtsStateArray()
Definition mtsStateArray.h:68
bool SetDataSize(const size_t size)
Definition mtsStateArray.h:72
VectorType::const_iterator const_iterator
Definition mtsStateArray.h:53
VectorType Data
Definition mtsStateArray.h:58
const value_type & Element(index_type index) const
Definition mtsStateArray.h:87
std::vector< value_type > VectorType
Definition mtsStateArray.h:51
bool Get(index_type index, mtsGenericObject &object) const
Definition mtsStateArray.h:161
const mtsGenericObject & operator[](index_type index) const
Definition mtsStateArray.h:93
value_type & Element(index_type index)
Definition mtsStateArray.h:88
VectorType::iterator iterator
Definition mtsStateArray.h:52
void Copy(index_type indexTo, index_type indexFrom)
Definition mtsStateArray.h:116
#define CMN_ASSERT(expr)
Definition cmnAssert.h:97
Class register definitions and log macros.
Declaration of cmnLogger amd macros for human readable logging.
#define CMN_LOG_RUN_ERROR
Definition cmnLogger.h:166
#define CMN_LOG_INIT_ERROR
Definition cmnLogger.h:162
Defines a base class the state data array.
size_type size(void) const
Definition vctDynamicConstMatrixBase.h:228