LCOV - code coverage report
Current view: top level - co - objectVersion.h (source / functions) Hit Total Coverage
Test: Collage Lines: 10 16 62.5 %
Date: 2016-12-14 01:26:48 Functions: 2 4 50.0 %

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

Generated by: LCOV version 1.11