ICU 4.8.1.1  4.8.1.1
 All Data Structures 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-2010, 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>
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:
147  T *ptr;
148 private:
149  // No comparison operators with other LocalPointerBases.
150  bool operator==(const LocalPointerBase &other);
151  bool operator!=(const LocalPointerBase &other);
152  // No ownership transfer: No copy constructor, no assignment operator.
153  LocalPointerBase(const LocalPointerBase &other);
154  void operator=(const LocalPointerBase &other);
155  // No heap allocation. Use only on the stack.
156  static void * U_EXPORT2 operator new(size_t size);
157  static void * U_EXPORT2 operator new[](size_t size);
158 #if U_HAVE_PLACEMENT_NEW
159  static void * U_EXPORT2 operator new(size_t, void *ptr);
160 #endif
161 };
162 
181 template<typename T>
182 class LocalPointer : public LocalPointerBase<T> {
183 public:
189  explicit LocalPointer(T *p=NULL) : LocalPointerBase<T>(p) {}
194  ~LocalPointer() {
195  delete LocalPointerBase<T>::ptr;
196  }
203  void adoptInstead(T *p) {
204  delete LocalPointerBase<T>::ptr;
205  LocalPointerBase<T>::ptr=p;
206  }
207 };
208 
227 template<typename T>
228 class LocalArray : public LocalPointerBase<T> {
229 public:
235  explicit LocalArray(T *p=NULL) : LocalPointerBase<T>(p) {}
240  ~LocalArray() {
241  delete[] LocalPointerBase<T>::ptr;
242  }
249  void adoptInstead(T *p) {
250  delete[] LocalPointerBase<T>::ptr;
251  LocalPointerBase<T>::ptr=p;
252  }
260  T &operator[](ptrdiff_t i) const { return LocalPointerBase<T>::ptr[i]; }
261 };
262 
286 #define U_DEFINE_LOCAL_OPEN_POINTER(LocalPointerClassName, Type, closeFunction) \
287  class LocalPointerClassName : public LocalPointerBase<Type> { \
288  public: \
289  explicit LocalPointerClassName(Type *p=NULL) : LocalPointerBase<Type>(p) {} \
290  ~LocalPointerClassName() { closeFunction(ptr); } \
291  void adoptInstead(Type *p) { \
292  closeFunction(ptr); \
293  ptr=p; \
294  } \
295  }
296 
298 
299 #endif /* U_SHOW_CPLUSPLUS_API */
300 #endif /* __LOCALPOINTER_H__ */
U_EXPORT UBool operator==(const StringPiece &x, const StringPiece &y)
Global operator == for StringPiece.
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:131
#define NULL
Define NULL if necessary, to 0 for C++ and to ((void *)0) for C.
Definition: utypes.h:299
#define U_EXPORT2
Definition: platform.h:314
UBool operator!=(const StringPiece &x, const StringPiece &y)
Global operator != for StringPiece.
Definition: stringpiece.h:218
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:132
Basic definitions for ICU, for both C and C++ APIs.
int8_t UBool
The ICU boolean type.
Definition: umachine.h:228