Skip to content

Commit

Permalink
Adding raw string parsing in factories for Python side clients, still…
Browse files Browse the repository at this point in the history
… some issues when trying to store Python lambdas
  • Loading branch information
SamFlt committed Nov 12, 2024
1 parent 6352ee9 commit ae34fcd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
30 changes: 12 additions & 18 deletions modules/python/config/rbt.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@
]
}
],
"methods": [
{
"static": false,
"signature": "void registerTypeRaw(const std::string&, const std::function<std::shared_ptr<T>(const std::string &)>)",
"keep_alive": [
[
1,
3
]
]
}
],
"acknowledge_pointer_or_ref_fields": [
"std::map<std::string, std::function<std::shared_ptr<T>(const nlohmann::json&)>>",
"std::function<std::string(const nlohmann::json&)>"
Expand Down Expand Up @@ -222,12 +234,6 @@
"static": true,
"signature": "vpRBDriftDetectorFactory& getFactory()",
"return_policy": "reference",
"keep_alive": [
[
1,
0
]
],
"returns_ref_ok": true
}
]
Expand All @@ -238,12 +244,6 @@
"static": true,
"signature": "vpObjectMaskFactory& getFactory()",
"return_policy": "reference",
"keep_alive": [
[
1,
0
]
],
"returns_ref_ok": true
}
]
Expand All @@ -254,12 +254,6 @@
"static": true,
"signature": "vpRBFeatureTrackerFactory& getFactory()",
"return_policy": "reference",
"keep_alive": [
[
1,
0
]
],
"returns_ref_ok": true
}
]
Expand Down
18 changes: 17 additions & 1 deletion modules/tracker/rbt/include/visp3/rbt/vpDynamicFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,33 @@ class VISP_EXPORT vpDynamicFactory
#if defined(VISP_HAVE_NLOHMANN_JSON)
void registerType(const std::string &key, const std::function<std::shared_ptr<T>(const nlohmann::json &)> &function)
{
if (m_jsonBuilders.find(key) != m_jsonBuilders.end()) {
if (m_jsonBuilders.find(key) != m_jsonBuilders.end() || m_jsonRawBuilders.find(key) != m_jsonRawBuilders.end()) {
throw vpException(vpException::badValue, "Type %s was already registered in the factory", key.c_str());
}
m_jsonBuilders[key] = function;
}

void registerTypeRaw(const std::string &key, const std::function<std::shared_ptr<T>(const std::string &)> function)
{
if (m_jsonBuilders.find(key) != m_jsonBuilders.end() || m_jsonRawBuilders.find(key) != m_jsonRawBuilders.end()) {
throw vpException(vpException::badValue, "Type %s was already registered in the factory", key.c_str());
}
std::cout << "IN REGISTERING RAW TYPE" << std::endl;
m_jsonRawBuilders[key] = function;
std::cout << "IN REGISTERING RAW TYPE END" << std::endl;

}

std::shared_ptr<T> buildFromJson(const nlohmann::json &j)
{
const std::string key = m_keyFinder(j);

if (m_jsonBuilders.find(key) != m_jsonBuilders.end()) {
return m_jsonBuilders[key](j);
}
else if (m_jsonRawBuilders.find(key) != m_jsonRawBuilders.end()) {
return m_jsonRawBuilders[key](j.dump());
}
else {
return nullptr;
}
Expand All @@ -82,6 +96,8 @@ class VISP_EXPORT vpDynamicFactory

#if defined(VISP_HAVE_NLOHMANN_JSON)
std::map<std::string, std::function<std::shared_ptr<T>(const nlohmann::json &)>> m_jsonBuilders;
std::map<std::string, std::function<std::shared_ptr<T>(const std::string &)>> m_jsonRawBuilders;

std::function<std::string(const nlohmann::json &)> m_keyFinder; //! Function to retrieve the key from a json object
#endif
};
Expand Down

0 comments on commit ae34fcd

Please sign in to comment.