Skip to content

Commit 5f8dfc4

Browse files
authored
prevent misinterpretation ofParameter<bool> ctor when single arg is string (#8132)
1 parent 034204f commit 5f8dfc4

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

libs/openFrameworks/types/ofParameter.h

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
#include "ofColor.h"
1010
#include "ofLog.h"
11-
#include "ofRectangle.h"
1211
#include "ofMathConstants.h"
12+
#include "ofRectangle.h"
1313
#include "ofUtils.h" // ofToString
1414

1515
template <typename ParameterType>
@@ -498,10 +498,46 @@ typename std::enable_if<!of::priv::has_loading_support<ParameterType>::value, Pa
498498
template <typename ParameterType>
499499
class ofParameter : public ofAbstractParameter {
500500
public:
501+
502+
/// \brief constructs a default ofParameter of type ParameterType
503+
/// \tparam ParameterType the type of the Value held by the ofParameter
501504
ofParameter();
505+
506+
/// \brief constructs an ofParameter of type ParameterType as an alias of the Value of another ofParameter
507+
/// \tparam ParameterType the type of the value held by the ofParameter
508+
/// \param v the ofParameter to link to it's value
502509
ofParameter(const ofParameter<ParameterType> & v);
510+
511+
/// \brief constructs an ofParameter of type ParameterType initialized to value of same-type v
512+
/// \tparam ParameterType the type of the value held by the ofParameter
513+
/// \param v the value to initialize to
503514
ofParameter(const ParameterType & v);
515+
516+
/// \brief constructs an ofParameter of type ParameterType initialized to value of v
517+
/// where v is convertible to ParameterType, with an exception for bool which can cause
518+
/// unexpected behavious (as string and char arrays are convertible to bool)
519+
/// \tparam ParameterType the type of the value held by the ofParameter
520+
/// \tparam Arg a type convertible to ParameterType
521+
/// \param v the value to initialize to
522+
template <
523+
typename Arg,
524+
typename = std::enable_if_t<(std::is_convertible_v<Arg, ParameterType> and
525+
!((std::is_same_v<ParameterType, bool>)and!(std::is_arithmetic_v<Arg>)))>>
526+
ofParameter(const Arg & v);
527+
528+
/// \brief constructs a named ofParameter of type ParameterType initialized to value of v
529+
/// \tparam ParameterType the type of the value held by the ofParameter
530+
/// \param name name of the parameter
531+
/// \param v the value to initialize to
504532
ofParameter(const std::string & name, const ParameterType & v);
533+
534+
/// \brief constructs a named ofParameter of type ParameterType initialized to value of v,
535+
/// with non-enforced constraints on the range of possible values
536+
/// \tparam ParameterType the type of the value held by the ofParameter
537+
/// \param name name of the parameter
538+
/// \param v the value to initialize to
539+
/// \param min the minimum value to be held
540+
/// \param max the maximum value to be held
505541
ofParameter(const std::string & name, const ParameterType & v, const ParameterType & min, const ParameterType & max);
506542

507543
const ParameterType & get() const;
@@ -677,7 +713,8 @@ ofParameter<ParameterType>::ofParameter(const ofParameter<ParameterType> & v)
677713
, setMethod(std::bind(&ofParameter<ParameterType>::eventsSetValue, this, std::placeholders::_1)) { }
678714

679715
template <typename ParameterType>
680-
ofParameter<ParameterType>::ofParameter(const ParameterType & v)
716+
template <typename Arg, typename>
717+
ofParameter<ParameterType>::ofParameter(const Arg & v)
681718
: obj(std::make_shared<Value>(v))
682719
, setMethod(std::bind(&ofParameter<ParameterType>::eventsSetValue, this, std::placeholders::_1)) { }
683720

0 commit comments

Comments
 (0)