1 
2 //
3 // This source file is part of appleseed.
4 // Visit https://appleseedhq.net/ for additional information and resources.
5 //
6 // This software is released under the MIT license.
7 //
8 // Copyright (c) 2017-2018 Francois Beaune, The appleseedhq Organization
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining a copy
11 // of this software and associated documentation files (the "Software"), to deal
12 // in the Software without restriction, including without limitation the rights
13 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 // copies of the Software, and to permit persons to whom the Software is
15 // furnished to do so, subject to the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be included in
18 // all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 // THE SOFTWARE.
27 //
28 
29 #pragma once
30 
31 // appleseed.foundation headers.
32 #include "foundation/core/concepts/noncopyable.h"
33 #include "foundation/utility/autoreleaseptr.h"
34 #include "foundation/utility/registrar.h"
35 
36 // Standard headers.
37 #include <cassert>
38 #include <string>
39 #include <utility>
40 
41 // Forward declarations.
42 namespace renderer  { class Plugin; }
43 
44 namespace renderer
45 {
46 
47 //
48 // Base class for factory registrar implementations.
49 //
50 
51 template <typename EntityType, typename FactoryType, typename FactoryArrayType>
52 class EntityFactoryRegistrarImpl
53   : public foundation::NonCopyable
54 {
55   public:
56     // Register a factory.
57     void register_factory(foundation::auto_release_ptr<FactoryType> factory);
58 
59     // Register a factory defined in a plugin.
60     void register_factory_plugin(Plugin* plugin, void* plugin_entry_point);
61 
62     // Retrieve registered factories.
63     FactoryArrayType get_factories() const;
64 
65     // Lookup a factory by name.
66     const FactoryType* lookup(const char* name) const;
67 
68   private:
69     foundation::Registrar<FactoryType> m_registrar;
70 };
71 
72 
73 //
74 // EntityFactoryRegistrar class implementation.
75 //
76 
77 template <typename EntityType, typename FactoryType, typename FactoryArrayType>
register_factory(foundation::auto_release_ptr<FactoryType> factory)78 void EntityFactoryRegistrarImpl<EntityType, FactoryType, FactoryArrayType>::register_factory(foundation::auto_release_ptr<FactoryType> factory)
79 {
80     const std::string model = factory->get_model();
81     m_registrar.insert(model, std::move(factory));
82 }
83 
84 template <typename EntityType, typename FactoryType, typename FactoryArrayType>
register_factory_plugin(Plugin * plugin,void * plugin_entry_point)85 void EntityFactoryRegistrarImpl<EntityType, FactoryType, FactoryArrayType>::register_factory_plugin(Plugin* plugin, void* plugin_entry_point)
86 {
87     const auto create_factory = reinterpret_cast<FactoryType* (*)()>(plugin_entry_point);
88     register_factory(foundation::auto_release_ptr<FactoryType>(create_factory()));
89 }
90 
91 template <typename EntityType, typename FactoryType, typename FactoryArrayType>
get_factories()92 FactoryArrayType EntityFactoryRegistrarImpl<EntityType, FactoryType, FactoryArrayType>::get_factories() const
93 {
94     FactoryArrayType factories;
95 
96     for (const auto& item : m_registrar.items())
97         factories.push_back(item.second);
98 
99     return factories;
100 }
101 
102 template <typename EntityType, typename FactoryType, typename FactoryArrayType>
lookup(const char * name)103 const FactoryType* EntityFactoryRegistrarImpl<EntityType, FactoryType, FactoryArrayType>::lookup(const char* name) const
104 {
105     assert(name);
106     return m_registrar.lookup(name);
107 }
108 
109 }   // namespace renderer
110