epoc32/include/stdapis/stlportv5/stl/_auto_ptr.h
author William Roberts <williamr@symbian.org>
Wed, 31 Mar 2010 12:27:01 +0100
branchSymbian2
changeset 3 e1b950c65cb4
parent 2 epoc32/include/stdapis/stlport/stl/_auto_ptr.h@2fe1408b6811
child 4 837f303aceeb
permissions -rw-r--r--
Attempt to represent the S^2->S^3 header reorganisation as a series of "hg rename" operations
     1 /*
     2  * © Portions copyright (c) 2006-2007 Nokia Corporation.  All rights reserved.
     3  * Copyright (c) 1997-1999
     4  * Silicon Graphics Computer Systems, Inc.
     5  *
     6  * Copyright (c) 1999
     7  * Boris Fomitchev
     8  *
     9  * This material is provided "as is", with absolutely no warranty expressed
    10  * or implied. Any use is at your own risk.
    11  *
    12  * Permission to use or copy this software for any purpose is hereby granted
    13  * without fee, provided the above notices are retained on all copies.
    14  * Permission to modify the code and to distribute modified code is granted,
    15  * provided the above notices are retained, and a notice that the code was
    16  * modified is included with the above copyright notice.
    17  *
    18  */
    19 
    20 #ifndef _STLP_AUTO_PTR_H
    21 # define _STLP_AUTO_PTR_H
    22 
    23 _STLP_BEGIN_NAMESPACE
    24 // implementation primitive
    25 class __ptr_base {
    26 public:
    27   void* _M_p;
    28   void  __set(const void* __p) { _M_p = __CONST_CAST(void*,__p); }
    29   void  __set(void* __p) { _M_p = __p; }
    30 };
    31 
    32 template <class _Tp> class auto_ptr_ref {
    33 public:
    34   __ptr_base& _M_r;
    35   _Tp* const _M_p;
    36 
    37   auto_ptr_ref(__ptr_base& __r, _Tp* __p) : _M_r(__r), _M_p(__p) {  }
    38 
    39   _Tp* release() const { _M_r.__set((void*)0); return _M_p; }
    40 
    41 };
    42 
    43 template<class _Tp> class auto_ptr :  public __ptr_base {
    44 public:
    45   typedef _Tp element_type;
    46   typedef auto_ptr<_Tp>           _Self;
    47 
    48   _Tp* release() {
    49     _Tp* __px = this->get();
    50     this->_M_p = 0;
    51     return __px;
    52   }
    53 
    54   void reset(_Tp* __px=0) {
    55     _Tp* __pt = this->get();
    56     if (__px != __pt)
    57       delete __pt;
    58     this->__set(__px);
    59   }
    60 
    61   _Tp* get() const { return __REINTERPRET_CAST(_Tp*,__CONST_CAST(void*,_M_p)); }
    62 
    63 # if !defined (_STLP_NO_ARROW_OPERATOR)
    64   _Tp* operator->() const {
    65     _STLP_VERBOSE_ASSERT(get()!=0, _StlMsg_AUTO_PTR_NULL)
    66     return get();
    67   }
    68 # endif
    69   _Tp& operator*() const  {
    70     _STLP_VERBOSE_ASSERT(get()!=0, _StlMsg_AUTO_PTR_NULL)
    71     return *get();
    72   }
    73 
    74   auto_ptr() {
    75     this->_M_p = 0;
    76 # ifdef _STLP_USE_TRAP_LEAVE
    77     CleanupStack::PushL(TCleanupItem(Close, (void*)this));
    78 # endif
    79   }
    80 
    81   explicit auto_ptr(_Tp* __px) {
    82     this->__set(__px);
    83 # ifdef _STLP_USE_TRAP_LEAVE
    84     CleanupStack::PushL(TCleanupItem(Close, (void*)this));
    85 # endif
    86   }
    87 
    88 #if defined (_STLP_MEMBER_TEMPLATES)
    89 # if !defined (_STLP_NO_TEMPLATE_CONVERSIONS)
    90   template<class _Tp1> auto_ptr(auto_ptr<_Tp1>& __r) {
    91     _Tp* __conversionCheck = __r.release();
    92     this->__set(__conversionCheck);
    93 # ifdef _STLP_USE_TRAP_LEAVE
    94     CleanupStack::PushL(TCleanupItem(Close, (void*)this));
    95 # endif
    96   }
    97 # endif
    98   template<class _Tp1> auto_ptr<_Tp>& operator=(auto_ptr<_Tp1>& __r) {
    99     _Tp* __conversionCheck = __r.release();
   100     reset(__conversionCheck);
   101     return *this;
   102   }
   103 #endif /* _STLP_MEMBER_TEMPLATES */
   104 
   105   auto_ptr(_Self& __r)
   106   { this->__set(__r.release());
   107 # ifdef _STLP_USE_TRAP_LEAVE
   108     CleanupStack::PushL(TCleanupItem(Close, (void*)this));
   109 # endif
   110   }
   111 
   112   _Self& operator=(_Self& __r)  {
   113     reset(__r.release());
   114     return *this;
   115   }
   116 
   117   ~auto_ptr() { _STLP_POP_ITEM reset(0); }
   118 
   119   auto_ptr(auto_ptr_ref<_Tp> __r) {
   120     this->__set(__r.release());
   121 # ifdef _STLP_USE_TRAP_LEAVE
   122     CleanupStack::PushL(TCleanupItem(Close, (void*)this));
   123 # endif
   124   }
   125 
   126   _Self& operator=(auto_ptr_ref<_Tp> __r) {
   127     reset(__r.release());
   128     return *this;
   129   }
   130 
   131 
   132   _Self& operator=(_Tp* __px) {
   133   	reset(__px);
   134     return *this;
   135   }
   136 
   137 
   138 
   139 # if defined(_STLP_MEMBER_TEMPLATES) && !defined(_STLP_NO_TEMPLATE_CONVERSIONS)
   140   template<class _Tp1> operator auto_ptr_ref<_Tp1>() {
   141     return auto_ptr_ref<_Tp1>(*this, this->get());
   142   }
   143   template<class _Tp1> operator auto_ptr<_Tp1>() {
   144     return auto_ptr<_Tp1>(release());
   145   }
   146 # else
   147   operator auto_ptr_ref<_Tp>()
   148   { return auto_ptr_ref<_Tp>(*this, this->get()); }
   149 # endif
   150 
   151 # ifdef _STLP_USE_TRAP_LEAVE
   152   static void Close(void* aPtr);
   153 # endif
   154 
   155 };
   156 
   157 # ifdef _STLP_USE_TRAP_LEAVE
   158 template <class _Tp>
   159 void
   160 auto_ptr<_Tp>::Close(void* aPtr)
   161 {
   162   auto_ptr<_Tp>* self = (auto_ptr<_Tp>*)aPtr;
   163   self->reset(0);
   164 }
   165 # endif
   166 
   167 
   168 _STLP_END_NAMESPACE
   169 
   170 #endif /* _STLP_AUTO_PTR_H */
   171 
   172 // Local Variables:
   173 // mode:C++
   174 // End:
   175