ctkPimpl.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**
  2. \page CorePimpl CTK Pimpl Macros
  3. \brief Utility macros for handling private implementations. It is in addition
  4. to QtGlobal: Q_DECLARE_PRIVATE, Q_DECLARE_PUBLIC,
  5. Q_D and Q_Q.
  6. Application code generally doesn't have to be concerned about hiding its
  7. implementation details, but when writing library code it is important to
  8. maintain a constant interface, both source and binary. Maintaining a constant
  9. source interface is easy enough, but keeping the binary interface constant
  10. means moving implementation details into a private class. The PIMPL, or
  11. d-pointer, idiom is a common method of implementing this separation. ctkPimpl
  12. offers a convenient way to connect the public and private sides of your class.
  13. \section start Getting Started
  14. Before you declare the public class, you need to make a forward declaration
  15. of the private class. The private class must have the same name as the public
  16. class, followed by the word Private.
  17. \subsection pub The Public Class
  18. Generally, you shouldn't keep any data members in the public class without a
  19. good reason. Functions that are part of the public interface should be declared
  20. in the public class, and functions that need to be available to subclasses (for
  21. calling or overriding) should be in the protected section of the public class.
  22. To connect the private class to the public class, include the
  23. Q_DECLARE_PRIVATE macro in the private section of the public class.
  24. Additionally, you must construct the d_ptr pointer in the constructor of the
  25. public class.
  26. \subsection priv The Private Class
  27. As mentioned above, data members should usually be kept in the private class.
  28. This allows the memory layout of the private class to change without breaking
  29. binary compatibility for the public class. Functions that exist only as
  30. implementation details, or functions that need access to private data members,
  31. should be implemented here.
  32. To define the private class, nothing special needs to be done, except if you
  33. want the private class to have access to the public class. Then use
  34. Q_DECLARE_PUBLIC and create a public class pointer member. The constructor of
  35. the private class should take a public class reference as a parameter.
  36. \section cross Accessing Private Members
  37. Use the Q_D() macros from functions in
  38. the public class to access the private class. Similarly, functions in the
  39. private class can invoke functions in the public class by using the Q_Q()
  40. macro.
  41. \section example Example
  42. Header file (ctkFooObject.h):
  43. \code
  44. // Qt includes
  45. #include <QObject>
  46. // CTK includes
  47. #include "ctkCoreExport.h"
  48. class ctkFooObjectPrivate;
  49. class CTK_CORE_EXPORT ctkFooObject: public QObject
  50. {
  51. public:
  52. ctkFooObject(QObject* parent = 0);
  53. virtual ~ctkFooObject();
  54. void setProperty(double property);
  55. double property()const;
  56. protected:
  57. QScopedPointer<ctkFooObjectPrivate> d_ptr;
  58. private:
  59. Q_DECLARE_PRIVATE(ctkFooObject);
  60. Q_DISABLE_COPY(ctkFooObject);
  61. };
  62. \endcode
  63. Implementation file (ctkFooObject.cpp):
  64. \code
  65. // CTK includes
  66. #include "ctkFooObject.h"
  67. class ctkFooObjectPrivate
  68. {
  69. public:
  70. void processSomething();
  71. double MyProperty;
  72. };
  73. ctkFooObject::ctkFooObject(QObject* parentObject)
  74. : d_ptr(new ctkFooObjectPrivate)
  75. {
  76. Q_D(ctkFooObject);
  77. d->MyProperty = 10.;
  78. }
  79. void ctkFooObject::setProperty(double newProperty)
  80. {
  81. Q_D(ctkFooObject);
  82. d->MyProperty = newProperty;
  83. }
  84. double ctkFooObject::property()const
  85. {
  86. Q_D(const ctkFooObject);
  87. return d->MyProperty;
  88. }
  89. \endcode
  90. */
  91. #ifndef __ctkPimpl_h
  92. #define __ctkPimpl_h
  93. // Qt includes
  94. #include <QtGlobal>
  95. /*!
  96. * \ingroup Core
  97. * @{
  98. */
  99. /*!
  100. * Define a public class constructor with no argument
  101. *
  102. * Also make sure the Pimpl is initalized
  103. * \see \ref CorePimpl
  104. */
  105. #define CTK_CONSTRUCTOR_NO_ARG_CPP(PUB) \
  106. PUB::PUB(): d_ptr(new PUB##Private) \
  107. { \
  108. }
  109. /*!
  110. * Define a public class constructor with one argument
  111. *
  112. * Also make sure the Pimpl is initalized
  113. * \see \ref CorePimpl
  114. */
  115. #define CTK_CONSTRUCTOR_1_ARG_CPP(PUB, _ARG1) \
  116. PUB::PUB(_ARG1 _parent) \
  117. : Superclass( _parent ) \
  118. , d_ptr(new PUB##Private) \
  119. { \
  120. }
  121. /*!
  122. * Define the setter in the public class.
  123. *
  124. * This should be put in the .cxx file of the public class. The parameter are
  125. * the name of the public class (PUB), the type of the argument to return (_TYPE),
  126. * the name of the getter(_NAME) and the name of the variable in the Private class(_VARNAME).
  127. * \see \ref CorePimpl
  128. */
  129. #define CTK_SET_CPP(PUB, _TYPE, _NAME, _VARNAME) \
  130. void PUB::_NAME(_TYPE var) \
  131. { \
  132. Q_D(PUB); \
  133. d->_VARNAME = var; \
  134. }
  135. /*!
  136. * Define the setter in the public class.
  137. *
  138. * This should be put in the .cxx file of the public class. The parameter are
  139. * the name of the public class (PUB), the type of the argument to return (_TYPE),
  140. * the name of the setter(_NAME) and the name of the variable in the Private class(_VARNAME).
  141. * \see \ref CorePimpl
  142. */
  143. #define CTK_GET_CPP(PUB, _TYPE, _NAME, _VARNAME) \
  144. _TYPE PUB::_NAME()const \
  145. { \
  146. Q_D(const PUB); \
  147. return d->_VARNAME; \
  148. }
  149. /**@}*/
  150. #endif