Ice 3.7 C++11 API Reference
UniquePtr.h
Go to the documentation of this file.
1 //
2 // Copyright (c) ZeroC, Inc. All rights reserved.
3 //
4 
5 #ifndef ICE_UNIQUE_PTR_H
6 #define ICE_UNIQUE_PTR_H
7 
8 #include <Ice/Config.h>
9 
10 namespace IceInternal
11 {
12 
13 #ifdef ICE_CPP11_MAPPING
14 
15 template<typename T>
16 using UniquePtr = std::unique_ptr<T>;
17 
18 #else
19 
20 template<typename T>
21 class UniquePtr
22 {
23 public:
24 
25  explicit UniquePtr(T* ptr = 0) :
26  _ptr(ptr)
27  {
28  }
29 
30  ~UniquePtr()
31  {
32  if(_ptr != 0)
33  {
34  delete _ptr;
35  }
36  }
37 
38  T* release()
39  {
40  T* r = _ptr;
41  _ptr = 0;
42  return r;
43  }
44 
45  void reset(T* ptr = 0)
46  {
47  assert(ptr == 0 || ptr != _ptr);
48 
49  if(_ptr != 0)
50  {
51  delete _ptr;
52  }
53  _ptr = ptr;
54  }
55 
56  T& operator*() const
57  {
58  return *_ptr;
59  }
60 
61  T* operator->() const
62  {
63  return _ptr;
64  }
65 
66  T* get() const
67  {
68  return _ptr;
69  }
70 
71  operator bool() const
72  {
73  return _ptr != 0;
74  }
75 
76  void swap(UniquePtr& a)
77  {
78  T* tmp = a._ptr;
79  a._ptr = _ptr;
80  _ptr = tmp;
81  }
82 
83 private:
84 
85  UniquePtr(UniquePtr&);
86  UniquePtr& operator=(UniquePtr&);
87 
88  T* _ptr;
89 };
90 
91 #endif
92 
93 }
94 
95 #endif
Config.h