ctkPimpl.h

Go to the documentation of this file.
00001 
00048 #ifndef __ctkPimpl_h
00049 #define __ctkPimpl_h
00050 
00051 // Qt includes
00052 #include <QtGlobal>
00053 
00059 #define CTK_CONSTRUCTOR_NO_ARG_CXX(PUB)  \
00060   PUB::PUB()                             \
00061     {                                    \
00062     CTK_INIT_PRIVATE(PUB);               \
00063     }
00064 
00070 #define CTK_CONSTRUCTOR_1_ARG_CXX(PUB, _ARG1)   \
00071   PUB::PUB(_ARG1 _parent) :                     \
00072     Superclass( _parent )                       \
00073     {                                           \
00074     CTK_INIT_PRIVATE(PUB);                      \
00075     }
00076     
00084 #define CTK_SET_CXX(PUB, _TYPE, _NAME, _VARNAME)    \
00085   void PUB::_NAME(_TYPE var)                        \
00086   {                                                 \
00087     ctk_d.ref()._VARNAME =  var;                    \
00088   }
00089 
00097 #define CTK_GET_CXX(PUB, _TYPE, _NAME, _VARNAME)   \
00098   _TYPE PUB::_NAME()const                          \
00099   {                                                \
00100     return ctk_d.ref()._VARNAME;                   \
00101   }
00102 
00109 #define CTK_DECLARE_PRIVATE(PUB)                                          \
00110 friend class PUB##Private; ctkPrivateInterface<PUB, PUB##Private> ctk_d;  \
00111 typedef PUB Self;
00112 
00118 #define CTK_DECLARE_PUBLIC(PUB) friend class PUB;
00119 
00125 #define CTK_INIT_PRIVATE(PUB) ctk_d.setPublic(this)
00126 
00131 #define CTK_D(PUB) PUB##Private* d = ctk_d()
00132 #define CTK_D_REF(PUB) PUB##Private& d = ctk_d.ref()
00133 
00138 #define CTK_P(PUB) PUB* p = ctk_p()
00139 #define CTK_P_REF(PUB) PUB& p = ctk_p_ref()
00140 
00141 #ifdef CTK_DOXYGEN_RUN
00142 
00147 ctkPrivate<PUB>* ctk_d();
00148 
00155 const ctkPrivate<PUB>* ctk_d();
00156 
00162 PUB& ctk_p_ref();
00163 PUB* ctk_p();
00164 
00171 const PUB& ctk_p_ref();
00172 const PUB* ctk_p();
00173 #endif
00174 
00175 template <typename PUB>
00176 class ctkPrivate
00177 {
00178 public:
00179   virtual ~ctkPrivate(){}
00180   inline void CTK_setPublic(PUB* pub)
00181     {
00182     Q_ASSERT(pub);
00183     ctk_p_ptr = pub;
00184     }
00185 
00186 protected:
00187   inline PUB& ctk_p_ref()
00188     {
00189     Q_ASSERT(this->ctk_p_ptr);
00190     return *this->ctk_p_ptr;
00191     }
00192   inline const PUB& ctk_p_ref() const
00193     {
00194     Q_ASSERT(this->ctk_p_ptr);
00195     return *this->ctk_p_ptr;
00196     }
00197 
00198   inline PUB* ctk_p()
00199     {
00200     Q_ASSERT(this->ctk_p_ptr);
00201     return this->ctk_p_ptr;
00202     }
00203   inline const PUB* ctk_p() const
00204     {
00205     Q_ASSERT(this->ctk_p_ptr);
00206     return this->ctk_p_ptr;
00207     }
00208 
00209 private:
00210   PUB* ctk_p_ptr;
00211 };
00212 
00213 template <typename PUB, typename PVT>
00214 class ctkPrivateInterface
00215 {
00216   friend class ctkPrivate<PUB>;
00217 public:
00218   ctkPrivateInterface()
00219     {
00220     this->pvt = new PVT;
00221     }
00222   ~ctkPrivateInterface()
00223     {
00224     delete this->pvt;
00225     }
00226 
00227   inline void setPublic(PUB* pub)
00228     {
00229     Q_ASSERT(pub);
00230     this->pvt->CTK_setPublic(pub);
00231     }
00232   inline PVT& ref()
00233     {
00234     return *static_cast<PVT*>(this->pvt);
00235     }
00236   inline const PVT& ref() const
00237     {
00238     return *static_cast<PVT*>(this->pvt);
00239     }
00240   inline PVT* operator()()
00241     {
00242     return static_cast<PVT*>(this->pvt);
00243     }
00244   inline const PVT* operator()() const
00245     {
00246     return static_cast<PVT*>(this->pvt);
00247     }
00248 private:
00249   ctkPrivate<PUB>* pvt;
00250 };
00251 
00252 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines