Collage  1.4.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> // 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 Distributable : public T, public Object
40 {
41 public:
43  Distributable() : T(), Object(), _dirty( false ) {}
44 
47  : T( rhs ), Object( rhs ), _dirty( false ) {}
48 
49  virtual ~Distributable() {}
50 
52  bool isDirty() const final { return _dirty; }
53 
55  uint128_t commit( const uint32_t incarnation = CO_COMMIT_NEXT ) final
56  {
57  const uint128_t& version = co::Object::commit( incarnation );
58  _dirty = false;
59  return version;
60  }
61 
63  void notifyChanged() final { _dirty = true; }
64 
65 private:
66  ChangeType getChangeType() const final { return INSTANCE; }
67 
68  void getInstanceData( co::DataOStream& os ) final
69  {
70  const auto& data = T::toBinary();
71  os << data.size << Array< const void >( data.ptr.get(), data.size );
72  }
73 
74  void applyInstanceData( co::DataIStream& is ) final
75  {
76  const size_t size = is.read< size_t >();
77  T::fromBinary( is.getRemainingBuffer( size ), size );
78  }
79 
80  bool _dirty;
81 };
82 
83 template< class T > inline
84 std::ostream& operator << ( std::ostream& os, const Distributable< T >& object )
85 {
86  return os << static_cast< const T& >( object );
87 }
88 }
89 
90 #endif
A distributed object.
Definition: object.h:45
virtual CO_API uint128_t commit(const uint32_t incarnation=CO_COMMIT_NEXT)
Commit a new version of this object.
ChangeType
Object change handling characteristics, see Programming Guide.
Definition: object.h:49
Distributable Collage object for any servus::Serializable object.
Definition: distributable.h:39
A std::ostream-like interface for object serialization.
Definition: dataOStream.h:48
uint128_t commit(const uint32_t incarnation=CO_COMMIT_NEXT) final
Definition: distributable.h:55
Object-oriented network library.
Definition: barrier.h:27
use only instance data
Definition: object.h:53
void notifyChanged() final
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
Distributable(const Distributable &rhs)
Copy-construct a distributable object.
Definition: distributable.h:46
Distributable()
Construct a new distributable object.
Definition: distributable.h:43