cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cmnSerializer.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */
3 
4 /*
5 
6  Author(s): Anton Deguet, Min Yang Jung
7  Created on: 2007-04-08
8 
9  (C) Copyright 2007-2013 Johns Hopkins University (JHU), All Rights Reserved.
10 
11 --- begin cisst license - do not edit ---
12 
13 This software is provided "as is" under an open source license, with
14 no warranty. The complete license can be found in license.txt and
15 http://www.cisst.org/cisst/license.txt.
16 
17 --- end cisst license ---
18 
19 */
20 
21 
26 #pragma once
27 
28 #ifndef _cmnSerializer_h
29 #define _cmnSerializer_h
30 
35 #include <cisstCommon/cmnThrow.h>
36 
37 #include <string>
38 #include <vector>
39 #include <fstream>
40 #include <cstddef>
41 
42 #include <cisstCommon/cmnExport.h>
43 
44 
45 // Implementation class to provide serialization when _elementType is not derived from cmnGenericObject
46 template <class _elementType, bool>
48 {
49 public:
50  static void SerializeRaw(std::ostream & outputStream, const _elementType & data) throw (std::runtime_error)
51  {
52  outputStream.write(reinterpret_cast<const char *>(&data), sizeof(_elementType));
53  if (outputStream.fail()) {
54  cmnThrow("cmnSerializeRaw(_elementType): Error occured with std::ostream::write");
55  }
56  }
57 };
58 
59 // Implementation class to provide serialization when _elementType is derived from cmnGenericObject
60 template <class _elementType>
61 class cmnSerializeRawImpl<_elementType, true>
62 {
63 public:
64  static void SerializeRaw(std::ostream & outputStream, const _elementType & data) throw (std::runtime_error)
65  {
66  data.SerializeRaw(outputStream);
67  }
68 };
69 
77 template <class _elementType>
78 inline void cmnSerializeRaw(std::ostream & outputStream, const _elementType & data)
79  throw (std::runtime_error)
80 {
82  impl::SerializeRaw(outputStream, data);
83 }
84 
85 
93 inline void cmnSerializeSizeRaw(std::ostream & outputStream, const size_t & data)
94  throw (std::runtime_error)
95 {
96  unsigned long long int dataToSend = data;
97  cmnSerializeRaw(outputStream, dataToSend);
98 }
99 
100 
106 inline void cmnSerializeRaw(std::ostream & outputStream, const std::string & data)
107  throw (std::runtime_error)
108 {
109  const std::string::size_type size = data.size();
110  cmnSerializeSizeRaw(outputStream, size);
111  outputStream.write(data.c_str(), size * sizeof(std::string::value_type));
112  if (outputStream.fail()) {
113  cmnThrow("cmnSerializeRaw(std::string): Error occured with std::ostream::write");
114  }
115 }
116 
117 
124 template <class _elementType>
125 inline void cmnSerializeRaw(std::ostream & outputStream, const std::vector<_elementType> & data)
126  throw (std::runtime_error)
127 {
128  const typename std::vector<_elementType>::size_type size = data.size();
129  cmnSerializeSizeRaw(outputStream, size);
130  for (size_t i = 0; i < size; i++) {
131  cmnSerializeRaw(outputStream, data[i]);
132  if (outputStream.fail()) {
133  cmnThrow("cmnSerializeRaw(std::vector<_elementType>): Error occured with std::ostream::write");
134  }
135  }
136 }
137 
138 
166 
167 public:
168 
172  typedef unsigned long long int TypeId;
173 
180  cmnSerializer(std::ostream & outputStream);
181 
183  ~cmnSerializer();
184 
186  bool ServicesSerialized(const cmnClassServicesBase *servicesPointer) const;
187 
189  void Reset(void);
190 
209  void Serialize(const cmnGenericObject & object, const bool serializeObject = true);
210 
211 
235  void SerializeServices(const cmnClassServicesBase * servicesPointer);
236 
237 
238  private:
239 
240  std::ostream & OutputStream;
241 
243  typedef std::list<const cmnClassServicesBase *> ServicesContainerType;
244  typedef ServicesContainerType::const_iterator const_iterator;
245  typedef ServicesContainerType::iterator iterator;
246 
247  ServicesContainerType ServicesContainer;
248 };
249 
250 
251 // Add services instantiation
253 
254 
255 #endif // _cmnSerialize_h
256 
#define CISST_EXPORT
Definition: cmnExportMacros.h:50
static void SerializeRaw(std::ostream &outputStream, const _elementType &data)
Definition: cmnSerializer.h:64
Portability across compilers and operating systems tools.
void cmnSerializeSizeRaw(std::ostream &outputStream, const size_t &data)
Definition: cmnSerializer.h:93
size_t size_type
Definition: vctContainerTraits.h:35
Base class for high level objects.
Definition: cmnGenericObject.h:51
Class register definitions and log macros.
Serialization utility class.This class allows to serialize objects of different types (all derived fr...
Definition: cmnSerializer.h:164
static void SerializeRaw(std::ostream &outputStream, const _elementType &data)
Definition: cmnSerializer.h:50
Macros to export the symbols of cisstCommon (in a Dll).
Declaration of the class cmnTypeTraits.
Defines cmnGenericObject.
Definition: cmnSerializer.h:47
void cmnSerializeRaw(std::ostream &outputStream, const _elementType &data)
Definition: cmnSerializer.h:78
#define CMN_DECLARE_SERVICES(hasDynamicCreation, lod)
Definition: cmnClassRegisterMacros.h:116
#define cmnThrow(a)
Definition: MinimalCmn.h:4
unsigned long long int TypeId
Definition: cmnSerializer.h:172
Base class for class services.
Definition: cmnClassServicesBase.h:45
Declaration of the template function cmnThrow.
const int CMN_NO_DYNAMIC_CREATION
Definition: cmnClassRegisterMacros.h:328
#define CMN_DECLARE_SERVICES_INSTANTIATION(className)
Definition: cmnClassRegisterMacros.h:202
#define CMN_LOG_ALLOW_DEFAULT
Definition: cmnLogLoD.h:76