Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

TypeFactory.h

Go to the documentation of this file.
00001 // -*- C++ -*- 00002 00003 // TypeFactory.cc 00004 // Copyright (c) 2001 by Nicolas Chapados 00005 // Copyright (c) 2003 Pascal Vincent 00006 00007 // Redistribution and use in source and binary forms, with or without 00008 // modification, are permitted provided that the following conditions are met: 00009 // 00010 // 1. Redistributions of source code must retain the above copyright 00011 // notice, this list of conditions and the following disclaimer. 00012 // 00013 // 2. Redistributions in binary form must reproduce the above copyright 00014 // notice, this list of conditions and the following disclaimer in the 00015 // documentation and/or other materials provided with the distribution. 00016 // 00017 // 3. The name of the authors may not be used to endorse or promote 00018 // products derived from this software without specific prior written 00019 // permission. 00020 // 00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00024 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00025 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00026 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00027 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00028 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00029 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00030 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00031 // 00032 // This file is part of the PLearn library. For more information on the PLearn 00033 // library, go to the PLearn Web site at www.plearn.org 00034 00037 #ifndef TYPEFACTORY_H 00038 #define TYPEFACTORY_H 00039 00040 #include <string> 00041 #include <map> 00042 //#include <vector> 00043 #include "OptionBase.h" 00044 00045 namespace PLearn { 00046 using namespace std; 00047 00048 // Predeclarations 00049 class Object; 00050 00053 typedef Object* (*NEW_OBJECT)(); 00054 typedef OptionList& (*GETOPTIONLIST_METHOD)(); 00055 typedef bool (*ISA_METHOD)(Object* o); 00056 00057 class TypeMapEntry 00058 { 00059 public: 00060 string type_name; 00061 string parent_class; // name of parent class 00062 NEW_OBJECT constructor; 00063 GETOPTIONLIST_METHOD getoptionlist_method; 00064 ISA_METHOD isa_method; 00065 string one_line_descr; 00066 string multi_line_help; 00067 00068 TypeMapEntry(const string& the_type_name, 00069 const string& the_parent_class="", 00070 NEW_OBJECT the_constructor=0, 00071 GETOPTIONLIST_METHOD the_getoptionlist_method=0, 00072 ISA_METHOD the_isa_method=0, 00073 const string& the_one_line_descr = "", 00074 const string& the_multi_line_help = "") 00075 :type_name(the_type_name), 00076 parent_class(the_parent_class), 00077 constructor(the_constructor), 00078 getoptionlist_method(the_getoptionlist_method), 00079 isa_method(the_isa_method), 00080 one_line_descr(the_one_line_descr), 00081 multi_line_help(the_multi_line_help) 00082 {} 00083 }; 00084 00085 typedef map<string,TypeMapEntry> TypeMap; 00086 00087 00088 //########################### CLASS TYPEFACTORY ############################ 00090 00091 class TypeFactory 00092 { 00093 protected: 00094 TypeMap type_map_; 00095 00096 public: 00097 // Default constructor, destructor, etc. 00098 00100 static void register_type(const string& type_name, 00101 const string& parent_class, 00102 NEW_OBJECT constructor, 00103 GETOPTIONLIST_METHOD getoptionlist_method, 00104 ISA_METHOD isa_method, 00105 const string& one_line_descr, 00106 const string& multi_line_help); 00107 00109 void registerType(const TypeMapEntry& entry); 00110 00112 void unregisterType(string type_name); 00113 00115 bool isRegistered(string type_name) const; 00116 00119 Object* newObject(string type_name) const; 00120 00123 bool isAbstract(string type_name) const; 00124 00125 const TypeMap& getTypeMap() const 00126 { return type_map_; } 00127 00129 static TypeFactory& instance(); 00130 00131 }; 00132 00133 00135 void displayObjectHelp(ostream& out, const string& classname); 00136 00137 00138 } // end of namespace PLearn 00139 00140 #endif

Generated on Tue Aug 17 16:09:14 2004 for PLearn by doxygen 1.3.7