]> git.stg.codes - stg.git/blob - include/stg/resetable.h
a451b357326cd946c72b77ddf7b4bd57c65b1a36
[stg.git] / include / stg / resetable.h
1 /*
2  * Copyright (c) 2001 by Peter Simons <simons@cryp.to>.
3  * All rights reserved.
4  */
5
6 #ifndef RESETABLE_VARIABLE_H
7 #define RESETABLE_VARIABLE_H
8
9 // This is a wrapper class about variables where you want to keep
10 // track of whether it has been assigened yet or not.
11
12 template <typename T>
13 class RESETABLE
14 {
15 public:
16     typedef T value_type;
17
18     RESETABLE() : value(), is_set(false) {}
19     RESETABLE(const T & v) : value(v), is_set(true) {}
20
21     RESETABLE(const RESETABLE<T> & rvalue)
22         : value(rvalue.value),
23           is_set(rvalue.is_set)
24     {}
25
26     RESETABLE<T> & operator=(const RESETABLE<T> & rhs)
27     {
28         value = rhs.value;
29         is_set = rhs.is_set;
30         return *this;
31     }
32
33     RESETABLE<T> & operator=(const T & rhs)
34     {
35         value = rhs;
36         is_set = true;
37         return *this;
38     }
39
40     const T & const_data() const throw() { return value; }
41     T & data() throw() { return value; }
42     const T & data() const throw() { return value; }
43     bool empty() const throw() { return !is_set; }
44     void reset() throw() { is_set = false; }
45     void splice(const RESETABLE<T> & rhs)
46     {
47         if (rhs.is_set)
48         {
49             value = rhs.value;
50             is_set = true;
51         }
52     }
53     void maybeSet(value_type& dest) const
54     {
55         if (is_set)
56             dest = value;
57     }
58
59 private:
60     value_type value;
61     bool       is_set;
62 };
63
64 #endif // RESETABLE_VARIABLE_H