ICU 52.1  52.1
localpointer.h
Go to the documentation of this file.
1 /*
2 *******************************************************************************
3 *
4 * Copyright (C) 2009-2012, 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 
44 
63 template<typename T>
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) {}
200  }
207  void adoptInstead(T *p) {
210  }
211 };
212 
231 template<typename T>
232 class LocalArray : public LocalPointerBase<T> {
233 public:
239  explicit LocalArray(T *p=NULL) : LocalPointerBase<T>(p) {}
253  LocalArray(T *p, UErrorCode &errorCode) : LocalPointerBase<T>(p) {
254  if(p==NULL && U_SUCCESS(errorCode)) {
255  errorCode=U_MEMORY_ALLOCATION_ERROR;
256  }
257  }
263  delete[] LocalPointerBase<T>::ptr;
264  }
271  void adoptInstead(T *p) {
272  delete[] LocalPointerBase<T>::ptr;
274  }
282  T &operator[](ptrdiff_t i) const { return LocalPointerBase<T>::ptr[i]; }
283 };
284 
308 #define U_DEFINE_LOCAL_OPEN_POINTER(LocalPointerClassName, Type, closeFunction) \
309  class LocalPointerClassName : public LocalPointerBase<Type> { \
310  public: \
311  explicit LocalPointerClassName(Type *p=NULL) : LocalPointerBase<Type>(p) {} \
312  ~LocalPointerClassName() { closeFunction(ptr); } \
313  void adoptInstead(Type *p) { \
314  closeFunction(ptr); \
315  ptr=p; \
316  } \
317  }
318 
320 
321 #endif /* U_SHOW_CPLUSPLUS_API */
322 #endif /* __LOCALPOINTER_H__ */
void adoptInstead(T *p)
Deletes the object it owns, and adopts (takes ownership of) the one passed in.
Definition: localpointer.h:207
bool operator==(const T *other) const
Comparison with a simple pointer, so that existing code with ==NULL need not be changed.
Definition: localpointer.h:97
#define U_SUCCESS(x)
Does the error code indicate success?
Definition: utypes.h:706
LocalArray(T *p=NULL)
Constructor takes ownership.
Definition: localpointer.h:239
T * getAlias() const
Access without ownership change.
Definition: localpointer.h:111
T * operator->() const
Access without ownership change.
Definition: localpointer.h:123
"Smart pointer" class, deletes objects via the standard C++ delete operator.
Definition: localpointer.h:186
LocalArray(T *p, UErrorCode &errorCode)
Constructor takes ownership and reports an error if NULL.
Definition: localpointer.h:253
U_EXPORT UBool operator==(const StringPiece &x, const StringPiece &y)
Global operator == for StringPiece.
T * ptr
Actual pointer.
Definition: localpointer.h:151
UBool isNull() const
NULL check.
Definition: localpointer.h:83
Memory allocation error.
Definition: utypes.h:513
T & operator*() const
Access without ownership change.
Definition: localpointer.h:117
LocalPointer(T *p=NULL)
Constructor takes ownership.
Definition: localpointer.h:193
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:129
T * orphan()
Gives up ownership; the internal pointer becomes NULL.
Definition: localpointer.h:130
LocalPointerBase(T *p=NULL)
Constructor takes ownership.
Definition: localpointer.h:71
"Smart pointer" base class; do not use directly: use LocalPointer etc.
Definition: localpointer.h:64
UBool operator!=(const StringPiece &x, const StringPiece &y)
Global operator != for StringPiece.
Definition: stringpiece.h:218
#define NULL
Define NULL if necessary, to 0 for C++ and to ((void *)0) for C.
Definition: utypes.h:186
"Smart pointer" class, deletes objects via the C++ array delete[] operator.
Definition: localpointer.h:232
~LocalPointer()
Destructor deletes the object it owns.
Definition: localpointer.h:198
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:130
void adoptInstead(T *p)
Deletes the array it owns, and adopts (takes ownership of) the one passed in.
Definition: localpointer.h:271
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers...
Definition: utypes.h:476
bool operator!=(const T *other) const
Comparison with a simple pointer, so that existing code with !=NULL need not be changed.
Definition: localpointer.h:105
~LocalArray()
Destructor deletes the array it owns.
Definition: localpointer.h:262
~LocalPointerBase()
Destructor deletes the object it owns.
Definition: localpointer.h:77
Basic definitions for ICU, for both C and C++ APIs.
UBool isValid() const
NULL check.
Definition: localpointer.h:89
void adoptInstead(T *p)
Deletes the object it owns, and adopts (takes ownership of) the one passed in.
Definition: localpointer.h:142
T & operator[](ptrdiff_t i) const
Array item access (writable).
Definition: localpointer.h:282
int8_t UBool
The ICU boolean type.
Definition: umachine.h:200