Mitsuba Renderer  0.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
plugin.h
Go to the documentation of this file.
1 /*
2  This file is part of Mitsuba, a physically based rendering system.
3 
4  Copyright (c) 2007-2014 by Wenzel Jakob and others.
5 
6  Mitsuba is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License Version 3
8  as published by the Free Software Foundation.
9 
10  Mitsuba is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #pragma once
20 #if !defined(__MITSUBA_CORE_PLUGIN_H_)
21 #define __MITSUBA_CORE_PLUGIN_H_
22 
23 #include <mitsuba/mitsuba.h>
24 #include <boost/filesystem.hpp>
25 #include <boost/scoped_ptr.hpp>
26 
28 
29 /**
30  * \brief Abstract plugin class -- represents loadable configurable objects
31  * and utilities.
32  *
33  * Please see the \ref ConfigurableObject and \ref Utility classes for
34  * details.
35  *
36  * \ingroup libcore
37  */
39 public:
40  /// Load a plugin from the supplied path
41  Plugin(const std::string &shortName, const fs::path &path);
42 
43  /// Virtual destructor
44  virtual ~Plugin();
45 
46  /// Is this a configurable object plugin or an utility plugin?
47  bool isUtility() const;
48 
49  /// Return an instance of the class implemented by this plugin
50  ConfigurableObject *createInstance(const Properties &props) const;
51 
52  /// Return an utility instance (if this is an utility plugin)
53  Utility *createUtility() const;
54 
55  /// Return a description of this plugin
56  std::string getDescription() const;
57 
58  /// Return the path of this plugin
59  const fs::path &getPath() const;
60 
61  /// Return a short name of this plugin
62  const std::string &getShortName() const;
63 protected:
64  /// Resolve the given symbol and return a pointer
65  void *getSymbol(const std::string &sym);
66  /// Check whether a certain symbol is provided by the plugin
67  bool hasSymbol(const std::string &sym) const;
68 private:
69  struct PluginPrivate;
70  boost::scoped_ptr<PluginPrivate> d;
71 };
72 
73 /**
74  * \brief The plugin manager is responsible for resolving and
75  * loading external plugins.
76  *
77  * Ordinarily, this class will be used by making repeated calls to
78  * the \ref createObject() methods. The generated instances are then
79  * assembled into a final object graph, such as a scene. One such
80  * examples is the \ref SceneHandler class, which parses an XML
81  * scene file by esentially translating the XML elements into calls
82  * to \ref createObject().
83  *
84  * Since this kind of construction method can be tiresome when
85  * dynamically building scenes from Python, this class has an
86  * additional Python-only method \c create(), which works as follows:
87  *
88  * \code
89  * from mitsuba.core import *
90  *
91  * pmgr = PluginManager.getInstance()
92  * camera = pmgr.create({
93  * "type" : "perspective",
94  * "toWorld" : Transform.lookAt(
95  * Point(0, 0, -10),
96  * Point(0, 0, 0),
97  * Vector(0, 1, 0)
98  * ),
99  * "film" : {
100  * "type" : "ldrfilm",
101  * "width" : 1920,
102  * "height" : 1080
103  * }
104  * })
105  * \endcode
106  *
107  * The above snippet constructs a \ref Camera instance from a
108  * plugin named \c perspective.so/dll/dylib and adds a child object
109  * named \c film, which is a \ref Film instance loaded from the
110  * plugin \c ldrfilm.so/dll/dylib. By the time the function
111  * returns, the object hierarchy has already been assembled, and the
112  * \ref ConfigurableObject::configure() methods of every object
113  * has been called.
114  *
115  * \ingroup libcore
116  * \ingroup libpython
117  */
119 public:
120  /// Return the global plugin manager
121  inline static PluginManager *getInstance() {
122  return m_instance;
123  }
124 
125  /// Ensure that a plugin is loaded and ready
126  void ensurePluginLoaded(const std::string &name);
127 
128  /// Return the list of loaded plugins
129  std::vector<std::string> getLoadedPlugins() const;
130 
131  /**
132  * \brief Instantiate a plugin, verify its type,
133  * and return the newly created instance.
134  *
135  * \param classType Expected type of the plugin. An
136  * exception will be thrown if it turns out not
137  * to derive from this class.
138  * \param props A \ref Properties instance containing
139  * all information required to find and construct
140  * the plugin.
141  */
142  ConfigurableObject *createObject(
143  const Class *classType,
144  const Properties &props
145  );
146 
147  /**
148  * \brief Instantiate a plugin and return the new
149  * instance (without verifying its type).
150  *
151  * \param props A \ref Properties instance containing
152  * all information required to find and construct
153  * the plugin.
154  */
155  ConfigurableObject *createObject(
156  const Properties &props
157  );
158 
159  /// Initializes the global plugin manager instance
160  static void staticInitialization();
161 
162  /// Free the memory taken by staticInitialization()
163  static void staticShutdown();
164 
166 protected:
167  PluginManager();
168 
169  /// Destruct and unload all plugins
170  ~PluginManager();
171 private:
172  std::map<std::string, Plugin *> m_plugins;
173  mutable ref<Mutex> m_mutex;
174  static ref<PluginManager> m_instance;
175 };
176 
178 
179 #endif /* __MITSUBA_CORE_PLUGIN_H_ */
Generic serializable object, which supports construction from a Properties instance.
Definition: cobject.h:40
static void staticInitialization()
Initializes the built-in reference count debugger (if enabled)
#define MTS_EXPORT_CORE
Definition: getopt.h:29
static PluginManager * getInstance()
Return the global plugin manager.
Definition: plugin.h:121
Abstract utility class – can be used to implement loadable utility plugins that perform various actio...
Definition: util.h:32
#define MTS_NAMESPACE_BEGIN
Definition: platform.h:137
#define MTS_DECLARE_CLASS()
This macro must be used in the initial definition in classes that derive from Object.
Definition: class.h:158
Reference counting helper.
Definition: ref.h:40
Stores meta-information about Object instances.
Definition: class.h:43
Associative parameter map for constructing subclasses of ConfigurableObject.
Definition: properties.h:46
Parent of all Mitsuba classes.
Definition: object.h:38
The plugin manager is responsible for resolving and loading external plugins.
Definition: plugin.h:118
static void staticShutdown()
Free the memory taken by staticInitialization()
Abstract plugin class – represents loadable configurable objects and utilities.
Definition: plugin.h:38
#define MTS_NAMESPACE_END
Definition: platform.h:138
Thin wrapper around the recursive boost thread lock.
Definition: lock.h:34