src/supertux/object_factory.hpp

Go to the documentation of this file.
00001 //  SuperTux
00002 //  Copyright (C) 2004 Ricardo Cruz <rick2@aeiou.pt>
00003 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
00004 //
00005 //  This program is free software: you can redistribute it and/or modify
00006 //  it under the terms of the GNU General Public License as published by
00007 //  the Free Software Foundation, either version 3 of the License, or
00008 //  (at your option) any later version.
00009 //
00010 //  This program is distributed in the hope that it will be useful,
00011 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 //  GNU General Public License for more details.
00014 //
00015 //  You should have received a copy of the GNU General Public License
00016 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017 
00018 #ifndef HEADER_SUPERTUX_SUPERTUX_OBJECT_FACTORY_HPP
00019 #define HEADER_SUPERTUX_SUPERTUX_OBJECT_FACTORY_HPP
00020 
00021 #include <map>
00022 #include <assert.h>
00023 
00024 #include "supertux/direction.hpp"
00025 #include "util/reader_fwd.hpp"
00026 
00027 class Vector;
00028 class GameObject;
00029 
00030 class AbstractObjectFactory
00031 {
00032 public:
00033   virtual ~AbstractObjectFactory()
00034   { }
00035 
00039   virtual GameObject* create(const Reader& reader) = 0;
00040 };
00041 
00042 template<class C>
00043 class ConcreteObjectFactory : public AbstractObjectFactory
00044 {
00045 public:
00046   ConcreteObjectFactory() {}  
00047   ~ConcreteObjectFactory() {}
00048 
00049   GameObject* create(const Reader& reader)
00050   {
00051     return new C(reader);
00052   }
00053 };
00054 
00055 class ObjectFactory
00056 {
00057 public:
00058   static ObjectFactory& instance();
00059 
00060 private:
00061   typedef std::map<std::string, AbstractObjectFactory*> Factories;
00062   Factories factories;
00063 
00064 public:
00065   ObjectFactory();
00066   ~ObjectFactory();
00067 
00068   GameObject* create(const std::string& name, const Reader& reader);
00069   GameObject* create(const std::string& name, const Vector& pos, const Direction dir = AUTO);
00070 
00071 private:
00072   template<class C>
00073   void add_factory(const char* name)
00074   {
00075     assert(factories.find(name) == factories.end());
00076     factories[name] = new ConcreteObjectFactory<C>();
00077   }
00078   void init_factories();
00079 };
00080 
00081 #endif
00082 
00083 /* EOF */

Generated on Mon Apr 21 03:38:17 2014 for SuperTux by  doxygen 1.5.1