23 #include <visiontransfer/parameter.h> 28 using namespace internal;
31 : uid(uid), name(uid), governorType(GOVERNOR_NONE), invokeGovernorOnInit(false), accessForConfig(ACCESS_NONE), accessForApi(ACCESS_NONE), interactionHint(INTERACTION_ACTIVE), isModified(false) {
35 std::string result = governorString;
36 std::set<char> subst{
'P',
'O',
'N',
'E',
'D'};
37 std::ostringstream ss;
42 int foundRightmost = -1;
44 for (
auto ch: subst) {
45 std::string lookfor =
"%";
47 foundAt = result.rfind(lookfor, where);
48 if (foundAt > foundRightmost) {
49 foundRightmost = foundAt;
53 if (foundRightmost >= 0) {
57 ss <<
"\"" <<
getUid() <<
"\"";
61 ss <<
"\"" << getCurrent<std::string>() <<
"\"";
65 if (first) first =
false;
81 ss << newVal.getTensorDimension();
83 auto sh = newVal.getTensorShape();
91 if (newVal.isScalar()) {
92 ss <<
"\"" << newVal.getValue<std::string>() <<
"\"";
96 if (first) first =
false;
103 result.replace(foundRightmost, 2, ss.str());
105 if (where == 0)
break;
106 where = (foundRightmost > 0) ? foundRightmost-1 : 0;
116 currentValue.setType(this->type);
118 switch (this->type) {
119 case ParameterValue::ParameterType::TYPE_INT:
120 currentValue.setValue(from.
getCurrent<
int>());
break;
121 case ParameterValue::ParameterType::TYPE_DOUBLE:
122 currentValue.setValue(from.
getCurrent<
double>());
break;
123 case ParameterValue::ParameterType::TYPE_STRING:
124 case ParameterValue::ParameterType::TYPE_SAFESTRING:
125 case ParameterValue::ParameterType::TYPE_COMMAND:
126 currentValue.setValue(from.
getCurrent<std::string>());
break;
128 case ParameterValue::ParameterType::TYPE_BOOL:
129 currentValue.setValue(from.
getCurrent<
bool>());
break;
131 case ParameterValue::ParameterType::TYPE_TENSOR:
133 throw std::runtime_error(
"Cannot assign tensors with unequal shape");
136 case ParameterValue::ParameterType::TYPE_UNDEFINED:
137 throw std::runtime_error(
"Cannot assign a value to an undefined parameter");
140 throw std::runtime_error(std::string(
"Unchecked invalid value ") + from.
getCurrent<std::string>() +
" for parameter " +
getUid());
148 throw std::runtime_error(std::string(
"Cannot set current value from default - no default value set for ") +
getUid());
150 switch (this->type) {
151 case ParameterValue::ParameterType::TYPE_INT:
152 currentValue.setValue(getDefault<int>());
break;
153 case ParameterValue::ParameterType::TYPE_DOUBLE:
154 currentValue.setValue(getDefault<double>());
break;
155 case ParameterValue::ParameterType::TYPE_STRING:
156 case ParameterValue::ParameterType::TYPE_SAFESTRING:
157 currentValue.setValue(getDefault<std::string>());
break;
159 case ParameterValue::ParameterType::TYPE_BOOL:
160 currentValue.setValue(getDefault<bool>());
break;
162 case ParameterValue::ParameterType::TYPE_TENSOR:
163 if (
hasCurrent() && (currentValue.getTensorNumElements() != defaultValue.getTensorNumElements())) {
164 throw std::runtime_error(std::string(
"Mismatching current and default tensor sizes for ") +
getUid());
168 case ParameterValue::ParameterType::TYPE_COMMAND:
171 case ParameterValue::ParameterType::TYPE_UNDEFINED:
172 throw std::runtime_error(
"Cannot assign a value to an undefined parameter");
182 if (
hasIncrement() && ((this->type==ParameterValue::TYPE_INT) || (this->type==ParameterValue::TYPE_DOUBLE))) {
184 double inc = getIncrement<double>();
186 double min = getMin<double>();
187 return (
int) (min + inc * ((int) (val-min)/inc));
189 return (
int) (inc * ((int) val/inc));
197 if (
hasIncrement() && ((this->type==ParameterValue::TYPE_INT) || (this->type==ParameterValue::TYPE_DOUBLE))) {
199 double inc = getIncrement<double>();
201 double min = getMin<double>();
202 return min + inc * ((int) (val-min)/inc);
204 return inc * ((int) val/inc);
212 if (
hasIncrement() && ((this->type==ParameterValue::TYPE_INT) || (this->type==ParameterValue::TYPE_DOUBLE))) {
214 double inc = getIncrement<double>();
216 double min = getMin<double>();
233 throw std::runtime_error(
"Tried getTensorDataReference(), but no value set and no default defined");
245 throw std::runtime_error(
"Tried getTensorData(), but no value set and no default defined");
254 throw std::runtime_error(
"Tried getTensorDefaultDataReference(), but no value set and no default defined");
262 throw std::runtime_error(
"Tried getTensorDefaultData(), but no default defined");
std::string interpolateCommandLine(const ParameterValue &newVal)
bool isValidNewValue(T t) const
unsigned int getTensorDimension() const
bool hasIncrement() const
Parameter & setCurrentFromDefault()
static double anyToDouble(T val)
Converts any type to a double.
std::vector< double > & getTensorDefaultDataReference()
Parameter & setCurrentFrom(const Parameter &from)
std::string getUid() const
std::vector< double > & getTensorDataReference()
std::vector< double > getTensorDefaultData() const
std::vector< double > getTensorData() const
Return a copy of the internal tensor data.
std::vector< unsigned int > getTensorShape() const
static std::string anyToString(T val)
Converts any type to a string.
Parameter & setTensorData(const std::vector< double > &data)
std::vector< double > & getTensorDataReference()
Return a reference to the internal tensor data (caution)
std::vector< double > getTensorData() const