Skip to content

Commit

Permalink
Updating the auto-generator to remove the use of the custom values co…
Browse files Browse the repository at this point in the history
…nstructor in python
  • Loading branch information
whaeck committed Nov 3, 2021
1 parent 9891c0a commit a25a0da
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 30 deletions.
56 changes: 29 additions & 27 deletions autogen/json2class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2159,24 +2159,21 @@ void filePythonClass(const InfoSpecs &specs, const PerClass &per)
{ "quad" , { "long double" , "quads" } }
};

std::string dataType = "unknownType";
std::string dataName = "data";
std::vector< std::pair< std::string, std::string > > dataTypesNames;
if (per.isData) {
// try to find per.dataType in the map
auto it = map.find(per.dataType);
if (it == map.end()) {
// look in metadata for valueType; if found,
// try to find its default value in the map
for (auto &m : per.metadata) {
if (m.name == "valueType") {
it = map.find(m.defaultValue);
break;
}
}
}
if (it != map.end()) {
dataType = it->second.first;
dataName = it->second.second;
// this is a node with a fixed data type
dataTypesNames.emplace_back( it->second.first, it->second.second );
}
else {
// this is a node with a runtime data type: select types to expose
std::array< std::string, 3 > types = { "int", "double", "string" };
for ( const auto& type : types ) {
it = map.find( type );
dataTypesNames.emplace_back( it->second.first, it->second.second );
}
}
}

Expand Down Expand Up @@ -2227,11 +2224,11 @@ void filePythonClass(const InfoSpecs &specs, const PerClass &per)
out();
out(1,"// wrap the component");
out(1,"component");
out(2,".def(");

// python::init<...>
// python::init<...> for attributes and children
out(2,".def(");
out(3,"python::init<");
int count = 0, total = per.nfields() + per.isData;
int count = 0, total = per.nfields();
for (auto &m : per.metadata)
out(4,"const @ &@",
m.isDefaulted ? "std::optional<" + m.type + ">" : m.typeFull,
Expand All @@ -2240,11 +2237,7 @@ void filePythonClass(const InfoSpecs &specs, const PerClass &per)
out(4,"const @ &@", c.typeFull, ++count < total ? "," : "");
for (auto &v : per.variants)
out(4,"const @ &@", v.typeFull, ++count < total ? "," : "");
if (per.isData)
out(4,"const std::vector<@> &", dataType);
out(3,">(),");

// python::arg...
for (auto &m : per.metadata)
out(3,"python::arg(\"@\")@,",
namePython(m.name),
Expand All @@ -2254,11 +2247,20 @@ void filePythonClass(const InfoSpecs &specs, const PerClass &per)
c.isOptional ? " = std::nullopt" : "");
for (auto &v : per.variants)
out(3,"python::arg(\"@\"),", namePython(v.name));
if (per.isData)
out(3,"python::arg(\"values\"),");
out(3,"Component::documentation(\"constructor\").data()");
out(2,")");

// python::init<...> for each data type and name pair
for ( const auto& dataTypeName : dataTypesNames ) {
out(2,".def(");
out(3,"python::init<");
out(4,"const std::vector<@> &", dataTypeName.first);
out(3,">(),");
out(3,"python::arg(\"@\"),", dataTypeName.second);
out(3,"Component::documentation(\"constructor\").data()");
out(2,")");
}

// .def_property_readonly...
for (auto &m : per.metadata) {
const auto pyname = namePython(m.name);
Expand Down Expand Up @@ -2300,11 +2302,11 @@ void filePythonClass(const InfoSpecs &specs, const PerClass &per)
out(2,")");
}

if (per.isData) {
for ( const auto& dataTypeName : dataTypesNames ) {
out(2,".def_property_readonly(");
out(3,"\"@\",", dataName);
out(3,"[] (const Component &self) { return self.@(); },", dataName);
out(3,"Component::documentation(\"@\").data()", dataName);
out(3,"\"@\",", dataTypeName.second);
out(3,"[] (const Component &self) { return self.@(); },", dataTypeName.second);
out(3,"Component::documentation(\"@\").data()", dataTypeName.second);
out(2,")");
}

Expand Down
6 changes: 3 additions & 3 deletions python/src/v1.9/containers/Values.python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,21 @@ void wrapValues(python::module &module)
)
.def(
python::init<
const std::vector<Integer32> &
const std::vector<int> &
>(),
python::arg("ints"),
Component::documentation("constructor").data()
)
.def(
python::init<
const std::vector<Float64> &
const std::vector<double> &
>(),
python::arg("doubles"),
Component::documentation("constructor").data()
)
.def(
python::init<
const std::vector<UTF8Text> &
const std::vector<std::string> &
>(),
python::arg("strings"),
Component::documentation("constructor").data()
Expand Down

0 comments on commit a25a0da

Please sign in to comment.