Skip to content
Snippets Groups Projects
Commit f28a09ed authored by Cyril Moineau's avatar Cyril Moineau
Browse files

[PyBind] Add support for setAttrPy for Dynamic and Static Attributes.

parent a5fb8430
No related branches found
No related tags found
2 merge requests!105version 0.2.0,!73Quality of life
Pipeline #37457 passed
...@@ -69,6 +69,11 @@ public: ...@@ -69,6 +69,11 @@ public:
* be agnostic from its return type. * be agnostic from its return type.
*/ */
virtual py::object getAttrPy(const std::string& name) const = 0; virtual py::object getAttrPy(const std::string& name) const = 0;
/* Bindable set function, does not recquire any templating.
* This is thanks to py::object which allow the function to
* be agnostic from ``value`` type.
*/
virtual void setAttrPy(const std::string& name, py::object&& value) = 0;
#endif #endif
virtual ~Attributes() {} virtual ~Attributes() {}
}; };
......
...@@ -135,7 +135,7 @@ public: ...@@ -135,7 +135,7 @@ public:
assert(res.second && "attribute already exists"); assert(res.second && "attribute already exists");
} }
void setAttrPy(const std::string& name, py::object&& value) void setAttrPy(const std::string& name, py::object&& value) override final
{ {
auto resPy = mAttrsPy.emplace(std::make_pair(name, value)); auto resPy = mAttrsPy.emplace(std::make_pair(name, value));
if (!resPy.second) if (!resPy.second)
...@@ -204,7 +204,7 @@ private: ...@@ -204,7 +204,7 @@ private:
// Stores C++ attributes (copy) and Python-only attributes // Stores C++ attributes (copy) and Python-only attributes
// Code should be compiled with -fvisibility=hidden // Code should be compiled with -fvisibility=hidden
// See https://pybind11.readthedocs.io/en/stable/faq.html: // See https://pybind11.readthedocs.io/en/stable/faq.html:
// “‘SomeClass’ declared with greater visibility than the type of its // “‘SomeClass’ declared with greater visibility than the type of its
// field ‘SomeClass::member’ [-Wattributes]” // field ‘SomeClass::member’ [-Wattributes]”
// This map will only be populated if Python interpreter is running // This map will only be populated if Python interpreter is running
std::map<std::string, py::object> mAttrsPy; std::map<std::string, py::object> mAttrsPy;
......
...@@ -212,7 +212,22 @@ public: ...@@ -212,7 +212,22 @@ public:
} }
AIDGE_THROW_OR_ABORT(py::value_error, "attribute \"%s\" not found", name.c_str()); AIDGE_THROW_OR_ABORT(py::value_error, "attribute \"%s\" not found", name.c_str());
}; }
void setAttrPy(const std::string& name, py::object&& value) override final{
for (std::size_t i = 0; i < size(EnumStrings<ATTRS_ENUM>::data); ++i) {
if (name == EnumStrings<ATTRS_ENUM>::data[i]) {
// Cannot update attribute using reference has it would require templating
// Use a dirty
auto tmpAttr = py::cast(mAttrs);
py::detail::accessor_policies::tuple_item::set(tmpAttr, static_cast<py::size_t>(i), value);
mAttrs = py::cast<std::tuple<T...>>(tmpAttr);
return;
}
}
AIDGE_THROW_OR_ABORT(py::value_error, "attribute \"%s\" not found", name.c_str());
}
#endif #endif
private: private:
......
...@@ -21,7 +21,8 @@ void init_Attributes(py::module& m){ ...@@ -21,7 +21,8 @@ void init_Attributes(py::module& m){
.def("has_attr", &Attributes::hasAttr, py::arg("name")) .def("has_attr", &Attributes::hasAttr, py::arg("name"))
.def("get_attr_type", &Attributes::getAttrType, py::arg("name")) .def("get_attr_type", &Attributes::getAttrType, py::arg("name"))
.def("get_attrs_name", &Attributes::getAttrsName) .def("get_attrs_name", &Attributes::getAttrsName)
.def("get_attr", &Attributes::getAttrPy, py::arg("name")); .def("get_attr", &Attributes::getAttrPy, py::arg("name"))
.def("set_attr", &Attributes::setAttrPy, py::arg("name"), py::arg("value"));
py::class_<DynamicAttributes, std::shared_ptr<DynamicAttributes>, Attributes>(m, "DynamicAttributes") py::class_<DynamicAttributes, std::shared_ptr<DynamicAttributes>, Attributes>(m, "DynamicAttributes")
.def("add_attr", &DynamicAttributes::addAttrPy, py::arg("name"), py::arg("value")) .def("add_attr", &DynamicAttributes::addAttrPy, py::arg("name"), py::arg("value"))
......
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