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