Skip to content
Snippets Groups Projects
Commit a71cc8b1 authored by Olivier BICHLER's avatar Olivier BICHLER
Browse files

Improved DynamicParameters design

parent 33e842fb
No related branches found
No related tags found
No related merge requests found
......@@ -69,12 +69,12 @@ public:
*/
template<class T> T& getParameter(const std::string& name)
{
return any_cast_ref<T>(mBuffer[mParams.at(name)]);
return any_cast_ref<T>(mParams.at(name));
}
template<class T> const T& getParameter(const std::string& name) const
{
return any_cast_ref<T>(mBuffer[mParams.at(name)]);
return any_cast_ref<T>(mParams.at(name));
}
///\brief Add a parameter value, identified by its name
......@@ -86,8 +86,7 @@ public:
/// internal buffer in a new location (previous value is still in memory at its previous location)
template<class T> void addParameter(const std::string& name, T&& value)
{
mParams[name] = mBuffer.size(); // Copy pointer offset
mBuffer.push_back(_any(std::forward<T>(value)));
mParams.emplace(std::make_pair(name, _any(std::forward<T>(value))));
}
//////////////////////////////////////
......@@ -98,7 +97,7 @@ public:
}
std::string getParameterType(const std::string& name) const override final {
return mBuffer[mParams.at(name)].type().name();
return mParams.at(name).type().name();
}
std::vector<std::string> getParametersName() const override final {
......@@ -109,37 +108,38 @@ public:
}
#ifdef PYBIND
/**
* @detail See https://github.com/pybind/pybind11/issues/1590 as to why a
* generic type caster for std::any is not feasable.
*/
py::object getPy(const std::string& name) const {
py::object res = py::none();
const std::string paramType = getParameterType(name);
if(paramType == typeid(int).name())
const auto& paramType = mParams.at(name).type();
if(paramType == typeid(int))
res = py::cast(getParameter<int>(name));
else if(paramType == typeid(float).name())
else if(paramType == typeid(float))
res = py::cast(getParameter<float>(name));
else if(paramType == typeid(bool).name())
else if(paramType == typeid(bool))
res = py::cast(getParameter<bool>(name));
else if(paramType == typeid(std::string).name())
else if(paramType == typeid(std::string))
res = py::cast(getParameter<std::string>(name));
else if(paramType == typeid(std::vector<bool>).name())
else if(paramType == typeid(std::vector<bool>))
res = py::cast(getParameter<std::vector<bool>>(name));
else if(paramType == typeid(std::vector<int>).name())
else if(paramType == typeid(std::vector<int>))
res = py::cast(getParameter<std::vector<int>>(name));
else if(paramType == typeid(std::vector<float>).name())
else if(paramType == typeid(std::vector<float>))
res = py::cast(getParameter<std::vector<float>>(name));
else if(paramType == typeid(std::vector<std::string>).name())
else if(paramType == typeid(std::vector<std::string>))
res = py::cast(getParameter<std::vector<std::string>>(name));
else {
throw py::key_error("Failed to convert parameter type " + name + ", this issue may come from typeid function which gave an unknown key : [" + paramType + "]. Please open an issue asking to add the support for this key.");
throw py::key_error("Failed to convert parameter type " + name + ", this issue may come from typeid function which gave an unknown key : [" + paramType.name() + "]. Please open an issue asking to add the support for this key.");
}
return res;
};
#endif
private:
std::map<std::string, std::size_t> mParams; // { Param name : offset }
///\brief All raw pointers to parameters values concatenated. Use custom any class compatible with C++14.
std::vector<_any> mBuffer;
std::map<std::string, _any> mParams;
};
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment