Servus  1.6.0
C++ network oriented utilities including a zeroconf implementation
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 #include <functional> // function
27 #include <memory> // shared_ptr
28 
29 namespace servus
30 {
31 
34 {
35 public:
36  SERVUS_API Serializable();
37  SERVUS_API virtual ~Serializable();
38 
42  struct Data
43  {
44  Data() : size ( 0 ) {}
45  std::shared_ptr< const void > ptr;
46  size_t size;
47  };
48 
50  virtual std::string getTypeName() const = 0;
51 
53  SERVUS_API virtual uint128_t getTypeIdentifier() const;
54 
56  virtual std::string getSchema() const { return std::string(); }
57 
62  SERVUS_API bool fromBinary( const Data& data );
63  SERVUS_API bool fromBinary( const void* data, const size_t size );
64 
73  SERVUS_API Data toBinary() const;
74 
79  SERVUS_API bool fromJSON( const std::string& json );
80 
82  SERVUS_API std::string toJSON() const;
84 
88  typedef std::function< void() > DeserializedCallback;
89  typedef std::function< void() > SerializeCallback;
90 
100  SERVUS_API void registerDeserializedCallback( const DeserializedCallback& );
101 
111  SERVUS_API void registerSerializeCallback( const SerializeCallback& );
113 
114 protected:
115  SERVUS_API Serializable( const Serializable& );
116  SERVUS_API Serializable& operator=( const Serializable& );
117 #ifdef SERVUS_USE_CXX11
118  SERVUS_API Serializable( Serializable&& );
119  SERVUS_API Serializable& operator=( Serializable&& );
120 #endif
121 
122 private:
130  virtual bool _fromBinary( const void* /*data*/, const size_t /*size*/ )
131  { throw std::runtime_error( "Binary deserialization not implemented" );}
132  virtual Data _toBinary() const
133  { throw std::runtime_error( "Binary serialization not implemented" ); }
134 
135  virtual bool _fromJSON( const std::string& /*json*/ )
136  { throw std::runtime_error( "JSON deserialization not implemented" ); }
137  virtual std::string _toJSON() const
138  { throw std::runtime_error( "JSON serialization not implemented" ); }
140 
141  class Impl;
142  Impl* _impl;
143 };
144 
145 }
146 
147 #endif // SERVUS_SERIALIZABLE_H
size_t size
The size of the binary serialization.
Definition: serializable.h:46
Defines export visibility macros for library Servus.
std::string toJSON() const
std::function< void() > DeserializedCallback
Callbacks for change notifications.
Definition: serializable.h:88
std::shared_ptr< const void > ptr
ptr to the binary serialization
Definition: serializable.h:45
void registerDeserializedCallback(const DeserializedCallback &)
Register a function called after the object has been updated remotely (via a subscriber, a http server, loading from file...).
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.
void registerSerializeCallback(const SerializeCallback &)
Register a function to be called when the serializable object is about to be serialized.
Interface for serializable objects.
Definition: serializable.h:33
virtual std::string getSchema() const
Definition: serializable.h:56
Pointer + size wrapper for binary serialization.
Definition: serializable.h:42
A base type for 128 bit unsigned integer values.
Definition: uint128_t.h:46
Data toBinary() const
Get a binary representation of this object.