47 #ifndef _MIRA_XMLSERIALIZER_H_ 48 #define _MIRA_XMLSERIALIZER_H_ 55 #include <boost/algorithm/string.hpp> 56 #include <boost/range/algorithm.hpp> 57 #include <boost/range/algorithm_ext/erase.hpp> 73 inline std::string replaceSpaces(
const std::string& s)
75 return boost::replace_all_copy(s,
" ",
"_");
78 inline std::string replaceSpecialChars(
const std::string& s)
80 std::string result = boost::replace_all_copy(s,
" ",
"_");
81 boost::remove_erase_if(result, boost::is_any_of(
"[]"));
145 mRoot(iXmlDom.root()), mNode(iXmlDom.root()), mOutputFormat(
format)
156 mRoot(iRoot), mNode(iRoot), mOutputFormat(
format)
162 template <
typename T>
165 return this->version<T>(
version,
false);
175 template <
typename T>
178 return this->version<T>(
version,
true);
183 template <
typename T>
186 version = this->
template queryDesiredClassVersion<T>(
version, acceptDesiredVersion);
189 if ((vf == 0) || (vf == 1))
233 if(!mPointerClass.empty()) {
235 mPointerClass.clear();
242 assert(mNode!=mRoot.
end());
254 mPointerClass =
type;
277 std::string mPointerClass;
323 mRoot(iXmlDom.root()), mNode(iXmlDom.root())
332 mRoot(iRoot), mNode(iRoot)
339 template <
typename T>
342 mDeserializeFromThisNode=
false;
343 mNextFindIteratorStack.clear();
346 Base::deserialize(name,value);
348 MIRA_RETHROW(ex,
"in tag starting at: " << mNode.uri() <<
"(" << mNode.line() <<
")");
357 template <
typename T>
360 mDeserializeFromThisNode=
true;
361 mNextFindIteratorStack.clear();
364 Base::deserialize(name,value);
367 <<
"(" << mNode.line() <<
")");
374 template <
typename T>
379 int vf = this->forcedDeserializeVersion();
380 if ((vf == 0) || (vf == 1))
381 return versionLegacy(expectedVersion);
383 std::string
type = typeName<T>();
384 for (
int n = 0;
true; ++n) {
386 if (versionNode == mNode.
cend()) {
387 if (mNode.has_attribute(
"version")) {
389 "but read an anonymous version attribute instead. " 390 "Please update your config xml (" 391 << mNode.uri() <<
" , line " << mNode.line() <<
").";
393 VersionType version = mNode.get_attribute<
int>(
"version");
394 checkVersion<T>(version, expectedVersion);
404 checkVersion<T>(version, expectedVersion);
414 return versionLegacy(expectedVersion);
417 template <
typename T>
420 return this->version<T>(expectedVersion);
427 VersionType version = mNode.get_attribute<
int>(
"version", 0);
428 checkVersion<void>(version, expectedVersion);
432 template <
typename T>
435 if (version > expectedVersion) {
436 MIRA_LOG(
WARNING) <<
"Trying to deserialize XML data of a newer version (" << (int)version <<
437 ") of type " << typeName<T>() <<
" into an older version (" << (int)expectedVersion <<
").";
446 Base::atomic(member);
448 if(mAtomicFromAttribute.empty())
449 member = fromString<T>(*mNode.content_begin());
452 member = mNode.get_attribute<T>(mAtomicFromAttribute);
457 if(meta.
name!=NULL) {
458 if(!mAtomicFromAttribute.empty()) {
459 MIRA_THROW(XIO,
"Cannot deserialize '" << mAtomicFromAttribute <<
460 "' from an attribute as it is a complex type, that " 461 "must be stored as an XML node.");
471 << Private::replaceSpaces(meta.
name)
480 if(meta.
name!=NULL) {
481 assert(mNode!=mRoot);
483 mNode = mNode.parent();
484 assert(mNode!=mRoot.end());
501 if(mNode.get_attribute<std::string>(
"nullptr",
"false") ==
"true") {
507 std::string ref = mNode.get_attribute<std::string>(
"ref",
"");
510 pointer = resolveReference<T>(ref);
515 Base::pointer(pointer);
520 return mNode.get_attribute<std::string>(
"class",
"");
528 bool fromThisNode = mDeserializeFromThisNode;
529 bool fromAttribute =
false;
534 }
catch(XMemberNotFound& ex) {
540 std::string
id = Private::replaceSpecialChars(meta.
id);
548 if(!mNode.has_attribute(
id)) {
549 MIRA_THROW(XMemberNotFound,
"Node (or attribute) '" <<
id <<
552 mAtomicFromAttribute = id;
553 fromAttribute =
true;
560 MIRA_RETHROW(ex,
"in tag starting at: " << mNode.uri() <<
"(" << mNode.line() <<
")");
566 mDeserializeFromThisNode=
false;
569 Base::invokeMemberOverwrite(member, meta);
574 mAtomicFromAttribute.clear();
575 if(!fromThisNode && !fromAttribute) {
577 rewindFindIterator(meta.
getName());
584 mAtomicFromAttribute.clear();
585 if(!fromThisNode && !fromAttribute)
601 bool mDeserializeFromThisNode;
608 std::string mAtomicFromAttribute;
617 typedef std::map<std::string, XMLDom::const_iterator> NextFindIteratorMap;
618 std::list<NextFindIteratorMap> mNextFindIteratorStack;
622 assert(mNextFindIteratorStack.size()>=1);
623 NextFindIteratorMap& m = mNextFindIteratorStack.back();
629 NextFindIteratorMap::iterator p = m.
find(name);
636 i = find(j, mNode.
end(), name);
648 mNextFindIteratorStack.push_back(NextFindIteratorMap());
654 mNextFindIteratorStack.pop_back();
657 void rewindFindIterator(
const std::string& name)
659 assert(mNextFindIteratorStack.size()>=1);
660 NextFindIteratorMap& m = mNextFindIteratorStack.back();
662 NextFindIteratorMap::iterator p = m.find(name);
666 if (m[name] == mNode.
begin())
679 namespace serialization {
690 template<
typename Collection>
691 struct ReflectCollectionCount<XMLSerializer, Collection>
693 static void reflect(XMLSerializer& r, uint32& ioCount)
699 template<
typename Collection,
bool HasKeyType = true,
bool HasMappedType = true>
711 template<
typename Collection>
712 struct CheckKeyCount<Collection,
713 (bool)!
std::is_void_v<typename Collection::key_type>,
714 (bool)!std::is_void_v<typename Collection::mapped_type>>
718 return boost::algorithm::starts_with(attr.first,
"key");
724 uint32 keyCount = (uint32)std::count(node.begin(), node.end(),
"key");
728 keyCount = (uint32)std::count_if(node.attribute_cbegin(), node.attribute_cend(),
731 if (keyCount != itemCount) {
732 MIRA_THROW(XIO,
"Node for map (or map-like collection) has " << keyCount
733 <<
" <key> tags and " << itemCount <<
" <item> tags.");
744 template<
typename Collection>
745 struct ReflectCollectionCount<XMLDeserializer, Collection>
749 return boost::algorithm::starts_with(attr.first,
"item");
753 static void reflect(XMLDeserializer& r, uint32& ioCount)
758 ioCount = (uint32)std::count(node.begin(), node.end(),
"item");
762 ioCount = (uint32)std::count_if(node.attribute_cbegin(), node.attribute_cend(),
766 CheckKeyCount<Collection>::check(node, ioCount);
770 template<
typename Reflector,
typename Container>
struct ReflectReadMapItems;
776 template<
typename Container>
777 struct ReflectReadMapItems<XMLSerializer, Container>
779 typedef typename Container::value_type
value_type;
780 typedef typename Container::key_type key_type;
782 static void reflect(XMLSerializer& r, Container& c)
788 key_type& nonconstkey =
const_cast<key_type&
>(p.first);
801 template<
typename Reflector,
typename Container>
struct ReflectWriteMapItems;
807 template<
typename Container>
808 struct ReflectWriteMapItems<XMLDeserializer, Container>
810 typedef typename Container::iterator
iterator;
811 typedef typename Container::value_type value_type;
812 typedef typename Container::key_type key_type;
813 typedef typename Container::mapped_type mapped_type;
815 static void reflect(XMLDeserializer& r, Container& c, uint32 count)
822 for(uint32
id=0;
id<count; ++id) {
824 if(IsPointerOrSharedPointer<key_type>::value) {
832 iterator it = c.insert(hint, value_type(key, mapped_type()));
This object can use object tracking internally, but the object tracking system's state remains unchan...
Definition: ReflectControlFlags.h:82
T get_attribute(const std::string &name) const
Return an attribute value cast to a given type.
Definition: XMLDom.h:633
Provides type trait that indicates whether a type is a pointer type or a shared pointer.
A STL conform DOM reader/writer for XML.
Container::iterator iterator
Definition: StlCollections.h:393
void object(T &member)
Is called for each complex object.
Definition: RecursiveMemberReflector.h:301
A STL conform wrapper for libxml2 to read XML files as DOM.
Definition: XMLDom.h:73
const_sibling_iterator end() const
Get the const end node sibling_iterator.
Definition: XMLDom.h:542
MIRA_DEPRECATED("Please call as version<MyType>(v) or version(v, this)", VersionType version(VersionType expectedVersion))
Definition: XMLSerializer.h:410
Contains the Serializer template, a base class for all serializers.
std::string pointerClassType()
Definition: XMLSerializer.h:518
#define MIRA_PROPERTY_WITH_ID(reflector, name, id, var,...)
Definition: ReflectorInterface.h:1009
typename ReflectorInterface< XMLSerializer >::AcceptDesiredVersion AcceptDesiredVersion
Definition: AbstractReflector.h:195
void deserialize(const std::string &name, T &value)
overwritten from Deserializer, to handle exceptions and to add additional information about file name...
Definition: XMLSerializer.h:340
Definition: XMLSerializer.h:121
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
void member(const char *name, T &member, const char *comment, ReflectCtrlFlags flags=REFLECT_CTRLFLAG_NONE)
Definition: RecursiveMemberReflector.h:862
OutputFormat format
Definition: XMLSerializer.h:144
const std::string & getHumanReadableFullID(int objectID) const
Returns the full human readable object id / name for the given internal objectID. ...
Definition: Serializer.h:456
XMLDom::iterator getNode()
Accessor for special serialize/deserialize methods below.
Definition: XMLSerializer.h:267
VersionType version(VersionType version, AcceptDesiredVersion, const T *caller=NULL)
Definition: XMLSerializer.h:176
Is a special reflector that is used for serialization.
Definition: Serializer.h:126
XMLDeserializer(const XMLDom::const_iterator &iRoot)
Create a new XML deserialization on top of the passed XMLDom iterator node.
Definition: XMLSerializer.h:331
#define MIRA_LOG(level)
Use this macro to log data.
Definition: LoggingCore.h:529
#define MIRA_RETHROW(ex, msg)
Macro for rethrowing an exception with file and line information and for adding additional informatio...
Definition: Exception.h:148
No comments are written to output document.
Definition: XMLSerializer.h:132
const_content_iterator content_begin() const
Get the const iterator to the first content.
Definition: XMLDom.h:557
sibling_iterator end()
Get the end node sibling_iterator.
VersionType version(VersionType version, const T *caller=NULL)
Definition: XMLSerializer.h:163
uint8 VersionType
Definition: ReflectorInterface.h:72
#define MIRA_THROW(ex, msg)
Macro for throwing an exception.
Definition: Exception.h:82
std::string toString(const T &value, int precision=-1)
Converts any data type to string (the data type must support the stream << operator).
Definition: ToString.h:256
Const sibling_iterator for iterating over xml nodes that have the same parent (siblings) ...
Definition: XMLDom.h:671
Serializer for serializing objects in XML format.
Definition: XMLSerializer.h:119
const ReflectMemberMeta & getCurrentMemberMeta() const
Returns the meta-information of the current member that is reflected.
Definition: RecursiveMemberReflector.h:459
XMLDom::sibling_iterator & add_content(const std::string &content)
Add content to this node.
static void reflect(Reflector &r, uint32 &ioCount)
Definition: ReflectCollection.h:71
std::pair< std::string, std::string > Attribute
An XML attribute.
Definition: XMLDom.h:86
static void reflect(Reflector &r, Container &c)
Definition: StlCollections.h:343
static void reflect(Reflector &r, Container &c, uint32 count)
Definition: StlCollections.h:395
XMLDom::sibling_iterator & add_attribute(const Attribute &attribute)
Add an attribute to this node.
PropertyHint type(const std::string &t)
Sets the attribute "type" to the specified value.
Definition: PropertyHint.h:295
Default format.
Definition: XMLSerializer.h:131
Contains base class for all deserializers.
void endTag(const ReflectMemberMeta &meta)
Definition: XMLSerializer.h:478
Container::value_type value_type
Definition: StlCollections.h:341
OutputFormat
Flags for XML output format.
Definition: XMLSerializer.h:129
const_sibling_iterator begin() const
Get the const sibling_iterator to the first sub node.
Definition: XMLDom.h:516
void pointerNull()
Definition: XMLSerializer.h:259
void invokeMemberOverwrite(T &member, const ReflectMemberMeta &meta)
Definition: XMLSerializer.h:526
const_sibling_iterator cend() const
Get the const end node sibling_iterator.
XMLDom::sibling_iterator & add_comment(const std::string &comment)
Add a comment to this node.
void pointer(T *&pointer)
Definition: XMLSerializer.h:498
Iterator for iterating over xml nodes that have the same parent (sibligs)
Definition: XMLDom.h:758
void object(T &member)
Definition: XMLSerializer.h:221
Base class for exceptions.
Definition: Exception.h:199
Base::VersionType VersionType
Definition: Serializer.h:154
serialization::VersionType VersionType
Definition: XMLSerializer.h:372
VersionType version(VersionType expectedVersion, const T *caller=NULL)
Definition: XMLSerializer.h:375
typename ReflectorInterface< XMLSerializer >::VersionType VersionType
Definition: AbstractReflector.h:165
static int forcedSerializeVersion()
Returns either the version number from value of environment variable 'MIRA_FORCE_SERIALIZE_VERSION', or -1 (= do not force a version).
Definition: Serializer.h:145
void pointerWithClassType(const std::string &type)
Definition: XMLSerializer.h:253
const_sibling_iterator const_iterator
Definition: XMLDom.h:1113
Definition: LoggingCore.h:76
void atomic(T &member)
Definition: XMLSerializer.h:444
sibling_iterator parent()
Get the parent node sibling_iterator for this node.
VersionType version(VersionType expectedVersion, AcceptDesiredVersion, const T *caller=NULL)
Definition: XMLSerializer.h:418
#define MIRA_MEMBER_WITH_ID(reflector, name, id, var,...)
Macro that should be used to reflect a member if a manually specified ID is used. ...
Definition: ReflectorInterface.h:1003
atomic members are written as attributes instead of childs
Definition: XMLSerializer.h:133
void deserializeFromNode(const char *name, T &value)
In contrast to the deserialize() method this method will use the node that was specified in the const...
Definition: XMLSerializer.h:358
XMLDom::sibling_iterator add_child(const std::string &name, const NameSpace &ns=NameSpace())
Add a sub node to this node.
void atomic(T &member)
Definition: XMLSerializer.h:207
XMLDeserializer(const XMLDom &iXmlDom)
Create a new XML deserialization on top of the passed XMLDom document.
Definition: XMLSerializer.h:322
MIRA_DEPRECATED("Please call as version<MyType>(v) or version(v, this)", VersionType version(VersionType version))
Definition: XMLSerializer.h:168
void beginTag(const ReflectMemberMeta &meta)
Definition: XMLSerializer.h:455
MIRA_ENUM_TO_FLAGS_INCLASS(OutputFormat) XMLSerializer(XMLDom &iXmlDom
Create a new XML serialization on top of the passed XMLDom document.
void pointerReference(int referencedObjectID)
Definition: XMLSerializer.h:247
XMLDom::const_iterator getNode()
Definition: XMLSerializer.h:591
XMLSerializerTag Tag
Definition: XMLSerializer.h:126
Deserializer for serializing objects from XML format.
Definition: XMLSerializer.h:313
const_sibling_iterator find(const std::string &name, std::size_t nth=0) const
Find the nth subnode with a given name.