gtsam  3.2.1
gtsam
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
serializationTestHelpers.h
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
20 #pragma once
21 
22 #include <sstream>
23 #include <string>
24 
26 
27 // whether to print the serialized text to stdout
28 const bool verbose = false;
29 
30 namespace gtsam {
31 namespace serializationTestHelpers {
32 
33 
34 // Templated round-trip serialization
35 template<class T>
36 void roundtrip(const T& input, T& output) {
37  // Serialize
38  std::string serialized = serialize(input);
39  if (verbose) std::cout << serialized << std::endl << std::endl;
40 
41  deserialize(serialized, output);
42 }
43 
44 // This version requires equality operator
45 template<class T>
46 bool equality(const T& input = T()) {
47  T output;
48  roundtrip<T>(input,output);
49  return input==output;
50 }
51 
52 // This version requires equals
53 template<class T>
54 bool equalsObj(const T& input = T()) {
55  T output;
56  roundtrip<T>(input,output);
57  return input.equals(output);
58 }
59 
60 // De-referenced version for pointers
61 template<class T>
62 bool equalsDereferenced(const T& input) {
63  T output;
64  roundtrip<T>(input,output);
65  return input->equals(*output);
66 }
67 
68 // Templated round-trip serialization using XML
69 template<class T>
70 void roundtripXML(const T& input, T& output) {
71  // Serialize
72  std::string serialized = serializeXML<T>(input);
73  if (verbose) std::cout << serialized << std::endl << std::endl;
74 
75  // De-serialize
76  deserializeXML(serialized, output);
77 }
78 
79 // This version requires equality operator
80 template<class T>
81 bool equalityXML(const T& input = T()) {
82  T output;
83  roundtripXML<T>(input,output);
84  return input==output;
85 }
86 
87 // This version requires equals
88 template<class T>
89 bool equalsXML(const T& input = T()) {
90  T output;
91  roundtripXML<T>(input,output);
92  return input.equals(output);
93 }
94 
95 // This version is for pointers
96 template<class T>
97 bool equalsDereferencedXML(const T& input = T()) {
98  T output;
99  roundtripXML<T>(input,output);
100  return input->equals(*output);
101 }
102 
103 // Templated round-trip serialization using XML
104 template<class T>
105 void roundtripBinary(const T& input, T& output) {
106  // Serialize
107  std::string serialized = serializeBinary<T>(input);
108  if (verbose) std::cout << serialized << std::endl << std::endl;
109 
110  // De-serialize
111  deserializeBinary(serialized, output);
112 }
113 
114 // This version requires equality operator
115 template<class T>
116 bool equalityBinary(const T& input = T()) {
117  T output;
118  roundtripBinary<T>(input,output);
119  return input==output;
120 }
121 
122 // This version requires equals
123 template<class T>
124 bool equalsBinary(const T& input = T()) {
125  T output;
126  roundtripBinary<T>(input,output);
127  return input.equals(output);
128 }
129 
130 // This version is for pointers
131 template<class T>
132 bool equalsDereferencedBinary(const T& input = T()) {
133  T output;
134  roundtripBinary<T>(input,output);
135  return input->equals(*output);
136 }
137 
138 } // \namespace serializationTestHelpers
139 } // \namespace gtsam
140 
Convenience functions for serializing data structures via boost.serialization.