All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OptionalValue.h
Go to the documentation of this file.
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2013, SimQuest Solutions Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef SURGSIM_DATASTRUCTURES_OPTIONALVALUE_H
17 #define SURGSIM_DATASTRUCTURES_OPTIONALVALUE_H
18 
20 
21 namespace SurgSim
22 {
23 namespace DataStructures
24 {
25 
26 template <class T>
30 {
31 public:
32 
35  {
36  }
37 
40  explicit OptionalValue(const T& value) : m_hasValue(true), m_value(value)
41  {
42  }
43 
47  {
48  if (m_hasValue)
49  {
50  m_value = other.m_value;
51  }
52  }
53 
56  bool hasValue() const
57  {
58  return m_hasValue;
59  }
60 
62  void invalidate()
63  {
64  m_hasValue = false;
65  }
66 
69  void setValue(const T& val)
70  {
71  m_hasValue = true;
72  m_value = val;
73  }
74 
78  const T& getValue() const
79  {
80  SURGSIM_ASSERT(m_hasValue) << "Tried to fetch a value from an invalid OptionalValue";
81  return m_value;
82  }
83 
89  const T& operator*() const
90  {
91  SURGSIM_ASSERT(m_hasValue) << "Tried to fetch a value from an invalid OptionalValue";
92  return m_value;
93  }
94 
98  bool operator==(const OptionalValue<T>& rhs) const
99  {
100  if (m_hasValue == true && rhs.m_hasValue == true)
101  {
102  return m_value == rhs.m_value;
103  }
104  else
105  {
106  return m_hasValue == rhs.m_hasValue;
107  }
108  }
109 
110 
114  bool operator==(const T& rhs) const
115  {
116  if (m_hasValue)
117  {
118  return m_value == rhs;
119  }
120  else
121  {
122  return false;
123  }
124  }
125 
129  bool operator!=(const OptionalValue<T>& rhs) const
130  {
131  return !(*this == rhs);
132  }
133 
137  bool operator!=(const T& rhs) const
138  {
139  return !(*this == rhs);
140  }
141 
146  {
147  m_hasValue = rhs.m_hasValue;
148  if (m_hasValue)
149  {
150  m_value = rhs.m_value;
151  }
152  return *this;
153  }
154 
159  OptionalValue& operator=(const T& rhs)
160  {
161  setValue(rhs);
162  return *this;
163  }
164 
165 private:
168 };
169 
170 }; // Datastructures
171 }; // SurgSim
172 
173 #endif
bool operator!=(const T &rhs) const
Inequality operator.
Definition: OptionalValue.h:137
bool operator!=(const OptionalValue< T > &rhs) const
Inequality operator.
Definition: OptionalValue.h:129
void invalidate()
Mark this object as invalid.
Definition: OptionalValue.h:62
OptionalValue(const T &value)
Constructor that assigns a value.
Definition: OptionalValue.h:40
T m_value
Definition: OptionalValue.h:167
bool m_hasValue
Definition: OptionalValue.h:166
Container class that can indicate whether the object has been assigned a value.
Definition: OptionalValue.h:29
#define SURGSIM_ASSERT(condition)
Assert that condition is true.
Definition: Assert.h:77
OptionalValue()
Default Constructor, no value.
Definition: OptionalValue.h:34
bool operator==(const T &rhs) const
Equality operator.
Definition: OptionalValue.h:114
bool operator==(const OptionalValue< T > &rhs) const
Equality operator.
Definition: OptionalValue.h:98
OptionalValue & operator=(const OptionalValue &rhs)
Assignment operator.
Definition: OptionalValue.h:145
OptionalValue(const OptionalValue &other)
Copy constructor.
Definition: OptionalValue.h:46
The header that provides the assertion API.
bool hasValue() const
Query if this object has been assigned a value.
Definition: OptionalValue.h:56
OptionalValue & operator=(const T &rhs)
Assignment operator from template type, after this hasValue() is true even if the right hand side was...
Definition: OptionalValue.h:159
const T & operator*() const
Gets the value.
Definition: OptionalValue.h:89
void setValue(const T &val)
Set the value of this object, and mark it as valid.
Definition: OptionalValue.h:69
const T & getValue() const
Gets the value.
Definition: OptionalValue.h:78