Skip to content

Commit

Permalink
added new option type
Browse files Browse the repository at this point in the history
  • Loading branch information
namdre committed Nov 27, 2018
1 parent 9f84925 commit 9195bdf
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 0 deletions.
65 changes: 65 additions & 0 deletions src/utils/options/Option.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ Option::getIntVector() const {
throw InvalidArgument("This is not an int vector-option");
}

const FloatVector&
Option::getFloatVector() const {
throw InvalidArgument("This is not an float vector-option");
}

bool
Option::markSet() {
Expand Down Expand Up @@ -506,6 +510,67 @@ Option_IntVector::getValueString() const {
}


/* -------------------------------------------------------------------------
* Option_UFloatVector - methods
* ----------------------------------------------------------------------- */
Option_FloatVector::Option_FloatVector()
: Option() {
myTypeName = "FLOAT[]";
}


Option_FloatVector::Option_FloatVector(const FloatVector& value)
: Option(true), myValue(value) {
myTypeName = "FLOAT[]";
}


Option_FloatVector::Option_FloatVector(const Option_FloatVector& s)
: Option(s), myValue(s.myValue) {}


Option_FloatVector::~Option_FloatVector() {}


Option_FloatVector&
Option_FloatVector::operator=(const Option_FloatVector& s) {
Option::operator=(s);
myValue = s.myValue;
return (*this);
}


const FloatVector&
Option_FloatVector::getFloatVector() const {
return myValue;
}


bool
Option_FloatVector::set(const std::string& v) {
myValue.clear();
try {
if (v.find(';') != std::string::npos) {
WRITE_WARNING("Please note that using ';' as list separator is deprecated.\n From 1.0 onwards, only ',' will be accepted.");
}
StringTokenizer st(v, ";,", true);
while (st.hasNext()) {
myValue.push_back(StringUtils::toDouble(st.next()));
}
return markSet();
} catch (EmptyData&) {
throw ProcessError("Empty element occurred in " + v);
} catch (...) {
throw ProcessError("'" + v + "' is not a valid float vector.");
}
}


std::string
Option_FloatVector::getValueString() const {
return joinToString(myValue, ',');
}


/****************************************************************************/

83 changes: 83 additions & 0 deletions src/utils/options/Option.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
* @brief Definition of a vector of ints
*/
typedef std::vector<int> IntVector;
/**
* @typedef FloatVector
* @brief Definition of a vector of doubles
*/
typedef std::vector<double> FloatVector;


/* -------------------------------------------------------------------------
Expand Down Expand Up @@ -140,6 +145,16 @@ class Option {
*/
virtual const IntVector& getIntVector() const;

/** @brief Returns the stored float vector
*
* Option_FloatVector returns the stored float vector in this method's reimplementation.
* All other option classes do not override this method which throws an InvalidArgument-exception.
*
* @return Returns the stored float vector if being an instance of Option_FloatVector
* @exception InvalidArgument If the class is not an instance of Option_FloatVector
*/
virtual const FloatVector& getFloatVector() const;


/** @brief Stores the given value
*
Expand Down Expand Up @@ -702,6 +717,74 @@ class Option_IntVector : public Option {
};


/* -------------------------------------------------------------------------
* Option_FloatVector
* ----------------------------------------------------------------------- */
class Option_FloatVector : public Option {
public:
/** @brief Constructor for an option with no default value
*/
Option_FloatVector();


/** @brief Constructor for an option with a default value
*
* @param[in] value This option's default value
*/
Option_FloatVector(const FloatVector& value);


/** @brief Copy constructor */
Option_FloatVector(const Option_FloatVector& s);


/** @brief Destructor */
virtual ~Option_FloatVector();


/** @brief Assignment operator */
Option_FloatVector& operator=(const Option_FloatVector& s);


/** @brief Returns the stored float vector
* @see const FloatVector &Option::getFloatVector()
* @return Returns the stored float vector
*/
const FloatVector& getFloatVector() const;


/** @brief Stores the given value after parsing it into a vector of integers
*
* The value is converted into a vector of integers and stored in "myValue".
* Then, "markSet" is called in order to know that a value has been set.
*
* The method returns whether the value could be set (the return value from
* "markSet").
*
* If the string could not be converted into a vector of integers, an InvalidArgument
* is thrown.
*
* @see bool Option::set(std::string v)
* @return Whether the new value could be set
* @exception InvalidArgument If the value could not be converted into a vector of integers
*/
bool set(const std::string& v);


/** @brief Returns the string-representation of the value
*
* The stored value is encoded into a string and returned.
*
* @see std::string Option::getValueString()
* @return The stored value encoded into a string
*/
std::string getValueString() const;


private:
/** the value, valid only when the base-classes "myAmSet"-member is true */
FloatVector myValue;
};
#endif

/****************************************************************************/
Expand Down
7 changes: 7 additions & 0 deletions src/utils/options/OptionsCont.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,13 @@ OptionsCont::getIntVector(const std::string& name) const {
}


const FloatVector&
OptionsCont::getFloatVector(const std::string& name) const {
Option* o = getSecure(name);
return o->getFloatVector();
}


bool
OptionsCont::set(const std::string& name, const std::string& value) {
Option* o = getSecure(name);
Expand Down
12 changes: 12 additions & 0 deletions src/utils/options/OptionsCont.h
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,18 @@ class OptionsCont {
*/
const IntVector& getIntVector(const std::string& name) const;

/** @brief Returns the list of double-value of the named option (only for Option_FloatVector)
*
* This method returns the float-vector-value of an existing float-vector-option.
* If the named option does not exist or is not a float-vector-option, an
* InvalidArgument is thrown.
*
* @param[in] name The name of the option to return the float-vector-value of
* @return The float-vector-value of the named, existing float-vector-option
* @exception InvalidArgument If the option does not exist or is not a float-vector-option
*/
const FloatVector& getFloatVector(const std::string& name) const;


/** @brief Returns the list of string-vector-value of the named option (only for Option_String)
*
Expand Down

0 comments on commit 9195bdf

Please sign in to comment.