4 * @brief C++11 style smart pointer for use by settings plug-ins.
6 * @author Ossama Othman @<ossama.othman@@intel.com@>
9 * Copyright 2013 Intel Corporation All Rights Reserved.
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation;
14 * version 2.1 of the License.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 * Boston, MA 02110-1301 USA
27 #ifndef IVI_SETTINGS_SMART_PTR_HPP
28 #define IVI_SETTINGS_SMART_PTR_HPP
42 * @brief C++11 style smart pointer for use by settings plug-ins.
44 * This typename exposes a C++11-style smart pointer interface to
45 * allow RAII based handling of resources provided by third party
46 * libraries, such as Glib. Core functionality is implemented in
47 * traits that this typename template leverages.
54 typedef T element_type;
55 typedef traits<T> traits_type;
57 constexpr smart_ptr() : p_(nullptr) {}
59 constexpr smart_ptr(std::nullptr_t) : p_(nullptr) {}
61 explicit smart_ptr(pointer p) : p_(p) {}
63 smart_ptr(smart_ptr&& u) : p_(u.p_) { u.p_ = nullptr; }
65 ~smart_ptr() { traits_type::release(p_); }
68 operator=(smart_ptr && r)
79 operator=(std::nullptr_t)
89 pointer const old = p_;
96 reset(pointer p = pointer())
102 void swap(smart_ptr& other) { std::swap(p_, other.p_); }
104 pointer get() const { return p_; }
106 explicit operator bool() const { return p_ != nullptr; }
108 typename std::add_lvalue_reference<T>::type
112 * @todo Throw exception if @c p_ @c == @c nullptr.
117 pointer operator->() const { return p_; }
122 * @name Prevent copying
125 smart_ptr(smart_ptr const &) = delete;
126 smart_ptr & operator=(smart_ptr const &) = delete;
131 /// Pointer to the object being managed.
136 template<typename T1, typename T2>
138 operator==(smart_ptr<T1> const & x, smart_ptr<T2> const & y)
140 return x.get() == y.get();
143 template<typename T1, typename T2>
145 operator!=(smart_ptr<T1> const & x, smart_ptr<T2> const & y)
147 return x.get() != y.get();
150 template<typename T1, typename T2>
152 operator<(smart_ptr<T1> const & x, smart_ptr<T2> const & y)
155 std::common_type<typename smart_ptr<T1>::pointer,
156 typename smart_ptr<T2>::pointer>::type type;
158 return std::less<type>()(x.get(), y.get());
162 template<typename T1, typename T2>
163 bool operator<=(smart_ptr<T1> const & x, smart_ptr<T2> const & y)
168 template<typename T1, typename T2>
170 operator>(smart_ptr<T1> const & x, smart_ptr<T2> const & y)
175 template<typename T1, typename T2>
177 operator>=(smart_ptr<T1> const & x, smart_ptr<T2> const & y)
184 operator==(smart_ptr<T> const & x, std::nullptr_t) noexcept
191 operator==(std::nullptr_t, smart_ptr<T> const & x) noexcept
198 operator!=(smart_ptr<T> const & x, std::nullptr_t) noexcept
200 return static_cast<bool>(x);
205 operator!=(std::nullptr_t, smart_ptr<T> const & x) noexcept
207 return static_cast<bool>(x);
212 operator<(smart_ptr<T> const & x, std::nullptr_t)
214 return std::less<typename smart_ptr<T>::pointer>()(x.get(), nullptr);
219 operator<(std::nullptr_t, smart_ptr<T> const & y)
221 return std::less<typename smart_ptr<T>::pointer>()(nullptr, y.get());
226 operator<=(smart_ptr<T> const & x, std::nullptr_t)
233 operator<=(std::nullptr_t, smart_ptr<T> const & y)
240 operator>(smart_ptr<T> const & x, std::nullptr_t)
242 return !(nullptr < x);
247 operator>(std::nullptr_t, smart_ptr<T> const & y)
249 return !(y < nullptr);
254 operator>=(smart_ptr<T> const & x, std::nullptr_t)
256 return !(x < nullptr);
261 operator>=(std::nullptr_t, smart_ptr<T> const & y)
263 return !(nullptr < y);
273 swap(ivi::settings::smart_ptr<T> & lhs,
274 ivi::settings::smart_ptr<T> & rhs)
280 struct hash<ivi::settings::smart_ptr<T>>
282 typedef ivi::settings::smart_ptr<T> argument_type;
283 typedef std::size_t result_type;
288 operator()(argument_type const & p) const
290 typedef typename argument_type::pointer key_type;
292 return hash<key_type>(p.get());
297 #endif /* IVI_SETTINGS_SMART_PTR_HPP */
303 // indent-tabs-mode: nil