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
114 bool isValid() const {
115 return valid_;
116 }
117
123 operator const value_type &() const {
124 sparta_assert(valid_ == true, "ValidValue is not valid for conversion!");
125 return value_;
126 }
127
134 operator value_type & () {
135 sparta_assert(valid_ == true, "ValidValue is not valid for conversion!");
136 return value_;
137 }
138
143 const value_type & getValue() const {
144 sparta_assert(valid_ == true, "ValidValue is not valid for getting!");
145 return value_;
146 }
147
154 sparta_assert(valid_ == true, "ValidValue is not valid for getting!");
155 return value_;
156 }
157
161 void clearValid() {
162 valid_ = false;
163 }
164
165 private:
166 bool valid_ = false;
167 value_type value_;
168 };
169
170template<class ValidValT>
171std::ostream & operator<<(std::ostream & os, const sparta::utils::ValidValue<ValidValT> & vv)
172{
173 if(vv.isValid()) {
174 os << vv.getValue();
175 }
176 else {
177 os << "<invalid ValidValue>";
178 }
179 return os;
180}
181}
182}
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.
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.
bool operator!=(const value_type &val) const
Compare not equal.
bool operator==(const value_type &val) const
Compare equal.
T value_type
Convenient typedef for the value type.
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.
Macros for handling exponential backoff.
std::ostream & operator<<(std::ostream &o, const SimulationInfo &info)
ostream insertion operator for SimulationInfo