Servus  1.5.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 #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  std::shared_ptr<const void> ptr;
48  size_t size;
49  };
50 
52  virtual std::string getTypeName() const = 0;
53 
55  SERVUS_API virtual uint128_t getTypeIdentifier() const;
56 
58  virtual std::string getSchema() const { return std::string(); }
63  SERVUS_API bool fromBinary(const Data& data);
64  SERVUS_API bool fromBinary(const void* data, const size_t size);
65 
74  SERVUS_API Data toBinary() const;
75 
80  SERVUS_API bool fromJSON(const std::string& json);
81 
83  SERVUS_API std::string toJSON() const;
85 
89  typedef std::function<void()> DeserializedCallback;
90  typedef std::function<void()> SerializeCallback;
91 
101  SERVUS_API void registerDeserializedCallback(const DeserializedCallback&);
102 
112  SERVUS_API void registerSerializeCallback(const SerializeCallback&);
114 
115 protected:
116  SERVUS_API Serializable(const Serializable&);
117  SERVUS_API Serializable& operator=(const Serializable&);
118  SERVUS_API Serializable(Serializable&&);
119  SERVUS_API Serializable& operator=(Serializable&&);
120 
121 private:
129  virtual bool _fromBinary(const void* /*data*/, const size_t /*size*/)
130  {
131  throw std::runtime_error("Binary deserialization not implemented");
132  }
133  virtual Data _toBinary() const
134  {
135  throw std::runtime_error("Binary serialization not implemented");
136  }
137 
138  virtual bool _fromJSON(const std::string& /*json*/)
139  {
140  throw std::runtime_error("JSON deserialization not implemented");
141  }
142  virtual std::string _toJSON() const
143  {
144  throw std::runtime_error("JSON serialization not implemented");
145  }
147 
148  class Impl;
149  Impl* _impl;
150 };
151 }
152 
153 #endif // SERVUS_SERIALIZABLE_H
size_t size
The size of the binary serialization.
Definition: serializable.h:48
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:89
virtual uint128_t getTypeIdentifier() const
std::shared_ptr< const void > ptr
ptr to the binary serialization
Definition: serializable.h:47
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:58
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