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

Class.hpp

Go to the documentation of this file.
00001 /* 00002 * Copyright (C) 2004 Andrew Sutton 00003 * 00004 * This library is free software; you can redistribute it and/or 00005 * modify it under the terms of the GNU Lesser General Public 00006 * License as published by the Free Software Foundation; either 00007 * version 2.1 of the License, or (at your option) any later version. 00008 * 00009 * This library is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 * Lesser General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU Lesser General Public 00015 * License along with this library; if not, write to: 00016 * 00017 * The Free Software Foundation, Inc. 00018 * 59 Temple Place, Suite 330 00019 * Boston, MA 02111-1307 USA 00020 * 00021 * Contact: 00022 * Andrew Sutton asutton@cs.kent.edu 00023 */ 00024 00025 #ifndef OMF_CLASS_HPP 00026 #define OMF_CLASS_HPP 00027 00028 // std includes 00029 #include <iostream> 00030 #include <string> 00031 #include <set> 00032 00033 // OMF includes 00034 #include <OMF/Set.hpp> 00035 00036 namespace OMF 00037 { 00038 // forward declarations 00039 class Object; 00040 00044 class Class 00045 { 00046 protected: 00047 Class() : 00048 _ofClass(), 00049 _ofType(), 00050 _name() 00051 {} 00052 00054 Class &init(const std::string &name) 00055 { 00056 if(_name.empty()) { 00057 _name = name; 00058 } 00059 return *this; 00060 } 00061 00062 public: 00063 virtual ~Class() 00064 { 00065 // we could clean up remaining instances, but the process 00066 // is going away 00067 } 00068 00072 const std::string &name() const 00073 { return _name; } 00074 00079 virtual Object *createInstance() 00080 { return 0; } 00081 00088 virtual void destroyInstance(Object *) 00089 {} 00090 00092 const OMF::Set &allOfClass() const 00093 { return _ofClass; } 00094 00096 const OMF::Set &allOfType() const 00097 { return _ofType; } 00098 00104 void addType(Object *type) 00105 { _ofType.insert(type); } 00106 00112 void removeType(Object *type) 00113 { _ofType.erase(type); } 00114 00115 protected: 00116 OMF::Set _ofClass; 00117 OMF::Set _ofType; 00118 00119 private: 00120 std::string _name; 00121 }; 00122 00128 struct Concrete 00129 { 00130 static const bool abstract = false; 00131 }; 00132 00138 struct Abstract 00139 { 00140 static const bool abstract = true; 00141 }; 00142 00143 00152 template <class Type, class Factory = Concrete> 00153 class BasicClass : public Class 00154 { 00155 typedef BasicClass<Type, Factory> Self; 00156 00157 protected: 00158 BasicClass() : 00159 Class() 00160 {} 00161 00162 public: 00163 virtual ~BasicClass() 00164 {} 00165 00172 static Self &instance(const std::string &name = "") 00173 { 00174 static Self _instance; 00175 return static_cast<Self &>(_instance.init(name)); 00176 } 00177 00183 Type *create() 00184 { 00185 Type *ret = 0; 00186 if(!Factory::abstract) { 00187 // create the object 00188 ret = new Type; 00189 00190 // add to the instance set 00191 _ofClass.add(ret); 00192 } 00193 return ret; 00194 } 00195 00201 void destroy(Object *instance) 00202 { 00203 _ofClass.remove(instance); 00204 } 00205 00210 virtual Object *createInstance() 00211 { 00212 return create(); 00213 } 00214 00215 virtual void destroyInstance(Object *instance) 00216 { 00217 destroy(instance); 00218 } 00219 }; 00220 } 00221 00222 #endif

Generated on Fri Sep 10 13:07:32 2004 for OpenModelingFramework by doxygen 1.3.8