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)
87 pointer const old = p_;
94 reset(pointer p = pointer())
100 void swap(smart_ptr& other) { std::swap(p_, other.p_); }
102 pointer get() const { return p_; }
104 explicit operator bool() const { return p_ != nullptr; }
106 typename std::add_lvalue_reference<T>::type
110 * @todo Throw exception if @c p_ @c == @c nullptr.
115 pointer operator->() const { return p_; }
120 * @name Prevent copying
123 smart_ptr(smart_ptr const &) = delete;
124 smart_ptr & operator=(smart_ptr const &) = delete;
129 /// Pointer to the object being managed.
134 template<typename T1, typename T2>
136 operator==(smart_ptr<T1> const & x, smart_ptr<T2> const & y)
138 return x.get() == y.get();
141 template<typename T1, typename T2>
143 operator!=(smart_ptr<T1> const & x, smart_ptr<T2> const & y)
145 return x.get() != y.get();
148 template<typename T1, typename T2>
150 operator<(smart_ptr<T1> const & x, smart_ptr<T2> const & y)
153 std::common_type<typename smart_ptr<T1>::pointer,
154 typename smart_ptr<T2>::pointer>::type type;
156 return std::less<type>()(x.get(), y.get());
160 template<typename T1, typename T2>
161 bool operator<=(smart_ptr<T1> const & x, smart_ptr<T2> const & y)
166 template<typename T1, typename T2>
168 operator>(smart_ptr<T1> const & x, smart_ptr<T2> const & y)
173 template<typename T1, typename T2>
175 operator>=(smart_ptr<T1> const & x, smart_ptr<T2> const & y)
182 operator==(smart_ptr<T> const & x, std::nullptr_t) noexcept
189 operator==(std::nullptr_t, smart_ptr<T> const & x) noexcept
196 operator!=(smart_ptr<T> const & x, std::nullptr_t) noexcept
198 return static_cast<bool>(x);
203 operator!=(std::nullptr_t, smart_ptr<T> const & x) noexcept
205 return static_cast<bool>(x);
210 operator<(smart_ptr<T> const & x, std::nullptr_t)
212 return std::less<typename smart_ptr<T>::pointer>()(x.get(), nullptr);
217 operator<(std::nullptr_t, smart_ptr<T> const & y)
219 return std::less<typename smart_ptr<T>::pointer>()(nullptr, y.get());
224 operator<=(smart_ptr<T> const & x, std::nullptr_t)
231 operator<=(std::nullptr_t, smart_ptr<T> const & y)
238 operator>(smart_ptr<T> const & x, std::nullptr_t)
240 return !(nullptr < x);
245 operator>(std::nullptr_t, smart_ptr<T> const & y)
247 return !(y < nullptr);
252 operator>=(smart_ptr<T> const & x, std::nullptr_t)
254 return !(x < nullptr);
259 operator>=(std::nullptr_t, smart_ptr<T> const & y)
261 return !(nullptr < y);
271 swap(ivi::settings::smart_ptr<T> & lhs,
272 ivi::settings::smart_ptr<T> & rhs)
278 struct hash<ivi::settings::smart_ptr<T>>
280 typedef ivi::settings::smart_ptr<T> argument_type;
281 typedef std::size_t result_type;
286 operator()(argument_type const & p) const
288 typedef typename argument_type::pointer key_type;
290 return hash<key_type>(p.get());
295 #endif /* IVI_SETTINGS_SMART_PTR_HPP */
301 // indent-tabs-mode: nil