Servus  1.3.1
C++ network oriented utilities including a zeroconf implementation
serializable.h
1 /* Copyright (c) 2016, Human Brain Project
2  * Stefan.Eilemann@epfl.ch
3  *
4  * This file is part of Servus <https://github.com/HBPVIS/Servus>
5  *
6  * This library is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Lesser General Public License version 3.0 as published
8  * by the Free Software Foundation.
9  *
10  * This library is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #ifndef SERVUS_SERIALIZABLE_H
21 #define SERVUS_SERIALIZABLE_H
22 
23 #include <servus/api.h>
24 #include <servus/types.h>
25 
26 #ifdef SERVUS_USE_CXX03
27 # include <boost/function/function0.hpp>
28 # include <boost/shared_ptr.hpp>
29 #else
30 # include <functional> // function
31 # include <memory> // shared_ptr
32 #endif
33 
34 namespace servus
35 {
36 
39 {
40 public:
41  SERVUS_API Serializable();
42  SERVUS_API virtual ~Serializable();
43 
47  struct Data
48  {
49  Data() : size ( 0 ) {}
50 #ifdef SERVUS_USE_CXX03
51  boost::shared_ptr< const void > ptr;
52 #else
53  std::shared_ptr< const void > ptr;
54 #endif
55  size_t size;
56  };
57 
59  virtual std::string getTypeName() const = 0;
60 
62  SERVUS_API virtual uint128_t getTypeIdentifier() const;
63 
68  SERVUS_API bool fromBinary( const Data& data );
69  SERVUS_API bool fromBinary( const void* data, const size_t size );
70 
79  SERVUS_API Data toBinary() const;
80 
85  SERVUS_API bool fromJSON( const std::string& json );
86 
88  SERVUS_API std::string toJSON() const;
90 
94 #ifdef SERVUS_USE_CXX03
95  typedef boost::function< void() > DeserializedCallback;
96  typedef boost::function< void() > SerializeCallback;
97 #else
98  typedef std::function< void() > DeserializedCallback;
99  typedef std::function< void() > SerializeCallback;
100 #endif
101 
111  SERVUS_API void registerDeserializedCallback(
112  const DeserializedCallback& callback );
113 
123  SERVUS_API void registerSerializeCallback(
124  const SerializeCallback& callback );
126 
127 private:
135  virtual bool _fromBinary( const void* /*data*/, const size_t /*size*/ )
136  { throw std::runtime_error( "Binary deserialization not implemented" );}
137  virtual Data _toBinary() const
138  { throw std::runtime_error( "Binary serialization not implemented" ); }
139 
140  virtual bool _fromJSON( const std::string& /*json*/ )
141  { throw std::runtime_error( "JSON deserialization not implemented" ); }
142  virtual std::string _toJSON() const
143  { throw std::runtime_error( "JSON serialization not implemented" ); }
145 
146  Serializable( const Serializable& );
147  Serializable& operator=( const Serializable& );
148 #ifdef SERVUS_USE_CXX11
149  Serializable( Serializable&& ) = delete;
150  Serializable& operator=( Serializable&& ) = delete;
151 #endif
152 
153  class Impl;
154  Impl* _impl;
155 };
156 
157 }
158 
159 #endif // SERVUS_SERIALIZABLE_H
size_t size
The size of the binary serialization.
Definition: serializable.h:55
Defines export visibility macros for library Servus.
std::string toJSON() const
std::function< void() > DeserializedCallback
Callbacks for change notifications.
Definition: serializable.h:98
void registerSerializeCallback(const SerializeCallback &callback)
Register a function to be called when the serializable object is about to be serialized.
std::shared_ptr< const void > ptr
ptr to the binary serialization
Definition: serializable.h:53
virtual std::string getTypeName() const =0
virtual uint128_t getTypeIdentifier() const
bool fromJSON(const std::string &json)
Update this serializable from its JSON representation.
bool fromBinary(const Data &data)
Update this serializable from its binary representation.
Interface for serializable objects.
Definition: serializable.h:38
Pointer + size wrapper for binary serialization.
Definition: serializable.h:47
A base type for 128 bit unsigned integer values.
Definition: uint128_t.h:46
void registerDeserializedCallback(const DeserializedCallback &callback)
Register a function called after the object has been updated remotely (via a subscriber, a http server, loading from file...).
Data toBinary() const
Get a binary representation of this object.