DataTypeRegistry.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (C) 2014 by Ralf Kaestner *
3  * ralf.kaestner@gmail.com *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the Lesser GNU General Public License as published by*
7  * the Free Software Foundation; either version 3 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * Lesser GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the Lesser GNU General Public License *
16  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17  ******************************************************************************/
18 
22 
23 namespace variant_topic_tools {
24 
25 /*****************************************************************************/
26 /* Static initializations */
27 /*****************************************************************************/
28 
29 DataTypeRegistry::ImplPtr DataTypeRegistry::impl(new DataTypeRegistry::Impl());
30 
31 /*****************************************************************************/
32 /* Constructors and Destructor */
33 /*****************************************************************************/
34 
36  if (impl->dataTypesByIdentifier.empty()) {
37  addBuiltinDataType<bool>("bool");
38  addBuiltinDataType<double>("float64");
39  addBuiltinDataType<float>("float32");
40  addBuiltinDataType<int16_t>("int16");
41  addBuiltinDataType<int32_t>("int32");
42  addBuiltinDataType<int64_t>("int64");
43  addBuiltinDataType<int8_t>("int8");
44  addBuiltinDataType<uint16_t>("uint16");
45  addBuiltinDataType<uint32_t>("uint32");
46  addBuiltinDataType<uint64_t>("uint64");
47  addBuiltinDataType<uint8_t>("uint8");
48 
49  addBuiltinDataType<uint8_t>("char");
50  addBuiltinDataType<int8_t>("byte");
51 
52  addBuiltinDataType<ros::Duration>("duration");
53  addBuiltinDataType<std::string>("string");
54  addBuiltinDataType<ros::Time>("time");
55  }
56 }
57 
59 }
60 
62 }
63 
65 }
66 
67 /*****************************************************************************/
68 /* Accessors */
69 /*****************************************************************************/
70 
71 DataType DataTypeRegistry::getDataType(const std::string& identifier) {
72  boost::unordered_map<std::string, DataType>::const_iterator it =
73  impl->dataTypesByIdentifier.find(identifier);
74 
75  if (it != impl->dataTypesByIdentifier.end())
76  return it->second;
77 
78  std::string name, memberType;
79  size_t size;
80 
81  if (MessageDefinitionParser::matchArrayType(identifier, memberType,
82  size)) {
83  boost::unordered_map<std::string, DataType>::const_iterator jt =
84  impl->dataTypesByIdentifier.find(memberType);
85 
86  if (jt != impl->dataTypesByIdentifier.end())
87  return addArrayDataType(jt->second, size);
88  }
89 
90  return DataType();
91 }
92 
93 DataType DataTypeRegistry::getDataType(const std::string& identifier) const {
94  boost::unordered_map<std::string, DataType>::const_iterator it =
95  impl->dataTypesByIdentifier.find(identifier);
96 
97  if (it != impl->dataTypesByIdentifier.end())
98  return it->second;
99  else
100  return DataType();
101 }
102 
103 DataType DataTypeRegistry::getDataType(const std::type_info& typeInfo) const {
104  boost::unordered_multimap<const std::type_info*, DataType, TypeInfoHash>::
105  const_iterator it = impl->dataTypesByInfo.find(&typeInfo);
106 
107  if (it != impl->dataTypesByInfo.end())
108  return it->second;
109  else
110  return DataType();
111 }
112 
113 /*****************************************************************************/
114 /* Methods */
115 /*****************************************************************************/
116 
118  size_t numMembers) {
119  ArrayDataType arrayDataType(memberType, numMembers);
120  addDataType(arrayDataType);
121 
122  return arrayDataType;
123 }
124 
126  identifier, const MessageFieldCollection<MessageConstant>& constantMembers,
127  const MessageFieldCollection<MessageVariable>& variableMembers) {
128  MessageDataType messageDataType(identifier, constantMembers,
129  variableMembers);
130  addDataType(messageDataType);
131 
132  return messageDataType;
133 }
134 
136  identifier, const std::string& definition) {
137  MessageDataType messageDataType(identifier, definition);
138  addDataType(messageDataType);
139 
140  return messageDataType;
141 }
142 
144  if (dataType.isValid()) {
145  boost::unordered_map<std::string, DataType>::iterator it =
146  impl->dataTypesByIdentifier.find(dataType.getIdentifier());
147 
148  if (it == impl->dataTypesByIdentifier.end()) {
149  impl->dataTypesByIdentifier.insert(
150  std::make_pair(dataType.getIdentifier(), dataType));
151 
152  if (dataType.hasTypeInfo())
153  impl->dataTypesByInfo.insert(
154  std::make_pair(&dataType.getTypeInfo(), dataType));
155  }
156  else if (!it->second.hasTypeInfo() && dataType.hasTypeInfo()) {
157  it->second = dataType;
158 
159  impl->dataTypesByInfo.insert(
160  std::make_pair(&dataType.getTypeInfo(), dataType));
161  }
162  else
163  throw AmbiguousDataTypeIdentifierException(it->first);
164  }
165  else
166  throw InvalidDataTypeException();
167 }
168 
170  if (dataType.isValid()) {
171  boost::unordered_map<std::string, DataType>::iterator it =
172  impl->dataTypesByIdentifier.find(dataType.getIdentifier());
173 
174  if ((it != impl->dataTypesByIdentifier.end()) &&
175  (it->second.impl == dataType.impl))
176  impl->dataTypesByIdentifier.erase(it);
177 
178  if (dataType.hasTypeInfo()) {
179  typedef boost::unordered_multimap<const std::type_info*, DataType,
180  TypeInfoHash>::iterator Iterator;
181 
182  std::pair<Iterator, Iterator> range =
183  impl->dataTypesByInfo.equal_range(&dataType.getTypeInfo());
184  for (Iterator it = range.first; it != range.second; ++it) {
185  if (it->second.impl == dataType.impl) {
186  impl->dataTypesByInfo.erase(it);
187  break;
188  }
189  }
190  }
191  }
192  else
193  throw InvalidDataTypeException();
194 }
195 
197  impl->dataTypesByIdentifier.clear();
198  impl->dataTypesByInfo.clear();
199 }
200 
201 void DataTypeRegistry::write(std::ostream& stream) const {
202  for (boost::unordered_map<std::string, DataType>::const_iterator
203  it = impl->dataTypesByIdentifier.begin();
204  it != impl->dataTypesByIdentifier.end(); ++it) {
205  if (it != impl->dataTypesByIdentifier.begin())
206  stream << "\n";
207  stream << it->second;
208  }
209 }
210 
211 /*****************************************************************************/
212 /* Operators */
213 /*****************************************************************************/
214 
215 DataType DataTypeRegistry::operator[](const std::string& identifier) {
216  return getDataType(identifier);
217 }
218 
219 DataType DataTypeRegistry::operator[](const std::string& identifier) const {
220  return getDataType(identifier);
221 }
222 
223 DataType DataTypeRegistry::operator[](const std::type_info& typeInfo) const {
224  return getDataType(typeInfo);
225 }
226 
227 std::ostream& operator<<(std::ostream& stream, const DataTypeRegistry&
228  dataTypeRegistry) {
229  dataTypeRegistry.write(stream);
230  return stream;
231 }
232 
233 }
Exception thrown in case of an ambiguous data type identifier.
Definition: Exceptions.h:82
Exception thrown in case of an invalid data type.
Definition: Exceptions.h:51
bool hasTypeInfo() const
True, if this data type has type information.
Definition: DataType.cpp:137
void clear()
Clear the data type registry.
Header file providing the MessageDefinitionParser class interface.
MessageDataType addMessageDataType()
Add a message data type to the data type registry (strong-typed version templated on the message type...
const std::type_info & getTypeInfo() const
Retrieve the type information associated with this data type.
Definition: DataType.cpp:84
bool isValid() const
True, if this data type is valid.
Definition: DataType.cpp:133
ImplPtr impl
The data type&#39;s implementation adapter.
Definition: DataType.h:226
const std::string & getIdentifier() const
Retrieve the identifier representing this data type.
Definition: DataType.cpp:75
void write(std::ostream &stream) const
Write the data type registry to a stream.
Header file defining exceptions for the variant topic tools.
void removeDataType(const DataType &dataType)
Remove a data type from the data type registry.
Header file providing the DataTypeRegistry class interface.
DataType operator[](const std::string &identifier)
Operator for retrieving a data type from the registry by identifier (non-const version) ...
std::ostream & operator<<(std::ostream &stream, const DataType &dataType)
Operator for writing the data type to a stream.
Definition: DataType.cpp:190
static bool matchArrayType(const std::string &expression, std::string &memberType, size_t &size)
Match an array type expression.
boost::shared_ptr< Impl > ImplPtr
Declaration of the data type registry implementation pointer type.
void addDataType()
Add a data type to the data type registry (templated version)
static ImplPtr impl
The data type registry&#39;s implementation.
Type information hash.
Definition: TypeInfoHash.h:31
ArrayDataType addArrayDataType()
Add an array data type to the data type registry (strong-typed version templated on the array type) ...
DataType getDataType()
Retrieve a data type from the registry by type information (templated non-const version) ...
Forward declaration of the message field collection.
Definition: Forwards.h:64


variant_topic_tools
Author(s): Ralf Kaestner
autogenerated on Sat Jan 9 2021 03:56:49