source: rrlib_rtti/tGenericObject.cpp @ 56:bf4a5a235e00

Last change on this file since 56:bf4a5a235e00 was 56:bf4a5a235e00, checked in by Max Reichardt <mreichardt@…>, 5 years ago

Made rrlib_xml optional. Binaries that initialize types are only determined on linux systems.

File size: 3.9 KB
Line 
1//
2// You received this file as part of RRLib
3// Robotics Research Library
4//
5// Copyright (C) Finroc GbR (finroc.org)
6//
7// This program is free software; you can redistribute it and/or modify
8// it under the terms of the GNU General Public License as published by
9// the Free Software Foundation; either version 2 of the License, or
10// (at your option) any later version.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License along
18// with this program; if not, write to the Free Software Foundation, Inc.,
19// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20//
21//----------------------------------------------------------------------
22/*!\file    rrlib/rtti/tGenericObject.cpp
23 *
24 * \author  Max Reichardt
25 *
26 * \date    2013-05-18
27 *
28 */
29//----------------------------------------------------------------------
30#include "rrlib/rtti/tGenericObject.h"
31
32//----------------------------------------------------------------------
33// External includes (system with <>, local with "")
34//----------------------------------------------------------------------
35
36//----------------------------------------------------------------------
37// Internal includes with ""
38//----------------------------------------------------------------------
39
40//----------------------------------------------------------------------
41// Debugging
42//----------------------------------------------------------------------
43#include <cassert>
44
45//----------------------------------------------------------------------
46// Namespace usage
47//----------------------------------------------------------------------
48
49//----------------------------------------------------------------------
50// Namespace declaration
51//----------------------------------------------------------------------
52namespace rrlib
53{
54namespace rtti
55{
56
57//----------------------------------------------------------------------
58// Forward declarations / typedefs / enums
59//----------------------------------------------------------------------
60
61//----------------------------------------------------------------------
62// Const values
63//----------------------------------------------------------------------
64
65//----------------------------------------------------------------------
66// Implementation
67//----------------------------------------------------------------------
68
69void tGenericObject::Deserialize(serialization::tInputStream& stream, serialization::tDataEncoding enc)
70{
71  if (enc == serialization::tDataEncoding::BINARY)
72  {
73    Deserialize(stream);
74  }
75  else if (enc == serialization::tDataEncoding::STRING)
76  {
77    serialization::tStringInputStream sis(stream.ReadString());
78    Deserialize(sis);
79  }
80  else
81  {
82#ifdef _LIB_RRLIB_XML_PRESENT_
83    std::string s = stream.ReadString();
84    xml::tDocument d(s.c_str(), s.length(), false);
85    xml::tNode& n = d.RootNode();
86    Deserialize(n);
87#else
88    throw std::invalid_argument("XML support not available");
89#endif
90  }
91}
92
93void tGenericObject::Serialize(serialization::tOutputStream& stream, serialization::tDataEncoding enc) const
94{
95  if (enc == serialization::tDataEncoding::BINARY)
96  {
97    Serialize(stream);
98  }
99  else if (enc == serialization::tDataEncoding::STRING)
100  {
101    serialization::tStringOutputStream sos;
102    Serialize(sos);
103    stream.WriteString(sos.ToString());
104  }
105  else
106  {
107#ifdef _LIB_RRLIB_XML_PRESENT_
108    xml::tDocument d;
109    xml::tNode& n = d.AddRootNode("value");
110    Serialize(n);
111    stream.WriteString(n.GetXMLDump(true));
112#else
113    throw std::invalid_argument("XML support not available");
114#endif
115  }
116}
117
118//----------------------------------------------------------------------
119// End of namespace declaration
120//----------------------------------------------------------------------
121}
122}
Note: See TracBrowser for help on using the repository browser.