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

value cannot be moved in addAttr/setAttr (because needed in 2 places!)

parent b00cc2ba
No related branches found
No related tags found
1 merge request!16Unified interface for attributes
Pipeline #32397 failed
...@@ -67,6 +67,29 @@ class test_operator_binding(unittest.TestCase): ...@@ -67,6 +67,29 @@ class test_operator_binding(unittest.TestCase):
self.generic_operator.add_attr("l_str", ["ok"]) self.generic_operator.add_attr("l_str", ["ok"])
self.assertEqual(self.generic_operator.get_attr("l_str"), ["ok"]) self.assertEqual(self.generic_operator.get_attr("l_str"), ["ok"])
def test_dynamicattribute_binding(self):
# Check original C++ attributes are binded
attrs = aidge_core.test_DynamicAttributes_binding()
self.assertEqual(attrs.has_attr("a"), True)
self.assertEqual(attrs.get_attr("a"), 42)
self.assertEqual(attrs.has_attr("b"), True)
self.assertEqual(attrs.get_attr("b"), "test")
self.assertEqual(attrs.has_attr("c"), True)
self.assertEqual(attrs.get_attr("c"), [True, False, True])
self.assertEqual(attrs.get_attrs_name(), {"a", "b", "c"})
self.assertEqual(attrs.has_attr("d"), False)
# Add Python attributes
attrs.add_attr("d", 18.56)
self.assertEqual(attrs.get_attr("d"), 18.56)
self.assertEqual(attrs.has_attr("d"), True)
self.assertEqual(attrs.get_attrs_name(), {"a", "b", "c", "d"})
self.assertEqual(attrs.has_attr("e"), False)
# Check that added Python attribute is accessible in C++
# Return the value of an attribute named "d" of type float64 (double in C++)
self.assertEqual(aidge_core.test_DynamicAttributes_binding_check(attrs), 18.56)
def test_compute_output_dims(self): def test_compute_output_dims(self):
in_dims=[25, 25] in_dims=[25, 25]
input = aidge_core.Producer(in_dims, name="In") input = aidge_core.Producer(in_dims, name="In")
......
...@@ -84,16 +84,16 @@ public: ...@@ -84,16 +84,16 @@ public:
///\tparam T expected Attribute type ///\tparam T expected Attribute type
///\param name Attribute name ///\param name Attribute name
///\param value Attribute value ///\param value Attribute value
template<class T> void addAttr(const std::string& name, T&& value) template<class T> void addAttr(const std::string& name, const T& value)
{ {
const auto& res = mAttrs.emplace(std::make_pair(name, libany::any(std::forward<T>(value)))); const auto& res = mAttrs.emplace(std::make_pair(name, libany::any(value)));
assert(res.second && "attribute already exists"); assert(res.second && "attribute already exists");
#ifdef PYBIND #ifdef PYBIND
// We cannot handle Python object if the Python interpreter is not running // We cannot handle Python object if the Python interpreter is not running
if (Py_IsInitialized()) { if (Py_IsInitialized()) {
// Keep a copy of the attribute in py::object that is updated everytime // Keep a copy of the attribute in py::object that is updated everytime
mAttrsPy.emplace(std::make_pair(name, py::cast(std::forward<T>(value)))); mAttrsPy.emplace(std::make_pair(name, py::cast(value)));
} }
#endif #endif
} }
...@@ -102,19 +102,19 @@ public: ...@@ -102,19 +102,19 @@ public:
///\tparam T expected Attribute type ///\tparam T expected Attribute type
///\param name Attribute name ///\param name Attribute name
///\param value Attribute value ///\param value Attribute value
template<class T> void setAttr(const std::string& name, T&& value) template<class T> void setAttr(const std::string& name, const T& value)
{ {
auto res = mAttrs.emplace(std::make_pair(name, libany::any(std::forward<T>(value)))); auto res = mAttrs.emplace(std::make_pair(name, libany::any(value)));
if (!res.second) if (!res.second)
res.first->second = std::move(libany::any(std::forward<T>(value))); res.first->second = libany::any(value);
#ifdef PYBIND #ifdef PYBIND
// We cannot handle Python object if the Python interpreter is not running // We cannot handle Python object if the Python interpreter is not running
if (Py_IsInitialized()) { if (Py_IsInitialized()) {
// Keep a copy of the attribute in py::object that is updated everytime // Keep a copy of the attribute in py::object that is updated everytime
auto resPy = mAttrsPy.emplace(std::make_pair(name, py::cast(std::forward<T>(value)))); auto resPy = mAttrsPy.emplace(std::make_pair(name, py::cast(value)));
if (!resPy.second) if (!resPy.second)
resPy.first->second = std::move(py::cast(std::forward<T>(value))); resPy.first->second = std::move(py::cast(value));
} }
#endif #endif
} }
......
...@@ -4,6 +4,18 @@ ...@@ -4,6 +4,18 @@
namespace py = pybind11; namespace py = pybind11;
namespace Aidge { namespace Aidge {
DynamicAttributes test_DynamicAttributes_binding() {
DynamicAttributes attrs;
attrs.addAttr<int>("a", 42);
attrs.addAttr<std::string>("b", "test");
attrs.addAttr<std::vector<bool>>("c", {true, false, true});
return attrs;
}
double test_DynamicAttributes_binding_check(DynamicAttributes& attrs) {
return attrs.getAttr<double>("d");
}
void init_Attributes(py::module& m){ void init_Attributes(py::module& m){
py::class_<Attributes, std::shared_ptr<Attributes>>(m, "Attributes") py::class_<Attributes, std::shared_ptr<Attributes>>(m, "Attributes")
.def("has_attr", &Attributes::hasAttr, py::arg("name")) .def("has_attr", &Attributes::hasAttr, py::arg("name"))
...@@ -15,6 +27,10 @@ void init_Attributes(py::module& m){ ...@@ -15,6 +27,10 @@ void init_Attributes(py::module& m){
.def("add_attr", &DynamicAttributes::addAttrPy, py::arg("name"), py::arg("value")) .def("add_attr", &DynamicAttributes::addAttrPy, py::arg("name"), py::arg("value"))
.def("set_attr", &DynamicAttributes::setAttrPy, py::arg("name"), py::arg("value")) .def("set_attr", &DynamicAttributes::setAttrPy, py::arg("name"), py::arg("value"))
.def("del_attr", &DynamicAttributes::delAttr, py::arg("name")); .def("del_attr", &DynamicAttributes::delAttr, py::arg("name"));
m.def("test_DynamicAttributes_binding", &test_DynamicAttributes_binding);
m.def("test_DynamicAttributes_binding_check", &test_DynamicAttributes_binding_check, py::arg("attrs"));
} }
} }
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