Lumiera  0.pre.03
»edit your freedom«
optional-ref.hpp
Go to the documentation of this file.
1 /*
2  OPTIONAL-REF.hpp - optional and switchable reference
3 
4  Copyright (C) Lumiera.org
5  2010, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 */
22 
23 
31 #ifndef LIB_OPTIONAL_REF_H
32 #define LIB_OPTIONAL_REF_H
33 
34 #include "lib/error.hpp"
35 
36 
37 namespace lib {
38 
39  using LERR_(BOTTOM_VALUE);
40 
41 
42 
43 
44 
60  template<typename T>
62  {
63  T* ref_;
64 
65 
66  public:
67  OptionalRef()
68  : ref_(0)
69  { }
70 
71  ~OptionalRef()
72  {
73  clear();
74  }
75 
76  // using default copy operations...
77 
78  explicit
79  OptionalRef(T& target)
80  : ref_(&target)
81  { }
82 
83  explicit operator bool() const { return isValid(); }
84 
85 
86  T&
87  operator() () const
88  {
89  if (!isValid())
90  throw lumiera::error::Logic ("access to this object is (not/yet) enabled"
91  , LERR_(BOTTOM_VALUE));
92  return *ref_;
93  }
94 
95 
96  /* === mutations ===*/
97 
98  void
99  link_to (T& target)
100  {
101  ref_ = &target;
102  }
103 
104  void
105  clear()
106  {
107  ref_ = 0;
108  }
109 
110 
111  /* === comparison and diagnostics === */
112 
113  bool
114  isValid() const
115  {
116  return bool(ref_);
117  }
118 
119  bool
120  points_to (T const& target) const
121  {
122  return isValid()
123  && ref_ == &target;
124  }
125 
126  friend bool
127  operator== (OptionalRef const& r1, OptionalRef const& r2)
128  {
129  return r1.ref_ == r2.ref_;
130  }
131  friend bool
132  operator!= (OptionalRef const& r1, OptionalRef const& r2)
133  {
134  return r1.ref_ != r2.ref_;
135  }
136 
137  // mixed comparisons
138  friend bool
139  operator== (OptionalRef const& ref, T const& otherTarget)
140  {
141  return ref() == otherTarget;
142  }
143 
144  friend bool operator== (T const& otherTarget, OptionalRef const& ref) { return ref == otherTarget; }
145  friend bool operator!= (T const& otherTarget, OptionalRef const& ref) { return !(ref == otherTarget); }
146  friend bool operator!= (OptionalRef const& ref, T const& otherTarget) { return !(ref == otherTarget); }
147  };
148 
149 
150  template<typename T>
152  optionalRefTo (T& target)
153  {
154  return OptionalRef<T> (target);
155  }
156 
157 
158 
159 } // namespace lib
160 #endif
Optional or switchable link to an existing object.
OptionalRef(T &target)
Implementation namespace for support and library code.
Derived specific exceptions within Lumiera&#39;s exception hierarchy.
Definition: error.hpp:199
Lumiera error handling (C++ interface).