g2o
scoped_pointer.h
Go to the documentation of this file.
1 // g2o - General Graph Optimization
2 // Copyright (C) 2011 R. Kuemmerle
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 
27 #ifndef G2O_SCOPED_POINTER_H
28 #define G2O_SCOPED_POINTER_H
29 
30 #include <cassert>
31 
32 namespace g2o {
33 
34  namespace {
35  struct ScopedPointerDeleter
36  {
37  template<typename T>
38  void operator()(T* t) { delete t;}
39  };
40  }
41 
45  template <typename T, typename Del = ScopedPointerDeleter>
47  {
48  public:
49  ScopedPointer(T* t = 0) : _pointer(t) {}
50 
52  {
53  Del deleter;
54  deleter(_pointer);
55  }
56 
58  T& operator*() const { return *_pointer;}
60  T* operator->() const { return _pointer;}
62  T* get() const { return _pointer;}
63 
67  void reset(T* p)
68  {
69  assert((p == 0 || p != _pointer) && "ScopedPointer should not reset with itself");
70  ScopedPointer aux(p);
71  swap(aux);
72  }
73 
78  {
79  T* aux = b._pointer;
80  b._pointer = _pointer;
81  _pointer = aux;
82  }
83 
84  protected:
86 
87  // do not allow to copy the object
88  private:
90  const ScopedPointer& operator=(const ScopedPointer&);
91  };
92 
96  template <typename T>
98  {
99  public:
100  ScopedArray(T* t = 0) : _pointer(t) {}
101 
103  {
104  delete[] _pointer;
105  }
106 
107  T & operator[](std::ptrdiff_t i) const
108  {
109  assert(_pointer != 0 && i >= 0);
110  return _pointer[i];
111  }
112 
114  T* get() const { return _pointer;}
115 
119  void reset(T* p)
120  {
121  assert((p == 0 || p != _pointer) && "ScopedPointer should not reset with itself");
122  ScopedArray aux(p);
123  swap(aux);
124  }
125 
129  void swap(ScopedArray & b)
130  {
131  T* aux = b._pointer;
132  b._pointer = _pointer;
133  _pointer = aux;
134  }
135 
136  protected:
138 
139  // do not allow to copy the object
140  private:
141  ScopedArray(const ScopedArray&);
142  const ScopedArray& operator=(const ScopedArray&);
143  };
144 
145 } // end namespace
146 
147 #endif
T * operator->() const
access the pointer via ->
T & operator[](std::ptrdiff_t i) const
void swap(ScopedPointer &b)
T & operator*() const
dereference the pointer
a scoped pointer for an objectarray, i.e., object will be deleted on leaving the scope ...
a scoped pointer for an array, i.e., array will be deleted on leaving the scope
void swap(ScopedArray &b)