/src/libtomahawk/thirdparty/kdsingleapplicationguard/pimpl_ptr.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 203 lines · 59 code · 23 blank · 121 comment · 0 complexity · c91aec68888812231bd3059d9a4fa0b7 MD5 · raw file

  1. #include "pimpl_ptr.h"
  2. /*!
  3. \class pimpl_ptr:
  4. \ingroup core smartptr
  5. \brief Owning pointer for private implementations
  6. \since_c 2.1
  7. (The exception safety of this class has not been evaluated yet.)
  8. pimpl_ptr is a smart immutable pointer, which owns the contained object. Unlike other smart pointers,
  9. it creates a standard constructed object when instanciated via the
  10. \link pimpl_ptr() standard constructor\endlink.
  11. Additionally, pimpl_ptr respects constness of the pointer object and returns \c const \c T* for
  12. a const pimpl_ptr object.
  13. The content of a pimpl_ptr cannot be changed during it's lifetime.
  14. \section general-use General Use
  15. The general use case of pimpl_ptr is the "Pimpl Idiom", i.e. hiding the private implementation of a class
  16. from the user's compiler which see \c MyClass as
  17. \code
  18. class MyClass
  19. {
  20. public:
  21. MyClass();
  22. ~MyClass();
  23. // public class API
  24. int value() const;
  25. private:
  26. class Private; // defined later
  27. kdtools::pimpl_ptr< Private > d;
  28. };
  29. \endcode
  30. but not the private parts of it. These can only be seen (and accessed) by the code knowing \c MyClass::Private:
  31. \code
  32. class MyClass::Private
  33. {
  34. public:
  35. int value;
  36. };
  37. MyClass::MyClass()
  38. {
  39. // d was automatically filled with new Private
  40. d->value = 42;
  41. }
  42. MyClass::~MyClass()
  43. {
  44. // the content of d gets deleted automatically
  45. }
  46. int MyClass::value() const
  47. {
  48. // access the private part:
  49. // since MyClass::value() is const, the returned pointee is const, too
  50. return d->value;
  51. }
  52. \endcode
  53. */
  54. /*!
  55. \fn pimpl_ptr::pimpl_ptr()
  56. Default constructor. Constructs a pimpl_tr that contains (owns) a standard constructed
  57. instance of \c T.
  58. \post \c *this owns a new object.
  59. */
  60. /*!
  61. \fn pimpl_ptr::pimpl_ptr( T * t )
  62. Constructor. Constructs a pimpl_ptr that contains (owns) \a t.
  63. \post get() == obj
  64. */
  65. /*!
  66. \fn pimpl_ptr::~pimpl_ptr()
  67. Destructor.
  68. \post The object previously owned by \c *this has been deleted.
  69. */
  70. /*!
  71. \fn const T * pimpl_ptr::get() const
  72. \returns a const pointer to the contained (owned) object.
  73. \overload
  74. */
  75. /*!
  76. \fn T * pimpl_ptr::get()
  77. \returns a pointer to the contained (owned) object.
  78. */
  79. /*!
  80. \fn const T & pimpl_ptr::operator*() const
  81. Dereference operator. Returns \link get() *get()\endlink.
  82. \overload
  83. */
  84. /*!
  85. \fn T & pimpl_ptr::operator*()
  86. Dereference operator. Returns \link get() *get()\endlink.
  87. */
  88. /*!
  89. \fn const T * pimpl_ptr::operator->() const
  90. Member-by-pointer operator. Returns get().
  91. \overload
  92. */
  93. /*!
  94. \fn T * pimpl_ptr::operator->()
  95. Member-by-pointer operator. Returns get().
  96. */
  97. #ifdef KDTOOLSCORE_UNITTESTS
  98. #include <kdunittest/test.h>
  99. #include <QObject>
  100. #include <QPointer>
  101. namespace
  102. {
  103. struct ConstTester
  104. {
  105. bool isConst()
  106. {
  107. return false;
  108. }
  109. bool isConst() const
  110. {
  111. return true;
  112. }
  113. };
  114. }
  115. KDAB_UNITTEST_SIMPLE( pimpl_ptr, "kdcoretools" ) {
  116. {
  117. kdtools::pimpl_ptr< QObject > p;
  118. assertNotNull( p.get() );
  119. assertNull( p->parent() );
  120. }
  121. {
  122. QPointer< QObject > o;
  123. {
  124. kdtools::pimpl_ptr< QObject > qobject( new QObject );
  125. o = qobject.get();
  126. assertEqual( o, qobject.operator->() );
  127. assertEqual( o, &(qobject.operator*()) );
  128. }
  129. assertNull( o );
  130. }
  131. {
  132. const kdtools::pimpl_ptr< QObject > qobject( new QObject );
  133. const QObject* o = qobject.get();
  134. assertEqual( o, qobject.operator->() );
  135. assertEqual( o, &(qobject.operator*()) );
  136. }
  137. {
  138. kdtools::pimpl_ptr< QObject > o1;
  139. assertTrue( o1 );
  140. kdtools::pimpl_ptr< QObject > o2( 0 );
  141. assertFalse( o2 );
  142. }
  143. {
  144. const kdtools::pimpl_ptr< ConstTester > o1;
  145. kdtools::pimpl_ptr< ConstTester > o2;
  146. assertTrue( o1->isConst() );
  147. assertFalse( o2->isConst() );
  148. assertTrue( (*o1).isConst() );
  149. assertFalse( (*o2).isConst() );
  150. assertTrue( o1.get()->isConst() );
  151. assertFalse( o2.get()->isConst() );
  152. }
  153. }
  154. #endif // KDTOOLSCORE_UNITTESTS