]> git.stg.codes - stg.git/blob - include/stg/resetable.h
Add declaration for operator<< for resetable
[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 T>
21 class RESETABLE
22 {
23 public:
24     typedef T value_type;
25
26     RESETABLE() : value(), is_set(false) {}
27
28     RESETABLE(const RESETABLE<value_type> & rvalue)
29         : value(rvalue.value),
30           is_set(rvalue.is_set)
31     {}
32
33     RESETABLE(const value_type& val) : value(val), is_set(true) {}
34
35     RESETABLE<value_type> & operator=(const RESETABLE<value_type> & rvalue)
36     {
37         value = rvalue.value;
38         is_set = rvalue.is_set;
39         return *this;
40     }
41
42     RESETABLE<value_type> & operator=(const value_type& rhs)
43     {
44         value = rhs;
45         is_set = true;
46         return *this;
47     }
48
49     const value_type & const_data() const throw() { return value; }
50     value_type & data() throw() { return value; }
51     operator const value_type&() const throw() { return value; }
52     bool res_empty() const throw() { return !is_set; }
53     void reset() throw() { is_set = false; }
54
55 private:
56     value_type value;
57     bool       is_set;
58 };
59
60 template <typename T>
61 std::ostream & operator<<(std::ostream & o, const RESETABLE<T> & v);
62
63 template <typename T>
64 inline
65 std::ostream & operator<<(std::ostream & o, const RESETABLE<T> & v)
66 {
67     return o << v.const_data();
68 }
69
70 #endif // RESETABLE_VARIABLE_H