ctkPimpl.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. \brief Utility macros for handling private implementations. It is in addition
  3. to QtGlobal: Q_DECLARE_PRIVATE, Q_DECLARE_PUBLIC,
  4. Q_D and Q_Q.
  5. Application code generally doesn't have to be concerned about hiding its
  6. implementation details, but when writing library code it is important to
  7. maintain a constant interface, both source and binary. Maintaining a constant
  8. source interface is easy enough, but keeping the binary interface constant
  9. means moving implementation details into a private class. The PIMPL, or
  10. d-pointer, idiom is a common method of implementing this separation. ctkPimpl
  11. offers a convenient way to connect the public and private sides of your class.
  12. \section start Getting Started
  13. Before you declare the public class, you need to make a forward declaration
  14. of the private class. The private class must have the same name as the public
  15. class, followed by the word Private.
  16. \subsection pub The Public Class
  17. Generally, you shouldn't keep any data members in the public class without a
  18. good reason. Functions that are part of the public interface should be declared
  19. in the public class, and functions that need to be available to subclasses (for
  20. calling or overriding) should be in the protected section of the public class.
  21. To connect the private class to the public class, include the
  22. Q_DECLARE_PRIVATE macro in the private section of the public class.
  23. Additionally, you must construct the d_ptr pointer in the constructor of the
  24. public class.
  25. \subsection priv The Private Class
  26. As mentioned above, data members should usually be kept in the private class.
  27. This allows the memory layout of the private class to change without breaking
  28. binary compatibility for the public class. Functions that exist only as
  29. implementation details, or functions that need access to private data members,
  30. should be implemented here.
  31. To define the private class, nothing special needs to be done, except if you
  32. want the private class to have access to the public class. Then use
  33. Q_DECLARE_PUBLIC and create a public class pointer member. The constructor of
  34. the private class should take a public class reference as a parameter.
  35. \section cross Accessing Private Members
  36. Use the Q_D() macros from functions in
  37. the public class to access the private class. Similarly, functions in the
  38. private class can invoke functions in the public class by using the Q_Q()
  39. macro.
  40. */
  41. #ifndef __ctkPimpl_h
  42. #define __ctkPimpl_h
  43. // Qt includes
  44. #include <QtGlobal>
  45. /*!
  46. * Define a public class constructor with no argument
  47. *
  48. * Also make sure the Pimpl is initalized
  49. */
  50. #define CTK_CONSTRUCTOR_NO_ARG_CPP(PUB) \
  51. PUB::PUB(): d_ptr(new PUB##Private) \
  52. { \
  53. }
  54. /*!
  55. * Define a public class constructor with one argument
  56. *
  57. * Also make sure the Pimpl is initalized
  58. */
  59. #define CTK_CONSTRUCTOR_1_ARG_CPP(PUB, _ARG1) \
  60. PUB::PUB(_ARG1 _parent) \
  61. : Superclass( _parent ) \
  62. , d_ptr(new PUB##Private) \
  63. { \
  64. }
  65. /*!
  66. * Define the setter in the public class.
  67. *
  68. * This should be put in the .cxx file of the public class. The parameter are
  69. * the name of the public class (PUB), the type of the argument to return (_TYPE),
  70. * the name of the getter(_NAME) and the name of the variable in the Private class(_VARNAME).
  71. */
  72. #define CTK_SET_CPP(PUB, _TYPE, _NAME, _VARNAME) \
  73. void PUB::_NAME(_TYPE var) \
  74. { \
  75. Q_D(PUB); \
  76. d->_VARNAME = var; \
  77. }
  78. /*!
  79. * Define the setter in the public class.
  80. *
  81. * This should be put in the .cxx file of the public class. The parameter are
  82. * the name of the public class (PUB), the type of the argument to return (_TYPE),
  83. * the name of the setter(_NAME) and the name of the variable in the Private class(_VARNAME).
  84. */
  85. #define CTK_GET_CPP(PUB, _TYPE, _NAME, _VARNAME) \
  86. _TYPE PUB::_NAME()const \
  87. { \
  88. Q_D(const PUB); \
  89. return d->_VARNAME; \
  90. }
  91. #endif