ICU 55.1 55.1

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
localpointer.h
Go to the documentation of this file.
1 /*
2 *******************************************************************************
3 *
4 * Copyright (C) 2009-2014, International Business Machines
5 * Corporation and others. All Rights Reserved.
6 *
7 *******************************************************************************
8 * file name: localpointer.h
9 * encoding: US-ASCII
10 * tab size: 8 (not used)
11 * indentation:4
12 *
13 * created on: 2009nov13
14 * created by: Markus W. Scherer
15 */
16
17 #ifndef __LOCALPOINTER_H__
18 #define __LOCALPOINTER_H__
19
39 #include "unicode/utypes.h"
40
41 #if U_SHOW_CPLUSPLUS_API
42
43 U_NAMESPACE_BEGIN
44
63 template<typename T>
64 class LocalPointerBase {
65 public:
71 explicit LocalPointerBase(T *p=NULL) : ptr(p) {}
77 ~LocalPointerBase() { /* delete ptr; */ }
83 UBool isNull() const { return ptr==NULL; }
89 UBool isValid() const { return ptr!=NULL; }
97 bool operator==(const T *other) const { return ptr==other; }
105 bool operator!=(const T *other) const { return ptr!=other; }
111 T *getAlias() const { return ptr; }
117 T &operator*() const { return *ptr; }
123 T *operator->() const { return ptr; }
130 T *orphan() {
131 T *p=ptr;
132 ptr=NULL;
133 return p;
134 }
142 void adoptInstead(T *p) {
143 // delete ptr;
144 ptr=p;
145 }
146 protected:
151 T *ptr;
152 private:
153 // No comparison operators with other LocalPointerBases.
154 bool operator==(const LocalPointerBase &other);
155 bool operator!=(const LocalPointerBase &other);
156 // No ownership transfer: No copy constructor, no assignment operator.
157 LocalPointerBase(const LocalPointerBase &other);
158 void operator=(const LocalPointerBase &other);
159 // No heap allocation. Use only on the stack.
160 static void * U_EXPORT2 operator new(size_t size);
161 static void * U_EXPORT2 operator new[](size_t size);
162 #if U_HAVE_PLACEMENT_NEW
163 static void * U_EXPORT2 operator new(size_t, void *ptr);
164 #endif
165 };
166
185 template<typename T>
186 class LocalPointer : public LocalPointerBase<T> {
187 public:
193 explicit LocalPointer(T *p=NULL) : LocalPointerBase<T>(p) {}
194 #ifndef U_HIDE_DRAFT_API
195
208 LocalPointer(T *p, UErrorCode &errorCode) : LocalPointerBase<T>(p) {
209 if(p==NULL && U_SUCCESS(errorCode)) {
210 errorCode=U_MEMORY_ALLOCATION_ERROR;
211 }
212 }
213 #endif /* U_HIDE_DRAFT_API */
214
218 ~LocalPointer() {
219 delete LocalPointerBase<T>::ptr;
220 }
227 void adoptInstead(T *p) {
228 delete LocalPointerBase<T>::ptr;
229 LocalPointerBase<T>::ptr=p;
230 }
231 #ifndef U_HIDE_DRAFT_API
232
247 void adoptInsteadAndCheckErrorCode(T *p, UErrorCode &errorCode) {
248 if(U_SUCCESS(errorCode)) {
249 delete LocalPointerBase<T>::ptr;
250 LocalPointerBase<T>::ptr=p;
251 if(p==NULL) {
252 errorCode=U_MEMORY_ALLOCATION_ERROR;
253 }
254 } else {
255 delete p;
256 }
257 }
258 #endif /* U_HIDE_DRAFT_API */
259 };
260
279 template<typename T>
280 class LocalArray : public LocalPointerBase<T> {
281 public:
287 explicit LocalArray(T *p=NULL) : LocalPointerBase<T>(p) {}
292 ~LocalArray() {
293 delete[] LocalPointerBase<T>::ptr;
294 }
301 void adoptInstead(T *p) {
302 delete[] LocalPointerBase<T>::ptr;
303 LocalPointerBase<T>::ptr=p;
304 }
312 T &operator[](ptrdiff_t i) const { return LocalPointerBase<T>::ptr[i]; }
313 };
314
338 #define U_DEFINE_LOCAL_OPEN_POINTER(LocalPointerClassName, Type, closeFunction) \
339 class LocalPointerClassName : public LocalPointerBase<Type> { \
340 public: \
341 explicit LocalPointerClassName(Type *p=NULL) : LocalPointerBase<Type>(p) {} \
342 ~LocalPointerClassName() { closeFunction(ptr); } \
343 void adoptInstead(Type *p) { \
344 closeFunction(ptr); \
345 ptr=p; \
346 } \
347 }
348
349 U_NAMESPACE_END
350
351 #endif /* U_SHOW_CPLUSPLUS_API */
352 #endif /* __LOCALPOINTER_H__ */