Line data Source code
1 : /** \file changeable.hpp
2 : * \brief A simple class to track member data changes
3 : *
4 : * \author Jared R. Males (jaredmales@gmail.com)
5 : *
6 : * \ingroup utils_files
7 : *
8 : */
9 :
10 : #ifndef mx_base_changeable_hpp
11 : #define mx_base_changeable_hpp
12 :
13 : #include <cstdint>
14 : #include <limits>
15 :
16 : namespace mx
17 : {
18 : namespace base
19 : {
20 :
21 : /// A simple class to track member data changes.
22 : /** Maintains a monotonic counter that derived classes increment anytime a member
23 : * datum is changed. This allows for, say, re-initializing automatically on a subsequent
24 : * call to a function.
25 : *
26 : * \ingroup base
27 : */
28 : template <class _derivedT>
29 : class changeable
30 : {
31 :
32 : public:
33 : typedef _derivedT derivedT;
34 :
35 : /// The integer type of the counter.
36 : typedef uint64_t changeT;
37 :
38 : private:
39 : /// The counter itself.
40 : changeT m_change{ 0 };
41 :
42 : /// A marker for last change, set by \ref setChangePoint
43 : changeT m_changePoint{ std::numeric_limits<changeT>::max() };
44 :
45 : public:
46 : /// Increment the counter
47 : /** Call this function anytime the derived class changes something important.
48 : *
49 : */
50 18 : void changed()
51 : {
52 18 : ++m_change;
53 18 : }
54 :
55 : /// Get the value of the counter
56 : /** Get this at a checkpoint and compare it later to decide if any
57 : * action should be taken.
58 : */
59 0 : changeT change()
60 : {
61 0 : return m_change;
62 : }
63 :
64 5 : void setChangePoint()
65 : {
66 5 : m_changePoint = m_change;
67 5 : }
68 :
69 11 : bool isChanged()
70 : {
71 11 : return ( m_change != m_changePoint );
72 : }
73 : };
74 :
75 : } // namespace base
76 : } // namespace mx
77 :
78 : #endif // mx_base_changeable_hpp
|