1 #ifndef MARLINMT_PARAMETER_h 2 #define MARLINMT_PARAMETER_h 1 58 const std::string &na,
59 const std::string &desc,
60 std::shared_ptr<T> addr ) :
64 _typeIndex(typeid(T)),
66 _validator(createValidator<T>(nullptr)) {
82 const std::string &na,
83 const std::string &desc,
84 std::shared_ptr<T> addr,
89 _typeIndex(typeid(T)),
91 _defaultValue(
std::make_shared<T>(
std::move(defVal))),
92 _validator(createValidator<T>(nullptr)) {
104 const std::string& name()
const ;
109 const std::string& description()
const ;
119 bool hasDefault()
const ;
124 std::string str()
const ;
129 std::string defaultStr()
const ;
134 void str(
const std::string &
value ) ;
139 std::string typeStr()
const ;
144 const std::type_index &typeIndex()
const ;
149 template <
typename T>
151 return ( std::type_index(
typeid(T)) == _typeIndex ) ;
157 template <
typename T>
159 if( not isType<T>() ) {
169 template <
typename T>
172 _validator = createValidator( validator ) ;
181 template <
typename T>
182 inline void set(
const T &val ) {
184 if( _validator.has_value() ) {
186 if (not validatorT( val ) ) {
187 MARLINMT_THROW(
"Parameter '" + name() +
"': invalid parameter value" ) ;
190 *std::static_pointer_cast<T>( _value ).
get() = val ;
198 template <
typename T>
199 inline T
get()
const {
202 if( not hasDefault() ) {
205 return *std::static_pointer_cast<T>( _defaultValue ).
get() ;
207 return *std::static_pointer_cast<T>( _value ).
get() ;
216 template <
typename T>
217 inline T
get(
const T &fallback )
const {
230 template <
typename T>
232 if( not hasDefault() ) {
233 MARLINMT_THROW(
"Parameter '" + name() +
"' has no default value" ) ;
235 return *std::static_pointer_cast<T>( _defaultValue ).
get() ;
251 template <
typename T>
254 _resetFunction = [
this] { *std::static_pointer_cast<T>( _value ).
get() = T() ; };
256 _fromStrFunction = [
this] (
ValueType ptr,
const std::string &
value ) {
258 if( _validator.has_value() ) {
260 if( not validatorT( valueT ) ) {
261 MARLINMT_THROW(
"Parameter '" + name() +
"': invalid parameter value" ) ;
264 *std::static_pointer_cast<T>( ptr ).
get() = valueT ;
269 template <
typename T>
271 std::any validator {} ;
272 if(
nullptr != validatorT ) {
273 validator = validatorT ;
282 std::string _name {} ;
284 std::string _description {} ;
294 bool _isSet {
false} ;
317 using ParameterMap = std::map<std::string, std::shared_ptr<ParameterImpl>> ;
329 template <
typename T>
331 checkParameter( name ) ;
332 auto param = std::make_shared<ParameterImpl>( paramType, name, desc,
value ) ;
333 _parameters[ name ] = param ;
346 template <
typename T>
347 inline std::shared_ptr<ParameterImpl>
addParameter(
EParameterType paramType,
const std::string &name,
const std::string &desc, std::shared_ptr<T>
value, T defVal ) {
348 checkParameter( name ) ;
349 auto param = std::make_shared<ParameterImpl>( paramType, name, desc,
value, std::move(defVal) ) ;
350 _parameters[ name ] = param ;
359 template <
typename T>
361 checkParameter( name ) ;
362 return _parameters.find( name )->second->get<T>() ;
372 template <
typename T>
373 inline T
parameter(
const std::string &name,
const T &fallback )
const {
374 checkParameter( name ) ;
375 return _parameters.find( name )->second->get<T>( fallback ) ;
383 void checkParameter(
const std::string &name )
const ;
390 bool exists(
const std::string &name )
const ;
397 bool isSet(
const std::string &name )
const ;
426 template <
typename T>
442 _impl = std::make_shared<ParameterImpl>( paramType, na, desc, _value ) ;
454 _impl = std::make_shared<ParameterImpl>( paramType, na, desc, _value, defVal ) ;
466 _impl = conf.
addParameter( paramType, na, desc, _value ) ;
479 _impl = conf.
addParameter( paramType, na, desc, _value, defVal ) ;
486 return _impl->type() ;
492 inline const std::string&
name()
const {
493 return _impl->name() ;
500 return _impl->description() ;
507 return _impl->isSet() ;
514 return _impl->hasDefault() ;
520 inline std::string
str()
const {
521 return _impl->str() ;
528 return _impl->defaultStr() ;
535 return _impl->typeStr() ;
542 return _impl->typeIndex() ;
548 inline operator T()
const {
556 inline T
get()
const {
557 return _impl->get<T>() ;
564 inline T
get(
const T &fallback )
const {
565 return _impl->get<T>( fallback ) ;
574 _impl->set(
value ) ;
590 _impl->setValidator( validator ) ;
595 std::shared_ptr<T> _value { std::make_shared<T>() } ;
597 std::shared_ptr<ParameterImpl> _impl {} ;
607 template <
typename T>
625 inline Parameter(
const std::string &na,
const std::string &desc ) :
637 inline Parameter(
const std::string &na,
const std::string &desc,
const T &defVal ) :
676 template <
typename T>
710 inline VectorParameter(
const std::string &na,
const std::string &desc,
const std::vector<T> &defVal ) :
741 auto at(
typename std::vector<T>::size_type idx )
const {
return Base::_value->at(idx) ; }
742 auto operator[](
typename std::vector<T>::size_type idx )
const {
return *(Base::_value)[idx] ; }
743 auto front()
const {
return Base::_value->front() ; }
744 auto back()
const {
return Base::_value->back() ; }
745 auto data()
const {
return Base::_value->data() ; }
746 auto begin()
const {
return Base::_value->begin() ; }
747 auto end()
const {
return Base::_value->end() ; }
748 auto cbegin()
const {
return Base::_value->cbegin() ; }
749 auto cend()
const {
return Base::_value->cend() ; }
750 auto rbegin()
const {
return Base::_value->rbegin() ; }
751 auto rend()
const {
return Base::_value->rend() ; }
752 auto crbegin()
const {
return Base::_value->crbegin() ; }
753 auto crend()
const {
return Base::_value->crend() ; }
754 auto empty()
const {
return Base::_value->empty() ; }
755 auto size()
const {
return Base::_value->size() ; }
756 auto max_size()
const {
return Base::_value->max_size() ; }
761 template <
typename T>
762 inline std::ostream &operator <<( std::ostream &stream, const Parameter<T> &rhs ) {
763 stream << rhs.get() ;
769 template <
typename T,
typename S>
771 return ( lhs.
get() == rhs ) ;
776 template <
typename T,
typename S>
778 return ( lhs.
get() != rhs ) ;
783 template <
typename T,
typename S>
784 inline bool operator <( const Parameter<T> &lhs,
const S &rhs ) {
785 return ( lhs.get() < rhs ) ;
790 template <
typename T,
typename S>
791 inline bool operator <=( const Parameter<T> &lhs,
const S &rhs ) {
792 return ( lhs.get() <= rhs ) ;
797 template <
typename T,
typename S>
799 return ( lhs.
get() > rhs ) ;
804 template <
typename T,
typename S>
806 return ( lhs.
get() >= rhs ) ;
bool hasDefault() const
Whether the parameter has a default value.
bool operator>(const Parameter< T > &lhs, const S &rhs)
Configurable class Interface for configuring components in the framework.
Parameter(Configurable &conf, const std::string &na, const std::string &desc, const T &defVal)
Constructor.
ParameterBase(Configurable &conf, EParameterType paramType, const std::string &na, const std::string &desc, const T &defVal)
Constructor.
T parameter(const std::string &name) const
Get a parameter value.
VectorParameter(const std::string &na, const std::string &desc, const std::vector< T > &defVal)
Constructor.
std::function< std::string()> TypeFunction
T parameter(const std::string &name, const T &fallback) const
Get a parameter value.
void construct()
Construct the parameter (called from ctor)
ParameterMap::const_iterator const_iterator
ParameterMap::iterator iterator
EParameterType
EParameterType enumerator Enumerates parameter types supported by Marlin.
ParameterImpl class Abstract internal implementation of a parameter.
std::any ValidatorFunction
std::map< std::string, std::shared_ptr< ParameterImpl > > ParameterMap
constexpr unsigned long long value(const Flag_t &flag)
std::function< void()> ResetFunction
ParameterBase(EParameterType paramType, const std::string &na, const std::string &desc)
Constructor.
Parameter<T> class High level interface to register simple parameter values (int, float...
ParameterImpl(EParameterType paramType, const std::string &na, const std::string &desc, std::shared_ptr< T > addr)
Constructor.
void setValidator(ValidatorFunctionT< T > validator)
Set the validator function.
const std::string & description() const
Get the parameter description.
bool operator!=(const Parameter< T > &lhs, const S &rhs)
const std::type_index & typeIndex() const
Get a type index object of the underlying type.
std::string str() const
Get the parameter value as string.
ValidatorFunction createValidator(ValidatorFunctionT< T > validatorT) const
Construct the validator function.
std::function< void(ValueType, const std::string &)> FromStrFunction
std::function< bool(const T &)> ValidatorFunctionT
VectorParameter(Configurable &conf, const std::string &na, const std::string &desc)
Constructor.
std::function< std::string(ValueType)> StrFunction
std::shared_ptr< ParameterImpl > addParameter(EParameterType paramType, const std::string &name, const std::string &desc, std::shared_ptr< T > value, T defVal)
Add a parameter.
void checkType() const
Throw an exception if the internal type doesn't match the template parameter type.
#define MARLINMT_THROW(message)
std::type_index _typeIndex
The type index object of the underlying parameter type.
Parameter(const std::string &na, const std::string &desc)
Constructor.
VectorParameter(Configurable &conf, const std::string &na, const std::string &desc, const std::vector< T > &defVal)
Constructor.
static T from_string(const std::string &str)
auto operator[](typename std::vector< T >::size_type idx) const
std::shared_ptr< ParameterImpl > addParameter(EParameterType paramType, const std::string &name, const std::string &desc, std::shared_ptr< T > value)
Add a parameter.
ParameterBase<T> class Base interface for user parameters.
auto at(typename std::vector< T >::size_type idx) const
bool isType() const
Check whether the template parameter matches the internal implementation type.
bool operator==(const Parameter< T > &lhs, const S &rhs)
T get() const
Get the parameter value.
Simple (scalar) parameter.
static std::string to_string(const T &value)
EParameterType type() const
Get the parameter type.
VectorParameter<T> class.
T getDefault() const
Get the default parameter value if set.
std::string defaultStr() const
Get the parameter default value as string.
Parameter(const std::string &na, const std::string &desc, const T &defVal)
Constructor.
ParameterImpl(EParameterType paramType, const std::string &na, const std::string &desc, std::shared_ptr< T > addr, T defVal)
Constructor.
Parameter(Configurable &conf, const std::string &na, const std::string &desc)
Constructor.
void setValidator(ValidatorFunctionT< T > validator)
Set the parameter validator function.
std::string typeStr() const
Get the parameter type as string.
bool operator>=(const Parameter< T > &lhs, const S &rhs)
std::shared_ptr< void > ValueType
ParameterBase(Configurable &conf, EParameterType paramType, const std::string &na, const std::string &desc)
Constructor.
VectorParameter(const std::string &na, const std::string &desc)
Constructor.
const std::string & name() const
Get the property name.
void reset()
Reset the parameter value (only)
ParameterBase(EParameterType paramType, const std::string &na, const std::string &desc, const T &defVal)
Constructor.
bool isSet() const
Whether the parameter has been set.