aboutsummaryrefslogtreecommitdiffstats
path: root/host/include/uhd/utils/dirty_tracked.hpp
blob: 2d5403ce61b3bf2d146b6f58fefaab1beff59d5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//
// Copyright 2010-2015 Ettus Research LLC
// Copyright 2018 Ettus Research, a National Instruments Company
//
// SPDX-License-Identifier: GPL-3.0-or-later
//

#ifndef INCLUDED_UHD_UTILS_DIRTY_TRACKED_HPP
#define INCLUDED_UHD_UTILS_DIRTY_TRACKED_HPP

namespace uhd {
/*!
 * A class that wraps a data value with a dirty flag
 * When the client uses the assignment operator on this object, the object
 * automatically dirties itself if the assigned type is not equal the underlying
 * data. Data can be cleaned using the mark_clean entry-point.
 *
 * Requirements for data_t
 * - Must have a default constructor
 * - Must have a copy constructor
 * - Must have an assignment operator (=)
 * - Must have an equality operator (==)
 */
template <typename data_t>
class dirty_tracked
{
public:
    /*!
     * Default ctor: Initialize to default value and dirty
     */
    dirty_tracked()
        : _data()
        , // data_t must have a default ctor
        _dirty(true)
    {
    }

    /*!
     * Initialize to specified value and dirty
     */
    dirty_tracked(const data_t& value)
        : _data(value)
        , // data_t must have a copy ctor
        _dirty(true)
    {
    }

    dirty_tracked(const uhd::dirty_tracked<data_t>&) = default;

    /*!
     * Get underlying data
     */
    inline const data_t& get() const
    {
        return _data;
    }

    /*!
     * Has the underlying data changed since the last
     * time it was cleaned?
     */
    inline bool is_dirty() const
    {
        return _dirty;
    }

    /*!
     * Mark the underlying data as clean
     */
    inline void mark_clean()
    {
        _dirty = false;
    }

    /*!
     * Mark the underlying data as dirty
     */
    inline void force_dirty()
    {
        _dirty = true;
    }

    /*!
     * Assignment with data.
     * Store the specified value and mark it as dirty
     * if it is not equal to the underlying data.
     */
    inline dirty_tracked& operator=(const data_t& value)
    {
        if (!(_data == value)) { // data_t must have an equality operator
            _dirty = true;
            _data  = value; // data_t must have an assignment operator
        }
        return *this;
    }

    /*!
     * Assignment with dirty tracked type.
     * Store the specified value from dirty type and mark it as dirty
     * if it is not equal to the underlying data.
     * This exists to optimize out an implicit cast from dirty_tracked
     * type to data type.
     */
    inline dirty_tracked& operator=(const dirty_tracked& source)
    {
        if (!(_data == source._data)) {
            _dirty = true;
            _data  = source._data;
        }
        return *this;
    }

    /*!
     * Explicit conversion from this type to data_t
     */
    inline operator const data_t&() const
    {
        return get();
    }

private:
    data_t _data;
    bool _dirty;
};

} // namespace uhd

#endif /* INCLUDED_UHD_UTILS_DIRTY_TRACKED_HPP */