- ProvideNumericId: template the id type (integral)
[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      * \code
30      * struct Foo : public base::ProvideNumericId<Foo,unsigned>
31      * {};
32      * Foo foo;
33      * foo.numericId(); // returns foo's NumericId.
34      * \endcode
35     */
36     template<class _Derived, class _NumericIdType>
37       struct ProvideNumericId
38       {
39       public:
40         /** \return The objects numeric Id. */
41         _NumericIdType numericId() const
42         { return _numericId; }
43
44       protected:
45         /** Default ctor */
46         ProvideNumericId()
47         : _numericId( nextId() )
48         {}
49         /** Copy ctor */
50         ProvideNumericId( const ProvideNumericId & /*rhs*/ )
51         : _numericId( nextId() )
52         {}
53         /** Assign */
54         ProvideNumericId & operator=( const ProvideNumericId & /*rhs*/ )
55         { return *this; }
56         /** Dtor */
57         ~ProvideNumericId()
58         {}
59       private:
60         /**  */
61         static _NumericIdType nextId()
62         {
63           static _NumericIdType _staticCounter = 0;
64           return ++_staticCounter;
65         }
66         /**  */
67         const _NumericIdType _numericId;
68       };
69     ///////////////////////////////////////////////////////////////////
70
71     /////////////////////////////////////////////////////////////////
72   } // namespace base
73   ///////////////////////////////////////////////////////////////////
74   /////////////////////////////////////////////////////////////////
75 } // namespace zypp
76 ///////////////////////////////////////////////////////////////////
77 #endif // ZYPP_BASE_PROVIDENUMERICID_H