Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5242 was 5156, checked in by bensch, 19 years ago

orxonox/trunk: no more segfaults wenn creating entities

File size: 2.8 KB
RevLine 
[4597]1/*
[4250]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
[4597]16/*!
[4885]17 * @file factory.h
18 * @brief A loadable object handler
[3940]19*/
20
[4250]21
[3940]22#ifndef _FACTORY_H
23#define _FACTORY_H
24
25class BaseObject;
26
[4239]27#include "tinyxml.h"
[4597]28#include "base_object.h"
[4171]29#include "debug.h"
[3940]30
[4597]31/**
[4885]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)
[4004]34*/
[4885]35#define CREATE_FACTORY(CLASS_NAME) \
36    tFactory<CLASS_NAME>* global_##CLASS_NAME##_Factory = new tFactory<CLASS_NAME>(#CLASS_NAME)
[4487]37
38//! The Factory is a loadable object handler
[4597]39class Factory : public BaseObject {
[3940]40
41 public:
[4487]42  Factory (const char* factoryName = NULL);
[5156]43  virtual ~Factory ();
[3940]44
[5155]45  void fabricate(const char* className, const char* entityName);
[4730]46  virtual BaseObject* fabricate(const TiXmlElement* root) = NULL;
[5156]47  virtual BaseObject* fabricateDirect() = NULL;
[4739]48
49  static void registerFactory( Factory* factory);
[4836]50  /** @returns the first factory */
[4739]51  static Factory* getFirst() { return Factory::first; };
[4597]52
[4932]53  protected:
54    /** sets the Next factory in the list @param nextFactory the next factory */
55    inline void setNext( Factory* nextFactory) { this->next = nextFactory; };
56    /** @returns the next factory */
57    Factory* getNext() const { return this->next; };
58
[4739]59  private:
60    Factory*          next;                 //!< pointer to the next factory.
61    static Factory*   first;                //!< A pointer to the first factory.
[3940]62};
63
[4487]64/**
[4836]65 *  a factory that is able to load any kind of Object
[4487]66   (this is a Functor)
67*/
[4004]68template<class T> class tFactory : public Factory
69{
70 public:
[4487]71  tFactory(const char* factoryName);
[4004]72  virtual ~tFactory();
[4597]73
[4004]74  private:
[4730]75  virtual BaseObject* fabricate(const TiXmlElement* root);
[5156]76  virtual BaseObject* fabricateDirect();
[4004]77};
78
[4487]79/**
[4836]80 *  construnts a factory with
81 * @param factoryName the name of the factory
[4487]82*/
[4004]83template<class T>
[4487]84tFactory<T>::tFactory(const char* factoryName) : Factory(factoryName)
[4004]85{
[4730]86  PRINTF(5)("Class: %s loadable\n", this->getName());
[4004]87}
88
[4597]89
[4004]90template<class T>
91tFactory<T>::~tFactory()
92{}
93
94template<class T>
[4597]95BaseObject* tFactory<T>::fabricate(const TiXmlElement* root)
96{
[5156]97  if (root == NULL)
98    return NULL;
99
[4730]100  if(!strcmp(root->Value(), this->getName()))
[4004]101    return new T ( root);
[4597]102  else if( getNext() != NULL)
[4004]103    return getNext()->fabricate( root);
[4597]104  else
[4004]105    return NULL;
106}
107
[5156]108template<class T>
109    BaseObject* tFactory<T>::fabricateDirect()
110{
111  return new T((const TiXmlElement*)NULL);
112}
113
[3940]114#endif /* _FACTORY_H */
115
Note: See TracBrowser for help on using the repository browser.