Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/util/loading/factory.h @ 10625

Last change on this file since 10625 was 9869, checked in by bensch, 18 years ago

orxonox/trunk: merged the new_class_id branche back to the trunk.
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/new_class_id trunk -r9683:HEAD
no conflicts… puh..

File size: 3.3 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
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 2, or (at your option)
9   any later version.
10
11   ### File Specific:
12   main-programmer: Christian Meyer
13   co-programmer: Benjamin Grauer
14*/
15
16/*!
17 * @file factory.h
18 * @brief A loadable object handler
19*/
20
21
22#ifndef __FACTORY_H
23#define __FACTORY_H
24
25class BaseObject;
26
27#include "parser/tinyxml/tinyxml.h"
28#include "base_object.h"
29#include <map>
30
31/**
32 * Creates a factory to a Loadable Class.
33 * this should be used at the beginning of all the Classes that should be loadable (in the cc-file)
34 */
35#define CREATE_FACTORY(CLASS_NAME) \
36    tFactory<CLASS_NAME> global_##CLASS_NAME##_Factory = tFactory<CLASS_NAME>(CLASS_NAME::staticClassID())
37
38//! The Factory is a loadable object handler
39class Factory : public BaseObject
40{
41  //! Declare the ObjectList at the BaseObject List
42  ObjectListDeclaration(Factory);
43public:
44  virtual ~Factory ();
45
46  static  BaseObject* fabricate(const std::string& className);
47  static  BaseObject* fabricate(const ClassID& classID);
48  static  BaseObject* fabricate(const TiXmlElement* root);
49
50  bool operator==(int classID) const;
51  bool operator==(const std::string& className) const;
52  /** @param classID the ID to compare @returns true if the ID's match */
53  bool operator==(const ClassID& classID) const { return _classID == classID; };
54
55
56  void debug() const;
57  static void debugAll();
58
59protected:
60  Factory (const ClassID& id);
61  /**
62   * @brief The core function of the Factory. Creates objects of the Factories Type.
63   * @param root The TiXmlElement of the Factory.
64   * @returns the created Object in BaseType* format.
65   */
66  virtual BaseObject* fabricateObject(const TiXmlElement* root = NULL) const = 0;
67
68private:
69  //! Copy Constructor is hidden.
70  Factory (const Factory&) {};
71
72protected:
73  /** The Type of the FactoryMap that is sorted by ID */
74  typedef std::map<const ClassID, Factory*>  FactoryIDMap;
75  /** The Type of the FactoryMap that is sorted by Name */
76  typedef std::map<std::string, Factory*>    FactoryStringMap;
77
78  const ClassID                 _classID;              //!< The Class-Identifyer of the Factory.
79  static FactoryIDMap           _factoryIDMap;         //!< List of Registered Factories
80  static FactoryStringMap       _factoryStringMap;     //!< List of Registered Factories
81};
82
83/**
84 *  @brief a factory that is able to load any kind of Object
85 * (this is a Functor)
86 */
87template<class T> class tFactory : public Factory
88{
89public:
90  /**
91   * @brief creates a new type Factory to enable the loading of T
92   * @param classID the ID of the Class to be created.
93   */
94  tFactory (const ClassID& classID)
95      : Factory(classID)
96  {  }
97  /**
98   * @brief copy constructor
99   * @param factory the Factory to copy
100   */
101  tFactory (const tFactory& factory) : Factory(factory._classID) {};
102
103  /**
104   * @brief fabricates an Object of type T, with the constructor T::T(const TiXmlElemnt*)
105   * @param root the TiXmlElement T should load parameters from.
106   * @return the newly fabricated T.
107   */
108  virtual BaseObject* fabricateObject(const TiXmlElement* root = NULL) const
109  {
110    return new T(root);
111  }
112};
113
114#endif /* _FACTORY_H */
115
Note: See TracBrowser for help on using the repository browser.