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