|
Line 0
a/Source/JavaScriptCore/wtf/efl/EflOwnPtr.h_sec1
|
|
|
1 |
/* |
| 2 |
* Copyright (C) 2006, 2007 Apple Inc. All rights reserved. |
| 3 |
* Copyright (C) 2008 Collabora Ltd. |
| 4 |
* Copyright (C) 2011 ProFUSION embedded systems |
| 5 |
* Copyright (C) 2011 Samsung Electronics |
| 6 |
* |
| 7 |
* This library is free software; you can redistribute it and/or |
| 8 |
* modify it under the terms of the GNU Library General Public |
| 9 |
* License as published by the Free Software Foundation; either |
| 10 |
* version 2 of the License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* This library is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 |
* Library General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Library General Public License |
| 18 |
* along with this library; see the file COPYING.LIB. If not, write to |
| 19 |
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 20 |
* Boston, MA 02110-1301, USA. |
| 21 |
* |
| 22 |
*/ |
| 23 |
|
| 24 |
#ifndef EflOwnPtr_h |
| 25 |
#define EflOwnPtr_h |
| 26 |
|
| 27 |
#include <algorithm> |
| 28 |
#include <cstdlib> |
| 29 |
#include <wtf/Assertions.h> |
| 30 |
#include <wtf/Noncopyable.h> |
| 31 |
|
| 32 |
namespace WTF { |
| 33 |
|
| 34 |
template <typename T> inline void freeOwnedEflPtr(T* ptr); |
| 35 |
template <> void freeOwnedEflPtr<Evas_Object>(Evas_Object*); |
| 36 |
|
| 37 |
template <typename T> class EflOwnPtr { |
| 38 |
WTF_MAKE_NONCOPYABLE(EflOwnPtr); |
| 39 |
public: |
| 40 |
explicit EflOwnPtr(T* ptr = 0) : m_ptr(ptr) { } |
| 41 |
~EflOwnPtr() { freeOwnedEflPtr(m_ptr); } |
| 42 |
|
| 43 |
T* get() const { return m_ptr; } |
| 44 |
T* release() |
| 45 |
{ |
| 46 |
T* ptr = m_ptr; |
| 47 |
m_ptr = 0; |
| 48 |
return ptr; |
| 49 |
} |
| 50 |
|
| 51 |
T*& outPtr() |
| 52 |
{ |
| 53 |
ASSERT(!m_ptr); |
| 54 |
return m_ptr; |
| 55 |
} |
| 56 |
|
| 57 |
void set(T* ptr) |
| 58 |
{ |
| 59 |
ASSERT(!ptr || m_ptr != ptr); |
| 60 |
freeOwnedEflPtr(m_ptr); |
| 61 |
m_ptr = ptr; |
| 62 |
} |
| 63 |
|
| 64 |
void clear() |
| 65 |
{ |
| 66 |
T* ptr = m_ptr; |
| 67 |
m_ptr = 0; |
| 68 |
freeOwnedEflPtr(ptr); |
| 69 |
} |
| 70 |
|
| 71 |
T& operator*() const |
| 72 |
{ |
| 73 |
ASSERT(m_ptr); |
| 74 |
return *m_ptr; |
| 75 |
} |
| 76 |
|
| 77 |
T* operator->() const |
| 78 |
{ |
| 79 |
ASSERT(m_ptr); |
| 80 |
return m_ptr; |
| 81 |
} |
| 82 |
|
| 83 |
bool operator!() const { return !m_ptr; } |
| 84 |
|
| 85 |
// This conversion operator allows implicit conversion to bool but not to other integer types. |
| 86 |
typedef T* EflOwnPtr::*UnspecifiedBoolType; |
| 87 |
operator UnspecifiedBoolType() const { return m_ptr ? &EflOwnPtr::m_ptr : 0; } |
| 88 |
|
| 89 |
void swap(EflOwnPtr& o) { std::swap(m_ptr, o.m_ptr); } |
| 90 |
|
| 91 |
private: |
| 92 |
T* m_ptr; |
| 93 |
}; |
| 94 |
|
| 95 |
template <typename T> inline void swap(EflOwnPtr<T>& a, EflOwnPtr<T>& b) |
| 96 |
{ |
| 97 |
a.swap(b); |
| 98 |
} |
| 99 |
|
| 100 |
template <typename T, typename U> inline bool operator==(const EflOwnPtr<T>& a, U* b) |
| 101 |
{ |
| 102 |
return a.get() == b; |
| 103 |
} |
| 104 |
|
| 105 |
template <typename T, typename U> inline bool operator==(T* a, const EflOwnPtr<U>& b) |
| 106 |
{ |
| 107 |
return a == b.get(); |
| 108 |
} |
| 109 |
|
| 110 |
template <typename T, typename U> inline bool operator!=(const EflOwnPtr<T>& a, U* b) |
| 111 |
{ |
| 112 |
return a.get() != b; |
| 113 |
} |
| 114 |
|
| 115 |
template <typename T, typename U> inline bool operator!=(T* a, const EflOwnPtr<U>& b) |
| 116 |
{ |
| 117 |
return a != b.get(); |
| 118 |
} |
| 119 |
|
| 120 |
template <typename T> inline typename EflOwnPtr<T>::PtrType getPtr(const EflOwnPtr<T>& p) |
| 121 |
{ |
| 122 |
return p.get(); |
| 123 |
} |
| 124 |
|
| 125 |
template <typename T> inline void freeOwned(T* ptr) |
| 126 |
{ |
| 127 |
std::free(ptr); |
| 128 |
} |
| 129 |
|
| 130 |
} // namespace WTF |
| 131 |
|
| 132 |
using WTF::EflOwnPtr; |
| 133 |
|
| 134 |
#endif // EflOwnPtr_h |
| 135 |
|