Skip to content

Commit

Permalink
include/nutconf.hpp: in Settable<T> operators to "return _value", thr…
Browse files Browse the repository at this point in the history
…ow() if it was not yet set() at all or since last clear() [networkupstools#2294, networkupstools#2433]

Signed-off-by: Jim Klimov <[email protected]>
  • Loading branch information
jimklimov committed May 6, 2024
1 parent 2b46c21 commit a478061
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions include/nutconf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,39 @@ class Settable
bool set()const{return _set;}
void clear(){_set = false;}

operator const Type&()const{return _value;}
operator Type&(){return _value;}
operator const Type&()const{
if (!set())
throw std::invalid_argument(
"Can not retrieve a Settable value of "
"an instance that was not assigned yet "
"(or was last known cleared)");
return _value;
}
operator Type&(){
if (!set())
throw std::invalid_argument(
"Can not retrieve a Settable value of "
"an instance that was not assigned yet "
"(or was last known cleared)");
return _value;
}

const Type& operator *()const{return _value;}
Type& operator *(){return _value;}
const Type& operator *()const{
if (!set())
throw std::invalid_argument(
"Can not retrieve a Settable value of "
"an instance that was not assigned yet "
"(or was last known cleared)");
return _value;
}
Type& operator *(){
if (!set())
throw std::invalid_argument(
"Can not retrieve a Settable value of "
"an instance that was not assigned yet "
"(or was last known cleared)");
return _value;
}

Settable<Type>& operator=(const Type& val){_value = val; _set = true; return *this;}

Expand Down

0 comments on commit a478061

Please sign in to comment.