1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/base/ProvideNumericId.h
12 #ifndef ZYPP_BASE_PROVIDENUMERICID_H
13 #define ZYPP_BASE_PROVIDENUMERICID_H
15 ///////////////////////////////////////////////////////////////////
17 { /////////////////////////////////////////////////////////////////
18 ///////////////////////////////////////////////////////////////////
20 { /////////////////////////////////////////////////////////////////
22 ///////////////////////////////////////////////////////////////////
24 // CLASS NAME : ProvideNumericId
26 /** Base class for objects providing a numeric Id.
27 * The ctor creates a NumericId from some static counter.
29 * The only assertion is that \c 0 is not used as an Id,
30 * \b unless the derived class explicitly requests this by
31 * using \ref ProvideNumericId( const void *const ).
33 * Why should you want to use \c 0 as an Id? E.g if your class
34 * provides some (singleton) No-object. Might be desirable to
35 * make the No-object have No-Id.
38 * struct Foo : public base::ProvideNumericId<Foo,unsigned>
41 * foo.numericId(); // returns foo's NumericId.
44 template<class TDerived, class TNumericIdType>
45 struct ProvideNumericId
48 /** \return The objects numeric Id. */
49 TNumericIdType numericId() const
50 { return _numericId; }
55 : _numericId( nextId() )
58 ProvideNumericId( const ProvideNumericId & /*rhs*/ )
59 : _numericId( nextId() )
62 ProvideNumericId & operator=( const ProvideNumericId & /*rhs*/ )
65 ProvideNumericId( ProvideNumericId && rhs )
66 : _numericId( rhs._numericId )
67 { /*rhs._numericId = 0;*/ }
69 ProvideNumericId & operator=( ProvideNumericId && rhs )
70 { if ( &rhs != this ) { _numericId = rhs._numericId; /*rhs._numericId = 0;*/ } return *this; }
76 * Explicitly request Id \c 0. Use it with care!
78 ProvideNumericId( const void *const )
82 /** Provide the next Id to use. */
83 static TNumericIdType nextId()
85 static TNumericIdType _staticCounter = 0;
86 // Assert not returning 0
87 return ++_staticCounter;
90 const TNumericIdType _numericId;
92 ///////////////////////////////////////////////////////////////////
94 /////////////////////////////////////////////////////////////////
96 ///////////////////////////////////////////////////////////////////
97 /////////////////////////////////////////////////////////////////
99 ///////////////////////////////////////////////////////////////////
100 #endif // ZYPP_BASE_PROVIDENUMERICID_H