17 #ifndef IGNITION_GAZEBO_COMPONENTS_COMPONENT_HH_ 18 #define IGNITION_GAZEBO_COMPONENTS_COMPONENT_HH_ 26 #include <ignition/common/Console.hh> 28 #include <ignition/gazebo/config.hh> 29 #include <ignition/gazebo/Export.hh> 52 template<
typename DataType,
typename Identifier,
54 decltype(std::declval<std::ostream &>() << std::declval<DataType const &>()),
55 typename std::enable_if<
57 std::is_convertible<Stream, std::ostream &>::value,
59 std::ostream &
toStream(std::ostream &_out, DataType
const &_data)
74 template<
typename DataType,
typename Identifier,
76 decltype(std::declval<std::ostream &>() << std::declval<
77 typename DataType::element_type
const &>()),
78 typename std::enable_if<
80 std::is_convertible<Stream, std::ostream &>::value,
82 std::ostream &
toStream(std::ostream &_out, DataType
const &_data)
96 template<
typename DataType,
typename Identifier,
typename... Ignored>
97 std::ostream &
toStream(std::ostream &_out, DataType
const &,
98 Ignored
const &..., ...)
100 static bool warned{
false};
103 ignwarn <<
"Trying to serialize component with data type [" 104 <<
typeid(DataType).name() <<
"], which doesn't have " 105 <<
"`operator<<`. Component will not be serialized." << std::endl;
119 template<
typename DataType,
typename Identifier,
121 decltype(std::declval<std::istream &>() >> std::declval<DataType &>()),
122 typename std::enable_if<
124 std::is_convertible<Stream, std::istream &>::value,
141 template<
typename DataType,
typename Identifier,
143 decltype(std::declval<std::istream &>() >> std::declval<
144 typename DataType::element_type &>()),
145 typename std::enable_if<
147 std::is_convertible<Stream, std::istream &>::value,
149 std::istream &
fromStream(std::istream &_in, DataType &_data)
163 template<
typename DataType,
typename Identifier,
typename... Ignored>
164 std::istream &
fromStream(std::istream &_in, DataType
const &,
165 Ignored
const &..., ...)
167 static bool warned{
false};
170 ignwarn <<
"Trying to deserialize component with data type [" 171 <<
typeid(DataType).name() <<
"], which doesn't have " 172 <<
"`operator>>`. Component will not be deserialized." << std::endl;
183 inline namespace IGNITION_GAZEBO_VERSION_NAMESPACE {
192 using NoData = std::add_lvalue_reference<void>;
245 ignwarn <<
"Trying to serialize copmponent which hasn't implemented " 246 <<
"the `Serialize` function. Component will not be serialized." 260 ignwarn <<
"Trying to deserialize copmponent which hasn't implemented " 261 <<
"the `Deserialize` function. Component will not be " 262 <<
"deserialized." << std::endl;
289 template <
typename DataType,
typename Identifier>
297 public:
explicit Component(
const DataType &_data);
301 public:
explicit Component(DataType &&_data);
318 Component &&_component) noexcept =
default;
328 public:
bool operator==(
const Component &_component)
const;
333 public:
bool operator!=(
const Component &_component)
const;
339 public:
void Serialize(std::ostream &_out)
const override;
342 public:
void Deserialize(std::istream &_in)
override;
346 public: DataType &Data();
350 public:
const DataType &Data()
const;
353 private: std::unique_ptr<ComponentPrivate<DataType>> dataPtr;
368 template <
typename Identifier>
404 public: uint64_t TypeId()
const override;
411 template <
typename DataType>
420 : data(std::move(_data))
429 template <
typename DataType,
typename Identifier>
436 template <
typename DataType,
typename Identifier>
443 template <
typename DataType,
typename Identifier>
450 template <
typename DataType,
typename Identifier>
459 template <
typename DataType,
typename Identifier>
462 return this->dataPtr->data;
466 template <
typename DataType,
typename Identifier>
469 return this->dataPtr->data;
473 template <
typename DataType,
typename Identifier>
477 this->dataPtr->data = _component.
Data();
482 template <
typename DataType,
typename Identifier>
486 return this->dataPtr->data == _component.
Data();
490 template <
typename DataType,
typename Identifier>
494 return this->dataPtr->data != _component.
Data();
498 template <
typename DataType,
typename Identifier>
501 toStream<DataType, Identifier>(_out, this->
Data());
505 template <
typename DataType,
typename Identifier>
508 fromStream<DataType, Identifier>(_in, this->
Data());
512 template <
typename DataType,
typename Identifier>
519 template <
typename Identifier>
527 template <
typename Identifier>
535 template <
typename Identifier>
friend std::ostream & operator<<(std::ostream &_out, const BaseComponent &_component)
Stream insertion operator. It exposes the component's serialized state which can be recreated by oper...
Definition: Component.hh:212
DataType data
The data being wrapped.
Definition: Component.hh:425
Component()
Default constructor.
Definition: Component.hh:430
bool operator!=(const Component &_component) const
Inequality operator.
Definition: Component.hh:492
static ComponentTypeId typeId
Unique ID for this component type. This is set through the Factory registration.
Definition: Component.hh:357
bool operator==(const Component &_component) const
Equality operator.
Definition: Component.hh:484
std::add_lvalue_reference< void > NoData
Convenient type to be used by components that don't wrap any data. I.e. they act as tags and their pr...
Definition: Component.hh:192
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:499
ComponentTypeId TypeId() const override
Returns the unique ID for the component's type. The ID is derived from the name that is manually chos...
Definition: Component.hh:513
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:258
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
std::ostream & toStream(std::ostream &_out, DataType const &_data)
Helper template to call stream operators only on types that support them. This version is called for ...
Definition: Component.hh:59
friend std::istream & operator>>(std::istream &_in, BaseComponent &_component)
Stream extraction operator. It parses the component's serialized state which is created by operator<<...
Definition: Component.hh:228
std::istream & fromStream(std::istream &_in, DataType &_data)
Helper template to call extract operators only on types that support them. This version is called for...
Definition: Component.hh:126
friend std::ostream & operator<<(std::ostream &_out, const Component< NoData, Identifier > &)
Components with no data are always serialize to an empty string.
Definition: Component.hh:387
Helper trait to determine if a type is shared_ptr or not.
Definition: Component.hh:33
Component & operator=(Component &&_component) noexcept=default
Move assignment operator.
uint64_t ComponentTypeId
A unique identifier for a component type. A component type must be derived from components::BaseCompo...
Definition: Types.hh:71
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:506
Specialization for components that don't wrap any data. This class to be used to create simple compon...
Definition: Component.hh:369
ComponentPrivate(DataType _data)
Constructor.
Definition: Component.hh:419
Definition: Component.hh:187
A component type that wraps any data type. The intention is for this class to be used to create simpl...
Definition: Component.hh:290
friend std::istream & operator>>(std::istream &_in, Component< NoData, Identifier > &)
Components with no data are always serialize to an empty string.
Definition: Component.hh:397
This library is part of the Ignition Robotics project.
Definition: Altimeter.hh:25
DataType & Data()
Get the mutable component data.
Definition: Component.hh:460
Base class for all components.
Definition: Component.hh:195