libyui  2.42.6
 All Classes Functions Variables Enumerations Friends
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  typedef typename boost::scoped_ptr<_Impl>::unspecified_bool_type unspecified_bool_type;
45 
46 public:
47  typedef _Impl element_type;
48 
49  explicit
50  ImplPtr( _Impl * impl_r = 0 ) : _impl( impl_r ) {}
51 
52 public:
53  void reset( _Impl * impl_r = 0 ) { _impl.reset( impl_r ); }
54 
55  void swap( ImplPtr rhs ) { _impl.swap( rhs._impl ); }
56 
57 public:
58  operator unspecified_bool_type() const { return _impl; }
59 
60  const _Impl & operator*() const { return *_impl; }
61  const _Impl * operator->() const { return _impl.get(); }
62  const _Impl * get() const { return _impl.get(); }
63 
64  _Impl & operator*() { return *_impl; }
65  _Impl * operator->() { return _impl.get(); }
66  _Impl * get() { return _impl.get(); }
67 
68 private:
69  boost::scoped_ptr<_Impl> _impl;
70 };
71 
72 template<class _Impl>
73 inline bool operator==( ImplPtr<_Impl> & lhs, ImplPtr<_Impl> & rhs ) { return lhs.get() == rhs.get(); }
74 
75 template<class _Impl>
76 inline bool operator!=( ImplPtr<_Impl> & lhs, ImplPtr<_Impl> & rhs ) { return lhs.get() != rhs.get(); }
77 
78 template<class _Impl>
79 inline bool operator< ( ImplPtr<_Impl> & lhs, ImplPtr<_Impl> & rhs ) { return lhs.get() < rhs.get(); }
80 
81 #endif // ImplPtr_h