Ignition Gazebo

API Reference

5.0.0
Component.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2018 Open Source Robotics Foundation
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  */
17 #ifndef IGNITION_GAZEBO_COMPONENTS_COMPONENT_HH_
18 #define IGNITION_GAZEBO_COMPONENTS_COMPONENT_HH_
19 
20 #include <cstdint>
21 #include <memory>
22 #include <string>
23 #include <sstream>
24 #include <utility>
25 
27 
28 #include <ignition/gazebo/config.hh>
29 #include <ignition/gazebo/Export.hh>
30 #include <ignition/gazebo/Types.hh>
31 
32 namespace ignition
33 {
34 namespace gazebo
35 {
36 // namespace ignition
37 // Inline bracket to help doxygen filtering.
38 inline namespace IGNITION_GAZEBO_VERSION_NAMESPACE {
39 namespace traits
40 {
42  template <typename T>
44  {
45  };
46 
48  template <typename T>
49  struct IsSharedPtr<std::shared_ptr<T>> : std::true_type
50  {
51  };
52 
61  template <typename Stream, typename DataType>
63  {
64  private: template <typename StreamArg, typename DataTypeArg>
65  static auto Test(int _test)
66  -> decltype(std::declval<StreamArg &>()
67  << std::declval<const DataTypeArg &>(), std::true_type());
68 
69  private: template <typename, typename>
70  static auto Test(...) -> std::false_type;
71 
72  public: static constexpr bool value = // NOLINT
73  decltype(Test<Stream, DataType>(true))::value;
74  };
75 
85  template <typename Stream, typename DataType>
87  {
88  private: template <typename StreamArg, typename DataTypeArg>
89  static auto Test(int _test)
90  -> decltype(std::declval<StreamArg &>() >> std::declval<DataTypeArg &>(),
91  std::true_type());
92 
93  private: template <typename, typename>
94  static auto Test(...) -> std::false_type;
95 
96  public: static constexpr bool value = // NOLINT
97  decltype(Test<Stream, DataType>(0))::value;
98  };
99 }
100 
101 namespace serializers
102 {
107  template <typename DataType>
109  {
111  public: static std::ostream &Serialize(std::ostream &_out,
112  const DataType &_data)
113  {
114  // cppcheck-suppress syntaxError
115  if constexpr (traits::IsSharedPtr<DataType>::value) // NOLINT
116  {
118  typename DataType::element_type>::value)
119  {
120  _out << *_data;
121  }
122  else
123  {
124  static bool warned{false};
125  if (!warned)
126  {
127  ignwarn << "Trying to serialize component with data type ["
128  << typeid(DataType).name() << "], which doesn't have "
129  << "`operator<<`. Component will not be serialized."
130  << std::endl;
131  warned = true;
132  }
133  }
134  }
136  {
137  _out << _data;
138  }
139  else
140  {
141  static bool warned{false};
142  if (!warned)
143  {
144  ignwarn << "Trying to serialize component with data type ["
145  << typeid(DataType).name() << "], which doesn't have "
146  << "`operator<<`. Component will not be serialized."
147  << std::endl;
148  warned = true;
149  }
150  }
151  return _out;
152  }
153 
157  public: static std::istream &Deserialize(std::istream &_in,
158  DataType &_data)
159  {
161  {
162  if constexpr (traits::IsInStreamable<std::istream,
163  typename DataType::element_type>::value)
164  {
165  _in >> *_data;
166  }
167  else
168  {
169  static bool warned{false};
170  if (!warned)
171  {
172  ignwarn << "Trying to deserialize component with data type ["
173  << typeid(DataType).name() << "], which doesn't have "
174  << "`operator>>`. Component will not be deserialized."
175  << std::endl;
176  warned = true;
177  }
178  }
179  }
181  {
182  _in >> _data;
183  }
184  else
185  {
186  static bool warned{false};
187  if (!warned)
188  {
189  ignwarn << "Trying to deserialize component with data type ["
190  << typeid(DataType).name() << "], which doesn't have "
191  << "`operator>>`. Component will not be deserialized."
192  << std::endl;
193  warned = true;
194  }
195  }
196  return _in;
197  }
198  };
199 }
200 
201 namespace components
202 {
207 }
208 
209 namespace serializers
210 {
212  template<> class DefaultSerializer<components::NoData>
213  {
214  public: static std::ostream &Serialize(std::ostream &_out)
215  {
216  _out << "-";
217  return _out;
218  }
219 
220  public: static std::istream &Deserialize(std::istream &_in)
221  {
222  return _in;
223  }
224  };
225 }
226 
227 namespace components
228 {
231  {
233  public: BaseComponent() = default;
234 
236  public: virtual ~BaseComponent() = default;
237 
243  public: virtual void Serialize(std::ostream &/*_out*/) const
244  {
245  static bool warned{false};
246  if (!warned)
247  {
248  ignwarn << "Trying to serialize component of type [" << this->TypeId()
249  << "], which hasn't implemented the `Serialize` function. "
250  << "Component will not be serialized." << std::endl;
251  warned = true;
252  }
253  };
254 
260  public: virtual void Deserialize(std::istream &/*_in*/)
261  {
262  static bool warned{false};
263  if (!warned)
264  {
265  ignwarn << "Trying to deserialize component of type ["
266  << this->TypeId() << "], which hasn't implemented the "
267  << "`Deserialize` function. Component will not be deserialized."
268  << std::endl;
269  warned = true;
270  }
271  };
272 
277  public: virtual ComponentTypeId TypeId() const = 0;
278  };
279 
314  template <typename DataType, typename Identifier,
315  typename Serializer = serializers::DefaultSerializer<DataType>>
316  class Component : public BaseComponent
317  {
319  public: using Type = DataType;
320 
322  public: Component() = default;
323 
326  public: explicit Component(DataType _data);
327 
329  public: ~Component() override = default;
330 
334  public: bool operator==(const Component &_component) const;
335 
339  public: bool operator!=(const Component &_component) const;
340 
341  // Documentation inherited
342  public: ComponentTypeId TypeId() const override;
343 
344  // Documentation inherited
345  public: void Serialize(std::ostream &_out) const override;
346 
347  // Documentation inherited
348  public: void Deserialize(std::istream &_in) override;
349 
355  public: DataType &Data();
356 
363  public: bool SetData(const DataType &_data,
364  const std::function<
365  bool(const DataType &, const DataType &)> &_eql);
366 
369  public: const DataType &Data() const;
370 
372  private: DataType data;
373 
376  public: inline static ComponentTypeId typeId{0};
377 
380  public: inline static std::string typeName;
381  };
382 
391  template <typename Identifier, typename Serializer>
392  class Component<NoData, Identifier, Serializer> : public BaseComponent
393  {
398  public: bool operator==(const Component<NoData, Identifier,
399  Serializer> &) const;
400 
405  public: bool operator!=(const Component<NoData, Identifier,
406  Serializer> &) const;
407 
408  // Documentation inherited
409  public: ComponentTypeId TypeId() const override;
410 
411  // Documentation inherited
412  public: void Serialize(std::ostream &_out) const override;
413 
414  // Documentation inherited
415  public: void Deserialize(std::istream &_in) override;
416 
419  public: inline static ComponentTypeId typeId{0};
420 
423  public: inline static std::string typeName;
424  };
425 
427  template <typename DataType, typename Identifier, typename Serializer>
429  : data(std::move(_data))
430  {
431  }
432 
434  template <typename DataType, typename Identifier, typename Serializer>
436  {
437  return this->data;
438  }
439 
441  template <typename DataType, typename Identifier, typename Serializer>
443  const DataType &_data,
444  const std::function<bool(const DataType &, const DataType &)> &_eql)
445  {
446  bool result = !_eql(_data, this->data);
447  this->data = _data;
448  return result;
449  }
450 
452  template <typename DataType, typename Identifier, typename Serializer>
454  {
455  return this->data;
456  }
457 
459  template <typename DataType, typename Identifier, typename Serializer>
461  const Component<DataType, Identifier, Serializer> &_component) const
462  {
463  return this->data == _component.Data();
464  }
465 
467  template <typename DataType, typename Identifier, typename Serializer>
469  const Component<DataType, Identifier, Serializer> &_component) const
470  {
471  return this->data != _component.Data();
472  }
473 
475  template <typename DataType, typename Identifier, typename Serializer>
477  std::ostream &_out) const
478  {
479  Serializer::Serialize(_out, this->Data());
480  }
481 
483  template <typename DataType, typename Identifier, typename Serializer>
485  std::istream &_in)
486  {
487  Serializer::Deserialize(_in, this->Data());
488  }
489 
491  template <typename DataType, typename Identifier, typename Serializer>
493  {
494  return typeId;
495  }
496 
498  template <typename Identifier, typename Serializer>
501  {
502  return true;
503  }
504 
506  template <typename Identifier, typename Serializer>
509  {
510  return false;
511  }
512 
514  template <typename Identifier, typename Serializer>
516  {
517  return typeId;
518  }
519 
521  template <typename Identifier, typename Serializer>
523  std::ostream &_out) const
524  {
525  Serializer::Serialize(_out);
526  }
527 
529  template <typename Identifier, typename Serializer>
531  std::istream &_in)
532  {
533  Serializer::Deserialize(_in);
534  }
535 }
536 }
537 }
538 }
539 #endif
Type trait that determines if a operator<< is defined on Stream and DataType, i.e, it checks if the function Stream& operator<<(Stream&, const DataType&) exists. Example:
Definition: Component.hh:62
Default serializer template to call stream operators only on types that support them. If the stream operator is not available, a warning message is printed.
Definition: Component.hh:108
bool operator!=(const Component &_component) const
Inequality operator.
Definition: Component.hh:468
static ComponentTypeId typeId
Unique ID for this component type. This is set through the Factory registration.
Definition: Component.hh:376
bool operator==(const Component &_component) const
Equality operator.
Definition: Component.hh:460
std::add_lvalue_reference< void > NoData
Convenient type to be used by components that don&#39;t wrap any data. I.e. they act as tags and their pr...
Definition: Component.hh:206
void Serialize(std::ostream &_out) const override
Fills a stream with a serialized version of the component. By default, it will leave the stream empty...
Definition: Component.hh:476
ComponentTypeId TypeId() const override
Returns the unique ID for the component&#39;s type. The ID is derived from the name that is manually chos...
Definition: Component.hh:492
virtual void Deserialize(std::istream &)
Fills a component based on a stream with a serialized data. By default, it will do nothing...
Definition: Component.hh:260
T endl(T... args)
virtual void Serialize(std::ostream &) const
Fills a stream with a serialized version of the component. By default, it will leave the stream empty...
Definition: Component.hh:243
STL class.
STL class.
Specialization for components that don&#39;t wrap any data. This class to be used to create simple compon...
Definition: Component.hh:392
static std::string typeName
Unique name for this component type. This is set through the Factory registration.
Definition: Component.hh:380
static std::ostream & Serialize(std::ostream &_out)
Definition: Component.hh:214
uint64_t ComponentTypeId
A unique identifier for a component type. A component type must be derived from components::BaseCompo...
Definition: Types.hh:86
DataType Type
Alias for DataType.
Definition: Component.hh:319
void Deserialize(std::istream &_in) override
Fills a component based on a stream with a serialized data. By default, it will do nothing...
Definition: Component.hh:484
static std::ostream & Serialize(std::ostream &_out, const DataType &_data)
Serialization.
Definition: Component.hh:111
static std::istream & Deserialize(std::istream &_in, DataType &_data)
Deserialization.
Definition: Component.hh:157
Helper trait to determine if a type is shared_ptr or not.
Definition: Component.hh:43
A component type that wraps any data type. The intention is for this class to be used to create simpl...
Definition: Component.hh:316
static std::istream & Deserialize(std::istream &_in)
Definition: Component.hh:220
This library is part of the Ignition Robotics project.
static std::string typeName
Unique name for this component type. This is set through the Factory registration.
Definition: Component.hh:423
STL class.
DataType & Data()
Get the mutable component data. This function will be deprecated in Gazebo 3, replaced by const DataT...
Definition: Component.hh:435
bool SetData(const DataType &_data, const std::function< bool(const DataType &, const DataType &)> &_eql)
Set the data of this component.
Definition: Component.hh:442
Base class for all components.
Definition: Component.hh:230
Type trait that determines if a operator>> is defined on Stream and DataType, i.e, it checks if the function Stream& operator>>(Stream&, DataType&) exists. Example:
Definition: Component.hh:86
#define ignwarn