17 #ifndef GZ_SIM_COMPONENTS_COMPONENT_HH_
18 #define GZ_SIM_COMPONENTS_COMPONENT_HH_
26 #include <gz/common/Console.hh>
28 #include <gz/sim/config.hh>
29 #include <gz/sim/Export.hh>
38 inline namespace GZ_SIM_VERSION_NAMESPACE {
61 template <
typename Stream,
typename DataType>
64 private:
template <
typename StreamArg,
typename DataTypeArg>
65 static auto Test(
int _test)
66 -> decltype(std::declval<StreamArg &>()
69 private:
template <
typename,
typename>
72 public:
static constexpr
bool value =
73 decltype(Test<Stream, DataType>(
true))::value;
85 template <
typename Stream,
typename DataType>
88 private:
template <
typename StreamArg,
typename DataTypeArg>
89 static auto Test(
int _test)
90 -> decltype(std::declval<StreamArg &>() >> std::declval<DataTypeArg &>(),
93 private:
template <
typename,
typename>
96 public:
static constexpr
bool value =
97 decltype(Test<Stream, DataType>(0))::value;
101 namespace serializers
107 template <
typename DataType>
112 const DataType &_data)
118 typename DataType::element_type>::value)
124 static bool warned{
false};
127 gzwarn <<
"Trying to serialize component with data type ["
128 <<
typeid(DataType).name() <<
"], which doesn't have "
129 <<
"`operator<<`. Component will not be serialized."
141 static bool warned{
false};
144 gzwarn <<
"Trying to serialize component with data type ["
145 <<
typeid(DataType).name() <<
"], which doesn't have "
146 <<
"`operator<<`. Component will not be serialized."
163 typename DataType::element_type>::value)
169 static bool warned{
false};
172 gzwarn <<
"Trying to deserialize component with data type ["
173 <<
typeid(DataType).name() <<
"], which doesn't have "
174 <<
"`operator>>`. Component will not be deserialized."
186 static bool warned{
false};
189 gzwarn <<
"Trying to deserialize component with data type ["
190 <<
typeid(DataType).name() <<
"], which doesn't have "
191 <<
"`operator>>`. Component will not be deserialized."
209 namespace serializers
247 static bool warned{
false};
250 gzwarn <<
"Trying to serialize component of type [" << this->TypeId()
251 <<
"], which hasn't implemented the `Serialize` function. "
252 <<
"Component will not be serialized." <<
std::endl;
266 static bool warned{
false};
269 gzwarn <<
"Trying to deserialize component of type ["
270 << this->TypeId() <<
"], which hasn't implemented the "
271 <<
"`Deserialize` function. Component will not be deserialized."
322 template <
typename DataType,
typename Identifier,
334 public:
explicit Component(DataType _data);
347 public:
bool operator!=(
const Component &_component)
const;
356 public:
void Serialize(
std::ostream &_out)
const override;
366 public: DataType &Data();
373 public:
bool SetData(
const DataType &_data,
375 bool(
const DataType &,
const DataType &)> &_eql);
379 public:
const DataType &Data()
const;
382 private: DataType data;
390 public:
inline static const char *typeName{
nullptr};
401 template <
typename Identifier,
typename Serializer>
409 Serializer> &_component)
const;
416 Serializer> &_component)
const;
425 public:
void Serialize(
std::ostream &_out)
const override;
436 public:
inline static const char *typeName{
nullptr};
440 template <
typename DataType,
typename Identifier,
typename Serializer>
442 : data(
std::move(_data))
447 template <
typename DataType,
typename Identifier,
typename Serializer>
454 template <
typename DataType,
typename Identifier,
typename Serializer>
456 const DataType &_data,
457 const std::function<
bool(
const DataType &,
const DataType &)> &_eql)
459 bool result = !_eql(_data, this->data);
465 template <
typename DataType,
typename Identifier,
typename Serializer>
472 template <
typename DataType,
typename Identifier,
typename Serializer>
476 return this->data == _component.
Data();
480 template <
typename DataType,
typename Identifier,
typename Serializer>
484 return this->data != _component.
Data();
488 template <
typename DataType,
typename Identifier,
typename Serializer>
492 Serializer::Serialize(_out, this->Data());
496 template <
typename DataType,
typename Identifier,
typename Serializer>
500 Serializer::Deserialize(_in, this->Data());
504 template <
typename DataType,
typename Identifier,
typename Serializer>
509 return std::make_unique<Component<DataType, Identifier, Serializer>>(
514 template <
typename DataType,
typename Identifier,
typename Serializer>
521 template <
typename Identifier,
typename Serializer>
529 template <
typename Identifier,
typename Serializer>
537 template <
typename Identifier,
typename Serializer>
541 return std::make_unique<Component<NoData, Identifier, Serializer>>();
545 template <
typename Identifier,
typename Serializer>
552 template <
typename Identifier,
typename Serializer>
556 Serializer::Serialize(_out);
560 template <
typename Identifier,
typename Serializer>
564 Serializer::Deserialize(_in);