PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/src/qt/qtbase/src/corelib/tools/qshareddata.cpp

https://gitlab.com/x33n/phantomjs
C++ | 613 lines | 3 code | 59 blank | 551 comment | 0 complexity | 5b2b16ba04fc60f0d0c9b862a3636758 MD5 | raw file
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
  4. ** Contact: http://www.qt-project.org/legal
  5. **
  6. ** This file is part of the QtCore module of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:LGPL$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and Digia. For licensing terms and
  14. ** conditions see http://qt.digia.com/licensing. For further information
  15. ** use the contact form at http://qt.digia.com/contact-us.
  16. **
  17. ** GNU Lesser General Public License Usage
  18. ** Alternatively, this file may be used under the terms of the GNU Lesser
  19. ** General Public License version 2.1 as published by the Free Software
  20. ** Foundation and appearing in the file LICENSE.LGPL included in the
  21. ** packaging of this file. Please review the following information to
  22. ** ensure the GNU Lesser General Public License version 2.1 requirements
  23. ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  24. **
  25. ** In addition, as a special exception, Digia gives you certain additional
  26. ** rights. These rights are described in the Digia Qt LGPL Exception
  27. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  28. **
  29. ** GNU General Public License Usage
  30. ** Alternatively, this file may be used under the terms of the GNU
  31. ** General Public License version 3.0 as published by the Free Software
  32. ** Foundation and appearing in the file LICENSE.GPL included in the
  33. ** packaging of this file. Please review the following information to
  34. ** ensure the GNU General Public License version 3.0 requirements will be
  35. ** met: http://www.gnu.org/copyleft/gpl.html.
  36. **
  37. **
  38. ** $QT_END_LICENSE$
  39. **
  40. ****************************************************************************/
  41. #include <qshareddata.h>
  42. QT_BEGIN_NAMESPACE
  43. /*!
  44. \class QSharedData
  45. \inmodule QtCore
  46. \brief The QSharedData class is a base class for shared data objects.
  47. \reentrant
  48. QSharedData is designed to be used with QSharedDataPointer or
  49. QExplicitlySharedDataPointer to implement custom \l{implicitly
  50. shared} or explicitly shared classes. QSharedData provides
  51. \l{thread-safe} reference counting.
  52. See QSharedDataPointer and QExplicitlySharedDataPointer for details.
  53. */
  54. /*! \fn QSharedData::QSharedData()
  55. Constructs a QSharedData object with a reference count of 0.
  56. */
  57. /*! \fn QSharedData::QSharedData(const QSharedData& other)
  58. Constructs a QSharedData object with reference count 0.
  59. \a other is ignored.
  60. */
  61. /*!
  62. \class QSharedDataPointer
  63. \inmodule QtCore
  64. \brief The QSharedDataPointer class represents a pointer to an implicitly shared object.
  65. \since 4.0
  66. \reentrant
  67. QSharedDataPointer\<T\> makes writing your own \l {implicitly
  68. shared} classes easy. QSharedDataPointer implements \l {thread-safe}
  69. reference counting, ensuring that adding QSharedDataPointers to your
  70. \l {reentrant} classes won't make them non-reentrant.
  71. \l {Implicit sharing} is used by many Qt classes to combine the
  72. speed and memory efficiency of pointers with the ease of use of
  73. classes. See the \l{Shared Classes} page for more information.
  74. \target Employee example
  75. Suppose you want to make an \c Employee class implicitly shared. The
  76. procedure is:
  77. \list
  78. \li Define the class \c Employee to have a single data member of
  79. type \c {QSharedDataPointer<EmployeeData>}.
  80. \li Define the \c EmployeeData class derived from \l QSharedData to
  81. contain all the data members you would normally have put in the
  82. \c Employee class.
  83. \endlist
  84. To show this in practice, we review the source code for the
  85. implicitly shared \c Employee class. In the header file we define the
  86. two classes \c Employee and \c EmployeeData.
  87. \snippet sharedemployee/employee.h 0
  88. In class \c Employee, note the single data member, a \e {d pointer}
  89. of type \c {QSharedDataPointer<EmployeeData>}. All accesses of
  90. employee data must go through the \e {d pointer's} \c
  91. {operator->()}. For write accesses, \c {operator->()} will
  92. automatically call detach(), which creates a copy of the shared data
  93. object if the shared data object's reference count is greater than
  94. 1. This ensures that writes to one \c Employee object don't affect
  95. any other \c Employee objects that share the same \c EmployeeData
  96. object.
  97. Class \c EmployeeData inherits QSharedData, which provides the
  98. \e{behind the scenes} reference counter. \c EmployeeData has a default
  99. constructor, a copy constructor, and a destructor. Normally, trivial
  100. implementations of these are all that is needed in the \e {data}
  101. class for an implicitly shared class.
  102. Implementing the two constructors for class \c Employee is also
  103. straightforward. Both create a new instance of \c EmployeeData
  104. and assign it to the \e{d pointer} .
  105. \snippet sharedemployee/employee.h 1
  106. \codeline
  107. \snippet sharedemployee/employee.h 2
  108. Note that class \c Employee also has a trivial copy constructor
  109. defined, which is not strictly required in this case.
  110. \snippet sharedemployee/employee.h 7
  111. The copy constructor is not strictly required here, because class \c
  112. EmployeeData is included in the same file as class \c Employee
  113. (\c{employee.h}). However, including the private subclass of
  114. QSharedData in the same file as the public class containing the
  115. QSharedDataPointer is not typical. Normally, the idea is to hide the
  116. private subclass of QSharedData from the user by putting it in a
  117. separate file which would not be included in the public file. In
  118. this case, we would normally put class \c EmployeeData in a separate
  119. file, which would \e{not} be included in \c{employee.h}. Instead, we
  120. would just predeclare the private subclass \c EmployeeData in \c
  121. {employee.h} this way:
  122. \code
  123. class EmployeeData;
  124. \endcode
  125. If we had done it that way here, the copy constructor shown would be
  126. required. Since the copy constructor is trivial, you might as well
  127. just always include it.
  128. Behind the scenes, QSharedDataPointer automatically increments the
  129. reference count whenever an \c Employee object is copied, assigned,
  130. or passed as a parameter. It decrements the reference count whenever
  131. an \c Employee object is deleted or goes out of scope. The shared
  132. \c EmployeeData object is deleted automatically if and when the
  133. reference count reaches 0.
  134. In a non-const member function of \c Employee, whenever the \e {d
  135. pointer} is dereferenced, QSharedDataPointer automatically calls
  136. detach() to ensure that the function operates on its own copy of the
  137. data.
  138. \snippet sharedemployee/employee.h 3
  139. \codeline
  140. \snippet sharedemployee/employee.h 4
  141. Note that if detach() is called more than once in a member function
  142. due to multiple dereferences of the \e {d pointer}, detach() will
  143. only create a copy of the shared data the first time it is called,
  144. if at all, because on the second and subsequent calls of detach(),
  145. the reference count will be 1 again.
  146. But note that in the second \c Employee constructor, which takes an
  147. employee ID and a name, both setId() and setName() are called, but
  148. they don't cause \e{copy on write}, because the reference count for
  149. the newly constructed \c EmployeeData object has just been set to 1.
  150. In \c Employee's \e const member functions, dereferencing the \e {d
  151. pointer} does \e not cause detach() to be called.
  152. \snippet sharedemployee/employee.h 5
  153. \codeline
  154. \snippet sharedemployee/employee.h 6
  155. Notice that there is no need to implement a copy constructor or an
  156. assignment operator for the \c Employee class, because the copy
  157. constructor and assignment operator provided by the C++ compiler
  158. will do the \e{member by member} shallow copy required. The only
  159. member to copy is the \e {d pointer}, which is a QSharedDataPointer,
  160. whose \c {operator=()} just increments the reference count of the
  161. shared \c EmployeeData object.
  162. \target Implicit vs Explicit Sharing
  163. \section1 Implicit vs Explicit Sharing
  164. Implicit sharing might not be right for the \c Employee class.
  165. Consider a simple example that creates two instances of the
  166. implicitly shared \c Employee class.
  167. \snippet sharedemployee/main.cpp 0
  168. After the second employee e2 is created and e1 is assigned to it,
  169. both \c e1 and \c e2 refer to Albrecht Durer, employee 1001. Both \c
  170. Employee objects point to the same instance of \c EmployeeData,
  171. which has reference count 2. Then \c {e1.setName("Hans Holbein")} is
  172. called to change the employee name, but because the reference count
  173. is greater than 1, a \e{copy on write} is performed before the name
  174. is changed. Now \c e1 and \c e2 point to different \c EmployeeData
  175. objects. They have different names, but both have ID 1001, which is
  176. probably not what you want. You can, of course, just continue with
  177. \c {e1.setId(1002)}, if you really mean to create a second, unique
  178. employee, but if you only want to change the employee's name
  179. everywhere, consider using \l {QExplicitlySharedDataPointer}
  180. {explicit sharing} in the \c Employee class instead of implicit
  181. sharing.
  182. If you declare the \e {d pointer} in the \c Employee class to be
  183. \c {QExplicitlySharedDataPointer<EmployeeData>}, then explicit
  184. sharing is used and \e{copy on write} operations are not performed
  185. automatically (i.e. detach() is not called in non-const
  186. functions). In that case, after \c {e1.setName("Hans Holbein")}, the
  187. employee's name has been changed, but both e1 and e2 still refer to
  188. the same instance of \c EmployeeData, so there is only one employee
  189. with ID 1001.
  190. In the member function documentation, \e{d pointer} always refers
  191. to the internal pointer to the shared data object.
  192. \section1 Optimize performance for usage in Qt Containers
  193. You should consider marking your implicitly shared class as a movable type
  194. using the Q_DECLARE_TYPEINFO() macro if it resembles the \c Employee class
  195. above and uses a QSharedDataPointer or QExplicitlySharedDataPointer as the
  196. only member. This can improve performance and memory efficiency when using
  197. Qt's \l{container classes}.
  198. \sa QSharedData, QExplicitlySharedDataPointer, QScopedPointer, QSharedPointer
  199. */
  200. /*! \typedef QSharedDataPointer::Type
  201. This is the type of the shared data object. The \e{d pointer}
  202. points to an object of this type.
  203. */
  204. /*! \typedef QSharedDataPointer::pointer
  205. \internal
  206. */
  207. /*! \fn T& QSharedDataPointer::operator*()
  208. Provides access to the shared data object's members.
  209. This function calls detach().
  210. */
  211. /*! \fn const T& QSharedDataPointer::operator*() const
  212. Provides const access to the shared data object's members.
  213. This function does \e not call detach().
  214. */
  215. /*! \fn T* QSharedDataPointer::operator->()
  216. Provides access to the shared data object's members.
  217. This function calls detach().
  218. */
  219. /*! \fn const T* QSharedDataPointer::operator->() const
  220. Provides const access to the shared data object's members.
  221. This function does \e not call detach().
  222. */
  223. /*! \fn QSharedDataPointer::operator T*()
  224. Returns a pointer to the shared data object.
  225. This function calls detach().
  226. \sa data(), constData()
  227. */
  228. /*! \fn QSharedDataPointer::operator const T*() const
  229. Returns a pointer to the shared data object.
  230. This function does \e not call detach().
  231. */
  232. /*! \fn T* QSharedDataPointer::data()
  233. Returns a pointer to the shared data object.
  234. This function calls detach().
  235. \sa constData()
  236. */
  237. /*! \fn const T* QSharedDataPointer::data() const
  238. Returns a pointer to the shared data object.
  239. This function does \e not call detach().
  240. */
  241. /*! \fn const T* QSharedDataPointer::constData() const
  242. Returns a const pointer to the shared data object.
  243. This function does \e not call detach().
  244. \sa data()
  245. */
  246. /*! \fn void QSharedDataPointer::swap(QSharedDataPointer &other)
  247. Swap this instance's shared data pointer with the shared
  248. data pointer in \a other.
  249. */
  250. /*!
  251. \fn QSharedDataPointer<T> &QSharedDataPointer::operator=(QSharedDataPointer<T> &&other)
  252. Move-assigns \a other to this QSharedDataPointer instance.
  253. \since 5.2
  254. */
  255. /*! \fn bool QSharedDataPointer::operator==(const QSharedDataPointer<T>& other) const
  256. Returns \c true if \a other and \e this have the same \e{d pointer}.
  257. This function does \e not call detach().
  258. */
  259. /*! \fn bool QSharedDataPointer::operator!=(const QSharedDataPointer<T>& other) const
  260. Returns \c true if \a other and \e this do \e not have the same
  261. \e{d pointer}. This function does \e not call detach().
  262. */
  263. /*! \fn QSharedDataPointer::QSharedDataPointer()
  264. Constructs a QSharedDataPointer initialized with a null \e{d pointer}.
  265. */
  266. /*!
  267. \fn QSharedDataPointer::QSharedDataPointer(QSharedDataPointer &&o)
  268. Move-constructs a QSharedDataPointer instance, making it point at the same
  269. object that \a o was pointing to.
  270. \since 5.2
  271. */
  272. /*! \fn QSharedDataPointer::~QSharedDataPointer()
  273. Decrements the reference count of the shared data object.
  274. If the reference count becomes 0, the shared data object
  275. is deleted. \e This is then destroyed.
  276. */
  277. /*! \fn QSharedDataPointer::QSharedDataPointer(T* sharedData)
  278. Constructs a QSharedDataPointer with \e{d pointer} set to
  279. \a sharedData and increments \a{sharedData}'s reference count.
  280. */
  281. /*! \fn QSharedDataPointer::QSharedDataPointer(const QSharedDataPointer<T>& other)
  282. Sets the \e{d pointer} of \e this to the \e{d pointer} in
  283. \a other and increments the reference count of the shared
  284. data object.
  285. */
  286. /*! \fn QSharedDataPointer<T>& QSharedDataPointer::operator=(const QSharedDataPointer<T>& other)
  287. Sets the \e{d pointer} of \e this to the \e{d pointer} of
  288. \a other and increments the reference count of the shared
  289. data object. The reference count of the old shared data
  290. object of \e this is decremented. If the reference count
  291. of the old shared data object becomes 0, the old shared
  292. data object is deleted.
  293. */
  294. /*! \fn QSharedDataPointer& QSharedDataPointer::operator=(T* sharedData)
  295. Sets the \e{d pointer} og \e this to \a sharedData and increments
  296. \a{sharedData}'s reference count. The reference count of the old
  297. shared data object of \e this is decremented. If the reference
  298. count of the old shared data object becomes 0, the old shared data
  299. object is deleted.
  300. */
  301. /*! \fn bool QSharedDataPointer::operator!() const
  302. Returns \c true if the \e{d pointer} of \e this is null.
  303. */
  304. /*! \fn void QSharedDataPointer::detach()
  305. If the shared data object's reference count is greater than 1, this
  306. function creates a deep copy of the shared data object and sets the
  307. \e{d pointer} of \e this to the copy.
  308. This function is called automatically by non-const member
  309. functions of QSharedDataPointer if \e{copy on write} is
  310. required. You don't need to call it yourself.
  311. */
  312. /*! \fn T *QSharedDataPointer::clone()
  313. \since 4.5
  314. Creates and returns a deep copy of the current data. This function
  315. is called by detach() when the reference count is greater than 1 in
  316. order to create the new copy. This function uses the \e {operator
  317. new} and calls the copy constructor of the type T.
  318. This function is provided so that you may support "virtual copy
  319. constructors" for your own types. In order to so, you should declare
  320. a template-specialization of this function for your own type, like
  321. the example below:
  322. \code
  323. template<>
  324. EmployeeData *QSharedDataPointer<EmployeeData>::clone()
  325. {
  326. return d->clone();
  327. }
  328. \endcode
  329. In the example above, the template specialization for the clone()
  330. function calls the \e {EmployeeData::clone()} virtual function. A
  331. class derived from EmployeeData could override that function and
  332. return the proper polymorphic type.
  333. */
  334. /*!
  335. \class QExplicitlySharedDataPointer
  336. \inmodule QtCore
  337. \brief The QExplicitlySharedDataPointer class represents a pointer to an explicitly shared object.
  338. \since 4.4
  339. \reentrant
  340. QExplicitlySharedDataPointer\<T\> makes writing your own explicitly
  341. shared classes easy. QExplicitlySharedDataPointer implements
  342. \l {thread-safe} reference counting, ensuring that adding
  343. QExplicitlySharedDataPointers to your \l {reentrant} classes won't
  344. make them non-reentrant.
  345. Except for one big difference, QExplicitlySharedDataPointer is just
  346. like QSharedDataPointer. The big difference is that member functions
  347. of QExplicitlySharedDataPointer \e{do not} do the automatic
  348. \e{copy on write} operation (detach()) that non-const members of
  349. QSharedDataPointer do before allowing the shared data object to be
  350. modified. There is a detach() function available, but if you really
  351. want to detach(), you have to call it yourself. This means that
  352. QExplicitlySharedDataPointers behave like regular C++ pointers,
  353. except that by doing reference counting and not deleting the shared
  354. data object until the reference count is 0, they avoid the dangling
  355. pointer problem.
  356. It is instructive to compare QExplicitlySharedDataPointer with
  357. QSharedDataPointer by way of an example. Consider the \l {Employee
  358. example} in QSharedDataPointer, modified to use explicit sharing as
  359. explained in the discussion \l {Implicit vs Explicit Sharing}.
  360. Note that if you use this class but find you are calling detach() a
  361. lot, you probably should be using QSharedDataPointer instead.
  362. In the member function documentation, \e{d pointer} always refers
  363. to the internal pointer to the shared data object.
  364. \sa QSharedData, QSharedDataPointer
  365. */
  366. /*! \fn T& QExplicitlySharedDataPointer::operator*() const
  367. Provides access to the shared data object's members.
  368. */
  369. /*! \fn T* QExplicitlySharedDataPointer::operator->()
  370. Provides access to the shared data object's members.
  371. */
  372. /*! \fn const T* QExplicitlySharedDataPointer::operator->() const
  373. Provides const access to the shared data object's members.
  374. */
  375. /*! \fn T* QExplicitlySharedDataPointer::data() const
  376. Returns a pointer to the shared data object.
  377. */
  378. /*! \fn const T* QExplicitlySharedDataPointer::constData() const
  379. Returns a const pointer to the shared data object.
  380. \sa data()
  381. */
  382. /*! \fn void QExplicitlySharedDataPointer::swap(QExplicitlySharedDataPointer &other)
  383. Swap this instance's explicitly shared data pointer with
  384. the explicitly shared data pointer in \a other.
  385. */
  386. /*! \fn bool QExplicitlySharedDataPointer::operator==(const QExplicitlySharedDataPointer<T>& other) const
  387. Returns \c true if \a other and \e this have the same \e{d pointer}.
  388. */
  389. /*!
  390. \fn QExplicitlySharedDataPointer<T> &QExplicitlySharedDataPointer::operator=(QExplicitlySharedDataPointer<T> &&other)
  391. Move-assigns \a other to this QExplicitlySharedDataPointer instance.
  392. \since 5.2
  393. */
  394. /*! \fn bool QExplicitlySharedDataPointer::operator==(const T* ptr) const
  395. Returns \c true if the \e{d pointer} of \e this is \a ptr.
  396. */
  397. /*! \fn bool QExplicitlySharedDataPointer::operator!=(const QExplicitlySharedDataPointer<T>& other) const
  398. Returns \c true if \a other and \e this do \e not have the same
  399. \e{d pointer}.
  400. */
  401. /*! \fn bool QExplicitlySharedDataPointer::operator!=(const T* ptr) const
  402. Returns \c true if the \e{d pointer} of \e this is \e not \a ptr.
  403. */
  404. /*! \fn QExplicitlySharedDataPointer::QExplicitlySharedDataPointer()
  405. Constructs a QExplicitlySharedDataPointer initialized with a null
  406. \e{d pointer}.
  407. */
  408. /*! \fn QExplicitlySharedDataPointer::~QExplicitlySharedDataPointer()
  409. Decrements the reference count of the shared data object.
  410. If the reference count becomes 0, the shared data object
  411. is deleted. \e This is then destroyed.
  412. */
  413. /*!
  414. \fn QExplicitlySharedDataPointer::QExplicitlySharedDataPointer(QExplicitlySharedDataPointer &&o)
  415. Move-constructs a QExplicitlySharedDataPointer instance, making it point at the same
  416. object that \a o was pointing to.
  417. \since 5.2
  418. */
  419. /*! \fn QExplicitlySharedDataPointer::QExplicitlySharedDataPointer(T* sharedData)
  420. Constructs a QExplicitlySharedDataPointer with \e{d pointer}
  421. set to \a sharedData and increments \a{sharedData}'s reference
  422. count.
  423. */
  424. /*! \fn QExplicitlySharedDataPointer::QExplicitlySharedDataPointer(const QExplicitlySharedDataPointer<T>& other)
  425. This standard copy constructor sets the \e {d pointer} of \e this to
  426. the \e {d pointer} in \a other and increments the reference count of
  427. the shared data object.
  428. */
  429. /*! \fn QExplicitlySharedDataPointer::QExplicitlySharedDataPointer(const QExplicitlySharedDataPointer<X>& other)
  430. This copy constructor is different in that it allows \a other to be
  431. a different type of explicitly shared data pointer but one that has
  432. a compatible shared data object. It performs a static cast of the
  433. \e{d pointer} in \a other and sets the \e {d pointer} of \e this to
  434. the converted \e{d pointer}. It increments the reference count of
  435. the shared data object.
  436. */
  437. /*! \fn QExplicitlySharedDataPointer<T>& QExplicitlySharedDataPointer::operator=(const QExplicitlySharedDataPointer<T>& other)
  438. Sets the \e{d pointer} of \e this to the \e{d pointer} of
  439. \a other and increments the reference count of the shared
  440. data object. The reference count of the old shared data
  441. object of \e this is decremented. If the reference count
  442. of the old shared data object becomes 0, the old shared
  443. data object is deleted.
  444. */
  445. /*! \fn QExplicitlySharedDataPointer& QExplicitlySharedDataPointer::operator=(T* sharedData)
  446. Sets the \e{d pointer} of \e this to \a sharedData and
  447. increments \a{sharedData}'s reference count. The reference
  448. count of the old shared data object of \e this is decremented.
  449. If the reference count of the old shared data object becomes
  450. 0, the old shared data object is deleted.
  451. */
  452. /*! \fn void QExplicitlySharedDataPointer::reset()
  453. Resets \e this to be null. i.e., this function sets the
  454. \e{d pointer} of \e this to 0, but first it decrements
  455. the reference count of the shared data object and deletes
  456. the shared data object if the reference count became 0.
  457. */
  458. /*! \fn QExplicitlySharedDataPointer::operator bool () const
  459. Returns \c true if the \e{d pointer} of \e this is \e not null.
  460. */
  461. /*! \fn bool QExplicitlySharedDataPointer::operator!() const
  462. Returns \c true if the \e{d pointer} of \e this is null.
  463. */
  464. /*! \fn void QExplicitlySharedDataPointer::detach()
  465. If the shared data object's reference count is greater than 1, this
  466. function creates a deep copy of the shared data object and sets the
  467. \e{d pointer} of \e this to the copy.
  468. Because QExplicitlySharedDataPointer does not do the automatic
  469. \e{copy on write} operations that members of QSharedDataPointer do,
  470. detach() is \e not called automatically anywhere in the member
  471. functions of this class. If you find that you are calling detach()
  472. everywhere in your code, consider using QSharedDataPointer instead.
  473. */
  474. /*! \fn T *QExplicitlySharedDataPointer::clone()
  475. \since 4.5
  476. Creates and returns a deep copy of the current data. This function
  477. is called by detach() when the reference count is greater than 1 in
  478. order to create the new copy. This function uses the \e {operator
  479. new} and calls the copy constructor of the type T.
  480. See QSharedDataPointer::clone() for an explanation of how to use it.
  481. */
  482. /*!
  483. \typedef QExplicitlySharedDataPointer::Type
  484. This is the type of the shared data object. The \e{d pointer}
  485. points to an object of this type.
  486. */
  487. /*! \typedef QExplicitlySharedDataPointer::pointer
  488. \internal
  489. */
  490. QT_END_NAMESPACE