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