Lightmetrica  0.0.1.50dbee3 (yosakoi)
 All Classes Functions Variables Typedefs Enumerations Enumerator
plugin.common.h
1 /*
2  Lightmetrica : A research-oriented renderer
3 
4  Copyright (c) 2014 Hisanari Otsu
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #pragma once
21 #ifndef LIB_LIGHTMETRICA_PLUGIN_COMMON_H
22 #define LIB_LIGHTMETRICA_PLUGIN_COMMON_H
23 
24 #include "component.h"
25 #include <string>
26 #include <functional>
27 
28 LM_NAMESPACE_BEGIN
29 
31 {
32 public:
33 
34  typedef std::function<Component* ()> CreateComponentFunc;
35 
36 private:
37 
38  PluginManager() {}
39 
40 private:
41 
42  LM_DISABLE_COPY_AND_MOVE(PluginManager);
43 
44 public:
45 
46  static bool Register(const std::string& interfaceType, const std::string& implType, const CreateComponentFunc& func);
47 
48 };
49 
50 template <typename ImplType, typename InterfaceType>
52 {
53 public:
54 
56  {
58  return instance;
59  }
60 
61 private:
62 
64  {
65  if (!PluginManager::Register(InterfaceType::InterfaceTypeName(), ImplType::ImplTypeName(), [](){ return new ImplType; }))
66  {
67  LM_LOG_ERROR("Failed to register plugin '" + std::string(ImplType::ImplTypeName()) + "'");
68  }
69  }
70 
71 private:
72 
77 
78 };
79 
80 LM_NAMESPACE_END
81 
82 #define LM_COMPONENT_REGISTER_PLUGIN_IMPL(ImplType, InterfaceType) \
83  namespace { \
84  \
85  template <typename T1, typename T2> \
86  class ComponentPluginFactoryEntryInstance; \
87  \
88  template <> \
89  class ComponentPluginFactoryEntryInstance<ImplType, InterfaceType> \
90  { \
91  static const ::lightmetrica::ComponentPluginFactoryEntry<ImplType, InterfaceType>& reg; \
92  }; \
93  \
94  const ::lightmetrica::ComponentPluginFactoryEntry<ImplType, InterfaceType>& \
95  ComponentPluginFactoryEntryInstance<ImplType, InterfaceType>::reg = \
96  ::lightmetrica::ComponentPluginFactoryEntry<ImplType, InterfaceType>::Instance(); \
97  \
98  LM_COMPONENT_CHECK_IS_DERIVED_CLASS(ImplType, InterfaceType); \
99  LM_COMPONENT_CHECK_IS_DERIVED_CLASS(InterfaceType, Component); \
100  LM_COMPONENT_CHECK_HAS_MEMBER_FUNCTION(ImplType, ImplTypeName); \
101  LM_COMPONENT_CHECK_HAS_MEMBER_FUNCTION(InterfaceType, InterfaceTypeName); \
102  \
103  }
104 
105 #endif // LIB_LIGHTMETRICA_PLUGIN_COMMON_H
Definition: plugin.common.h:51
Definition: plugin.common.h:30