1 /**************************************************************************
3 * Copyright 2013-2014 RAD Game Tools and Valve Software
4 * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 **************************************************************************/
27 // File: vogl_unique_ptr.h
28 #ifndef VOGL_UNIQUE_PTR_H
29 #define VOGL_UNIQUE_PTR_H
31 #include "vogl_core.h"
36 class vogl_unique_ptr_default_delete_policy
39 void swap(vogl_unique_ptr_default_delete_policy<T> &rhs)
41 VOGL_NOTE_UNUSED(rhs);
50 // First attempt at a std::unique_ptr-like object. This shit is subtle so there are probably dragons in here. Note I could care less about exception safety.
51 // pointer must be allocated using vogl_new().
52 // Using a policy to handle deletion, vs. an object type to avoid this object from growing larger than a ptr in the default case.
53 template <typename T, typename D = vogl_unique_ptr_default_delete_policy<T> >
54 class vogl_unique_ptr : public utils::relative_ops<vogl_unique_ptr<T, D> >, public D
57 typedef T element_type;
59 typedef D delete_policy;
66 vogl_unique_ptr(pointer p)
71 vogl_unique_ptr(vogl_unique_ptr &other)
72 : m_p(other.release())
81 void reset(pointer p = pointer())
83 VOGL_ASSUME(sizeof(T) > 0);
84 VOGL_ASSERT((!p) || (p != m_p));
86 pointer prev_ptr = m_p;
89 delete_policy::delete_ptr(prev_ptr);
99 vogl_unique_ptr &operator=(vogl_unique_ptr &rhs)
103 reset(rhs.release());
108 void swap(vogl_unique_ptr &other)
110 std::swap(m_p, other.m_p);
111 delete_policy::swap(other);
115 // Feels too dangerous without explicit, but we need C++0x for that.
116 explicit operator bool() const
127 pointer operator->() const
137 bool operator==(const vogl_unique_ptr &rhs) const
139 return m_p == rhs.m_p;
142 bool operator<(const vogl_unique_ptr &rhs) const
144 return m_p < rhs.m_p;
154 template <typename T>
155 inline void swap(vogl::vogl_unique_ptr<T> &a, vogl::vogl_unique_ptr<T> &b)
161 #endif // VOGL_UNIQUE_PTR_H