The Sparta Modeling Framework
Loading...
Searching...
No Matches
ValidValue.hpp
Go to the documentation of this file.
1// <Port> -*- C++ -*-
2
3
10#pragma once
11
12#include <iostream>
14
15namespace sparta
16{
17namespace utils
18{
19
24 template <typename T>
26 {
27 public:
29 typedef T value_type;
30
33 valid_(false),
34 value_()
35 {}
36
45 template<typename ...ArgsT>
46 ValidValue(ArgsT&& ...args) :
47 valid_(true),
48 value_(std::forward<ArgsT>(args)...)
49 {}
50
54 ValidValue(ValidValue &) = default;
55
58 valid_(v.valid_),
59 value_(std::move(v.value_))
60 {
61 v.valid_ = false;
62 }
63
65 ValidValue(const ValidValue &) = default;
66
68 ValidValue & operator=(const ValidValue&) = default;
69 ValidValue & operator=(ValidValue&&) = default;
70
77 valid_ = true;
78 return (value_ = val);
79 }
80
87 valid_ = true;
88 return (value_ = std::move(val));
89 }
90
96 bool operator==(const value_type & val) const {
97 sparta_assert(valid_ == true, "ValidValue is not valid for compare!");
98 return (value_ == val);
99 }
100
106 bool operator!=(const value_type & val) const {
107 return !operator==(val);
108 }
109
115 bool operator==(const ValidValue<T> & val) const {
116 sparta_assert(valid_ == true, "ValidValue is not valid for compare!");
117 return (value_ == val.value_);
118 }
119
125 bool operator!=(const ValidValue<T> & val) const {
126 return !operator==(val);
127 }
128
133 bool isValid() const {
134 return valid_;
135 }
136
142 operator const value_type &() const {
143 sparta_assert(valid_ == true, "ValidValue is not valid for conversion!");
144 return value_;
145 }
146
153 operator value_type & () {
154 sparta_assert(valid_ == true, "ValidValue is not valid for conversion!");
155 return value_;
156 }
157
162 const value_type & getValue() const {
163 sparta_assert(valid_ == true, "ValidValue is not valid for getting!");
164 return value_;
165 }
166
173 sparta_assert(valid_ == true, "ValidValue is not valid for getting!");
174 return value_;
175 }
176
180 void clearValid() {
181 valid_ = false;
182 }
183
187 template <typename Archive>
188 void serialize(Archive & ar, const unsigned int /* version */) {
189 // The operator& here is a reader/writer for the member variable RHS.
190 // We should always read and write the valid_ flag, but only do the
191 // same for value_ when valid.
192 ar & valid_;
193 if (valid_)
194 {
195 ar & value_;
196 }
197 }
198
199 private:
200 bool valid_ = false;
201 value_type value_;
202 };
203
204template<class ValidValT>
205std::ostream & operator<<(std::ostream & os, const sparta::utils::ValidValue<ValidValT> & vv)
206{
207 if(vv.isValid()) {
208 os << vv.getValue();
209 }
210 else {
211 os << "<invalid ValidValue>";
212 }
213 return os;
214}
215}
216}
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 the value is assigned.
ValidValue(ValidValue &&v)
Allow moves.
value_type operator=(const value_type &val)
Assignment.
T value_type
Convenient typedef for the value type.
bool operator==(const ValidValue< T > &val) const
Compare equal.
ValidValue(ArgsT &&...args)
Construct with a valid starting value.
ValidValue(const ValidValue &)=default
Allow copies.
ValidValue(ValidValue &)=default
ValidValue & operator=(const ValidValue &)=default
Allow assignments.
value_type operator=(value_type &&val)
Assignment.
value_type & getValue()
Get the value.
void serialize(Archive &ar, const unsigned int)
Boost::serialization support.
bool operator!=(const value_type &val) const
Compare not equal.
bool operator==(const value_type &val) const
Compare equal.
void clearValid()
Clear the validity of this object.
const value_type & getValue() const
Get the value - const version.
ValidValue()
Construct with no validity (not valid, uninitialized)
bool isValid() const
Is this value valid.
bool operator!=(const ValidValue< T > &val) const
Compare not equal.
Macros for handling exponential backoff.
std::ostream & operator<<(std::ostream &o, const SimulationInfo &info)
ostream insertion operator for SimulationInfo