]> git.stg.codes - stg.git/blob - include/stg/resetable.h
Various fixes of issues reported by static analyzers.
[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     explicit RESETABLE(const T & v) : value(v), is_set(true) {}
20
21     RESETABLE<T> & operator=(const T & rhs)
22     {
23         value = rhs;
24         is_set = true;
25         return *this;
26     }
27
28     const T & const_data() const throw() { return value; }
29     T & data() throw() { return value; }
30     const T & data() const throw() { return value; }
31     bool empty() const throw() { return !is_set; }
32     void reset() throw() { is_set = false; }
33     void splice(const RESETABLE<T> & rhs)
34     {
35         if (rhs.is_set)
36         {
37             value = rhs.value;
38             is_set = true;
39         }
40     }
41     void maybeSet(value_type& dest) const
42     {
43         if (is_set)
44             dest = value;
45     }
46
47 private:
48     value_type value;
49     bool       is_set;
50 };
51
52 #endif // RESETABLE_VARIABLE_H