Blender  V3.3
scoped_ptr.h
Go to the documentation of this file.
1 // Copyright (c) 2009 libmv authors.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to
5 // deal in the Software without restriction, including without limitation the
6 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 // sell copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19 // IN THE SOFTWARE.
20 
21 #ifndef LIBMV_BASE_SCOPED_PTR_H
22 #define LIBMV_BASE_SCOPED_PTR_H
23 
24 #include <cassert>
25 #include <cstddef>
26 
27 namespace libmv {
28 
33 template <typename T>
34 class scoped_ptr {
35  public:
36  scoped_ptr(T* resource) : resource_(resource) {}
37  ~scoped_ptr() { reset(0); }
38 
39  T* get() const { return resource_; }
40  T* operator->() const { return resource_; }
41  T& operator*() const { return *resource_; }
42 
43  void reset(T* new_resource) {
44  if (sizeof(T)) {
45  delete resource_;
46  }
47  resource_ = new_resource;
48  }
49 
50  T* release() {
51  T* released_resource = resource_;
52  resource_ = 0;
53  return released_resource;
54  }
55 
56  private:
57  // No copying allowed.
58  T* resource_;
59 };
60 
61 // Same as scoped_ptr but caller must allocate the data
62 // with new[] and the destructor will free the memory
63 // using delete[].
64 template <typename T>
65 class scoped_array {
66  public:
67  scoped_array(T* array) : array_(array) {}
69 
70  T* get() const { return array_; }
71 
72  T& operator[](std::ptrdiff_t i) const {
73  assert(i >= 0);
74  assert(array_ != NULL);
75  return array_[i];
76  }
77 
78  void reset(T* new_array) {
79  if (sizeof(T)) {
80  delete[] array_;
81  }
82  array_ = new_array;
83  }
84 
85  T* release() {
86  T* released_array = array_;
87  array_ = NULL;
88  return released_array;
89  }
90 
91  private:
92  T* array_;
93 
94  // Forbid comparison of different scoped_array types.
95  template <typename T2>
96  bool operator==(scoped_array<T2> const& p2) const;
97  template <typename T2>
98  bool operator!=(scoped_array<T2> const& p2) const;
99 
100  // Disallow evil constructors
101  scoped_array(const scoped_array&);
102  void operator=(const scoped_array&);
103 };
104 
105 } // namespace libmv
106 
107 #endif // LIBMV_BASE_SCOPED_PTR_H
void reset(T *new_array)
Definition: scoped_ptr.h:78
T * get() const
Definition: scoped_ptr.h:70
T & operator[](std::ptrdiff_t i) const
Definition: scoped_ptr.h:72
scoped_array(T *array)
Definition: scoped_ptr.h:67
T & operator*() const
Definition: scoped_ptr.h:41
void reset(T *new_resource)
Definition: scoped_ptr.h:43
scoped_ptr(T *resource)
Definition: scoped_ptr.h:36
T * get() const
Definition: scoped_ptr.h:39
T * operator->() const
Definition: scoped_ptr.h:40
#define T