LCOV - code coverage report
Current view: top level - co - distributable.h (source / functions) Hit Total Coverage
Test: Collage Lines: 25 25 100.0 %
Date: 2018-01-09 16:37:03 Functions: 9 9 100.0 %

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

Generated by: LCOV version 1.11