geolib2
serialization.cpp
Go to the documentation of this file.
1 #include "geolib/serialization.h"
2 
3 #include "geolib/Shape.h"
4 
5 #include <fstream>
6 
7 namespace geo {
8 
10 
12 
13 }
14 
16 }
17 
19  return shape->write(output);
20 }
21 
22 bool serialization::serialize(const Shape& shape, std::ostream& output) {
23  return shape.write(output);
24 }
25 
27  char shape_type[8];
28  input.read(shape_type, 8);
29 
30  deserializer_map::const_iterator s = deserializers_.find( shape_type );
31  if ( s == deserializers_.end() ) {
32  // input error: don't know how to deserialize the class
33  return ShapePtr();
34  }
35  return (s->second)( input ); // call the deserializer method
36 }
37 
38 void serialization::registerDeserializer(const std::string& shape_type, deserialization_method method ) {
39  std::string shape_type8 = " ";
40  for(unsigned int i = 0; i < std::min(shape_type.size(), shape_type8.size()); ++i) {
41  shape_type8[i] = shape_type[i];
42  }
43  deserializers_[shape_type8] = method;
44 }
45 
47  std::ifstream in;
48  in.open(filename.c_str(), std::ifstream::binary);
50  in.close();
51  return shape;
52 }
53 
54 void serialization::toFile(ShapeConstPtr shape, const std::string& filename) {
55  std::ofstream out;
56  out.open(filename.c_str(), std::ifstream::binary);
58  out.close();
59 }
60 
61 void serialization::toFile(const Shape& shape, const std::string& filename) {
62  std::ofstream out;
63  out.open(filename.c_str(), std::ifstream::binary);
65  out.close();}
66 
67 }
Shape.h
fstream
geo::ShapePtr
std::shared_ptr< Shape > ShapePtr
Definition: datatypes.h:12
std::string
std::shared_ptr
geo
Definition: Box.h:6
geo::serialization::deserializers_
static deserializer_map deserializers_
Definition: serialization.h:43
std::map::find
T find(T... args)
std::string::size
T size(T... args)
geo::serialization::serialization
serialization()
Definition: serialization.cpp:11
std::istream::read
T read(T... args)
geo::serialization::fromFile
static ShapePtr fromFile(const std::string &filename)
Definition: serialization.cpp:46
geo::Shape::write
virtual bool write(std::ostream &output) const
write, serialise the shape
Definition: Shape.cpp:439
geo::serialization::serialize
static bool serialize(ShapeConstPtr shape, std::ostream &output)
Definition: serialization.cpp:18
std::ostream
std::ofstream
std::string::c_str
T c_str(T... args)
std::ifstream::close
T close(T... args)
std::ifstream::open
T open(T... args)
geo::serialization::toFile
static void toFile(ShapeConstPtr shape, const std::string &filename)
Definition: serialization.cpp:54
geo::serialization::deserializer_map
std::map< std::string, deserialization_method > deserializer_map
Definition: serialization.h:15
std::min
T min(T... args)
geo::serialization::deserialize
static ShapePtr deserialize(std::istream &input)
Definition: serialization.cpp:26
geo::serialization::~serialization
virtual ~serialization()
Definition: serialization.cpp:15
serialization.h
std::map::end
T end(T... args)
std::istream
geo::serialization::registerDeserializer
static void registerDeserializer()
Definition: serialization.h:33
geo::Shape
A geometric description of a shape.
Definition: Shape.h:19
std::ifstream