Line data Source code
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 :
31 : /**
32 : * Distributable Collage object for any servus::Serializable object.
33 : *
34 : * Clients instantiate this object with a concrete Zerobuf object (or other
35 : * servus::Serializable) using CRTP. The base class T needs to implement and
36 : * call an abstract change notification method "virtual void notifyChanged() =
37 : * 0;" (Zerobuf does this).
38 : */
39 : template< class T > class Distributable : public T, public Object
40 : {
41 : public:
42 : /** Construct a new distributable object. @version 1.4 */
43 2 : Distributable() : T(), Object(), _dirty( false ) {}
44 :
45 : /** Copy-construct a distributable object. @version 1.4 */
46 : Distributable( const Distributable& rhs )
47 : : T( rhs ), Object( rhs ), _dirty( false ) {}
48 :
49 3 : virtual ~Distributable() {}
50 :
51 : /** @sa Object::dirty() */
52 1 : bool isDirty() const final { return _dirty; }
53 :
54 : /** @sa Object::commit() */
55 1 : uint128_t commit( const uint32_t incarnation = CO_COMMIT_NEXT ) final
56 : {
57 1 : const uint128_t& version = co::Object::commit( incarnation );
58 1 : _dirty = false;
59 1 : return version;
60 : }
61 :
62 : /** Call whenever the object has been modified so it can be distributed */
63 2 : void notifyChanged() final { _dirty = true; }
64 :
65 : private:
66 2 : ChangeType getChangeType() const final { return INSTANCE; }
67 :
68 2 : void getInstanceData( co::DataOStream& os ) final
69 : {
70 2 : const auto& data = T::toBinary();
71 2 : os << data.size << Array< const void >( data.ptr.get(), data.size );
72 2 : }
73 :
74 3 : void applyInstanceData( co::DataIStream& is ) final
75 : {
76 3 : const size_t size = is.read< size_t >();
77 3 : T::fromBinary( is.getRemainingBuffer( size ), size );
78 3 : }
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
|