- Your Widget Set For OpenGL
Main Page | Namespace List | Class Hierarchy | Class List | File List | Class Members | Related Pages

uobject.hpp

00001 /*************************************************************************** 00002 LibUFO - UI For OpenGL 00003 copyright : (C) 2001-2005 by Johannes Schmidt 00004 email : schmidtjf at users.sourceforge.net 00005 ------------------- 00006 00007 file : include/ufo/uobject.hpp 00008 begin : Tue May 8 2001 00009 $Id: uobject.hpp,v 1.17 2005/09/16 12:39:16 schmidtjf Exp $ 00010 ***************************************************************************/ 00011 00012 /*************************************************************************** 00013 * This library is free software; you can redistribute it and/or * 00014 * modify it under the terms of the GNU Lesser General Public * 00015 * License as published by the Free Software Foundation; either * 00016 * version 2.1 of the License, or (at your option) any later version. * 00017 * * 00018 * This library is distributed in the hope that it will be useful, * 00019 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00020 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00021 * Lesser General Public License for more details. * 00022 * * 00023 * You should have received a copy of the GNU Lesser General Public * 00024 * License along with this library; if not, write to the Free Software * 00025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * 00026 ***************************************************************************/ 00027 00028 #ifndef UOBJECT_HPP 00029 #define UOBJECT_HPP 00030 00031 #include "ufo_global.hpp" 00032 00033 #include "ucollectable.hpp" 00034 00035 #include "signals/usignal.hpp" 00036 00037 #include <string> 00038 #include <iostream> 00039 00040 #include <list> 00041 00042 // UFO RTTI is mainly used for debugging purposes 00043 00044 #ifdef UFO_RTTI 00045 00046 namespace ufo { 00047 class UObject; 00048 typedef UObject *(*UObjectConstructorFn)(void); 00049 } // namespace ufo 00050 00051 // 00052 // UFO RTTI declaration 00053 // 00054 00055 #define UFO_DECLARE_CLASS(name) \ 00056 public: \ 00057 static ufo::UClassInfo sm_class##name; \ 00058 virtual ufo::UClassInfo * getClassInfo() const \ 00059 { return &name::sm_class##name; } 00060 00061 // 00062 // UFO RTTI implementation 00063 // 00064 00065 // dynamic classes 00066 00067 // implements rtti for classes with default constructor 00068 /* 00069 #define UFO_IMPLEMENT_CLASS(name, basename) \ 00070 ufo::UObject * UConstructorFor##name() \ 00071 { return new name; } \ 00072 ufo::UClassInfo name::sm_class##name( \ 00073 #name, \ 00074 #basename, \ 00075 (unsigned int) sizeof(name), \ 00076 (ufo::UObjectConstructorFn) UConstructorFor##name); 00077 */ 00078 00079 #define UFO_IMPLEMENT_CLASS(name, basename) \ 00080 ufo::UClassInfo name::sm_class##name( \ 00081 #name, \ 00082 #basename, \ 00083 (unsigned int) sizeof(name), \ 00084 (ufo::UObjectConstructorFn) NULL); 00085 00086 // 00087 // class info structure 00088 // 00089 00090 // FIXME hash map would be better 00091 #include <map> 00092 namespace ufo { 00093 00094 class UFO_EXPORT UClassInfo { 00095 public: 00096 UClassInfo( 00097 const std::string & className, 00098 const std::string & baseName, 00099 unsigned int size, 00100 UObjectConstructorFn ctor) 00101 : m_className(className) 00102 , m_baseClassName(baseName) 00103 , m_objectSize(size) 00104 , m_objectConstructor(ctor) 00105 , m_baseInfo(NULL) 00106 , m_next(sm_first) 00107 { 00108 sm_first = this; 00109 } 00110 00111 ~UClassInfo(); 00112 00113 UObject * createObject() const { 00114 return m_objectConstructor ? (*m_objectConstructor)() : NULL; 00115 } 00116 00117 const std::string & getClassName() const { return m_className; } 00118 const std::string & getBaseClassName() const { return m_baseClassName; } 00119 const UClassInfo * getBaseClassInfo() const { return m_baseInfo; } 00120 unsigned int getSize() const { return m_objectSize; } 00121 00122 UObjectConstructorFn getConstructor() const { return m_objectConstructor; } 00123 static const UClassInfo * getFirst() { return sm_first; } 00124 const UClassInfo * getNext() const { return m_next; } 00125 00126 static UClassInfo * findClass(const std::string & className); 00127 00130 static void initClassInfo(); 00131 00132 private: 00133 std::string m_className; 00134 std::string m_baseClassName; 00135 unsigned int m_objectSize; 00136 UObjectConstructorFn m_objectConstructor; 00137 00138 // Pointers to base UClassInfos: set in InitializeClasses 00139 const UClassInfo * m_baseInfo; 00140 00141 // a simple single linked list for all class infos 00142 static UClassInfo * sm_first; 00143 UClassInfo * m_next; 00144 00145 static std::map<std::string, UClassInfo*> sm_classTable; 00146 00147 UFO_DECLARE_NO_COPY_CLASS(UClassInfo) 00148 }; 00149 00150 } // namespace ufo 00151 00152 #else // UFO_RTTI 00153 00154 #define UFO_DECLARE_CLASS(name) 00155 #define UFO_IMPLEMENT_CLASS(name, basename) 00156 00157 #endif // !UFO_RTTI 00158 00159 00160 #define UFO_DECLARE_DYNAMIC_CLASS(name) UFO_DECLARE_CLASS(name) 00161 #define UFO_DECLARE_ABSTRACT_CLASS(name) UFO_DECLARE_CLASS(name) 00162 00163 #define UFO_IMPLEMENT_DYNAMIC_CLASS(name, basename) UFO_IMPLEMENT_CLASS(name, basename) 00164 #define UFO_IMPLEMENT_DEFAULT_DYNAMIC_CLASS(name, basename) UFO_IMPLEMENT_CLASS(name, basename) 00165 #define UFO_IMPLEMENT_ABSTRACT_CLASS(name, basename) UFO_IMPLEMENT_CLASS(name, basename) 00166 #define UFO_IMPLEMENT_CTOR_DYNAMIC_CLASS(name, basename) UFO_IMPLEMENT_CLASS(name, basename) 00167 00168 namespace ufo { 00169 00170 struct UObjectSlotNode; 00171 00178 class UFO_EXPORT UObject : public virtual UCollectable { 00179 UFO_DECLARE_CLASS(UObject) 00180 friend struct UObjectSlotNode; 00181 public: 00182 UObject(); 00183 UObject(const UObject &); 00184 virtual ~UObject(); 00185 00193 virtual unsigned int hashCode() const; 00194 00197 virtual bool equals(const UObject * obj) const; 00198 00199 // FIXME 00200 // are there virtual operators? guess not. 00201 bool operator==(const UObject & obj) const; 00202 00204 virtual UObject * clone() const; 00205 00206 // 00207 // output 00208 // 00209 00211 virtual std::string toString() const; 00212 00214 friend std::ostream & operator<<(std::ostream & os, const UObject & o); 00216 friend std::ostream & operator<<(std::ostream & os, const UObject * o); 00217 00218 // 00219 // debugging 00220 // 00221 00222 const std::string & getName() const; 00224 void setName(const std::string & newNameA); 00225 00230 unsigned int objCount(); 00231 00235 00244 UCollectable * trackPointer(UCollectable * c); 00253 const UCollectable * trackPointer(const UCollectable * c); 00254 00262 bool releasePointer(UCollectable * c); 00270 bool releasePointer(const UCollectable * c); 00271 00276 void swapPointers(const UCollectable * oldObj, const UCollectable * newObj); 00277 00281 void releaseAllPointers(); 00282 00283 protected: // Protected methods 00287 virtual std::ostream & paramString(std::ostream & os) const; 00288 private: 00293 std::string m_name; 00294 00295 std::list<const UCollectable*> m_pointers; 00296 std::list<UObjectSlotNode*> m_objectSlots; 00297 00298 public: // Public Signals 00303 USignal1<UObject*> & sigDestroyed(); 00304 00305 private: // Private Signals 00306 USignal1<UObject*> m_sigDestroyed; 00307 }; 00308 00309 // 00310 // inline implemenation 00311 // 00312 00313 inline bool 00314 UObject::operator==(const UObject & obj) const { 00315 return equals(&obj); 00316 } 00317 00318 inline std::ostream & operator<<(std::ostream & os, const UObject & o) { 00319 return os << o.toString(); 00320 } 00321 00322 inline std::ostream & operator<<(std::ostream & os, const UObject * o) { 00323 if (o) { 00324 return os << o->toString(); 00325 } else { 00326 return os << "NULL"; 00327 } 00328 } 00329 00330 inline const std::string & 00331 UObject::getName() const { 00332 return m_name; 00333 } 00334 00335 inline void 00336 UObject::setName(const std::string & newNameA) { 00337 m_name = newNameA; 00338 } 00339 00340 // signals 00341 inline USignal1<UObject*> & 00342 UObject::sigDestroyed() { 00343 return m_sigDestroyed; 00344 } 00345 00346 } // namespace ufo 00347 00348 #endif // UOBJECT_HPP

The libUFO Project - written by Johannes Schmidt