AWViz-ROS
Loading...
Searching...
No Matches
plugin_factory.hpp
Go to the documentation of this file.
1// Copyright 2024 Kotaro Uetake.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef AWVIZ_COMMON__PLUGIN_FACTORY_HPP_
16#define AWVIZ_COMMON__PLUGIN_FACTORY_HPP_
17
18#include <pluginlib/class_loader.hpp>
19
20#include <iostream>
21#include <memory>
22#include <string>
23#include <vector>
24
25namespace awviz_common
26{
31{
32 std::string id;
33 std::string name;
34 std::string type;
35 std::string package;
36 std::string description;
37};
38
42template <typename T>
44{
45public:
52 PluginFactory(const std::string & package, const std::string & base_class_type)
53 {
54 class_loader_ = std::make_shared<pluginlib::ClassLoader<T>>(package, base_class_type);
55 }
56
62 std::string get_plugin_manifest_path(const std::string & lookup_name) const
63 {
64 return class_loader_->getPluginManifestPath(lookup_name);
65 }
66
72 PluginInfo get_plugin_info(const std::string & lookup_name) const
73 {
74 auto name = class_loader_->getName(lookup_name);
75 auto type = class_loader_->getClassType(lookup_name);
76 auto package = class_loader_->getClassPackage(lookup_name);
77 auto description = class_loader_->getClassDescription(lookup_name);
78 return {lookup_name, name, type, package, description};
79 }
80
87 virtual std::shared_ptr<T> create_instance(const std::string & lookup_name) const
88 {
89 try {
90 return class_loader_->createSharedInstance(lookup_name);
91 } catch (pluginlib::PluginlibException & ex) {
92 std::cerr << "[PluginlibException]: " << ex.what() << std::endl;
93 return nullptr;
94 }
95 }
96
102 std::vector<std::string> get_declared_classes() const
103 {
104 return class_loader_->getDeclaredClasses();
105 }
106
107private:
108 std::shared_ptr<pluginlib::ClassLoader<T>> class_loader_;
109};
110} // namespace awviz_common
111#endif // AWVIZ_COMMON__PLUGIN_FACTORY_HPP_
Abstract base class representing a plugin load-able class factory.
Definition: plugin_factory.hpp:44
virtual std::shared_ptr< T > create_instance(const std::string &lookup_name) const
Create a instance of the plugin.
Definition: plugin_factory.hpp:87
std::vector< std::string > get_declared_classes() const
Return a list of all available classes for this ClassLoader's base class type.
Definition: plugin_factory.hpp:102
PluginFactory(const std::string &package, const std::string &base_class_type)
Construct factory loading pluginlib::ClassLoader<T>.
Definition: plugin_factory.hpp:52
PluginInfo get_plugin_info(const std::string &lookup_name) const
Return plugin information.
Definition: plugin_factory.hpp:72
std::string get_plugin_manifest_path(const std::string &lookup_name) const
Return plugin manifest path.
Definition: plugin_factory.hpp:62
Definition: display.hpp:30
Struct to bundle the information of plugin.
Definition: plugin_factory.hpp:31
std::string type
Type of class.
Definition: plugin_factory.hpp:34
std::string package
Package name.
Definition: plugin_factory.hpp:35
std::string id
Class id.
Definition: plugin_factory.hpp:32
std::string name
Name of class.
Definition: plugin_factory.hpp:33
std::string description
Description of class.
Definition: plugin_factory.hpp:36