1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/base/DefaultIntegral.h
12 #ifndef ZYPP_BASE_DEFAULTINTEGRAL_H
13 #define ZYPP_BASE_DEFAULTINTEGRAL_H
16 #include <boost/static_assert.hpp>
17 #include <boost/type_traits/is_integral.hpp>
19 ///////////////////////////////////////////////////////////////////
21 { /////////////////////////////////////////////////////////////////
24 ///////////////////////////////////////////////////////////////////
26 // CLASS NAME : DefaultIntegral<_Tp,_Initial>
28 /** Integral type with defined initial value when default constructed.
31 * typedef DefaultIntegral<unsigned,0> Counter;
32 * std::map<KeyType,Counter> stats;
37 * \todo maybe specialize for bool, add logical and bit operators
38 * \todo let _Initial default to 0 then remove base/Counter.h
40 template<class _Tp, _Tp _Initial>
44 typedef _Tp value_type;
47 DefaultIntegral( _Tp val_r = _Initial )
49 { BOOST_STATIC_ASSERT(boost::is_integral<_Tp>::value); }
51 /** Conversion to _Tp. */
53 _Tp & get() { return _val; }
54 _Tp get() const { return _val; }
56 operator _Tp &() { return get(); }
57 operator _Tp () const { return get(); }
60 /** Reset to the defined initial value. */
61 DefaultIntegral & reset() { _val = _Initial; return *this; }
63 /** \name Arithmetic operations.
64 * \c + \c - \c * \c / are provided via conversion to _Tp.
67 DefaultIntegral & operator=( _Tp rhs ) { _val = rhs; return *this; }
68 DefaultIntegral & operator+=( _Tp rhs ) { _val += rhs; return *this; }
69 DefaultIntegral & operator-=( _Tp rhs ) { _val -= rhs; return *this; }
70 DefaultIntegral & operator*=( _Tp rhs ) { _val *= rhs; return *this; }
71 DefaultIntegral & operator/=( _Tp rhs ) { _val /= rhs; return *this; }
73 DefaultIntegral & operator++(/*prefix*/) { ++_val; return *this; }
74 DefaultIntegral & operator--(/*prefix*/) { --_val; return *this; }
76 DefaultIntegral operator++(int/*postfix*/) { return _val++; }
77 DefaultIntegral operator--(int/*postfix*/) { return _val--; }
84 /////////////////////////////////////////////////////////////////
86 ///////////////////////////////////////////////////////////////////
87 #endif // ZYPP_BASE_DEFAULTINTEGRAL_H