DumontEXE 0.0.1
metapointer.h
00001 #ifndef METAPOINTER_H_
00002 #define METAPOINTER_H_
00003 
00004 /*
00005 ** contributed by wz in #qt on freenode iirc Feb 18, 2008
00006 **
00007 ** <wz> This is a pointer wrapper that you can connect signals and slots to. 
00008 **  It will forward those to the pointer if set. 
00009 **  http://qtnode.net/index.php/pastebin/5445
00010 **
00011 ** <wz> It's use-case is something like a MDI window, you can connect all 
00012 **  menus to the MetaPointer, and then set always set the active window.
00013 **
00014 ** <wz> aep, IIRC you helped me a lot with it, so maybe you'd like it for 
00015 **  Qxt (now that I found a useful use-case).
00016 **
00017 ** <wz> It's far more elegant than the signal mapping done in the MDI 
00018 **  example, at least.
00019 */
00020 
00021 #ifndef Q_MOC_RUN
00022 
00023 #include <QObject>
00024 #include <QPointer>
00025 #include <QMetaObject>
00026 #include <QMetaMethod>
00027 #include <QList>
00028 
00029 template<class T>
00030 class MetaPointer : public QObject
00031 {
00032 public:
00033     MetaPointer(T* ptr = 0)
00034     {
00035         setPointer(ptr);
00036         
00037         setupMetaObject();
00038     }
00039     
00040     bool isNull() const
00041     {
00042         return p.isNull();
00043     }
00044     
00045     T* pointer() const
00046     {
00047         return p;
00048     }
00049     
00050     void setPointer(T* ptr)
00051     {
00052         if(p)
00053             foreach(int id, signalIds)
00054                 QMetaObject::disconnect(p, id, this, id);
00055         
00056         p = ptr;
00057         
00058         if(p)
00059             foreach(int id, signalIds)
00060                 QMetaObject::connect(p, id, this, id);
00061     }
00062     
00063     T& operator*()
00064     {
00065         return *p;
00066     }
00067     
00068     T* operator->() const
00069     {
00070         return p;
00071     }
00072     
00073     MetaPointer<T>& operator=(const MetaPointer<T>& r_ptr)
00074     {
00075         setPointer(r_ptr.p);
00076         
00077         return *this;
00078     }
00079     
00080     MetaPointer<T>& operator=(T* ptr)
00081     {
00082         setPointer(ptr);
00083         
00084         return *this;
00085     }
00086     
00087     operator const bool() const
00088     {
00089         return (bool)p;
00090     }
00091     
00092     operator bool()
00093     {
00094         return (bool)p;
00095     }
00096     
00097     operator MetaPointer<T>*()
00098     {
00099         return this;
00100     }
00101     
00102     operator const MetaPointer<T>*() const
00103     {
00104         return this;
00105     }
00106     
00107 private:
00108     QPointer<T> p;
00109     
00110 // begin Qt MetaObject System
00111 public:
00112     void setupMetaObject()
00113     {
00114         for(int i = 0; i < T::staticMetaObject.methodCount(); ++i)
00115         {
00116             QMetaMethod m = T::staticMetaObject.method(i);
00117             if(m.methodType() == QMetaMethod::Signal)
00118                 signalIds.append(i);
00119         }
00120     }
00121     
00122     const QMetaObject* metaObject() const
00123     {
00124         return &T::staticMetaObject;
00125     }
00126 
00127     void* qt_metacast(const char *_clname)
00128     {
00129         if(!_clname) return 0;
00130         if(p) return p->qt_metacast(_clname);
00131         return QObject::qt_metacast(_clname);
00132     }
00133 
00134     int qt_metacall(QMetaObject::Call _c, int _id, void **_a)
00135     {
00136         if(sender() == p && p)
00137         {
00138             QMetaObject::activate(this, _id, _a);
00139             return -1;
00140         }
00141         
00142         if(_id < T::staticMetaObject.methodCount())
00143         {
00144             if(p) return p->qt_metacall(_c, _id, _a);
00145             return _id;
00146         }
00147         
00148         _id -= T::staticMetaObject.methodCount();
00149         
00150         if(_c == QMetaObject::InvokeMetaMethod)
00151         {
00152             switch(_id)
00153             {
00154                 case 0: setPointer((*reinterpret_cast<T*(*)>(_a[1]))); break;
00155             }
00156             _id -= 1;
00157         }
00158         
00159         return _id;
00160     }
00161     
00162 private:
00163     QList<int> signalIds;
00164 // end Qt MetaObject System
00165 };
00166 
00167 #endif /*Q_MOC_RUN*/
00168 
00169 #endif /*METAPOINTER_H_*/
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties




~ ~ ~ ~ ~ ~
Source Code without Comments is like a Cranberry Garland
without the berries. Comment your Code!
 
Commence Database User Support Group Forum
http://newsgroup.showoff-db.org/
~ ~ ~ ~ ~ ~
Author: Mark Petryk
Lorimark Solutions, LLC
mark@lorimarksolutions.com