Skip to content

Commit

Permalink
fixup uptions
Browse files Browse the repository at this point in the history
  • Loading branch information
mwestphal committed Jul 14, 2024
1 parent 0bc6f00 commit 0cbbfbc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 40 deletions.
21 changes: 10 additions & 11 deletions library/public/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,45 +178,44 @@ class F3D_EXPORT options
option_variant_t getVariant(const std::string& name);
void setString(const std::string& name, std::string value);
std::string getString(const std::string& name);
std::vector<std::string> getOptionStrings();

///@{ @name Setters
/**
* Setters for all supported types.
*/
options& set(const std::string& name, bool value);
/* options& set(const std::string& name, bool value);
options& set(const std::string& name, int value);
options& set(const std::string& name, double value);
options& set(const std::string& name, const std::string& value);
options& set(const std::string& name, const char* value);
options& set(const std::string& name, const std::vector<int>& values);
options& set(const std::string& name, const std::vector<double>& values);
options& set(const std::string& name, std::initializer_list<int> values);
options& set(const std::string& name, std::initializer_list<double> values);
options& set(const std::string& name, std::initializer_list<double> values);*/
///@}

///@{ @name Reference Getters
/**
* Copy the option value into the provided reference, for all supported types.
*/
void get(const std::string& name, bool& value) const;
/* void get(const std::string& name, bool& value) const;
void get(const std::string& name, int& value) const;
void get(const std::string& name, double& value) const;
void get(const std::string& name, std::string& value) const;
void get(const std::string& name, std::vector<int>& value) const;
void get(const std::string& name, std::vector<double>& value) const;
void get(const std::string& name, std::vector<double>& value) const;*/
///@}

///@{ @name Explicit Copy Getters
/**
* Explicit getters for all supported types.
*/
bool getAsBool(const std::string& name) const;
/* bool getAsBool(const std::string& name) const;
int getAsInt(const std::string& name) const;
double getAsDouble(const std::string& name) const;
std::string getAsString(const std::string& name) const;
std::vector<int> getAsIntVector(const std::string& name) const;
std::vector<double> getAsDoubleVector(const std::string& name) const;
std::vector<double> getAsDoubleVector(const std::string& name) const;*/
///@}

///@{ @name Explicit Reference Getters
Expand All @@ -226,19 +225,19 @@ class F3D_EXPORT options
* Throw an options::incompatible_exception if the type is not compatible with the option.
* Throw an options::inexistent_exception if option does not exist.
*/
bool& getAsBoolRef(const std::string& name);
/* bool& getAsBoolRef(const std::string& name);
int& getAsIntRef(const std::string& name);
double& getAsDoubleRef(const std::string& name);
std::string& getAsStringRef(const std::string& name);
std::vector<int>& getAsIntVectorRef(const std::string& name);
std::vector<double>& getAsDoubleVectorRef(const std::string& name);
std::vector<double>& getAsDoubleVectorRef(const std::string& name);*/
///@}

/**
* A boolean option specific method to toggle it.
*/
options& toggle(const std::string& name);
options& toggleNew(const std::string& name);
/* options& toggle(const std::string& name);
options& toggleNew(const std::string& name);*/

/**
* Compare an option between this and a provided other.
Expand Down
57 changes: 28 additions & 29 deletions library/src/options.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace f3d
class options::internals
{
public:
using OptionVariant =
/* using OptionVariant =
std::variant<bool, int, double, std::string, std::vector<int>, std::vector<double>>;
template<typename T, typename U>
Expand Down Expand Up @@ -130,7 +130,7 @@ class options::internals
{
// TODO do we actually need this call ?
this->setVariant(name, value);
}
}*/

void setVariant(const std::string& name, option_variant_t value)
{
Expand Down Expand Up @@ -690,7 +690,7 @@ class options::internals
return var;
}

std::vector<std::string> getOptionStrings()
std::vector<std::string> getNames()
{
std::vector<std::string> vec{
"scene.up_direction",
Expand Down Expand Up @@ -835,9 +835,11 @@ class options::internals
if (std::holds_alternative<bool>(var))
{
// TODO implement proper parsing
bool b;
std::istringstream(value) >> b;
var = b;
bool b1;
bool b2;
std::istringstream(value) >> b1;
std::istringstream(value) >> std::boolalpha >> b2;
var = b1 || b2;
}
if (std::holds_alternative<int>(var))
{
Expand Down Expand Up @@ -916,7 +918,7 @@ class options::internals
return str;
}

std::map<std::string, OptionVariant> Options;
// std::map<std::string, OptionVariant> Options;

options_struct OptionStruct;
};
Expand All @@ -926,7 +928,7 @@ options::options()
: Internals(new options::internals)
{
detail::init::initialize();

/*
// Scene
this->Internals->initNew("scene.animation.autoplay", false); // bool
this->Internals->initNew("scene.animation.index", 0); // int
Expand Down Expand Up @@ -1024,7 +1026,7 @@ options::options()
// Interactor
this->Internals->init("interactor.axis", false); // bool
this->Internals->init("interactor.trackball", false); // bool
this->Internals->init("interactor.invert-zoom", false); // bool
this->Internals->init("interactor.invert-zoom", false); // bool*/
};

//----------------------------------------------------------------------------
Expand All @@ -1037,14 +1039,14 @@ options::~options()
options::options(const options& opt)
: Internals(new options::internals)
{
this->Internals->Options = opt.Internals->Options;
// this->Internals->Options = opt.Internals->Options;
this->Internals->OptionStruct = opt.Internals->OptionStruct;
}

//----------------------------------------------------------------------------
options& options::operator=(const options& opt) noexcept
{
this->Internals->Options = opt.Internals->Options;
// this->Internals->Options = opt.Internals->Options;
this->Internals->OptionStruct = opt.Internals->OptionStruct;
return *this;
}
Expand Down Expand Up @@ -1091,7 +1093,7 @@ std::string options::getString(const std::string& name)
{
return this->Internals->getString(name);
}

/*
//----------------------------------------------------------------------------
options& options::set(const std::string& name, bool value)
{
Expand Down Expand Up @@ -1276,16 +1278,22 @@ options& options::toggleNew(const std::string& name)
this->Internals->setVariant(name, !std::get<bool>(this->Internals->getVariant(name)));
return *this;
}

*/
//----------------------------------------------------------------------------
bool options::isSame(const options& other, const std::string& name) const
{
try
{
return this->Internals->Options.at(name) == other.Internals->Options.at(name);
std::cout<<name<<" : "<<std::to_string(this->Internals->getVariant(name) == other.Internals->getVariant(name))<<std::endl;
if (name == "ui.filename")
{
std::cout<<(std::get<bool>(this->Internals->getVariant(name)) ? "TRUE" : "FALSE") <<std::endl;
}
return this->Internals->getVariant(name) == other.Internals->getVariant(name);
}
catch (const std::out_of_range&)
{
// TODO error mgt
std::string error = "Options " + name + " does not exist";
log::error(error);
throw options::inexistent_exception(error + "\n");
Expand All @@ -1297,10 +1305,11 @@ options& options::copy(const options& from, const std::string& name)
{
try
{
this->Internals->Options.at(name) = from.Internals->Options.at(name);
this->Internals->setVariant(name, from.Internals->getVariant(name));
}
catch (const std::out_of_range&)
{
// TODO error mgt
std::string error = "Options " + name + " does not exist";
log::error(error);
throw options::inexistent_exception(error + "\n");
Expand All @@ -1311,26 +1320,21 @@ options& options::copy(const options& from, const std::string& name)
//----------------------------------------------------------------------------
std::vector<std::string> options::getNames()
{
std::vector<std::string> names;
names.reserve(this->Internals->Options.size());
for (const auto& [name, value] : this->Internals->Options)
{
names.emplace_back(name);
}
return names;
return this->Internals->getNames();
}

//----------------------------------------------------------------------------
std::pair<std::string, unsigned int> options::getClosestOption(const std::string& option) const
{
if (this->Internals->Options.find(option) != this->Internals->Options.end())
std::vector<std::string> names = this->Internals->getNames();
if (std::find(names.begin(), names.end(), option) != names.end())
{
return { option, 0 };
}

std::pair<std::string, int> ret = { "", std::numeric_limits<int>::max() };

for (const auto& [name, value] : this->Internals->Options)
for (const auto& name : names)
{
int distance = utils::textDistance(name, option);
if (distance < ret.second)
Expand All @@ -1342,11 +1346,6 @@ std::pair<std::string, unsigned int> options::getClosestOption(const std::string
return ret;
}

std::vector<std::string> options::getOptionStrings()
{
return this->Internals->getOptionStrings();
}

//----------------------------------------------------------------------------
options::incompatible_exception::incompatible_exception(const std::string& what)
: exception(what)
Expand Down

0 comments on commit 0cbbfbc

Please sign in to comment.