Collage  1.6.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/object.h> // default base class
24 #include <co/dataIStream.h> // used inline
25 #include <co/dataOStream.h> // used inline
26 #include <servus/serializable.h> //used inline
27 
28 namespace co
29 {
30 
39 template< class T, class S = Object, typename... Args > class Distributable : public T, public S
40 {
41 public:
43  Distributable( Args... args ) : T(), S( args... ), _dirty( false ) {}
44 
47  : T( rhs ), S( rhs ), _dirty( false ) {}
48 
49  virtual ~Distributable() {}
50 
52  bool isDirty() const final { return S::isDirty() || _dirty; }
53 
55  uint128_t commit( const uint32_t incarnation = CO_COMMIT_NEXT ) final
56  {
57  const uint128_t& version = S::commit( incarnation );
58  _dirty = false;
59  return version;
60  }
61 
63  void notifyChanged() override
64  {
65  T::notifyChanged();
66  if( S::isMaster( ))
67  _dirty = true;
68  }
69 
70 private:
71  typename S::ChangeType getChangeType() const final { return S::INSTANCE; }
72 
73  void getInstanceData( co::DataOStream& os ) override
74  {
75  S::getInstanceData( os );
76  const auto& data = T::toBinary();
77  os << data.size << Array< const void >( data.ptr.get(), data.size );
78  }
79 
80  void applyInstanceData( co::DataIStream& is ) override
81  {
82  S::applyInstanceData( is );
83  const size_t size = is.read< size_t >();
84  T::fromBinary( is.getRemainingBuffer( size ), size );
85  }
86 
87  bool _dirty;
88 };
89 
90 template< class T > inline
91 std::ostream& operator << ( std::ostream& os, const Distributable< T >& object )
92 {
93  return os << static_cast< const T& >( object );
94 }
95 }
96 
97 #endif
A distributed object.
Definition: object.h:45
Distributable Collage object for any servus::Serializable object.
Definition: distributable.h:39
A std::ostream-like interface for object serialization.
Definition: dataOStream.h:48
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:46
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:63
A std::istream-like input data stream for binary data.
Definition: dataIStream.h:41
bool isDirty() const final
Definition: distributable.h:52
uint128_t commit(const uint32_t incarnation=CO_COMMIT_NEXT) final
Definition: distributable.h:55