00001 #ifndef _QDBTSINGLETON_H
00002 #define _QDBTSINGLETON_H
00003
00004 #include <qmap.h>
00005 #include <qstring.h>
00006 #include <qstringlist.h>
00007
00008 #include <qdbt/qdbtglobal.h>
00009
00010 class QdbtBaseTabular;
00011
00012 class QDBT_EXPORT QdbtSingletonObject
00013 {
00014 public:
00015 virtual ~QdbtSingletonObject();
00016
00017 protected:
00018
00019 QdbtSingletonObject()
00020 {
00021 }
00022 };
00023
00024 class QDBT_EXPORT QdbtSingleton
00025 {
00026 class keyObject
00027 {
00028 public:
00029 keyObject() :
00030 _object( 0 ),
00031 _index( 0 )
00032 {
00033 }
00034 keyObject( const keyObject &obj ) :
00035
00036
00037 _object( obj._object ),
00038 _index( obj._index )
00039 {
00040 }
00041 keyObject & operator=( const keyObject &obj )
00042 {
00043
00044
00045 _object = obj._object;
00046 _index = obj._index;
00047 return *this;
00048 }
00049
00050 bool operator<( const keyObject &k ) const
00051 {
00052 return ( _object == k._object ?
00053 _index < k._index :
00054 _object < k._object );
00055 }
00056
00057 const QdbtBaseTabular *_object;
00058 int _index;
00059 };
00060
00061 static QStringList _indexLookup;
00062 static QMap< keyObject, QdbtSingletonObject * > _lookup;
00063
00064 public:
00065 static QdbtSingletonObject *checkPointer( const QdbtBaseTabular *object,
00066 const char *role );
00074 template < class T > static T checkPointer(
00075 const QdbtBaseTabular *object, const char *role )
00076 {
00077 T t = 0;
00078 try
00079 {
00080 t = dynamic_cast< T >( checkPointer( object, role ) );
00081 }
00082 catch ( ... )
00083 {
00084 }
00085 return t;
00086 }
00091 template < class T > static void checkPointer(
00092 const QdbtBaseTabular *object, const char *role, T result )
00093 {
00094 result = 0;
00095 try
00096 {
00097 result = dynamic_cast< T >( checkPointer( object, role ) );
00098 }
00099 catch ( ... )
00100 {
00101 }
00102 }
00103 static void setPointer( const QdbtBaseTabular *object,
00104 const char *role,
00105 QdbtSingletonObject *objectPointer );
00106 static void removePointers( const QdbtBaseTabular *object );
00107 static void removePointer( const QdbtSingletonObject *objectPointer );
00108 static void dump();
00109 };
00110
00111 #endif