Miam-Player  0.8.0
A nice music player
dptr.h
Go to the documentation of this file.
1 /******************************************************************************
2  dptr.h: An improved d-pointer interface from Qxt
3  Improved by Wang Bin <wbsecg1@gmail.com>, 2012
4 ******************************************************************************/
5 
6 /****************************************************************************
7 ** Copyright (c) 2006 - 2011, the LibQxt project.
8 ** See the Qxt AUTHORS file for a list of authors and copyright holders.
9 ** All rights reserved.
10 **
11 ** Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are met:
13 ** * Redistributions of source code must retain the above copyright
14 ** notice, this list of conditions and the following disclaimer.
15 ** * Redistributions in binary form must reproduce the above copyright
16 ** notice, this list of conditions and the following disclaimer in the
17 ** documentation and/or other materials provided with the distribution.
18 ** * Neither the name of the LibQxt project nor the
19 ** names of its contributors may be used to endorse or promote products
20 ** derived from this software without specific prior written permission.
21 **
22 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
23 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 ** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 ** DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
26 ** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 ** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 ** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 ** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 ** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 **
33 ** <http://libqxt.org> <foundation@libqxt.org>
34 *****************************************************************************/
35 /****************************************************************************
36 ** This file is derived from code bearing the following notice:
37 ** The sole author of this file, Adam Higerd, has explicitly disclaimed all
38 ** copyright interest and protection for the content within. This file has
39 ** been placed in the public domain according to United States copyright
40 ** statute and case law. In jurisdictions where this public domain dedication
41 ** is not legally recognized, anyone who receives a copy of this file is
42 ** permitted to use, modify, duplicate, and redistribute this file, in whole
43 ** or in part, with no restrictions or conditions. In these jurisdictions,
44 ** this file shall be copyright (C) 2006-2008 by Adam Higerd.
45 ****************************************************************************/
46 #ifndef DPTR_H
47 #define DPTR_H
48 
98 /*
99  * Initialize the dptr when calling Base(BasePrivate& d) ctor.
100  * The derived class using this ctor will reduce memory allocation
101  * p is a DerivedPrivate*
102  */
103 #define DPTR_INIT(p) dptr(p)
104 //put in protected
105 #define DPTR_DECLARE(Class) DPtrInterface<Class, Class##Private> dptr;
106 //put in private
107 #define DPTR_DECLARE_PRIVATE(Class) \
108  inline Class##Private& d_func() { return dptr.pri<Class##Private>(); } \
109  inline const Class##Private& d_func() const { return dptr.pri<Class##Private>(); } \
110  friend class Class##Private;
111 
112 #define DPTR_DECLARE_PUBLIC(Class) \
113  inline Class& q_func() { return *static_cast<Class*>(dptr_ptr()); } \
114  inline const Class& q_func() const { return *static_cast<const Class*>(dptr_ptr()); } \
115  friend class Class;
116 
117 #define DPTR_INIT_PRIVATE(Class) dptr.setPublic(this);
118 #define DPTR_D(Class) Class##Private& d = dptr.pri<Class##Private>()
119 #define DPTR_P(Class) Class& p = *static_cast<Class*>(dptr_ptr())
120 
121 //interface
122 template <typename PUB>
124 {
125 public:
126  virtual ~DPtrPrivate() {}
127  inline void DPTR_setPublic(PUB* pub) { dptr_p_ptr = pub; }
128 protected:
129  inline PUB& dptr_p() { return *dptr_p_ptr; }
130  inline const PUB& dptr_p() const { return *dptr_p_ptr; }
131  inline PUB* dptr_ptr() { return dptr_p_ptr; }
132  inline const PUB* dptr_ptr() const { return dptr_p_ptr; }
133 private:
134  PUB* dptr_p_ptr;
135 };
136 
137 //interface
138 template <typename PUB, typename PVT>
140 {
141  friend class DPtrPrivate<PUB>;
142 public:
143  DPtrInterface(PVT* d):pvt(d) {}
144  DPtrInterface():pvt(new PVT()) {}
146  if (pvt) {
147  delete pvt;
148  pvt = 0;
149  }
150  }
151  inline void setPublic(PUB* pub) { pvt->DPTR_setPublic(pub); }
152  template <typename T>
153  inline T& pri() { return *reinterpret_cast<T*>(pvt); }
154  template <typename T>
155  inline const T& pri() const { return *reinterpret_cast<T*>(pvt); } //static cast requires defination of T
156  inline PVT& operator()() { return *static_cast<PVT*>(pvt); }
157  inline const PVT& operator()() const { return *static_cast<PVT*>(pvt); }
158  inline PVT * operator->() { return static_cast<PVT*>(pvt); }
159  inline const PVT * operator->() const { return static_cast<PVT*>(pvt); }
160 private:
162  DPtrInterface& operator=(const DPtrInterface&);
163  DPtrPrivate<PUB>* pvt;
164 };
165 
166 #endif // DPTR_H
T & pri()
Definition: dptr.h:153
PVT & operator()()
Definition: dptr.h:156
DPtrInterface(PVT *d)
Definition: dptr.h:143
const PUB * dptr_ptr() const
Definition: dptr.h:132
void setPublic(PUB *pub)
Definition: dptr.h:151
PUB & dptr_p()
Definition: dptr.h:129
~DPtrInterface()
Definition: dptr.h:145
PVT * operator->()
Definition: dptr.h:158
Definition: dptr.h:139
Definition: dptr.h:123
PUB * dptr_ptr()
Definition: dptr.h:131
virtual ~DPtrPrivate()
Definition: dptr.h:126
void DPTR_setPublic(PUB *pub)
Definition: dptr.h:127
const PVT & operator()() const
Definition: dptr.h:157
const T & pri() const
Definition: dptr.h:155
const PVT * operator->() const
Definition: dptr.h:159
DPtrInterface()
Definition: dptr.h:144
const PUB & dptr_p() const
Definition: dptr.h:130