00001 //###################################################################### 00002 // 00003 // GraspIt! 00004 // Copyright (C) 2002-2009 Columbia University in the City of New York. 00005 // All rights reserved. 00006 // 00007 // GraspIt! is free software: you can redistribute it and/or modify 00008 // it under the terms of the GNU General Public License as published by 00009 // the Free Software Foundation, either version 3 of the License, or 00010 // (at your option) any later version. 00011 // 00012 // GraspIt! is distributed in the hope that it will be useful, 00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 // GNU General Public License for more details. 00016 // 00017 // You should have received a copy of the GNU General Public License 00018 // along with GraspIt!. If not, see <http://www.gnu.org/licenses/>. 00019 // 00020 // Author(s): Andrew T. Miller and Matei T. Ciocarlie 00021 // 00022 // $Id: worldElement.h,v 1.17 2009/06/18 20:41:02 saint Exp $ 00023 // 00024 //###################################################################### 00025 00026 00027 #ifndef WORLD_ELEMENT_FACTORY_H 00028 #define WORLD_ELEMENT_FACTORY_H 00029 00030 #include <string> 00031 #include <map> 00032 00033 class World; 00034 class WorldElement; 00035 00037 class WorldElementCreator 00038 { 00039 public: 00040 virtual WorldElement* operator() (World* parent, const char* name) = 0; 00041 }; 00042 00044 template <class E> 00045 class SimpleWorldElementCreator : public WorldElementCreator 00046 { 00047 public: 00048 virtual WorldElement* operator() (World* parent, const char* name) 00049 { 00050 return new E(parent, name); 00051 } 00052 }; 00053 00055 00058 class WorldElementFactory 00059 { 00060 private: 00062 std::map<std::string, WorldElementCreator*> mCreators; 00063 00064 public: 00066 WorldElementFactory(){} 00067 00069 ~WorldElementFactory(); 00070 00072 WorldElement* createElement(std::string elementType, World *parent,const char *name); 00073 00075 void registerCreator(std::string elementType, WorldElementCreator *creator); 00076 00078 static void registerBuiltinCreators(); 00079 }; 00080 00082 inline WorldElementFactory& getWorldElementFactory() 00083 { 00084 static WorldElementFactory wef; 00085 return wef; 00086 } 00087 00089 #define REGISTER_CREATOR(STRINGNAME,CLASSNAME) getWorldElementFactory().registerCreator( STRINGNAME, new SimpleWorldElementCreator<CLASSNAME>() ); 00090 00091 #endif