localpointer.h

Go to the documentation of this file.
00001 /*
00002 *******************************************************************************
00003 *
00004 *   Copyright (C) 2009-2012, International Business Machines
00005 *   Corporation and others.  All Rights Reserved.
00006 *
00007 *******************************************************************************
00008 *   file name:  localpointer.h
00009 *   encoding:   US-ASCII
00010 *   tab size:   8 (not used)
00011 *   indentation:4
00012 *
00013 *   created on: 2009nov13
00014 *   created by: Markus W. Scherer
00015 */
00016 
00017 #ifndef __LOCALPOINTER_H__
00018 #define __LOCALPOINTER_H__
00019 
00039 #include "unicode/utypes.h"
00040 
00041 #if U_SHOW_CPLUSPLUS_API
00042 
00043 U_NAMESPACE_BEGIN
00044 
00063 template<typename T>
00064 class LocalPointerBase {
00065 public:
00071     explicit LocalPointerBase(T *p=NULL) : ptr(p) {}
00077     ~LocalPointerBase() { /* delete ptr; */ }
00083     UBool isNull() const { return ptr==NULL; }
00089     UBool isValid() const { return ptr!=NULL; }
00097     bool operator==(const T *other) const { return ptr==other; }
00105     bool operator!=(const T *other) const { return ptr!=other; }
00111     T *getAlias() const { return ptr; }
00117     T &operator*() const { return *ptr; }
00123     T *operator->() const { return ptr; }
00130     T *orphan() {
00131         T *p=ptr;
00132         ptr=NULL;
00133         return p;
00134     }
00142     void adoptInstead(T *p) {
00143         // delete ptr;
00144         ptr=p;
00145     }
00146 protected:
00151     T *ptr;
00152 private:
00153     // No comparison operators with other LocalPointerBases.
00154     bool operator==(const LocalPointerBase &other);
00155     bool operator!=(const LocalPointerBase &other);
00156     // No ownership transfer: No copy constructor, no assignment operator.
00157     LocalPointerBase(const LocalPointerBase &other);
00158     void operator=(const LocalPointerBase &other);
00159     // No heap allocation. Use only on the stack.
00160     static void * U_EXPORT2 operator new(size_t size);
00161     static void * U_EXPORT2 operator new[](size_t size);
00162 #if U_HAVE_PLACEMENT_NEW
00163     static void * U_EXPORT2 operator new(size_t, void *ptr);
00164 #endif
00165 };
00166 
00185 template<typename T>
00186 class LocalPointer : public LocalPointerBase<T> {
00187 public:
00193     explicit LocalPointer(T *p=NULL) : LocalPointerBase<T>(p) {}
00198     ~LocalPointer() {
00199         delete LocalPointerBase<T>::ptr;
00200     }
00207     void adoptInstead(T *p) {
00208         delete LocalPointerBase<T>::ptr;
00209         LocalPointerBase<T>::ptr=p;
00210     }
00211 };
00212 
00231 template<typename T>
00232 class LocalArray : public LocalPointerBase<T> {
00233 public:
00239     explicit LocalArray(T *p=NULL) : LocalPointerBase<T>(p) {}
00244     ~LocalArray() {
00245         delete[] LocalPointerBase<T>::ptr;
00246     }
00253     void adoptInstead(T *p) {
00254         delete[] LocalPointerBase<T>::ptr;
00255         LocalPointerBase<T>::ptr=p;
00256     }
00264     T &operator[](ptrdiff_t i) const { return LocalPointerBase<T>::ptr[i]; }
00265 };
00266 
00290 #define U_DEFINE_LOCAL_OPEN_POINTER(LocalPointerClassName, Type, closeFunction) \
00291     class LocalPointerClassName : public LocalPointerBase<Type> { \
00292     public: \
00293         explicit LocalPointerClassName(Type *p=NULL) : LocalPointerBase<Type>(p) {} \
00294         ~LocalPointerClassName() { closeFunction(ptr); } \
00295         void adoptInstead(Type *p) { \
00296             closeFunction(ptr); \
00297             ptr=p; \
00298         } \
00299     }
00300 
00301 U_NAMESPACE_END
00302 
00303 #endif  /* U_SHOW_CPLUSPLUS_API */
00304 #endif  /* __LOCALPOINTER_H__ */

Generated on 25 Nov 2014 for ICU 50.1.2 by  doxygen 1.4.7