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> // 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 :
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 S = Object, typename... Args > class Distributable : public T, public S
40 : {
41 : public:
42 : /** Construct a new distributable object. @version 1.4 */
43 2 : Distributable( Args... args ) : T(), S( args... ), _dirty( false ) {}
44 :
45 : /** Copy-construct a distributable object. @version 1.4 */
46 : Distributable( const Distributable& rhs )
47 : : T( rhs ), S( rhs ), _dirty( false ) {}
48 :
49 3 : virtual ~Distributable() {}
50 :
51 : /** @sa Object::dirty() */
52 1 : bool isDirty() const final { return S::isDirty() || _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 = S::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() override
64 : {
65 2 : T::notifyChanged();
66 2 : if( S::isMaster( ))
67 1 : _dirty = true;
68 2 : }
69 :
70 : private:
71 2 : typename S::ChangeType getChangeType() const final { return S::INSTANCE; }
72 :
73 2 : void getInstanceData( co::DataOStream& os ) override
74 : {
75 2 : S::getInstanceData( os );
76 4 : const auto& data = T::toBinary();
77 2 : os << data.size << Array< const void >( data.ptr.get(), data.size );
78 2 : }
79 :
80 3 : void applyInstanceData( co::DataIStream& is ) override
81 : {
82 3 : S::applyInstanceData( is );
83 3 : const size_t size = is.read< size_t >();
84 3 : T::fromBinary( is.getRemainingBuffer( size ), size );
85 3 : }
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
|