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

- File List
- Globals

# localpointer.h

Go to the documentation of this file.

    00001 /*
    00002 *******************************************************************************
    00003 *
    00004 *   Copyright (C) 2009, 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:
    00147     T *ptr;
    00148 private:
    00149     // No comparison operators with other LocalPointerBases.
    00150     bool operator==(const LocalPointerBase &other);
    00151     bool operator!=(const LocalPointerBase &other);
    00152     // No ownership transfer: No copy constructor, no assignment operator.
    00153     LocalPointerBase(const LocalPointerBase &other);
    00154     void operator=(const LocalPointerBase &other);
    00155     // No heap allocation. Use only on the stack.
    00156     static void * U_EXPORT2 operator new(size_t size);
    00157     static void * U_EXPORT2 operator new[](size_t size);
    00158 #if U_HAVE_PLACEMENT_NEW
    00159     static void * U_EXPORT2 operator new(size_t, void *ptr);
    00160 #endif
    00161 };
    00162
    00181 template<typename T>
    00182 class LocalPointer : public LocalPointerBase<T> {
    00183 public:
    00189     explicit LocalPointer(T *p=NULL) : LocalPointerBase<T>(p) {}
    00194     ~LocalPointer() {
    00195         delete LocalPointerBase<T>::ptr;
    00196     }
    00203     void adoptInstead(T *p) {
    00204         delete LocalPointerBase<T>::ptr;
    00205         LocalPointerBase<T>::ptr=p;
    00206     }
    00207 };
    00208
    00227 template<typename T>
    00228 class LocalArray : public LocalPointerBase<T> {
    00229 public:
    00235     explicit LocalArray(T *p=NULL) : LocalPointerBase<T>(p) {}
    00240     ~LocalArray() {
    00241         delete[] LocalPointerBase<T>::ptr;
    00242     }
    00249     void adoptInstead(T *p) {
    00250         delete[] LocalPointerBase<T>::ptr;
    00251         LocalPointerBase<T>::ptr=p;
    00252     }
    00260     T &operator[](ptrdiff_t i) const { return LocalPointerBase<T>::ptr[i]; }
    00261 };
    00262
    00286 #define U_DEFINE_LOCAL_OPEN_POINTER(LocalPointerClassName, Type, closeFunction) \
    00287     class LocalPointerClassName : public LocalPointerBase<Type> { \
    00288     public: \
    00289         explicit LocalPointerClassName(Type *p=NULL) : LocalPointerBase<Type>(p) {} \
    00290         ~LocalPointerClassName() { closeFunction(ptr); } \
    00291         void adoptInstead(Type *p) { \
    00292             closeFunction(ptr); \
    00293             ptr=p; \
    00294         } \
    00295     }
    00296
    00297 U_NAMESPACE_END
    00298
    00299 #endif  /* U_SHOW_CPLUSPLUS_API */
    00300 #endif  /* __LOCALPOINTER_H__ */

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines