Skip to content

Commit

Permalink
options: Add a static parse API (#1581)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael MIGLIORE <[email protected]>
  • Loading branch information
mwestphal and Meakk authored Aug 23, 2024
1 parent bb18f04 commit c77fdcd
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 5 deletions.
20 changes: 16 additions & 4 deletions library/public/options.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,20 @@ public:
option_variant_t get(const std::string& name) const;
#endif

///@{
/**
* Set/Get an option as a string based on its name
* The setter use specific parsing, see the related doc
* Set an option as a string based on its name
* Use dedicated parsing code, see the related doc
* TODO add parsing documentation
* Throw an options::inexistent_exception if option does not exist.
* Throw an options::parsing_exception if parsing failed.
*/
options& setAsString(const std::string& name, const std::string& str);

/**
* Get an option as a string based on its name
* Throw an options::inexistent_exception if option does not exist.
*/
std::string getAsString(const std::string& name) const;
///@}

/**
* A boolean option specific method to toggle it.
Expand Down Expand Up @@ -102,6 +106,14 @@ public:
*/
std::pair<std::string, unsigned int> getClosestOption(const std::string& option) const;

/**
* Templated parsing method used internally to parse strings.
* Implemented only for: bool, int, double, ratio_t, string and double vector.
* Throw an options::parsing_exception if parsing failed.
*/
template<typename T>
static T parse(const std::string& str);

/**
* An exception that can be thrown by the options
* when parsing of a string into an option value fails
Expand Down
2 changes: 1 addition & 1 deletion library/public/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ using angle_deg_t = double;
/**
* Describe a ratio.
*/
class ratio_t
class F3D_EXPORT ratio_t
{
public:
ratio_t() = default;
Expand Down
16 changes: 16 additions & 0 deletions library/src/options.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,22 @@ std::pair<std::string, unsigned int> options::getClosestOption(const std::string
return ret;
}

//----------------------------------------------------------------------------
template<typename T>
T options::parse(const std::string& str)
{
return options_tools::parse<T>(str);
}

//----------------------------------------------------------------------------
#define F3D_DECL_TYPE(TYPE) template F3D_EXPORT TYPE options::parse<TYPE>(const std::string& str)
F3D_DECL_TYPE(bool);
F3D_DECL_TYPE(int);
F3D_DECL_TYPE(double);
F3D_DECL_TYPE(f3d::ratio_t);
F3D_DECL_TYPE(std::string);
F3D_DECL_TYPE(std::vector<double>);

//----------------------------------------------------------------------------
options::parsing_exception::parsing_exception(const std::string& what)
: exception(what)
Expand Down
33 changes: 33 additions & 0 deletions library/testing/TestSDKOptions.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -291,5 +291,38 @@ int TestSDKOptions(int argc, char* argv[])
std::cout << "Expected exception: " << ex.what() << std::endl;
}

// Test parse
if (f3d::options::parse<bool>(std::string("true")) != true)
{
std::cerr << "Options parse method not behaving as expected with bool." << std::endl;
return EXIT_FAILURE;
}
if (f3d::options::parse<int>(std::string("37")) != 37)
{
std::cerr << "Options parse method not behaving as expected with int." << std::endl;
return EXIT_FAILURE;
}
if (f3d::options::parse<double>(std::string("37.2")) != 37.2)
{
std::cerr << "Options parse method not behaving as expected with double." << std::endl;
return EXIT_FAILURE;
}
if (f3d::options::parse<f3d::ratio_t>(std::string("0.31")) != 0.31)
{
std::cerr << "Options parse method not behaving as expected with ratio_t." << std::endl;
return EXIT_FAILURE;
}
if (f3d::options::parse<std::string>(std::string("foobar")) != "foobar")
{
std::cerr << "Options parse method not behaving as expected with string." << std::endl;
return EXIT_FAILURE;
}
if (f3d::options::parse<std::vector<double>>(std::string("0.1, 0.2, 0.3")) !=
std::vector<double>({ 0.1, 0.2, 0.3 }))
{
std::cerr << "Options parse method not behaving as expected with double vector." << std::endl;
return EXIT_FAILURE;
}

return EXIT_SUCCESS;
}

0 comments on commit c77fdcd

Please sign in to comment.