oops
[supertux.git] / src / object_factory.hpp
1 //  $Id$
2 //
3 //  SuperTux -  A Jump'n Run
4 //  Copyright (C) 2004 Ricardo Cruz <rick2@aeiou.pt>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 #ifndef OBJECT_FACTORY_H
20 #define OBJECT_FACTORY_H
21
22 #include <string>
23 #include <map>
24
25 #include "lisp/lisp.hpp"
26 #include "game_object.hpp"
27 #include "math/vector.hpp"
28
29 class Factory
30 {
31 public:
32   virtual ~Factory()
33   { }
34     
35   /** Creates a new gameobject from a lisp node.
36    * Remember to delete the objects later
37    */
38   virtual GameObject* create_object(const lisp::Lisp& reader) = 0;
39 };
40
41 typedef std::map<std::string, Factory*> Factories;
42 extern Factories* object_factories;
43
44 GameObject* create_object(const std::string& name, const lisp::Lisp& reader);
45 GameObject* create_object(const std::string& name, const Vector& pos);
46
47 /** comment from Matze:
48  * Yes I know macros are evil, but in this specific case they save
49  * A LOT of typing and evil code duplication.
50  * I'll happily accept alternatives if someone can present me one that does
51  * not involve typing 4 or more lines for each object class
52  */
53 #define IMPLEMENT_FACTORY(CLASS, NAME)                            \
54 class INTERN_##CLASS##Factory : public Factory                    \
55 {                                                                 \
56 public:                                                           \
57   INTERN_##CLASS##Factory()                                       \
58   {                                                               \
59     if(object_factories == 0)                                     \
60       object_factories = new Factories;                           \
61                                                                   \
62     object_factories->insert(std::make_pair(NAME, this));         \
63   }                                                               \
64                                                                   \
65   virtual GameObject* create_object(const lisp::Lisp& reader)     \
66   {                                                               \
67     return new CLASS(reader);                                     \
68   }                                                               \
69 };                                                                \
70 static INTERN_##CLASS##Factory factory_##CLASS;
71
72 #endif