Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/lib/util/loading/factory.h @ 9684

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

orxonox/branches/new_class_id: slowly but surely reimplementing to the new groove… way to go

File size: 2.6 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 <vector>
30#include <list>
31
32/**
33 * Creates a factory to a Loadable Class.
34 * this should be used at the beginning of all the Classes that should be loadable (in the cc-file)
35 */
36#define CREATE_FACTORY(CLASS_NAME, CLASS_ID) \
37    tFactory<CLASS_NAME>* global_##CLASS_NAME##_Factory = new tFactory<CLASS_NAME>(#CLASS_NAME, CLASS_ID)
38
39//! The Factory is a loadable object handler
40class Factory : public BaseObject
41{
42  NewObjectListDeclaration(Factory);
43public:
44  virtual ~Factory ();
45
46  static void deleteFactories();
47
48  static  BaseObject* fabricate(const std::string& className);
49  static  BaseObject* fabricate(int classID);
50  static  BaseObject* fabricate(const TiXmlElement* root = NULL);
51
52
53  bool operator==(int classID) const;
54  bool operator==(const char* className) const;
55  bool operator==(const std::string& className) const;
56
57protected:
58  Factory (const std::string& factoryName, int classID);
59  virtual BaseObject* fabricateObject(const TiXmlElement* root = NULL) const = 0;
60
61protected:
62  const int                     classID;              //!< The Class-Identifyer of the Factory.
63  const std::string             className;            //!< The name of the Class.
64  static std::list<Factory*>*   factoryList;          //!< List of Registered Factories
65};
66
67/**
68 *  @brief a factory that is able to load any kind of Object
69 * (this is a Functor)
70 */
71template<class T> class tFactory : public Factory
72{
73public:
74  /**
75   * @brief creates a new type Factory to enable the loading of T
76   * @param factoryName the Name of the Factory to load.
77   * @param classID the ID of the Class to be created.
78   */
79  tFactory (const char* factoryName, int classID)
80      : Factory(factoryName, classID)
81  {  }
82
83private:
84  /**
85   * @brief fabricates an Object of type T, with the constructor T::T(const TiXmlElemnt*)
86   * @param root the TiXmlElement T should load parameters from.
87   * @return the newly fabricated T.
88   */
89  virtual BaseObject* fabricateObject(const TiXmlElement* root = NULL) const
90  {
91    return new T(root);
92  }
93};
94
95#endif /* _FACTORY_H */
96
Note: See TracBrowser for help on using the repository browser.