File indexing completed on 2024-06-23 05:14:18

0001 /****************************************************************************
0002 ** SPDX-FileCopyrightText: 2001-2007 Klarälvdalens Datakonsult AB. All rights reserved.
0003 **
0004 ** This file is part of the KD Tools library.
0005 **
0006 ** SPDX-License-Identifier: GPL-2.0-only
0007 **
0008 **********************************************************************/
0009 
0010 #pragma once
0011 
0012 #include <utils/kdtoolsglobal.h>
0013 
0014 namespace kdtools
0015 {
0016 
0017 template<typename T>
0018 class pimpl_ptr
0019 {
0020     KDAB_DISABLE_COPY(pimpl_ptr);
0021     T *d;
0022 
0023 public:
0024     pimpl_ptr()
0025         : d(new T)
0026     {
0027     }
0028     explicit pimpl_ptr(T *t)
0029         : d(t)
0030     {
0031     }
0032     ~pimpl_ptr()
0033     {
0034         delete d;
0035         d = nullptr;
0036     }
0037 
0038     T *get()
0039     {
0040         return d;
0041     }
0042     const T *get() const
0043     {
0044         return d;
0045     }
0046 
0047     T *operator->()
0048     {
0049         return get();
0050     }
0051     const T *operator->() const
0052     {
0053         return get();
0054     }
0055 
0056     T &operator*()
0057     {
0058         return *get();
0059     }
0060     const T &operator*() const
0061     {
0062         return *get();
0063     }
0064 
0065     KDAB_IMPLEMENT_SAFE_BOOL_OPERATOR(get())
0066 };
0067 
0068 // these are not implemented, so's we can catch their use at
0069 // link-time. Leaving them undeclared would open up a comparison
0070 // via operator unspecified-bool-type().
0071 template<typename T, typename S>
0072 void operator==(const pimpl_ptr<T> &, const pimpl_ptr<S> &);
0073 template<typename T, typename S>
0074 void operator!=(const pimpl_ptr<T> &, const pimpl_ptr<S> &);
0075 
0076 } // namespace kdtools