1 /*
2  * Copyright (C) 2017 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 
18 
19 #ifndef IGNITION_PLUGIN_DETAIL_LOADER_HH_
20 #define IGNITION_PLUGIN_DETAIL_LOADER_HH_
21 
22 #include <memory>
23 #include <string>
24 #include <unordered_set>
25 #include <ignition/plugin/EnablePluginFromThis.hh>
26 #include <ignition/plugin/Loader.hh>
27 
28 namespace ignition
29 {
30   namespace plugin
31   {
32     template <typename Interface>
PluginsImplementing() const33     std::unordered_set<std::string> Loader::PluginsImplementing() const
34     {
35       return this->PluginsImplementing(typeid(Interface).name(), false);
36     }
37 
38     template <typename PluginPtrType>
Instantiate(const std::string & _pluginNameOrAlias) const39     PluginPtrType Loader::Instantiate(
40         const std::string &_pluginNameOrAlias) const
41     {
42       const std::string &resolvedName = this->LookupPlugin(_pluginNameOrAlias);
43       if (resolvedName.empty())
44         return PluginPtr();
45 
46        PluginPtrType ptr(this->PrivateGetInfo(resolvedName),
47                          this->PrivateGetPluginDlHandlePtr(resolvedName));
48 
49        if (auto *enableFromThis =
50               ptr->template QueryInterface<EnablePluginFromThis>())
51          enableFromThis->PrivateSetPluginFromThis(ptr);
52 
53        return ptr;
54     }
55 
56     template <typename InterfaceType>
Factory(const std::string & _pluginNameOrAlias) const57     std::shared_ptr<InterfaceType> Loader::Factory(
58         const std::string &_pluginNameOrAlias) const
59     {
60       return this->Instantiate(_pluginNameOrAlias)
61           ->template QueryInterfaceSharedPtr<InterfaceType>();
62     }
63   }
64 }
65 
66 #endif
67