1 /****************************************************************************
3 ** Copyright (C) Qxt Foundation. Some rights reserved.
5 ** This file is part of the QxtCore module of the Qt eXTension library
7 ** This library is free software; you can redistribute it and/or modify it
8 ** under the terms of th Common Public License, version 1.0, as published by
11 ** This file is provided "AS IS", without WARRANTIES OR CONDITIONS OF ANY
12 ** KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY
13 ** WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR
14 ** FITNESS FOR A PARTICULAR PURPOSE.
16 ** You should have received a copy of the CPL along with this file.
17 ** See the LICENSE file and the cpl1.0.txt file included with the source
18 ** distribution for more information. If you did not receive a copy of the
19 ** license, contact the Qxt Foundation.
21 ** <http://libqxt.sourceforge.net> <foundation@libqxt.org>
23 ****************************************************************************/
25 \class QxtPimpl QxtPimpl
27 \brief Hide private details of a class
29 Application code generally doesn't have to be concerned about hiding its
30 implementation details, but when writing library code it is important to
31 maintain a constant interface, both source and binary. Maintaining a constant
32 source interface is easy enough, but keeping the binary interface constant
33 means moving implementation details into a private class. The PIMPL, or
34 d-pointer, idiom is a common method of implementing this separation. QxtPimpl
35 offers a convenient way to connect the public and private sides of your class.
37 \section start Getting Started
38 Before you declare the public class, you need to make a forward declaration
39 of the private class. The private class must have the same name as the public
40 class, followed by the word Private. For example, a class named MyTest would
41 declare the private class with:
46 \subsection pub The Public Class
47 Generally, you shouldn't keep any data members in the public class without a
48 good reason. Functions that are part of the public interface should be declared
49 in the public class, and functions that need to be available to subclasses (for
50 calling or overriding) should be in the protected section of the public class.
51 To connect the private class to the public class, include the
52 QXT_DECLARE_PRIVATE macro in the private section of the public class. In the
53 example above, the private class is connected as follows:
56 QXT_DECLARE_PRIVATE(MyTest);
59 Additionally, you must include the QXT_INIT_PRIVATE macro in the public class's
60 constructor. Continuing with the MyTest example, your constructor might look
65 QXT_INIT_PRIVATE(MyTest);
69 \subsection priv The Private Class
70 As mentioned above, data members should usually be kept in the private class.
71 This allows the memory layout of the private class to change without breaking
72 binary compatibility for the public class. Functions that exist only as
73 implementation details, or functions that need access to private data members,
74 should be implemented here.
76 To define the private class, inherit from the template QxtPrivate class, and
77 include the QXT_DECLARE_PUBLIC macro in its public section. The template
78 parameter should be the name of the public class. For example:
80 class MyTestPrivate : public QxtPrivate<MyTest> {
83 QXT_DECLARE_PUBLIC(MyTest);
87 \section cross Accessing Private Members
88 Use the qxt_d() function (actually a function-like object) from functions in
89 the public class to access the private class. Similarly, functions in the
90 private class can invoke functions in the public class by using the qxt_p()
91 function (this one's actually a function).
93 For example, assume that MyTest has methods named getFoobar and doBaz(),
94 and MyTestPrivate has a member named foobar and a method named doQuux().
95 The code might resemble this example:
97 int MyTest::getFoobar() {
98 return qxt_d().foobar;
101 void MyTestPrivate::doQuux() {
102 qxt_p().doBaz(foobar);
110 /*! \relates QxtPimpl
111 * Declares that a public class has a related private class.
113 * This shuold be put in the private section of the public class. The parameter is the name of the public class.
115 #define QXT_DECLARE_PRIVATE(PUB) friend class PUB##Private; QxtPrivateInterface<PUB, PUB##Private> qxt_d;
116 /*! \relates QxtPimpl
117 * Declares that a private class has a related public class.
119 * This may be put anywhere in the declaration of the private class. The parameter is the name of the public class.
121 #define QXT_DECLARE_PUBLIC(PUB) friend class PUB;
122 /*! \relates QxtPimpl
123 * Initializes resources owned by the private class.
125 * This should be called from the public class's constructor,
126 * before qxt_d() is used for the first time. The parameter is the name of the public class.
128 #define QXT_INIT_PRIVATE(PUB) qxt_d.setPublic(this);
130 #ifdef QXT_DOXYGEN_RUN
131 /*! \relates QxtPimpl
132 * Returns a reference to the private class.
134 * This function is only available in a class using \a QXT_DECLARE_PRIVATE.
136 QxtPrivate<PUB>& qxt_d();
138 /*! \relates QxtPimpl
139 * Returns a const reference to the private class.
141 * This function is only available in a class using \a QXT_DECLARE_PRIVATE.
142 * This overload will be automatically used in const functions.
144 const QxtPrivate<PUB>& qxt_d();
146 /*! \relates QxtPimpl
147 * Returns a reference to the public class.
149 * This function is only available in a class using \a QXT_DECLARE_PUBLIC.
153 /*! \relates QxtPimpl
154 * Returns a const reference to the public class.
156 * This function is only available in a class using \a QXT_DECLARE_PUBLIC.
157 * This overload will be automatically used in const functions.
162 #ifndef QXT_DOXYGEN_RUN
163 template <typename PUB>
167 virtual ~QxtPrivate()
169 inline void QXT_setPublic(PUB* pub)
179 inline const PUB& qxt_p() const
188 template <typename PUB, typename PVT>
189 class QxtPrivateInterface
191 friend class QxtPrivate<PUB>;
193 QxtPrivateInterface()
197 ~QxtPrivateInterface()
202 inline void setPublic(PUB* pub)
204 pvt->QXT_setPublic(pub);
206 inline PVT& operator()()
208 return *static_cast<PVT*>(pvt);
210 inline const PVT& operator()() const
212 return *static_cast<PVT*>(pvt);
215 QxtPrivate<PUB>* pvt;