]> git.stg.codes - stg.git/blob - include/stg/resetable.h
Headers moved to subdir stg
[stg.git] / include / stg / resetable.h
1  /*
2  $Revision: 1.9 $
3  $Date: 2010/03/11 14:42:04 $
4  $Author: faust $
5  */
6
7 /*
8  * Copyright (c) 2001 by Peter Simons <simons@cryp.to>.
9  * All rights reserved.
10  */
11
12 #ifndef RESETABLE_VARIABLE_H
13 #define RESETABLE_VARIABLE_H
14
15 // This is a wrapper class about variables where you want to keep
16 // track of whether it has been assigened yet or not.
17
18 #include <iostream>
19
20 template <typename varT>
21 class RESETABLE
22 {
23     template <typename varT1>
24     friend std::ostream & operator<<(std::ostream & o, RESETABLE<varT1> v);
25 public:
26     typedef varT value_type;
27
28     //-------------------------------------------------------------------------
29     RESETABLE()
30         : value(),
31           is_set(false)
32     {
33     }
34     //-------------------------------------------------------------------------
35     RESETABLE<value_type>(const RESETABLE<value_type> & rvalue)
36         : value(rvalue.value),
37           is_set(rvalue.is_set)
38     {
39     }
40     //-------------------------------------------------------------------------
41     RESETABLE(const value_type& val)
42         : value(val),
43           is_set(true)
44     {
45     }
46     //-------------------------------------------------------------------------
47     RESETABLE<value_type> & operator=(const RESETABLE<value_type> & rvalue)
48     {
49         value = rvalue.value;
50         is_set = rvalue.is_set;
51         return *this;
52     }
53     //-------------------------------------------------------------------------
54     RESETABLE<value_type> & operator= (const value_type& rhs)
55     {
56         value = rhs;
57         is_set = true;
58         return *this;
59     }
60     //-------------------------------------------------------------------------
61     const value_type& const_data() const throw()
62     {
63         return value;
64     }
65     //-------------------------------------------------------------------------
66     value_type& data() throw()
67     {
68         return value;
69     }
70     //-------------------------------------------------------------------------
71     operator const value_type&() const throw()
72     {
73         return value;
74     }
75     //-------------------------------------------------------------------------
76     bool res_empty() const throw()
77     {
78         return !is_set;
79     }
80     //-------------------------------------------------------------------------
81     void reset() throw()
82     {
83         is_set = false;
84     }
85     //-------------------------------------------------------------------------
86 protected:
87     value_type  value;
88     bool        is_set;
89 };
90 //-----------------------------------------------------------------------------
91 template <typename varT>
92 std::ostream & operator<<(std::ostream & o, RESETABLE<varT> v)
93 {
94     return o << v.value;
95 }
96 //-------------------------------------------------------------------------
97 #endif // RESETABLE_VARIABLE_H
98