Collage  1.7.0
High-performance C++ library for developing object-oriented distributed applications.
distributable.h
1 
2 /* Copyright (c) 2015-2016 Daniel.Nachbaur@epfl.ch
3  *
4  * This file is part of Collage <https://github.com/Eyescale/Collage>
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 2.1 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 CO_DISTRIBUTABLE_H
21 #define CO_DISTRIBUTABLE_H
22 
23 #include <co/dataIStream.h> // used inline
24 #include <co/dataOStream.h> // used inline
25 #include <co/object.h> // default base class
26 #include <servus/serializable.h> //used inline
27 
28 namespace co
29 {
38 template <class T, class S = Object, typename... Args>
39 class Distributable : public T, public S
40 {
41 public:
43  Distributable(Args... args)
44  : T()
45  , S(args...)
46  , _dirty(false)
47  {
48  }
49 
52  : T(rhs)
53  , S(rhs)
54  , _dirty(false)
55  {
56  }
57 
58  virtual ~Distributable() {}
60  bool isDirty() const final { return S::isDirty() || _dirty; }
62  uint128_t commit(const uint32_t incarnation = CO_COMMIT_NEXT) final
63  {
64  const uint128_t& version = S::commit(incarnation);
65  _dirty = false;
66  return version;
67  }
68 
70  void notifyChanged() override
71  {
72  T::notifyChanged();
73  if (S::isMaster())
74  _dirty = true;
75  }
76 
77 private:
78  typename S::ChangeType getChangeType() const final { return S::INSTANCE; }
79  void getInstanceData(co::DataOStream& os) override
80  {
81  S::getInstanceData(os);
82  const auto& data = T::toBinary();
83  os << data.size << Array<const void>(data.ptr.get(), data.size);
84  }
85 
86  void applyInstanceData(co::DataIStream& is) override
87  {
88  S::applyInstanceData(is);
89  const size_t size = is.read<size_t>();
90  T::fromBinary(is.getRemainingBuffer(size), size);
91  }
92 
93  bool _dirty;
94 };
95 
96 template <class T>
97 inline std::ostream& operator<<(std::ostream& os,
98  const Distributable<T>& object)
99 {
100  return os << static_cast<const T&>(object);
101 }
102 }
103 
104 #endif
Distributable Collage object for any servus::Serializable object.
Definition: distributable.h:39
A std::ostream-like interface for object serialization.
Definition: dataOStream.h:56
Object-oriented network library.
Definition: barrier.h:27
CO_API const void * getRemainingBuffer(const uint64_t size)
Distributable(const Distributable &rhs)
Copy-construct a distributable object.
Definition: distributable.h:51
Distributable(Args...args)
Construct a new distributable object.
Definition: distributable.h:43
void notifyChanged() override
Call whenever the object has been modified so it can be distributed.
Definition: distributable.h:70
A std::istream-like input data stream for binary data.
Definition: dataIStream.h:45
bool isDirty() const final
Definition: distributable.h:60
uint128_t commit(const uint32_t incarnation=CO_COMMIT_NEXT) final
Definition: distributable.h:62