Imported Upstream version 16.5.0
[platform/upstream/libzypp.git] / zypp / base / SerialNumber.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/base/SerialNumber.h
10  *
11 */
12 #ifndef ZYPP_BASE_SERIALNUMBER_H
13 #define ZYPP_BASE_SERIALNUMBER_H
14
15 #include <iosfwd>
16
17 #include "zypp/base/PtrTypes.h"
18
19 ///////////////////////////////////////////////////////////////////
20 namespace zypp
21 { /////////////////////////////////////////////////////////////////
22
23   ///////////////////////////////////////////////////////////////////
24   //
25   //    CLASS NAME : SerialNumber
26   //
27   /** Simple serial number provider.
28    *
29    * \ref serial returns a serial number. The number returned stays
30    * the same unless \ref setDirty was called to bring the object
31    * into \c dirty state. The next call to \ref serial will increment
32    * the serial number and bring the object into \c clean state.
33    *
34    * \code
35    * SerialNumber sno;
36    * sno.serial();             // SERIAL(0); () = clean
37    * sno.setDirty();           // SERIAL*0*; ** = dirty
38    * sno.serial();             // SERIAL(1)
39    * sno.setDirty();           // SERIAL*1*
40    * sno.setDirty();           // SERIAL*1*
41    * sno.serial();             // SERIAL(2)
42    * \endcode
43    */
44   class SerialNumber
45   {
46     friend std::ostream & operator<<( std::ostream & str, const SerialNumber & obj );
47
48     public:
49       /** Ctor taking initial \c dirty value. */
50       SerialNumber( bool dirty_r = false );
51       /** Dtor */
52       virtual ~SerialNumber();
53
54     public:
55       void setDirty()
56       { _dirty = true; }
57
58     public:
59       bool dirty() const
60       { return _dirty; }
61
62       bool clean() const
63       { return !_dirty; }
64
65       unsigned serial() const
66       {
67         if ( _dirty )
68         {
69           ++_serial;
70           _dirty = false;
71         }
72         return _serial;
73       }
74
75     private:
76       mutable bool     _dirty;
77       mutable unsigned _serial;
78   };
79   ///////////////////////////////////////////////////////////////////
80
81   /** \relates SerialNumber Stream output */
82   std::ostream & operator<<( std::ostream & str, const SerialNumber & obj );
83
84   ///////////////////////////////////////////////////////////////////
85   //
86   //    CLASS NAME : SerialNumberWatcher
87   //
88   /** Simple serial number watcher.
89    *
90    * \ref SerialNumberWatcher remembers a serial number
91    * and tells whenever new numbers you feed change.
92    *
93    * All methods are overloaded to take an \unsigned or a
94    * <tt>const SerialNumber &</tt> as argument.
95    *
96    * \code
97    * SerialNumber sno;
98    *
99    * void check()
100    * {
101    *   static SerialNumberWatcher watcher( sno );
102    *
103    *   if ( watcher.remember( sno ) )
104    *   {
105    *     cout << "Serial number changed." << endl;
106    *   }
107    * }
108    *
109    * int main()
110    * {
111    *   check();          // This call would trigger, if check used a
112    *                     // default constructed SerialNumberWatcher.
113    *
114    *   check();          //
115    *   sno.dirty();
116    *   check();          // "Serial number changed."
117    *   check();          //
118    *   sno.dirty();
119    *   check();          // "Serial number changed."
120    * \endcode
121    */
122   class SerialNumberWatcher
123   {
124     friend std::ostream & operator<<( std::ostream & str, const SerialNumberWatcher & obj );
125
126     public:
127       /** Ctor taking an initial \c serial value.
128        *
129        * A default constructed SerialNumberWatcher remembers the serial
130        * number <tt>(unsigned)-1</tt>. So it is most likely the the 1st
131        * call to \ref remember returns \ref isDirty.
132        *
133        * Vice versa, initializing the SerialNumberWatcher with the current
134        * SerialNumber, most likely prevents the 1st to \ref remember to
135        * return \ref isDirty.
136       */
137       SerialNumberWatcher( unsigned serial_r = (unsigned)-1 );
138       /** Ctor taking an initial \c serial value. */
139       SerialNumberWatcher( const SerialNumber & serial_r );
140       /** Dtor */
141       virtual ~SerialNumberWatcher();
142
143     public:
144       /** Return whether \c serial_r differs. */
145       bool isDirty( unsigned serial_r ) const
146       { return( _serial != serial_r ); }
147       /** \overload */
148       bool isDirty( const SerialNumber & serial_r ) const
149       { return( _serial != serial_r.serial() ); }
150
151       /** Return whether \c serial_r is still unchanged. */
152       bool isClean( unsigned serial_r ) const
153       { return( _serial == serial_r ); }
154       /** \overload */
155       bool isClean( const SerialNumber & serial_r ) const
156       { return( _serial == serial_r.serial() ); }
157
158     public:
159       /** Return \ref isDirty, storing \c serial_r as new value. */
160       bool remember( unsigned serial_r ) const
161       {
162         if ( isDirty( serial_r ) )
163         {
164           _serial = serial_r;
165           return true;
166         }
167         return false;
168       }
169       /** \overload */
170       bool remember( const SerialNumber & serial_r ) const
171       { return remember( serial_r.serial() ); }
172
173     private:
174       mutable unsigned _serial;
175   };
176   ///////////////////////////////////////////////////////////////////
177
178   /** \relates SerialNumberWatcher Stream output */
179   std::ostream & operator<<( std::ostream & str, const SerialNumberWatcher & obj );
180
181   /////////////////////////////////////////////////////////////////
182 } // namespace zypp
183 ///////////////////////////////////////////////////////////////////
184 #endif // ZYPP_BASE_SERIALNUMBER_H