OptionalValue.h
Go to the documentation of this file.
// This file is a part of the OpenSurgSim project.
// Copyright 2013, SimQuest Solutions Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef SURGSIM_DATASTRUCTURES_OPTIONALVALUE_H
#define SURGSIM_DATASTRUCTURES_OPTIONALVALUE_H
#include "SurgSim/Framework/Assert.h"
namespace SurgSim
{
namespace DataStructures
{
template <class T>
class OptionalValue
{
public:
OptionalValue() : m_hasValue(false)
{
}
explicit OptionalValue(const T& value) : m_hasValue(true), m_value(value)
{
}
OptionalValue(const OptionalValue& other) : m_hasValue(other.m_hasValue)
{
if (m_hasValue)
{
m_value = other.m_value;
}
}
bool hasValue() const
{
return m_hasValue;
}
void invalidate()
{
m_hasValue = false;
}
void setValue(const T& val)
{
m_hasValue = true;
m_value = val;
}
const T& getValue() const
{
SURGSIM_ASSERT(m_hasValue) << "Tried to fetch a value from an invalid OptionalValue";
return m_value;
}
const T& operator*() const
{
SURGSIM_ASSERT(m_hasValue) << "Tried to fetch a value from an invalid OptionalValue";
return m_value;
}
bool operator==(const OptionalValue<T>& rhs) const
{
if (m_hasValue == true && rhs.m_hasValue == true)
{
return m_value == rhs.m_value;
}
else
{
return m_hasValue == rhs.m_hasValue;
}
}
bool operator==(const T& rhs) const
{
if (m_hasValue)
{
return m_value == rhs;
}
else
{
return false;
}
}
bool operator!=(const OptionalValue<T>& rhs) const
{
return !(*this == rhs);
}
bool operator!=(const T& rhs) const
{
return !(*this == rhs);
}
OptionalValue& operator=(const OptionalValue& rhs)
{
m_hasValue = rhs.m_hasValue;
if (m_hasValue)
{
m_value = rhs.m_value;
}
return *this;
}
OptionalValue& operator=(const T& rhs)
{
setValue(rhs);
return *this;
}
private:
bool m_hasValue;
T m_value;
};
}; // Datastructures
}; // SurgSim
#endif
SurgSim::DataStructures::OptionalValue::operator*
const T & operator*() const
Gets the value.
Definition: OptionalValue.h:89
SurgSim
Definition: CompoundShapeToGraphics.cpp:29
SurgSim::DataStructures::OptionalValue::operator!=
bool operator!=(const OptionalValue< T > &rhs) const
Inequality operator.
Definition: OptionalValue.h:129
SurgSim::DataStructures::OptionalValue::invalidate
void invalidate()
Mark this object as invalid.
Definition: OptionalValue.h:62
SurgSim::DataStructures::OptionalValue::OptionalValue
OptionalValue(const T &value)
Constructor that assigns a value.
Definition: OptionalValue.h:40
SurgSim::DataStructures::OptionalValue::m_value
T m_value
Definition: OptionalValue.h:167
SurgSim::DataStructures::OptionalValue::m_hasValue
bool m_hasValue
Definition: OptionalValue.h:166
SurgSim::DataStructures::OptionalValue
Container class that can indicate whether the object has been assigned a value.
Definition: OptionalValue.h:29
SURGSIM_ASSERT
#define SURGSIM_ASSERT(condition)
Assert that condition is true.
Definition: Assert.h:77
SurgSim::DataStructures::OptionalValue::OptionalValue
OptionalValue()
Default Constructor, no value.
Definition: OptionalValue.h:34
SurgSim::DataStructures::OptionalValue::operator!=
bool operator!=(const T &rhs) const
Inequality operator.
Definition: OptionalValue.h:137
SurgSim::DataStructures::OptionalValue::operator=
OptionalValue & operator=(const OptionalValue &rhs)
Assignment operator.
Definition: OptionalValue.h:145
SurgSim::DataStructures::OptionalValue::OptionalValue
OptionalValue(const OptionalValue &other)
Copy constructor.
Definition: OptionalValue.h:46
Assert.h
The header that provides the assertion API.
SurgSim::DataStructures::OptionalValue::operator=
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
SurgSim::DataStructures::OptionalValue::operator==
bool operator==(const T &rhs) const
Equality operator.
Definition: OptionalValue.h:114
SurgSim::DataStructures::OptionalValue::hasValue
bool hasValue() const
Query if this object has been assigned a value.
Definition: OptionalValue.h:56
SurgSim::DataStructures::OptionalValue::setValue
void setValue(const T &val)
Set the value of this object, and mark it as valid.
Definition: OptionalValue.h:69
SurgSim::DataStructures::OptionalValue::operator==
bool operator==(const OptionalValue< T > &rhs) const
Equality operator.
Definition: OptionalValue.h:98
SurgSim::DataStructures::OptionalValue::getValue
const T & getValue() const
Gets the value.
Definition: OptionalValue.h:78