All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NamedData.h
Go to the documentation of this file.
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2012-2013, SimQuest Solutions Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef SURGSIM_DATASTRUCTURES_NAMEDDATA_H
17 #define SURGSIM_DATASTRUCTURES_NAMEDDATA_H
18 
19 #include <memory>
20 #include <string>
21 #include <vector>
22 
25 
26 namespace SurgSim
27 {
28 namespace DataStructures
29 {
32 typedef std::unordered_map<int, int> NamedDataCopyMap;
33 
35 
36 namespace Names
37 {
38 static const char* const BUTTON_0 = "button0";
39 static const char* const BUTTON_1 = "button1";
40 static const char* const BUTTON_2 = "button2";
41 static const char* const BUTTON_3 = "button3";
42 static const char* const BUTTON_4 = "button4";
43 
44 static const char* const POSE = "pose";
45 static const char* const INPUT_POSE = "inputPose";
46 
47 static const char* const ANGULAR_VELOCITY = "angularVelocity";
48 static const char* const LINEAR_VELOCITY = "linearVelocity";
49 static const char* const INPUT_ANGULAR_VELOCITY = "inputAngularVelocity";
50 static const char* const INPUT_LINEAR_VELOCITY = "inputLinearVelocity";
51 
52 static const char* const FORCE = "force";
53 static const char* const TORQUE = "torque";
54 
55 static const char* const DAMPER_JACOBIAN = "damperJacobian";
56 static const char* const SPRING_JACOBIAN = "springJacobian";
57 
58 static const char* const IS_HOMED = "isHomed";
59 static const char* const IS_ORIENTATION_HOMED = "isOrientationHomed";
60 static const char* const IS_POSITION_HOMED = "isPositionHomed";
61 
62 static const char* const DIGITAL_INPUT_PREFIX = "digitalInput";
63 static const char* const DIGITAL_OUTPUT_PREFIX = "digitalOutput";
64 static const char* const TIMER_INPUT_PREFIX = "timerInput";
65 static const char* const TIMER_OUTPUT_PREFIX = "timerOutput";
66 static const char* const ANALOG_INPUT_PREFIX = "analogInput";
67 static const char* const ANALOG_OUTPUT_PREFIX = "analogOutput";
68 };
69 
94 template <typename T>
95 class NamedData
96 {
97 public:
99  inline NamedData();
100 
105  inline explicit NamedData(std::shared_ptr<const IndexDirectory> directory);
106 
111  inline explicit NamedData(const std::vector<std::string>& names);
112 
116  inline NamedData(const NamedData& namedData);
117 
144  inline NamedData& operator=(const NamedData& namedData);
145 
149  inline NamedData(NamedData&& namedData);
150 
158  inline NamedData& operator=(NamedData&& namedData);
159 
165  inline bool isValid() const;
166 
170  inline std::shared_ptr<const IndexDirectory> getDirectory() const;
171 
175  inline int getIndex(const std::string& name) const;
176 
180  inline std::string getName(int index) const;
181 
187  inline bool hasEntry(int index) const;
188 
194  inline bool hasEntry(const std::string& name) const;
195 
203  inline bool hasData(int index) const;
204 
212  inline bool hasData(const std::string& name) const;
213 
222  inline bool get(int index, T* value) const;
223 
232  inline bool get(const std::string& name, T* value) const;
233 
240  inline bool set(int index, const T& value);
241 
248  inline bool set(const std::string& name, const T& value);
249 
254  inline bool reset(int index);
255 
260  inline bool reset(const std::string& name);
261 
263  inline void resetAll();
264 
268  inline size_t size() const;
269 
273  inline int getNumEntries() const;
274 
279  template <typename N>
280  void copy(const NamedData<N>& source, const NamedDataCopyMap& map);
281 
285  void cacheIndex(const std::string& name, int* index) const;
286 
287 private:
289  std::shared_ptr<const IndexDirectory> m_directory;
290 
292  std::vector<T> m_data;
293 
295  std::vector<bool> m_isDataValid;
296 };
297 
298 }; // namespace DataStructures
299 }; // namespace SurgSim
300 
301 
303 
304 
305 #endif // SURGSIM_DATASTRUCTURES_NAMEDDATA_H
A templated dictionary in which data can be accessed by name or index, with immutable names & indices...
Definition: NamedData.h:95
std::vector< T > m_data
The array of values.
Definition: NamedData.h:292
bool hasData(int index) const
Check whether the entry with the specified index contains valid data.
Definition: NamedData-inl.h:169
bool hasEntry(int index) const
Check whether the object contains an entry with the specified index.
Definition: NamedData-inl.h:153
std::shared_ptr< const IndexDirectory > getDirectory() const
Return the object's layout directory, which is its collection of names and indices.
Definition: NamedData-inl.h:127
std::string getName(int index) const
Given an index, return the corresponding name (or "").
Definition: NamedData-inl.h:143
bool set(int index, const T &value)
Record the data for an entry specified by an index.
Definition: NamedData-inl.h:228
int getNumEntries() const
Check the number of existing entries.
Definition: NamedData-inl.h:310
std::unordered_map< int, int > NamedDataCopyMap
The type used for copying values between two NamedData objects that cannot assign to each other...
Definition: NamedData.h:32
bool isValid() const
Check if the object has been initialized, which means it has a set of entries (i.e., names, indices, and the map between them).
Definition: NamedData-inl.h:121
int getIndex(const std::string &name) const
Given a name, return the corresponding index (or -1).
Definition: NamedData-inl.h:133
NamedData & operator=(const NamedData &namedData)
Copy the data from another object.
Definition: NamedData-inl.h:62
std::vector< bool > m_isDataValid
The array storing whether the data is currently valid.
Definition: NamedData.h:295
void resetAll()
Invalidate all entries— mark everything as not containing any valid data.
Definition: NamedData-inl.h:298
void copy(const NamedData< N > &source, const NamedDataCopyMap &map)
Copy the data from another NamedData, based on a map of indices.
Definition: NamedData-inl.h:317
bool reset(int index)
Invalidate an entry— mark it as not containing any valid data.
Definition: NamedData-inl.h:264
The header that provides the assertion API.
void cacheIndex(const std::string &name, int *index) const
Caches an entry's index if it is not already cached.
Definition: NamedData-inl.h:335
std::shared_ptr< const IndexDirectory > m_directory
The mapping between names and indices.
Definition: NamedData.h:289
NamedData()
Create an empty object, with no associated names and indices yet.
Definition: NamedData-inl.h:30
size_t size() const
Check the number of existing entries.
Definition: NamedData-inl.h:304