LCOV - code coverage report
Current view: top level - co - objectVersion.h (source / functions) Hit Total Coverage
Test: Collage Lines: 10 12 83.3 %
Date: 2018-01-09 16:37:03 Functions: 2 3 66.7 %

          Line data    Source code
       1             : 
       2             : /* Copyright (c) 2009-2017, Stefan Eilemann <eile@equalizergraphics.com>
       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_OBJECTVERSION_H
      21             : #define CO_OBJECTVERSION_H
      22             : 
      23             : #include <co/api.h>
      24             : #include <co/types.h>
      25             : #include <iostream>
      26             : #include <lunchbox/bitOperation.h>
      27             : 
      28             : namespace co
      29             : {
      30             : /** Special object version values */
      31         919 : static const uint128_t VERSION_NONE(0, 0);
      32         919 : static const uint128_t VERSION_FIRST(0, 1);
      33         919 : static const uint128_t VERSION_NEWEST(0, 0xfffffffffffffffbull);
      34         919 : static const uint128_t VERSION_OLDEST(0, 0xfffffffffffffffcull);
      35         919 : static const uint128_t VERSION_NEXT(0, 0xfffffffffffffffdull);
      36         919 : static const uint128_t VERSION_INVALID(0, 0xfffffffffffffffeull);
      37         919 : static const uint128_t VERSION_HEAD(0, 0xffffffffffffffffull);
      38             : 
      39             : /**
      40             :  * A helper struct bundling an object identifier and version.
      41             :  *
      42             :  * Primarily used for serialization. The struct either contains the object's
      43             :  * identifier and version (if it is registered or mapped), 0 and VERSION_NONE if
      44             :  * it is unmapped or if no object was given.
      45             :  */
      46           1 : struct ObjectVersion
      47             : {
      48             :     /** Construct a new, zero-initialized object version. @version 1.0 */
      49             :     CO_API ObjectVersion();
      50             : 
      51             :     /** Construct a new object version. @version 1.0 */
      52             :     CO_API ObjectVersion(const uint128_t& identifier, const uint128_t& version);
      53             : 
      54             :     /** Construct a new object version. @version 1.0 */
      55             :     CO_API explicit ObjectVersion(const Object* object);
      56             : 
      57             :     /** Construct a new object version. @version 1.2 */
      58             :     CO_API explicit ObjectVersion(const Object& object);
      59             : 
      60             :     /** Construct a new object version. @version 1.0 */
      61             :     template <class R>
      62             :     explicit ObjectVersion(lunchbox::RefPtr<R> object)
      63             :     {
      64             :         *this = object.get();
      65             :     }
      66             : 
      67             :     /** Assign a new identifier and version. @version 1.0 */
      68             :     CO_API ObjectVersion& operator=(const Object* object);
      69             : 
      70             :     /** @return true if both structs contain the same values. @version 1.0 */
      71             :     bool operator==(const ObjectVersion& value) const
      72             :     {
      73             :         return (identifier == value.identifier && version == value.version);
      74             :     }
      75             : 
      76             :     /** @return true if both structs have different values. @version 1.0 */
      77           1 :     bool operator!=(const ObjectVersion& value) const
      78             :     {
      79           1 :         return (identifier != value.identifier || version != value.version);
      80             :     }
      81             : 
      82             :     bool operator<(const ObjectVersion& rhs) const //!< @internal
      83             :     {
      84             :         return identifier < rhs.identifier ||
      85             :                (identifier == rhs.identifier && version < rhs.version);
      86             :     }
      87             : 
      88             :     bool operator>(const ObjectVersion& rhs) const //!< @internal
      89             :     {
      90             :         return identifier > rhs.identifier ||
      91             :                (identifier == rhs.identifier && version > rhs.version);
      92             :     }
      93             : 
      94             :     /** @return true if the identifier and version are set. */
      95             :     explicit operator bool() const
      96             :     {
      97             :         return identifier != 0 && version != VERSION_NONE;
      98             :     }
      99             : 
     100             :     /** @return true if the identifier or version are not set. */
     101             :     bool operator!() const
     102             :     {
     103             :         return identifier == 0 || version == VERSION_NONE;
     104             :     }
     105             : 
     106             :     uint128_t identifier; //!< the object identifier
     107             :     uint128_t version;    //!< the object version
     108             : };
     109             : 
     110           0 : inline std::ostream& operator<<(std::ostream& os, const ObjectVersion& ov)
     111             : {
     112           0 :     return os << "id " << ov.identifier << " v" << ov.version;
     113             : }
     114             : }
     115             : 
     116             : namespace std
     117             : {
     118             : /** ObjectVersion hash function. */
     119             : template <>
     120             : struct hash<co::ObjectVersion>
     121             : {
     122             :     template <typename P>
     123             :     size_t operator()(const P& key) const
     124             :     {
     125             :         return hash<uint64_t>()(hash_value(key.version) ^
     126             :                                 hash_value(key.identifier));
     127             :     }
     128             : };
     129             : }
     130             : #endif // CO_OBJECT_H

Generated by: LCOV version 1.11