|
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 |
// If we expand to using other data types, moving these typedefs to a |
| 33 |
// separate file should be considered. |
| 34 |
typedef struct _Evas_Object Evas_Object; |
| 35 |
|
| 36 |
namespace WTF { |
| 37 |
|
| 38 |
template <typename T> inline void freeOwnedEflPtr(T* ptr); |
| 39 |
template <> void freeOwnedEflPtr<Evas_Object>(Evas_Object*); |
| 40 |
|
| 41 |
template <typename T> class EflOwnPtr { |
| 42 |
WTF_MAKE_NONCOPYABLE(EflOwnPtr); |
| 43 |
public: |
| 44 |
explicit EflOwnPtr(T* ptr = 0) : m_ptr(ptr) { } |
| 45 |
~EflOwnPtr() { freeOwnedEflPtr(m_ptr); } |
| 46 |
|
| 47 |
T* get() const { return m_ptr; } |
| 48 |
T* release() |
| 49 |
{ |
| 50 |
T* ptr = m_ptr; |
| 51 |
m_ptr = 0; |
| 52 |
return ptr; |
| 53 |
} |
| 54 |
|
| 55 |
T*& outPtr() |
| 56 |
{ |
| 57 |
ASSERT(!m_ptr); |
| 58 |
return m_ptr; |
| 59 |
} |
| 60 |
|
| 61 |
void set(T* ptr) |
| 62 |
{ |
| 63 |
ASSERT(!ptr || m_ptr != ptr); |
| 64 |
freeOwnedEflPtr(m_ptr); |
| 65 |
m_ptr = ptr; |
| 66 |
} |
| 67 |
|
| 68 |
void clear() |
| 69 |
{ |
| 70 |
T* ptr = m_ptr; |
| 71 |
m_ptr = 0; |
| 72 |
freeOwnedEflPtr(ptr); |
| 73 |
} |
| 74 |
|
| 75 |
T& operator*() const |
| 76 |
{ |
| 77 |
ASSERT(m_ptr); |
| 78 |
return *m_ptr; |
| 79 |
} |
| 80 |
|
| 81 |
T* operator->() const |
| 82 |
{ |
| 83 |
ASSERT(m_ptr); |
| 84 |
return m_ptr; |
| 85 |
} |
| 86 |
|
| 87 |
bool operator!() const { return !m_ptr; } |
| 88 |
|
| 89 |
// This conversion operator allows implicit conversion to bool but not to other integer types. |
| 90 |
typedef T* EflOwnPtr::*UnspecifiedBoolType; |
| 91 |
operator UnspecifiedBoolType() const { return m_ptr ? &EflOwnPtr::m_ptr : 0; } |
| 92 |
|
| 93 |
void swap(EflOwnPtr& o) { std::swap(m_ptr, o.m_ptr); } |
| 94 |
|
| 95 |
private: |
| 96 |
T* m_ptr; |
| 97 |
}; |
| 98 |
|
| 99 |
template <typename T> inline void swap(EflOwnPtr<T>& a, EflOwnPtr<T>& b) |
| 100 |
{ |
| 101 |
a.swap(b); |
| 102 |
} |
| 103 |
|
| 104 |
template <typename T, typename U> inline bool operator==(const EflOwnPtr<T>& a, U* b) |
| 105 |
{ |
| 106 |
return a.get() == b; |
| 107 |
} |
| 108 |
|
| 109 |
template <typename T, typename U> inline bool operator==(T* a, const EflOwnPtr<U>& b) |
| 110 |
{ |
| 111 |
return a == b.get(); |
| 112 |
} |
| 113 |
|
| 114 |
template <typename T, typename U> inline bool operator!=(const EflOwnPtr<T>& a, U* b) |
| 115 |
{ |
| 116 |
return a.get() != b; |
| 117 |
} |
| 118 |
|
| 119 |
template <typename T, typename U> inline bool operator!=(T* a, const EflOwnPtr<U>& b) |
| 120 |
{ |
| 121 |
return a != b.get(); |
| 122 |
} |
| 123 |
|
| 124 |
template <typename T> inline typename EflOwnPtr<T>::PtrType getPtr(const EflOwnPtr<T>& p) |
| 125 |
{ |
| 126 |
return p.get(); |
| 127 |
} |
| 128 |
|
| 129 |
template <typename T> inline void freeOwned(T* ptr) |
| 130 |
{ |
| 131 |
std::free(ptr); |
| 132 |
} |
| 133 |
|
| 134 |
} // namespace WTF |
| 135 |
|
| 136 |
using WTF::EflOwnPtr; |
| 137 |
|
| 138 |
#endif // EflOwnPtr_h |
| 139 |
|