Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5155 was 5155, checked in by bensch, 20 years ago

orxonox/trunk: now one should be able to create entities on the Fly

File size: 2.5 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 "tinyxml.h"
28#include "base_object.h"
29#include "debug.h"
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 = new tFactory<CLASS_NAME>(#CLASS_NAME)
37
38//! The Factory is a loadable object handler
39class Factory : public BaseObject {
40
41 public:
42  Factory (const char* factoryName = NULL);
43  ~Factory ();
44
45  void fabricate(const char* className, const char* entityName);
46  virtual BaseObject* fabricate(const TiXmlElement* root) = NULL;
47
48  static void registerFactory( Factory* factory);
49  /** @returns the first factory */
50  static Factory* getFirst() { return Factory::first; };
51
52  protected:
53    /** sets the Next factory in the list @param nextFactory the next factory */
54    inline void setNext( Factory* nextFactory) { this->next = nextFactory; };
55    /** @returns the next factory */
56    Factory* getNext() const { return this->next; };
57
58  private:
59    Factory*          next;                 //!< pointer to the next factory.
60    static Factory*   first;                //!< A pointer to the first factory.
61};
62
63/**
64 *  a factory that is able to load any kind of Object
65   (this is a Functor)
66*/
67template<class T> class tFactory : public Factory
68{
69 public:
70  tFactory(const char* factoryName);
71  virtual ~tFactory();
72
73  private:
74  virtual BaseObject* fabricate(const TiXmlElement* root);
75};
76
77/**
78 *  construnts a factory with
79 * @param factoryName the name of the factory
80*/
81template<class T>
82tFactory<T>::tFactory(const char* factoryName) : Factory(factoryName)
83{
84  PRINTF(5)("Class: %s loadable\n", this->getName());
85}
86
87
88template<class T>
89tFactory<T>::~tFactory()
90{}
91
92template<class T>
93BaseObject* tFactory<T>::fabricate(const TiXmlElement* root)
94{
95  if(!strcmp(root->Value(), this->getName()))
96    return new T ( root);
97  else if( getNext() != NULL)
98    return getNext()->fabricate( root);
99  else
100    return NULL;
101}
102
103#endif /* _FACTORY_H */
104
Note: See TracBrowser for help on using the repository browser.