The Sparta Modeling Framework
Loading...
Searching...
No Matches
LockedValue.hpp
Go to the documentation of this file.
1// <LockedValue> -*- C++ -*-
2
3
10#pragma once
11
13
14namespace sparta{
15namespace utils{
16template<typename T>
17
41public:
43 typedef T value_type;
44
46 LockedValue() : is_locked_{false}, value_{}{}
47
49 explicit LockedValue(const T& value) : is_locked_{false}, value_{value}{}
50
52 LockedValue(const T& value, const bool lock) : is_locked_{lock}, value_{value}{}
53
56
58 // This asserts if the LockedValue instance is already locked.
59 LockedValue<T>& operator = (const T& value){
60 sparta_assert(!is_locked_,
61 "LockedValue is already locked and cannot be assigned a new value.");
62 value_ = value;
63 return *this;
64 }
65
67 // This asserts if the LockedValue instance is already locked.
68 void setAndLock(const T& value){
69 sparta_assert(!is_locked_,
70 "LockedValue is already locked and cannot be set a new value.");
71 value_ = value;
72 is_locked_ = true;
73 }
74
76 void lock(){
77 is_locked_ = true;
78 }
79
81 bool isLocked() const{
82 return is_locked_;
83 }
84
86 // Querying for the value should never throw, be it initialized or not.
87 // This is the const version.
88 const value_type& getValue() const{
89 return value_;
90 }
91
93 // querying for the value should never throw, be it initialized or not.
94 // this is the non-const version.
96 return value_;
97 }
98
100 // Converting to the value should never throw, be it initialized or not.
101 // This is the const-version.
102 operator const value_type& () const{
103 return value_;
104 }
105
107 // Converting to the value should never throw, be it initialized or not.
108 // This is the non-const-version.
109 operator value_type& (){
110 return value_;
111 }
112
114 bool operator == (const value_type& value) const{
115 return value_ == value;
116 }
117
119 bool operator != (const value_type& value) const{
120 return !operator == (value);
121 }
122private:
123 bool is_locked_ {false};
124 value_type value_;
125};
126
128template<class T>
129std::ostream& operator << (std::ostream& os, const sparta::utils::LockedValue<T>& value){
130 os << value.getValue();
131 return os;
132} // LockedValue
133} // namespace utils
134} // namespace sparta
Set of macros for Sparta assertions. Caught by the framework.
#define sparta_assert(...)
Simple variadic assertion that will throw a sparta_exception if the condition fails.
Provides a wrapper around a value to ensure that once the value is frozen or locked,...
void lock()
Lock the LockedValue instance immediately.
bool isLocked() const
Query if LockedValue instance is locked.
void setAndLock(const T &value)
This method assigns a value to LockedValue class and immediately locks it.
LockedValue< T > & operator=(const LockedValue< T > &)=delete
Copy-Assignment of a LockedValue to another LockedValue is a deleted function.
LockedValue()
This is the LockedValue Default Constructor.
const value_type & getValue() const
Get the value of the underlying resource of the LockedValue.
value_type & getValue()
get the value of the underlying resource of the lockedvalue.
bool operator!=(const value_type &value) const
Overload the compare not equal operator for LockedValue class.
LockedValue(const T &value, const bool lock)
This is the LockedValue Constructor with initial value and lock as parameter.
bool operator==(const value_type &value) const
Overload the compare equal operator for LockedValue class.
T value_type
Convenient typedef for the value type.
LockedValue(const T &value)
This is the LockedValue Constructor with initial value as parameter.
Macros for handling exponential backoff.