Imported Upstream version 17.25.4
[platform/upstream/libzypp.git] / zypp / base / ProvideNumericId.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/base/ProvideNumericId.h
10  *
11 */
12 #ifndef ZYPP_BASE_PROVIDENUMERICID_H
13 #define ZYPP_BASE_PROVIDENUMERICID_H
14
15 ///////////////////////////////////////////////////////////////////
16 namespace zypp
17 { /////////////////////////////////////////////////////////////////
18   ///////////////////////////////////////////////////////////////////
19   namespace base
20   { /////////////////////////////////////////////////////////////////
21
22     ///////////////////////////////////////////////////////////////////
23     //
24     //  CLASS NAME : ProvideNumericId
25     //
26     /** Base class for objects providing a numeric Id.
27      * The ctor creates a NumericId from some static counter.
28      *
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 ).
32      *
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.
36      *
37      * \code
38      * struct Foo : public base::ProvideNumericId<Foo,unsigned>
39      * {};
40      * Foo foo;
41      * foo.numericId(); // returns foo's NumericId.
42      * \endcode
43     */
44     template<class TDerived, class TNumericIdType>
45       struct ProvideNumericId
46       {
47       public:
48         /** \return The objects numeric Id. */
49         TNumericIdType numericId() const
50         { return _numericId; }
51
52       protected:
53         /** Default ctor */
54         ProvideNumericId()
55         : _numericId( nextId() )
56         {}
57         /** Copy ctor */
58         ProvideNumericId( const ProvideNumericId & /*rhs*/ )
59         : _numericId( nextId() )
60         {}
61         /** Assign */
62         ProvideNumericId & operator=( const ProvideNumericId & /*rhs*/ )
63         { return *this; }
64         /** Move ctor */
65         ProvideNumericId( ProvideNumericId && rhs )
66         : _numericId( rhs._numericId )
67         { /*rhs._numericId = 0;*/ }
68         /** Move Assign */
69         ProvideNumericId & operator=( ProvideNumericId && rhs )
70         { if ( &rhs != this ) { _numericId = rhs._numericId; /*rhs._numericId = 0;*/ } return *this; }
71         /** Dtor */
72         ~ProvideNumericId()
73         {}
74       protected:
75         /** No-Id ctor (0).
76          * Explicitly request Id \c 0. Use it with care!
77         */
78         ProvideNumericId( const void *const )
79         : _numericId( 0 )
80         {}
81       private:
82         /** Provide the next Id to use. */
83         static TNumericIdType nextId()
84         {
85           static TNumericIdType _staticCounter = 0;
86           // Assert not returning 0
87           return ++_staticCounter;
88         }
89         /**  */
90         const TNumericIdType _numericId;
91       };
92     ///////////////////////////////////////////////////////////////////
93
94     /////////////////////////////////////////////////////////////////
95   } // namespace base
96   ///////////////////////////////////////////////////////////////////
97   /////////////////////////////////////////////////////////////////
98 } // namespace zypp
99 ///////////////////////////////////////////////////////////////////
100 #endif // ZYPP_BASE_PROVIDENUMERICID_H