Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: setClassID implemented in all files

File size: 2.6 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/*!
[4250]17  \file factory.h
[4487]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"
[4240]28#include "load_param.h"
[4597]29#include "base_object.h"
[4171]30#include "debug.h"
[3940]31
[4597]32/**
[4004]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)
[4597]35  \todo make factoryName a BaseObject-parameter. (else it would be redundant)
[4004]36*/
[4597]37#define CREATE_FACTORY(CLASS_NAME) tFactory<CLASS_NAME>* global_##CLASS_NAME##Factory = new tFactory<CLASS_NAME>(#CLASS_NAME)
[4487]38
39//! The Factory is a loadable object handler
[4597]40class Factory : public BaseObject {
[3940]41
42 public:
[4487]43  Factory (const char* factoryName = NULL);
[3940]44  ~Factory ();
45
[4597]46
[4253]47  virtual BaseObject* fabricate(const TiXmlElement* root);
[4003]48  void initialize();
49  void registerFactory( Factory* factory);
[4487]50  void setFactoryName(const char* factoryName);
51  /** \returns the name of the factory */
52  const char* getFactoryName() { return this->factoryName; };
53  /** \brief 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(void) const { return this->next; };
[4597]57
[4487]58 protected:
59  char*         factoryName;          //!< the name of the factory
[3940]60 private:
[4487]61  Factory*      next;                 //!< pointer to the next factory.
[3940]62};
63
[4487]64/**
65   \brief a factory that is able to load any kind of Object
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:
[4253]75  BaseObject* fabricate(const TiXmlElement* root);
[4004]76};
77
[4487]78/**
79   \brief construnts a factory with
80   \param factoryName the name of the factory
81*/
[4004]82template<class T>
[4487]83tFactory<T>::tFactory(const char* factoryName) : Factory(factoryName)
[4004]84{
[4487]85  PRINTF(5)("fileName: %s loadable\n", this->factoryName);
[4004]86}
87
[4597]88
[4004]89template<class T>
90tFactory<T>::~tFactory()
91{}
92
93template<class T>
[4597]94BaseObject* tFactory<T>::fabricate(const TiXmlElement* root)
95{
[4020]96  if(!strcmp(root->Value(), getFactoryName()))
[4004]97    return new T ( root);
[4597]98  else if( getNext() != NULL)
[4004]99    return getNext()->fabricate( root);
[4597]100  else
[4004]101    return NULL;
102}
103
[3940]104#endif /* _FACTORY_H */
105
Note: See TracBrowser for help on using the repository browser.