Servus  1.6.0
C++ network oriented utilities including a zeroconf implementation
serializable.h
1 /* Copyright (c) 2016-2017, 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 {
33 {
34 public:
35  SERVUS_API Serializable();
36  SERVUS_API virtual ~Serializable();
37 
41  struct Data
42  {
43  Data()
44  : size(0)
45  {
46  }
47 
49  SERVUS_API Data clone();
50 
51  std::shared_ptr<const void> ptr;
52  size_t size;
53  };
54 
56  virtual std::string getTypeName() const = 0;
57 
59  SERVUS_API virtual uint128_t getTypeIdentifier() const;
60 
62  virtual std::string getSchema() const { return std::string(); }
67  SERVUS_API bool fromBinary(const Data& data);
68  SERVUS_API bool fromBinary(const void* data, const size_t size);
69 
78  SERVUS_API Data toBinary() const;
79 
84  SERVUS_API bool fromJSON(const std::string& json);
85 
87  SERVUS_API std::string toJSON() const;
89 
93  typedef std::function<void()> DeserializedCallback;
94  typedef std::function<void()> SerializeCallback;
95 
105  SERVUS_API void registerDeserializedCallback(const DeserializedCallback&);
106 
116  SERVUS_API void registerSerializeCallback(const SerializeCallback&);
118 
119 protected:
120  SERVUS_API Serializable(const Serializable&);
121  SERVUS_API Serializable& operator=(const Serializable&);
122  SERVUS_API Serializable(Serializable&&);
123  SERVUS_API Serializable& operator=(Serializable&&);
124 
125 private:
133  virtual bool _fromBinary(const void* /*data*/, const size_t /*size*/)
134  {
135  throw std::runtime_error("Binary deserialization not implemented");
136  }
137  virtual Data _toBinary() const
138  {
139  throw std::runtime_error("Binary serialization not implemented");
140  }
141 
142  virtual bool _fromJSON(const std::string& /*json*/)
143  {
144  throw std::runtime_error("JSON deserialization not implemented");
145  }
146  virtual std::string _toJSON() const
147  {
148  throw std::runtime_error("JSON serialization not implemented");
149  }
151 
152  class Impl;
153  Impl* _impl;
154 };
155 }
156 
157 #endif // SERVUS_SERIALIZABLE_H
size_t size
The size of the binary serialization.
Definition: serializable.h:52
Defines export visibility macros for library Servus.
std::string toJSON() const
void registerDeserializedCallback(const DeserializedCallback &)
Register a function called after the object has been updated remotely (via a subscriber, a http server, loading from file...).
std::function< void()> DeserializedCallback
Callbacks for change notifications.
Definition: serializable.h:93
virtual uint128_t getTypeIdentifier() const
std::shared_ptr< const void > ptr
ptr to the binary serialization
Definition: serializable.h:51
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:32
virtual std::string getSchema() const
Definition: serializable.h:62
Pointer + size wrapper for binary serialization.
Definition: serializable.h:41
A base type for 128 bit unsigned integer values.
Definition: uint128_t.h:48
Data toBinary() const
Get a binary representation of this object.
virtual std::string getTypeName() const =0