Nori  24
object.h
1 /*
2  This file is part of Nori, a simple educational ray tracer
3 
4  Copyright (c) 2015 by Wenzel Jakob, Romain Prévost
5 
6  Nori 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  Nori 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 #if !defined(__NORI_OBJECT_H)
20 #define __NORI_OBJECT_H
21 
22 #include <nori/proplist.h>
23 
24 NORI_NAMESPACE_BEGIN
25 
32 class NoriObject {
33 public:
34  enum EClassType {
35  EScene = 0,
36  EMesh,
37  ETexture,
38  EBSDF,
39  EPhaseFunction,
40  EEmitter,
41  EMedium,
42  ECamera,
43  EIntegrator,
44  ESampler,
45  ETest,
46  EReconstructionFilter,
47  EClassTypeCount
48  };
49 
51  static std::string classTypeName(EClassType type) {
52  switch (type) {
53  case EScene: return "scene";
54  case EMesh: return "shape";
55  case ETexture: return "texture";
56  case EBSDF: return "bsdf";
57  case EEmitter: return "emitter";
58  case ECamera: return "camera";
59  case EIntegrator: return "integrator";
60  case ESampler: return "sampler";
61  case ETest: return "test";
62  default: return "<unknown>";
63  }
64  }
65 
67  virtual ~NoriObject() { }
68 
73  virtual EClassType getClassType() const = 0;
74 
81  virtual void addChild(NoriObject *child) {
82  throw NoriException(
83  "NoriObject::addChild() is not implemented for objects of type '%s'!",
85  }
86 
94  virtual void setParent(NoriObject *parent) { /* Do nothing */ }
95 
107  virtual void activate() { /* Do nothing */ }
108 
110  virtual std::string toString() const = 0;
111 
112 
114  void setIdName(const std::string & name) { m_idname = name; }
115  const std::string & getIdName() const { return m_idname; }
116 
117 protected:
118  std::string m_idname;
119 
120 };
121 
129 public:
130  typedef std::function<NoriObject *(const PropertyList &)> Constructor;
131 
145  static void registerClass(const std::string &name, const Constructor &constr);
146 
158  static NoriObject *createInstance(const std::string &name,
159  const PropertyList &propList) {
160  if (!m_constructors || m_constructors->find(name) == m_constructors->end())
161  throw NoriException("A constructor for class \"%s\" could not be found!", name);
162  return (*m_constructors)[name](propList);
163  }
164 
165  static void printRegisteredClasses() {
166  if(m_constructors)
167  for(auto v : *m_constructors)
168  std::cout << v.first << std::endl;
169  }
170 
171 private:
172  static std::map<std::string, Constructor> *m_constructors;
173 };
174 
176 #define NORI_REGISTER_CLASS(cls, name) \
177  cls *cls ##_create(const PropertyList &list) { \
178  return new cls(list); \
179  } \
180  static struct cls ##_{ \
181  cls ##_() { \
182  NoriObjectFactory::registerClass(name, cls ##_create); \
183  } \
184  } cls ##__NORI_;
185 
187 #define NORI_REGISTER_TEMPLATED_CLASS(cls, T, name) \
188  cls<T> * cls ##_## T ##_create(const PropertyList &list) { \
189  return new cls<T>(list); \
190  } \
191  static struct cls ##_## T ##_{ \
192  cls ##_## T ##_() { \
193  NoriObjectFactory::registerClass(name, cls ##_## T ##_create); \
194  } \
195  } cls ## T ##__NORI_;
196 
197 NORI_NAMESPACE_END
198 
199 #endif /* __NORI_OBJECT_H */
Simple exception class, which stores a human-readable error description.
Definition: common.h:148
Factory for Nori objects.
Definition: object.h:128
static NoriObject * createInstance(const std::string &name, const PropertyList &propList)
Construct an instance from the class of the given name.
Definition: object.h:158
static void registerClass(const std::string &name, const Constructor &constr)
Register an object constructor with the object factory.
Definition: object.cpp:25
Base class of all objects.
Definition: object.h:32
virtual void activate()
Perform some action associated with the object.
Definition: object.h:107
virtual ~NoriObject()
Virtual destructor.
Definition: object.h:67
virtual void setParent(NoriObject *parent)
Set the parent object.
Definition: object.h:94
void setIdName(const std::string &name)
Allow to assign a name to the object.
Definition: object.h:114
virtual void addChild(NoriObject *child)
Add a child object to the current instance.
Definition: object.h:81
virtual std::string toString() const =0
Return a brief string summary of the instance (for debugging purposes)
static std::string classTypeName(EClassType type)
Turn a class type into a human-readable string.
Definition: object.h:51
virtual EClassType getClassType() const =0
Return the type of object (i.e. Mesh/BSDF/etc.) provided by this instance.
This is an associative container used to supply the constructors of NoriObject subclasses with parame...
Definition: proplist.h:32