libyui  3.4.2
ImplPtr.h
1 /*
2  Copyright (C) 2000-2012 Novell, Inc
3  This library is free software; you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as
5  published by the Free Software Foundation; either version 2.1 of the
6  License, or (at your option) version 3.0 of the License. This library
7  is distributed in the hope that it will be useful, but WITHOUT ANY
8  WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  License for more details. You should have received a copy of the GNU
11  Lesser General Public License along with this library; if not, write
12  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13  Floor, Boston, MA 02110-1301 USA
14 */
15 
16 
17 /*-/
18 
19  File: ImplPtr.h
20 
21  Author: Michael Andres <ma@suse.de>
22 
23 /-*/
24 
25 #ifndef ImplPtr_h
26 #define ImplPtr_h
27 
28 #include <boost/noncopyable.hpp>
29 #include <boost/scoped_ptr.hpp>
30 
31 /**
32  * Helper template class for implementation pointers (pointers to a private
33  * class or structure that hold the member variables of a higher-level class
34  * that is part of a public API).
35  *
36  * This pointer class maintains constness of its parent class, i.e. if it is
37  * used in a const class the class this pointer points to will also be const.
38  *
39  * This class automatically deletes the class it points to in its destructor.
40  **/
41 template<class _Impl>
42 class ImplPtr : private boost::noncopyable
43 {
44 #if defined( BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS ) || defined( BOOST_NO_CXX11_NULLPTR )
45  typedef typename boost::scoped_ptr<_Impl>::unspecified_bool_type unspecified_bool_type;
46 #endif
47 
48 public:
49  typedef _Impl element_type;
50 
51  explicit
52  ImplPtr( _Impl * impl_r = 0 ) : _impl( impl_r ) {}
53 
54 public:
55  void reset( _Impl * impl_r = 0 ) { _impl.reset( impl_r ); }
56 
57  void swap( ImplPtr rhs ) { _impl.swap( rhs._impl ); }
58 
59 public:
60 #if defined( BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS ) || defined( BOOST_NO_CXX11_NULLPTR )
61  operator unspecified_bool_type() const { return _impl; }
62 #else
63  explicit operator bool () const { return _impl.get() != 0; }
64 #endif
65 
66  const _Impl & operator*() const { return *_impl; }
67  const _Impl * operator->() const { return _impl.get(); }
68  const _Impl * get() const { return _impl.get(); }
69 
70  _Impl & operator*() { return *_impl; }
71  _Impl * operator->() { return _impl.get(); }
72  _Impl * get() { return _impl.get(); }
73 
74 private:
75  boost::scoped_ptr<_Impl> _impl;
76 };
77 
78 template<class _Impl>
79 inline bool operator==( ImplPtr<_Impl> & lhs, ImplPtr<_Impl> & rhs ) { return lhs.get() == rhs.get(); }
80 
81 template<class _Impl>
82 inline bool operator!=( ImplPtr<_Impl> & lhs, ImplPtr<_Impl> & rhs ) { return lhs.get() != rhs.get(); }
83 
84 template<class _Impl>
85 inline bool operator< ( ImplPtr<_Impl> & lhs, ImplPtr<_Impl> & rhs ) { return lhs.get() < rhs.get(); }
86 
87 #endif // ImplPtr_h
Helper template class for implementation pointers (pointers to a private class or structure that hold...
Definition: ImplPtr.h:42