Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | ******************************************************************************* |
michael@0 | 3 | * |
michael@0 | 4 | * Copyright (C) 2009-2012, International Business Machines |
michael@0 | 5 | * Corporation and others. All Rights Reserved. |
michael@0 | 6 | * |
michael@0 | 7 | ******************************************************************************* |
michael@0 | 8 | * file name: localpointer.h |
michael@0 | 9 | * encoding: US-ASCII |
michael@0 | 10 | * tab size: 8 (not used) |
michael@0 | 11 | * indentation:4 |
michael@0 | 12 | * |
michael@0 | 13 | * created on: 2009nov13 |
michael@0 | 14 | * created by: Markus W. Scherer |
michael@0 | 15 | */ |
michael@0 | 16 | |
michael@0 | 17 | #ifndef __LOCALPOINTER_H__ |
michael@0 | 18 | #define __LOCALPOINTER_H__ |
michael@0 | 19 | |
michael@0 | 20 | /** |
michael@0 | 21 | * \file |
michael@0 | 22 | * \brief C++ API: "Smart pointers" for use with and in ICU4C C++ code. |
michael@0 | 23 | * |
michael@0 | 24 | * These classes are inspired by |
michael@0 | 25 | * - std::auto_ptr |
michael@0 | 26 | * - boost::scoped_ptr & boost::scoped_array |
michael@0 | 27 | * - Taligent Safe Pointers (TOnlyPointerTo) |
michael@0 | 28 | * |
michael@0 | 29 | * but none of those provide for all of the goals for ICU smart pointers: |
michael@0 | 30 | * - Smart pointer owns the object and releases it when it goes out of scope. |
michael@0 | 31 | * - No transfer of ownership via copy/assignment to reduce misuse. Simpler & more robust. |
michael@0 | 32 | * - ICU-compatible: No exceptions. |
michael@0 | 33 | * - Need to be able to orphan/release the pointer and its ownership. |
michael@0 | 34 | * - Need variants for normal C++ object pointers, C++ arrays, and ICU C service objects. |
michael@0 | 35 | * |
michael@0 | 36 | * For details see http://site.icu-project.org/design/cpp/scoped_ptr |
michael@0 | 37 | */ |
michael@0 | 38 | |
michael@0 | 39 | #include "unicode/utypes.h" |
michael@0 | 40 | |
michael@0 | 41 | #if U_SHOW_CPLUSPLUS_API |
michael@0 | 42 | |
michael@0 | 43 | U_NAMESPACE_BEGIN |
michael@0 | 44 | |
michael@0 | 45 | /** |
michael@0 | 46 | * "Smart pointer" base class; do not use directly: use LocalPointer etc. |
michael@0 | 47 | * |
michael@0 | 48 | * Base class for smart pointer classes that do not throw exceptions. |
michael@0 | 49 | * |
michael@0 | 50 | * Do not use this base class directly, since it does not delete its pointer. |
michael@0 | 51 | * A subclass must implement methods that delete the pointer: |
michael@0 | 52 | * Destructor and adoptInstead(). |
michael@0 | 53 | * |
michael@0 | 54 | * There is no operator T *() provided because the programmer must decide |
michael@0 | 55 | * whether to use getAlias() (without transfer of ownership) or orpan() |
michael@0 | 56 | * (with transfer of ownership and NULLing of the pointer). |
michael@0 | 57 | * |
michael@0 | 58 | * @see LocalPointer |
michael@0 | 59 | * @see LocalArray |
michael@0 | 60 | * @see U_DEFINE_LOCAL_OPEN_POINTER |
michael@0 | 61 | * @stable ICU 4.4 |
michael@0 | 62 | */ |
michael@0 | 63 | template<typename T> |
michael@0 | 64 | class LocalPointerBase { |
michael@0 | 65 | public: |
michael@0 | 66 | /** |
michael@0 | 67 | * Constructor takes ownership. |
michael@0 | 68 | * @param p simple pointer to an object that is adopted |
michael@0 | 69 | * @stable ICU 4.4 |
michael@0 | 70 | */ |
michael@0 | 71 | explicit LocalPointerBase(T *p=NULL) : ptr(p) {} |
michael@0 | 72 | /** |
michael@0 | 73 | * Destructor deletes the object it owns. |
michael@0 | 74 | * Subclass must override: Base class does nothing. |
michael@0 | 75 | * @stable ICU 4.4 |
michael@0 | 76 | */ |
michael@0 | 77 | ~LocalPointerBase() { /* delete ptr; */ } |
michael@0 | 78 | /** |
michael@0 | 79 | * NULL check. |
michael@0 | 80 | * @return TRUE if ==NULL |
michael@0 | 81 | * @stable ICU 4.4 |
michael@0 | 82 | */ |
michael@0 | 83 | UBool isNull() const { return ptr==NULL; } |
michael@0 | 84 | /** |
michael@0 | 85 | * NULL check. |
michael@0 | 86 | * @return TRUE if !=NULL |
michael@0 | 87 | * @stable ICU 4.4 |
michael@0 | 88 | */ |
michael@0 | 89 | UBool isValid() const { return ptr!=NULL; } |
michael@0 | 90 | /** |
michael@0 | 91 | * Comparison with a simple pointer, so that existing code |
michael@0 | 92 | * with ==NULL need not be changed. |
michael@0 | 93 | * @param other simple pointer for comparison |
michael@0 | 94 | * @return true if this pointer value equals other |
michael@0 | 95 | * @stable ICU 4.4 |
michael@0 | 96 | */ |
michael@0 | 97 | bool operator==(const T *other) const { return ptr==other; } |
michael@0 | 98 | /** |
michael@0 | 99 | * Comparison with a simple pointer, so that existing code |
michael@0 | 100 | * with !=NULL need not be changed. |
michael@0 | 101 | * @param other simple pointer for comparison |
michael@0 | 102 | * @return true if this pointer value differs from other |
michael@0 | 103 | * @stable ICU 4.4 |
michael@0 | 104 | */ |
michael@0 | 105 | bool operator!=(const T *other) const { return ptr!=other; } |
michael@0 | 106 | /** |
michael@0 | 107 | * Access without ownership change. |
michael@0 | 108 | * @return the pointer value |
michael@0 | 109 | * @stable ICU 4.4 |
michael@0 | 110 | */ |
michael@0 | 111 | T *getAlias() const { return ptr; } |
michael@0 | 112 | /** |
michael@0 | 113 | * Access without ownership change. |
michael@0 | 114 | * @return the pointer value as a reference |
michael@0 | 115 | * @stable ICU 4.4 |
michael@0 | 116 | */ |
michael@0 | 117 | T &operator*() const { return *ptr; } |
michael@0 | 118 | /** |
michael@0 | 119 | * Access without ownership change. |
michael@0 | 120 | * @return the pointer value |
michael@0 | 121 | * @stable ICU 4.4 |
michael@0 | 122 | */ |
michael@0 | 123 | T *operator->() const { return ptr; } |
michael@0 | 124 | /** |
michael@0 | 125 | * Gives up ownership; the internal pointer becomes NULL. |
michael@0 | 126 | * @return the pointer value; |
michael@0 | 127 | * caller becomes responsible for deleting the object |
michael@0 | 128 | * @stable ICU 4.4 |
michael@0 | 129 | */ |
michael@0 | 130 | T *orphan() { |
michael@0 | 131 | T *p=ptr; |
michael@0 | 132 | ptr=NULL; |
michael@0 | 133 | return p; |
michael@0 | 134 | } |
michael@0 | 135 | /** |
michael@0 | 136 | * Deletes the object it owns, |
michael@0 | 137 | * and adopts (takes ownership of) the one passed in. |
michael@0 | 138 | * Subclass must override: Base class does not delete the object. |
michael@0 | 139 | * @param p simple pointer to an object that is adopted |
michael@0 | 140 | * @stable ICU 4.4 |
michael@0 | 141 | */ |
michael@0 | 142 | void adoptInstead(T *p) { |
michael@0 | 143 | // delete ptr; |
michael@0 | 144 | ptr=p; |
michael@0 | 145 | } |
michael@0 | 146 | protected: |
michael@0 | 147 | /** |
michael@0 | 148 | * Actual pointer. |
michael@0 | 149 | * @internal |
michael@0 | 150 | */ |
michael@0 | 151 | T *ptr; |
michael@0 | 152 | private: |
michael@0 | 153 | // No comparison operators with other LocalPointerBases. |
michael@0 | 154 | bool operator==(const LocalPointerBase &other); |
michael@0 | 155 | bool operator!=(const LocalPointerBase &other); |
michael@0 | 156 | // No ownership transfer: No copy constructor, no assignment operator. |
michael@0 | 157 | LocalPointerBase(const LocalPointerBase &other); |
michael@0 | 158 | void operator=(const LocalPointerBase &other); |
michael@0 | 159 | // No heap allocation. Use only on the stack. |
michael@0 | 160 | static void * U_EXPORT2 operator new(size_t size); |
michael@0 | 161 | static void * U_EXPORT2 operator new[](size_t size); |
michael@0 | 162 | #if U_HAVE_PLACEMENT_NEW |
michael@0 | 163 | static void * U_EXPORT2 operator new(size_t, void *ptr); |
michael@0 | 164 | #endif |
michael@0 | 165 | }; |
michael@0 | 166 | |
michael@0 | 167 | /** |
michael@0 | 168 | * "Smart pointer" class, deletes objects via the standard C++ delete operator. |
michael@0 | 169 | * For most methods see the LocalPointerBase base class. |
michael@0 | 170 | * |
michael@0 | 171 | * Usage example: |
michael@0 | 172 | * \code |
michael@0 | 173 | * LocalPointer<UnicodeString> s(new UnicodeString((UChar32)0x50005)); |
michael@0 | 174 | * int32_t length=s->length(); // 2 |
michael@0 | 175 | * UChar lead=s->charAt(0); // 0xd900 |
michael@0 | 176 | * if(some condition) { return; } // no need to explicitly delete the pointer |
michael@0 | 177 | * s.adoptInstead(new UnicodeString((UChar)0xfffc)); |
michael@0 | 178 | * length=s->length(); // 1 |
michael@0 | 179 | * // no need to explicitly delete the pointer |
michael@0 | 180 | * \endcode |
michael@0 | 181 | * |
michael@0 | 182 | * @see LocalPointerBase |
michael@0 | 183 | * @stable ICU 4.4 |
michael@0 | 184 | */ |
michael@0 | 185 | template<typename T> |
michael@0 | 186 | class LocalPointer : public LocalPointerBase<T> { |
michael@0 | 187 | public: |
michael@0 | 188 | /** |
michael@0 | 189 | * Constructor takes ownership. |
michael@0 | 190 | * @param p simple pointer to an object that is adopted |
michael@0 | 191 | * @stable ICU 4.4 |
michael@0 | 192 | */ |
michael@0 | 193 | explicit LocalPointer(T *p=NULL) : LocalPointerBase<T>(p) {} |
michael@0 | 194 | /** |
michael@0 | 195 | * Destructor deletes the object it owns. |
michael@0 | 196 | * @stable ICU 4.4 |
michael@0 | 197 | */ |
michael@0 | 198 | ~LocalPointer() { |
michael@0 | 199 | delete LocalPointerBase<T>::ptr; |
michael@0 | 200 | } |
michael@0 | 201 | /** |
michael@0 | 202 | * Deletes the object it owns, |
michael@0 | 203 | * and adopts (takes ownership of) the one passed in. |
michael@0 | 204 | * @param p simple pointer to an object that is adopted |
michael@0 | 205 | * @stable ICU 4.4 |
michael@0 | 206 | */ |
michael@0 | 207 | void adoptInstead(T *p) { |
michael@0 | 208 | delete LocalPointerBase<T>::ptr; |
michael@0 | 209 | LocalPointerBase<T>::ptr=p; |
michael@0 | 210 | } |
michael@0 | 211 | }; |
michael@0 | 212 | |
michael@0 | 213 | /** |
michael@0 | 214 | * "Smart pointer" class, deletes objects via the C++ array delete[] operator. |
michael@0 | 215 | * For most methods see the LocalPointerBase base class. |
michael@0 | 216 | * Adds operator[] for array item access. |
michael@0 | 217 | * |
michael@0 | 218 | * Usage example: |
michael@0 | 219 | * \code |
michael@0 | 220 | * LocalArray<UnicodeString> a(new UnicodeString[2]); |
michael@0 | 221 | * a[0].append((UChar)0x61); |
michael@0 | 222 | * if(some condition) { return; } // no need to explicitly delete the array |
michael@0 | 223 | * a.adoptInstead(new UnicodeString[4]); |
michael@0 | 224 | * a[3].append((UChar)0x62).append((UChar)0x63).reverse(); |
michael@0 | 225 | * // no need to explicitly delete the array |
michael@0 | 226 | * \endcode |
michael@0 | 227 | * |
michael@0 | 228 | * @see LocalPointerBase |
michael@0 | 229 | * @stable ICU 4.4 |
michael@0 | 230 | */ |
michael@0 | 231 | template<typename T> |
michael@0 | 232 | class LocalArray : public LocalPointerBase<T> { |
michael@0 | 233 | public: |
michael@0 | 234 | /** |
michael@0 | 235 | * Constructor takes ownership. |
michael@0 | 236 | * @param p simple pointer to an array of T objects that is adopted |
michael@0 | 237 | * @stable ICU 4.4 |
michael@0 | 238 | */ |
michael@0 | 239 | explicit LocalArray(T *p=NULL) : LocalPointerBase<T>(p) {} |
michael@0 | 240 | /** |
michael@0 | 241 | * Destructor deletes the array it owns. |
michael@0 | 242 | * @stable ICU 4.4 |
michael@0 | 243 | */ |
michael@0 | 244 | ~LocalArray() { |
michael@0 | 245 | delete[] LocalPointerBase<T>::ptr; |
michael@0 | 246 | } |
michael@0 | 247 | /** |
michael@0 | 248 | * Deletes the array it owns, |
michael@0 | 249 | * and adopts (takes ownership of) the one passed in. |
michael@0 | 250 | * @param p simple pointer to an array of T objects that is adopted |
michael@0 | 251 | * @stable ICU 4.4 |
michael@0 | 252 | */ |
michael@0 | 253 | void adoptInstead(T *p) { |
michael@0 | 254 | delete[] LocalPointerBase<T>::ptr; |
michael@0 | 255 | LocalPointerBase<T>::ptr=p; |
michael@0 | 256 | } |
michael@0 | 257 | /** |
michael@0 | 258 | * Array item access (writable). |
michael@0 | 259 | * No index bounds check. |
michael@0 | 260 | * @param i array index |
michael@0 | 261 | * @return reference to the array item |
michael@0 | 262 | * @stable ICU 4.4 |
michael@0 | 263 | */ |
michael@0 | 264 | T &operator[](ptrdiff_t i) const { return LocalPointerBase<T>::ptr[i]; } |
michael@0 | 265 | }; |
michael@0 | 266 | |
michael@0 | 267 | /** |
michael@0 | 268 | * \def U_DEFINE_LOCAL_OPEN_POINTER |
michael@0 | 269 | * "Smart pointer" definition macro, deletes objects via the closeFunction. |
michael@0 | 270 | * Defines a subclass of LocalPointerBase which works just |
michael@0 | 271 | * like LocalPointer<Type> except that this subclass will use the closeFunction |
michael@0 | 272 | * rather than the C++ delete operator. |
michael@0 | 273 | * |
michael@0 | 274 | * Requirement: The closeFunction must tolerate a NULL pointer. |
michael@0 | 275 | * (We could add a NULL check here but it is normally redundant.) |
michael@0 | 276 | * |
michael@0 | 277 | * Usage example: |
michael@0 | 278 | * \code |
michael@0 | 279 | * LocalUCaseMapPointer csm(ucasemap_open(localeID, options, &errorCode)); |
michael@0 | 280 | * utf8OutLength=ucasemap_utf8ToLower(csm.getAlias(), |
michael@0 | 281 | * utf8Out, (int32_t)sizeof(utf8Out), |
michael@0 | 282 | * utf8In, utf8InLength, &errorCode); |
michael@0 | 283 | * if(U_FAILURE(errorCode)) { return; } // no need to explicitly delete the UCaseMap |
michael@0 | 284 | * \endcode |
michael@0 | 285 | * |
michael@0 | 286 | * @see LocalPointerBase |
michael@0 | 287 | * @see LocalPointer |
michael@0 | 288 | * @stable ICU 4.4 |
michael@0 | 289 | */ |
michael@0 | 290 | #define U_DEFINE_LOCAL_OPEN_POINTER(LocalPointerClassName, Type, closeFunction) \ |
michael@0 | 291 | class LocalPointerClassName : public LocalPointerBase<Type> { \ |
michael@0 | 292 | public: \ |
michael@0 | 293 | explicit LocalPointerClassName(Type *p=NULL) : LocalPointerBase<Type>(p) {} \ |
michael@0 | 294 | ~LocalPointerClassName() { closeFunction(ptr); } \ |
michael@0 | 295 | void adoptInstead(Type *p) { \ |
michael@0 | 296 | closeFunction(ptr); \ |
michael@0 | 297 | ptr=p; \ |
michael@0 | 298 | } \ |
michael@0 | 299 | } |
michael@0 | 300 | |
michael@0 | 301 | U_NAMESPACE_END |
michael@0 | 302 | |
michael@0 | 303 | #endif /* U_SHOW_CPLUSPLUS_API */ |
michael@0 | 304 | #endif /* __LOCALPOINTER_H__ */ |