Line data Source code
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 : #include "serializable.h"
21 :
22 : #include "uint128_t.h"
23 :
24 : #include <cstring>
25 :
26 : namespace servus
27 : {
28 10 : class Serializable::Impl
29 : {
30 : public:
31 2 : void notifyDeserialized() const
32 : {
33 2 : if (deserialized)
34 0 : deserialized();
35 2 : }
36 :
37 2 : void notifySerialize() const
38 : {
39 2 : if (serialize)
40 0 : serialize();
41 2 : }
42 :
43 : Serializable::DeserializedCallback deserialized;
44 : Serializable::SerializeCallback serialize;
45 : };
46 :
47 0 : Serializable::Data Serializable::Data::clone()
48 : {
49 0 : Serializable::Data data;
50 0 : data.ptr = {new uint8_t[size], std::default_delete<uint8_t[]>()};
51 0 : data.size = size;
52 :
53 0 : ::memcpy((void*)data.ptr.get(), ptr.get(), size);
54 0 : return data;
55 : }
56 :
57 5 : Serializable::Serializable()
58 5 : : _impl(new Serializable::Impl())
59 : {
60 5 : }
61 :
62 10 : Serializable::~Serializable()
63 : {
64 5 : delete _impl;
65 5 : }
66 :
67 0 : Serializable::Serializable(const Serializable& rhs)
68 0 : : _impl(new Serializable::Impl(*rhs._impl))
69 : {
70 0 : }
71 :
72 0 : Serializable& Serializable::operator=(const Serializable& rhs)
73 : {
74 0 : if (this != &rhs)
75 0 : *_impl = *rhs._impl;
76 0 : return *this;
77 : }
78 :
79 0 : Serializable::Serializable(Serializable&& rhs)
80 0 : : _impl(nullptr)
81 : {
82 0 : std::swap(_impl, rhs._impl);
83 0 : }
84 :
85 0 : Serializable& Serializable::operator=(Serializable&& rhs)
86 : {
87 0 : std::swap(_impl, rhs._impl);
88 0 : return *this;
89 : }
90 :
91 0 : uint128_t Serializable::getTypeIdentifier() const
92 : {
93 0 : return make_uint128(getTypeName());
94 : }
95 :
96 0 : bool Serializable::fromBinary(const Data& data)
97 : {
98 0 : return fromBinary(data.ptr.get(), data.size);
99 : }
100 :
101 1 : bool Serializable::fromBinary(const void* data, const size_t size)
102 : {
103 1 : if (_fromBinary(data, size))
104 : {
105 1 : _impl->notifyDeserialized();
106 1 : return true;
107 : }
108 0 : return false;
109 : }
110 :
111 1 : Serializable::Data Serializable::toBinary() const
112 : {
113 1 : _impl->notifySerialize();
114 1 : return _toBinary();
115 : }
116 :
117 1 : bool Serializable::fromJSON(const std::string& json)
118 : {
119 1 : if (_fromJSON(json))
120 : {
121 1 : _impl->notifyDeserialized();
122 1 : return true;
123 : }
124 0 : return false;
125 : }
126 :
127 1 : std::string Serializable::toJSON() const
128 : {
129 1 : _impl->notifySerialize();
130 1 : return _toJSON();
131 : }
132 :
133 5 : void Serializable::registerDeserializedCallback(
134 : const DeserializedCallback& callback)
135 : {
136 5 : if (_impl->deserialized && callback)
137 : throw(
138 : std::runtime_error("A DeserializedCallback is already registered. "
139 2 : "Only one is supported at the moment"));
140 :
141 3 : _impl->deserialized = callback;
142 3 : }
143 :
144 5 : void Serializable::registerSerializeCallback(const SerializeCallback& callback)
145 : {
146 5 : if (_impl->serialize && callback)
147 : throw(
148 : std::runtime_error("A SerializeCallback is already registered. "
149 2 : "Only one is supported at the moment"));
150 :
151 3 : _impl->serialize = callback;
152 3 : }
153 : }
|