Skip to content
Snippets Groups Projects
Commit f35f31a7 authored by Joan Vallvé Navarro's avatar Joan Vallvé Navarro
Browse files

merge loader_utils with loader

parent 332f2a88
No related branches found
No related tags found
1 merge request!448Draft: Resolve "Implementation of new nodes creation"
Pipeline #21441 failed
...@@ -186,7 +186,6 @@ SET(SRCS ...@@ -186,7 +186,6 @@ SET(SRCS
src/utils/check_log.cpp src/utils/check_log.cpp
src/utils/graph_search.cpp src/utils/graph_search.cpp
src/utils/loader.cpp src/utils/loader.cpp
src/utils/loader_utils.cpp
src/utils/load_core.cpp src/utils/load_core.cpp
) )
......
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include <string> #include <string>
#include <map> #include <map>
#include <memory> #include <memory>
#include "yaml-cpp/yaml.h"
namespace wolf namespace wolf
{ {
...@@ -34,18 +35,28 @@ class Loader ...@@ -34,18 +35,28 @@ class Loader
public: public:
Loader(int flags = RTLD_LAZY); Loader(int flags = RTLD_LAZY);
~Loader(); ~Loader();
void load(std::string _libname_with_so);
void loadPlugin(std::string _plugin); void load(std::string _libname_with_so);
void close(std::string _libname_with_so); void loadPlugin(std::string _plugin);
void closePlugin(std::string _plugin);
void* isLoaded(std::string _libname_with_so) const; void close(std::string _libname_with_so);
void* isLoadedPlugin(std::string _plugin) const; void closePlugin(std::string _plugin);
void* isLoaded(std::string _libname_with_so) const;
void* isLoadedPlugin(std::string _plugin) const;
void searchAndLoadPlugins(const YAML::Node& _node);
size_t size() const; size_t size() const;
}; };
typedef std::shared_ptr<Loader> LoaderPtr; typedef std::shared_ptr<Loader> LoaderPtr;
inline size_t Loader::size() const static std::set<std::string> searchPlugins(const YAML::Node& _node, std::list<YAML::Node>& _visited_nodes);
static std::set<std::string> searchFieldsRecursive(const YAML::Node& _node,
std::list<YAML::Node>& _visited_nodes,
const std::string& field);
inline size_t Loader::size() const
{ {
return name_resources_.size(); return name_resources_.size();
} }
......
// WOLF - Copyright (C) 2020-2025
// Institut de Robòtica i Informàtica Industrial, CSIC-UPC.
// Authors: Joan Solà Ortega (jsola@iri.upc.edu) and
// Joan Vallvé Navarro (jvallve@iri.upc.edu)
// All rights reserved.
//
// This file is part of WOLF: http://www.iri.upc.edu/wolf
// WOLF is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 3
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#pragma once
#include "core/utils/loader.h"
#include "yaml-cpp/yaml.h"
#include "core/common/wolf.h"
namespace wolf
{
void searchAndLoadPlugins(const YAML::Node& _node, LoaderPtr _loader);
std::set<std::string> searchPlugins(const YAML::Node& _node, std::list<YAML::Node>& _visited_nodes);
std::set<std::string> searchFieldsRecursive(const YAML::Node& _node,
std::list<YAML::Node>& _visited_nodes,
const std::string& field);
} /* namespace wolf */
...@@ -31,7 +31,6 @@ ...@@ -31,7 +31,6 @@
#include "core/tree_manager/factory_tree_manager.h" #include "core/tree_manager/factory_tree_manager.h"
#include "core/tree_manager/tree_manager_base.h" #include "core/tree_manager/tree_manager_base.h"
#include "core/utils/loader.h" #include "core/utils/loader.h"
#include "core/utils/loader_utils.h"
// yaml-schema-cpp // yaml-schema-cpp
#include "yaml-schema-cpp/yaml_server.hpp" #include "yaml-schema-cpp/yaml_server.hpp"
...@@ -106,7 +105,7 @@ ProblemPtr Problem::autoSetup(YAML::Node _param_node, ...@@ -106,7 +105,7 @@ ProblemPtr Problem::autoSetup(YAML::Node _param_node,
// SCHEMA FOLDERS (optional _schema_folders specify folders where to search schemas before installed ones) // SCHEMA FOLDERS (optional _schema_folders specify folders where to search schemas before installed ones)
// Search and load plugins to get the installed schema folders registered // Search and load plugins to get the installed schema folders registered
if (not _loader) _loader = std::make_shared<Loader>(); if (not _loader) _loader = std::make_shared<Loader>();
searchAndLoadPlugins(server.getNode(), _loader); _loader->searchAndLoadPlugins(server.getNode());
// Add the installed schema folders after optional input folders // Add the installed schema folders after optional input folders
server.addFolderSchema(FolderRegistry::getRegisteredFolders()); server.addFolderSchema(FolderRegistry::getRegisteredFolders());
...@@ -191,7 +190,7 @@ SensorBasePtr Problem::installSensor(const YAML::Node& _sensor_node, std::vector ...@@ -191,7 +190,7 @@ SensorBasePtr Problem::installSensor(const YAML::Node& _sensor_node, std::vector
if (not _schema_folders.empty()) if (not _schema_folders.empty())
{ {
// Search and load plugins to get the installed schema folders registered // Search and load plugins to get the installed schema folders registered
searchAndLoadPlugins(_sensor_node, loader_); loader_->searchAndLoadPlugins(_sensor_node);
// Add the installed schema folders after optional input _schema_folders // Add the installed schema folders after optional input _schema_folders
FolderRegistry::appendRegisteredFolders(_schema_folders); FolderRegistry::appendRegisteredFolders(_schema_folders);
...@@ -241,7 +240,7 @@ SensorBasePtr Problem::installSensor(const std::string& _params_yaml_filen ...@@ -241,7 +240,7 @@ SensorBasePtr Problem::installSensor(const std::string& _params_yaml_filen
auto sensor_node = server.getNode(); auto sensor_node = server.getNode();
// Search and load plugins to get the installed schema folders registered // Search and load plugins to get the installed schema folders registered
searchAndLoadPlugins(sensor_node, loader_); loader_->searchAndLoadPlugins(sensor_node);
// Add the installed schema folders after optional input _schema_folders // Add the installed schema folders after optional input _schema_folders
server.addFolderSchema(FolderRegistry::getRegisteredFolders()); server.addFolderSchema(FolderRegistry::getRegisteredFolders());
...@@ -279,7 +278,7 @@ ProcessorBasePtr Problem::installProcessor(const YAML::Node& _processor_node, st ...@@ -279,7 +278,7 @@ ProcessorBasePtr Problem::installProcessor(const YAML::Node& _processor_node, st
if (not _schema_folders.empty()) if (not _schema_folders.empty())
{ {
// Search and load plugins to get the installed schema folders registered // Search and load plugins to get the installed schema folders registered
searchAndLoadPlugins(_processor_node, loader_); loader_->searchAndLoadPlugins(_processor_node);
// Add the installed schema folders after optional input _schema_folders // Add the installed schema folders after optional input _schema_folders
FolderRegistry::appendRegisteredFolders(_schema_folders); FolderRegistry::appendRegisteredFolders(_schema_folders);
...@@ -337,7 +336,7 @@ ProcessorBasePtr Problem::installProcessor(SensorBasePtr _sensor_corr ...@@ -337,7 +336,7 @@ ProcessorBasePtr Problem::installProcessor(SensorBasePtr _sensor_corr
auto processor_node = server.getNode(); auto processor_node = server.getNode();
// Search and load plugins to get the installed schema folders registered // Search and load plugins to get the installed schema folders registered
searchAndLoadPlugins(processor_node, loader_); loader_->searchAndLoadPlugins(processor_node);
// Add the installed schema folders after optional input _schema_folders // Add the installed schema folders after optional input _schema_folders
server.addFolderSchema(FolderRegistry::getRegisteredFolders()); server.addFolderSchema(FolderRegistry::getRegisteredFolders());
...@@ -382,7 +381,7 @@ ProcessorBasePtr Problem::installProcessor(const std::string& _params_yaml ...@@ -382,7 +381,7 @@ ProcessorBasePtr Problem::installProcessor(const std::string& _params_yaml
auto server = yaml_schema_cpp::YamlServer(_schema_folders, _params_yaml_filename); auto server = yaml_schema_cpp::YamlServer(_schema_folders, _params_yaml_filename);
// Search and load plugins to get the installed schema folders registered // Search and load plugins to get the installed schema folders registered
searchAndLoadPlugins(server.getNode(), loader_); loader_->searchAndLoadPlugins(server.getNode());
// Add the installed schema folders after optional input _schema_folders // Add the installed schema folders after optional input _schema_folders
server.addFolderSchema(FolderRegistry::getRegisteredFolders()); server.addFolderSchema(FolderRegistry::getRegisteredFolders());
......
...@@ -20,7 +20,6 @@ ...@@ -20,7 +20,6 @@
#include "core/trajectory/trajectory_base.h" #include "core/trajectory/trajectory_base.h"
#include "core/map/map_base.h" #include "core/map/map_base.h"
#include "core/landmark/landmark_base.h" #include "core/landmark/landmark_base.h"
#include "core/utils/loader_utils.h"
namespace wolf namespace wolf
{ {
......
...@@ -113,4 +113,57 @@ void* Loader::isLoadedPlugin(std::string _plugin) const ...@@ -113,4 +113,57 @@ void* Loader::isLoadedPlugin(std::string _plugin) const
return isLoaded("libwolf" + _plugin + lib_extension); return isLoaded("libwolf" + _plugin + lib_extension);
} }
void Loader::searchAndLoadPlugins(const YAML::Node& _node)
{
std::list<YAML::Node> visited_nodes;
auto plugins = searchPlugins(_node, visited_nodes);
for (auto plugin : plugins) this->loadPlugin(plugin); // if already loaded, does nothing
}
std::set<std::string> searchPlugins(const YAML::Node& _node, std::list<YAML::Node>& _visited_nodes)
{
return searchFieldsRecursive(_node, _visited_nodes, "plugin");
}
std::set<std::string> searchFieldsRecursive(const YAML::Node& _node,
std::list<YAML::Node>& _visited_nodes,
const std::string& lib_field)
{
std::set<std::string> libs;
if (std::find(_visited_nodes.begin(), _visited_nodes.end(), _node) != _visited_nodes.end()) return {};
_visited_nodes.push_back(_node);
if (_node.IsMap())
{
for (const auto& pair : _node)
{
// is plugin?
if (pair.first.as<std::string>() == lib_field)
libs.insert(pair.second.as<std::string>());
else
{
auto rec_libs = searchFieldsRecursive(pair.second, _visited_nodes, lib_field);
libs.insert(rec_libs.begin(), rec_libs.end());
}
}
}
else if (_node.IsSequence())
{
for (const auto& child : _node)
{
auto rec_libs = searchFieldsRecursive(child, _visited_nodes, lib_field);
libs.insert(rec_libs.begin(), rec_libs.end());
}
}
else if (_node.IsScalar())
{
return {};
}
return libs;
}
} // namespace wolf } // namespace wolf
// WOLF - Copyright (C) 2020-2025
// Institut de Robòtica i Informàtica Industrial, CSIC-UPC.
// Authors: Joan Solà Ortega (jsola@iri.upc.edu) and
// Joan Vallvé Navarro (jvallve@iri.upc.edu)
// All rights reserved.
//
// This file is part of WOLF: http://www.iri.upc.edu/wolf
// WOLF is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 3
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "core/utils/loader_utils.h"
namespace wolf
{
void searchAndLoadPlugins(const YAML::Node& _node, LoaderPtr _loader)
{
std::list<YAML::Node> visited_nodes;
auto plugins = searchPlugins(_node, visited_nodes);
for (auto plugin : plugins) _loader->loadPlugin(plugin); // if already loaded, does nothing
}
std::set<std::string> searchPlugins(const YAML::Node& _node, std::list<YAML::Node>& _visited_nodes)
{
return searchFieldsRecursive(_node, _visited_nodes, "plugin");
}
std::set<std::string> searchFieldsRecursive(const YAML::Node& _node,
std::list<YAML::Node>& _visited_nodes,
const std::string& lib_field)
{
std::set<std::string> libs;
if (std::find(_visited_nodes.begin(), _visited_nodes.end(), _node) != _visited_nodes.end()) return {};
_visited_nodes.push_back(_node);
if (_node.IsMap())
{
for (const auto& pair : _node)
{
// is plugin?
if (pair.first.as<std::string>() == lib_field)
libs.insert(pair.second.as<std::string>());
else
{
auto rec_libs = searchFieldsRecursive(pair.second, _visited_nodes, lib_field);
libs.insert(rec_libs.begin(), rec_libs.end());
}
}
}
else if (_node.IsSequence())
{
for (const auto& child : _node)
{
auto rec_libs = searchFieldsRecursive(child, _visited_nodes, lib_field);
libs.insert(rec_libs.begin(), rec_libs.end());
}
}
else if (_node.IsScalar())
{
return {};
}
return libs;
}
} // namespace wolf
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