Electroneum
archivertest.cpp File Reference
#include "archiver.h"
#include <iostream>
#include <vector>
Include dependency graph for archivertest.cpp:

Go to the source code of this file.

Classes

struct  Student
 
struct  Group
 
class  Shape
 
class  Circle
 
class  Box
 
class  Canvas
 

Functions

template<typename Archiver >
Archiveroperator & (Archiver &ar, Student &s)
 
std::ostream & operator<< (std::ostream &os, const Student &s)
 
void test1 ()
 
template<typename Archiver >
Archiveroperator & (Archiver &ar, Group &g)
 
std::ostream & operator<< (std::ostream &os, const Group &g)
 
void test2 ()
 
template<typename Archiver >
Archiveroperator & (Archiver &ar, Shape &s)
 
template<typename Archiver >
Archiveroperator & (Archiver &ar, Circle &c)
 
template<typename Archiver >
Archiveroperator & (Archiver &ar, Box &b)
 
template<typename Archiver >
Archiveroperator & (Archiver &ar, Shape *&shape)
 
template<typename Archiver >
Archiveroperator & (Archiver &ar, Canvas &c)
 
void test3 ()
 
int main ()
 

Function Documentation

◆ main()

int main ( )

Definition at line 283 of file archivertest.cpp.

283  {
284  test1();
285  test2();
286  test3();
287 }
void test3()
void test2()
void test1()
Here is the call graph for this function:

◆ operator &() [1/7]

template<typename Archiver >
Archiver& operator& ( Archiver ar,
Student s 
)

Definition at line 21 of file archivertest.cpp.

21  {
22  ar.StartObject();
23  ar.Member("name") & s.name;
24  ar.Member("age") & s.age;
25  ar.Member("height") & s.height;
26  ar.Member("canSwim") & s.canSwim;
27  return ar.EndObject();
28 }
double height
std::string name
bool canSwim
unsigned age

◆ operator &() [2/7]

template<typename Archiver >
Archiver& operator& ( Archiver ar,
Group g 
)

Definition at line 68 of file archivertest.cpp.

68  {
69  ar.StartObject();
70 
71  ar.Member("groupName");
72  ar & g.groupName;
73 
74  ar.Member("students");
75  size_t studentCount = g.students.size();
76  ar.StartArray(&studentCount);
77  if (ar.IsReader)
78  g.students.resize(studentCount);
79  for (size_t i = 0; i < studentCount; i++)
80  ar & g.students[i];
81  ar.EndArray();
82 
83  return ar.EndObject();
84 }
std::string groupName
std::vector< Student > students

◆ operator &() [3/7]

template<typename Archiver >
Archiver& operator& ( Archiver ar,
Shape s 
)

Definition at line 143 of file archivertest.cpp.

143  {
144  ar.Member("x") & s.x_;
145  ar.Member("y") & s.y_;
146  return ar;
147 }
double x_
double y_

◆ operator &() [4/7]

template<typename Archiver >
Archiver& operator& ( Archiver ar,
Circle c 
)

Definition at line 169 of file archivertest.cpp.

169  {
170  ar & static_cast<Shape&>(c);
171  ar.Member("radius") & c.radius_;
172  return ar;
173 }

◆ operator &() [5/7]

template<typename Archiver >
Archiver& operator& ( Archiver ar,
Box b 
)

Definition at line 195 of file archivertest.cpp.

195  {
196  ar & static_cast<Shape&>(b);
197  ar.Member("width") & b.width_;
198  ar.Member("height") & b.height_;
199  return ar;
200 }

◆ operator &() [6/7]

template<typename Archiver >
Archiver& operator& ( Archiver ar,
Shape *&  shape 
)

Definition at line 229 of file archivertest.cpp.

229  {
230  std::string type = ar.IsReader ? "" : shape->GetType();
231  ar.StartObject();
232  ar.Member("type") & type;
233  if (type == "Circle") {
234  if (ar.IsReader) shape = new Circle;
235  ar & static_cast<Circle&>(*shape);
236  }
237  else if (type == "Box") {
238  if (ar.IsReader) shape = new Box;
239  ar & static_cast<Box&>(*shape);
240  }
241  return ar.EndObject();
242 }
::std::string string
Definition: gtest-port.h:1097
virtual const char * GetType() const =0
Here is the call graph for this function:

◆ operator &() [7/7]

template<typename Archiver >
Archiver& operator& ( Archiver ar,
Canvas c 
)

Definition at line 245 of file archivertest.cpp.

245  {
246  size_t shapeCount = c.shapes_.size();
247  ar.StartArray(&shapeCount);
248  if (ar.IsReader) {
249  c.Clear();
250  c.shapes_.resize(shapeCount);
251  }
252  for (size_t i = 0; i < shapeCount; i++)
253  ar & c.shapes_[i];
254  return ar.EndArray();
255 }
void Clear()
Here is the call graph for this function:

◆ operator<<() [1/2]

std::ostream& operator<< ( std::ostream &  os,
const Student s 
)

Definition at line 30 of file archivertest.cpp.

30  {
31  return os << s.name << " " << s.age << " " << s.height << " " << s.canSwim;
32 }
double height
std::string name
bool canSwim
unsigned age

◆ operator<<() [2/2]

std::ostream& operator<< ( std::ostream &  os,
const Group g 
)

Definition at line 86 of file archivertest.cpp.

86  {
87  os << g.groupName << std::endl;
88  for (std::vector<Student>::const_iterator itr = g.students.begin(); itr != g.students.end(); ++itr)
89  os << *itr << std::endl;
90  return os;
91 }
std::string groupName
std::vector< Student > students

◆ test1()

void test1 ( )

Definition at line 34 of file archivertest.cpp.

34  {
36 
37  // Serialize
38  {
39  Student s("Lua", 9, 150.5, true);
40 
41  JsonWriter writer;
42  writer & s;
43  json = writer.GetString();
44  std::cout << json << std::endl;
45  }
46 
47  // Deserialize
48  {
49  Student s;
50  JsonReader reader(json.c_str());
51  reader & s;
52  std::cout << s << std::endl;
53  }
54 }
const char * GetString() const
Obtains the serialized JSON string.
Definition: archiver.cpp:226
::std::string string
Definition: gtest-port.h:1097
Represents a JSON reader which implements Archiver concept.
Definition: archiver.h:56
rapidjson::Document json
Definition: transport.cpp:49
Here is the call graph for this function:
Here is the caller graph for this function:

◆ test2()

void test2 ( )

Definition at line 93 of file archivertest.cpp.

93  {
95 
96  // Serialize
97  {
98  Group g;
99  g.groupName = "Rainbow";
100 
101  Student s1("Lua", 9, 150.5, true);
102  Student s2("Mio", 7, 120.0, false);
103  g.students.push_back(s1);
104  g.students.push_back(s2);
105 
106  JsonWriter writer;
107  writer & g;
108  json = writer.GetString();
109  std::cout << json << std::endl;
110  }
111 
112  // Deserialize
113  {
114  Group g;
115  JsonReader reader(json.c_str());
116  reader & g;
117  std::cout << g << std::endl;
118  }
119 }
const char * GetString() const
Obtains the serialized JSON string.
Definition: archiver.cpp:226
::std::string string
Definition: gtest-port.h:1097
Represents a JSON reader which implements Archiver concept.
Definition: archiver.h:56
std::string groupName
std::vector< Student > students
rapidjson::Document json
Definition: transport.cpp:49
Here is the call graph for this function:
Here is the caller graph for this function:

◆ test3()

void test3 ( )

Definition at line 257 of file archivertest.cpp.

257  {
259 
260  // Serialize
261  {
262  Canvas c;
263  c.AddShape(new Circle(1.0, 2.0, 3.0));
264  c.AddShape(new Box(4.0, 5.0, 6.0, 7.0));
265 
266  JsonWriter writer;
267  writer & c;
268  json = writer.GetString();
269  std::cout << json << std::endl;
270  }
271 
272  // Deserialize
273  {
274  Canvas c;
275  JsonReader reader(json.c_str());
276  reader & c;
277  c.Print(std::cout);
278  }
279 }
const char * GetString() const
Obtains the serialized JSON string.
Definition: archiver.cpp:226
::std::string string
Definition: gtest-port.h:1097
void AddShape(Shape *shape)
Represents a JSON reader which implements Archiver concept.
Definition: archiver.h:56
void Print(std::ostream &os)
rapidjson::Document json
Definition: transport.cpp:49
Here is the call graph for this function:
Here is the caller graph for this function: