Pallas Solver  0.1
C++ Global Optimization Algorithms
scoped_ptr.h
1 // Ceres Solver - A fast non-linear least squares minimizer
2 // Copyright 2015 Google Inc. All rights reserved.
3 // http://ceres-solver.org/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors may be
14 // used to endorse or promote products derived from this software without
15 // specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
28 //
29 // Author: jorg@google.com (Jorg Brown)
30 //
31 // This is an implementation designed to match the anticipated future TR2
32 // implementation of the scoped_ptr class, and its closely-related brethren,
33 // scoped_array, scoped_ptr_malloc, and make_scoped_ptr.
34 
35 #ifndef PALLAS_SCOPED_PTR_H
36 #define PALLAS_SCOPED_PTR_H
37 
38 #include <assert.h>
39 #include <stdlib.h>
40 #include <cstddef>
41 #include <algorithm>
42 
43 namespace pallas {
44 
45  template <class C> class scoped_ptr;
46  template <class C, class Free> class scoped_ptr_malloc;
47  template <class C> class scoped_array;
48 
49  template <class C>
50  scoped_ptr<C> make_scoped_ptr(C *);
60  template <class C>
61  class scoped_ptr {
62  public:
63  typedef C element_type;
71  explicit scoped_ptr(C* p = NULL) : ptr_(p) { }
72 
79  enum { type_must_be_complete = sizeof(C) };
80  delete ptr_;
81  }
82 
88  void reset(C* p = NULL) {
89  if (p != ptr_) {
90  enum { type_must_be_complete = sizeof(C) };
91  delete ptr_;
92  ptr_ = p;
93  }
94  }
95 
96 
101  C& operator*() const {
102  assert(ptr_ != NULL);
103  return *ptr_;
104  }
105 
110  C* operator->() const {
111  assert(ptr_ != NULL);
112  return ptr_;
113  }
114 
118  C* get() const { return ptr_; }
119 
123  bool operator==(const C* p) const { return ptr_ == p; }
124 
128  bool operator!=(const C* p) const { return ptr_ != p; }
129 
133  void swap(scoped_ptr& p2) {
134  C* tmp = ptr_;
135  ptr_ = p2.ptr_;
136  p2.ptr_ = tmp;
137  }
138 
146  C* release() {
147  C* retVal = ptr_;
148  ptr_ = NULL;
149  return retVal;
150  }
151 
152  private:
153  C* ptr_;
154 
159  friend scoped_ptr<C> make_scoped_ptr<C>(C *p);
160 
161  // Forbid comparison of scoped_ptr types. If C2 != C, it totally doesn't
162  // make sense, and if C2 == C, it still doesn't make sense because you should
163  // never have the same object owned by two different scoped_ptrs.
164  template <class C2> bool operator==(scoped_ptr<C2> const& p2) const;
165  template <class C2> bool operator!=(scoped_ptr<C2> const& p2) const;
166 
167  // Disallow evil constructors
168  scoped_ptr(const scoped_ptr&);
169  void operator=(const scoped_ptr&);
170  };
171 
172 // Free functions
173  template <class C>
174  inline void swap(scoped_ptr<C>& p1, scoped_ptr<C>& p2) {
175  p1.swap(p2);
176  }
177 
178  template <class C>
179  inline bool operator==(const C* p1, const scoped_ptr<C>& p2) {
180  return p1 == p2.get();
181  }
182 
183  template <class C>
184  inline bool operator==(const C* p1, const scoped_ptr<const C>& p2) {
185  return p1 == p2.get();
186  }
187 
188  template <class C>
189  inline bool operator!=(const C* p1, const scoped_ptr<C>& p2) {
190  return p1 != p2.get();
191  }
192 
193  template <class C>
194  inline bool operator!=(const C* p1, const scoped_ptr<const C>& p2) {
195  return p1 != p2.get();
196  }
197 
209  template <class C>
211  return scoped_ptr<C>(p);
212  }
213 
225  template <class C>
226  class scoped_array {
227  public:
228  typedef C element_type;
236  explicit scoped_array(C* p = NULL) : array_(p) { }
237 
244  enum { type_must_be_complete = sizeof(C) };
245  delete[] array_;
246  }
247 
253  void reset(C* p = NULL) {
254  if (p != array_) {
255  enum { type_must_be_complete = sizeof(C) };
256  delete[] array_;
257  array_ = p;
258  }
259  }
260 
265  C& operator[](std::ptrdiff_t i) const {
266  assert(i >= 0);
267  assert(array_ != NULL);
268  return array_[i];
269  }
270 
275  C* get() const {
276  return array_;
277  }
278 
283  bool operator==(const C* p) const { return array_ == p; }
284 
289  bool operator!=(const C* p) const { return array_ != p; }
290 
294  void swap(scoped_array& p2) {
295  C* tmp = array_;
296  array_ = p2.array_;
297  p2.array_ = tmp;
298  }
299 
307  C* release() {
308  C* retVal = array_;
309  array_ = NULL;
310  return retVal;
311  }
312 
313  private:
314  C* array_;
315 
316  // Forbid comparison of different scoped_array types.
317  template <class C2> bool operator==(scoped_array<C2> const& p2) const;
318  template <class C2> bool operator!=(scoped_array<C2> const& p2) const;
319 
320  // Disallow evil constructors
321  scoped_array(const scoped_array&);
322  void operator=(const scoped_array&);
323  };
324 
325 // Free functions
326  template <class C>
327  inline void swap(scoped_array<C>& p1, scoped_array<C>& p2) {
328  p1.swap(p2);
329  }
330 
331  template <class C>
332  inline bool operator==(const C* p1, const scoped_array<C>& p2) {
333  return p1 == p2.get();
334  }
335 
336  template <class C>
337  inline bool operator==(const C* p1, const scoped_array<const C>& p2) {
338  return p1 == p2.get();
339  }
340 
341  template <class C>
342  inline bool operator!=(const C* p1, const scoped_array<C>& p2) {
343  return p1 != p2.get();
344  }
345 
346  template <class C>
347  inline bool operator!=(const C* p1, const scoped_array<const C>& p2) {
348  return p1 != p2.get();
349  }
350 
356  public:
357  inline void operator()(void* x) const {
358  free(x);
359  }
360  };
361 
362 } // namespace pallas
363 
364 #endif // PALLAS_SCOPED_PTR_H
C element_type
Definition: scoped_ptr.h:228
void swap(scoped_ptr &p2)
Definition: scoped_ptr.h:133
void reset(C *p=NULL)
Deletes the current owned object, if any.
Definition: scoped_ptr.h:88
Definition: scoped_ptr.h:47
C * get() const
Accessor to get the owned object.
Definition: scoped_ptr.h:118
bool operator==(const C *p) const
Definition: scoped_ptr.h:123
C element_type
Definition: scoped_ptr.h:63
Definition: scoped_ptr.h:46
void reset(C *p=NULL)
Deletes the current owned object, if any.
Definition: scoped_ptr.h:253
Definition: basinhopping.h:51
bool operator!=(const C *p) const
Definition: scoped_ptr.h:289
C * operator->() const
Accessor to get the owned object.
Definition: scoped_ptr.h:110
scoped_array(C *p=NULL)
Constructor.
Definition: scoped_ptr.h:236
This class wraps the c library function free() in a class that can be passed as a template argument t...
Definition: scoped_ptr.h:355
~scoped_ptr()
Destructor.
Definition: scoped_ptr.h:78
bool operator!=(const C *p) const
Definition: scoped_ptr.h:128
Definition: scoped_ptr.h:45
void swap(scoped_array &p2)
Definition: scoped_ptr.h:294
C * release()
Release a pointer.
Definition: scoped_ptr.h:146
C & operator*() const
Accessor to get the owned object.
Definition: scoped_ptr.h:101
C * release()
Release an array.].
Definition: scoped_ptr.h:307
C * get() const
Get a pointer to the zeroth element of the current object.
Definition: scoped_ptr.h:275
C & operator[](std::ptrdiff_t i) const
Get one element of the current object.
Definition: scoped_ptr.h:265
friend scoped_ptr< C > make_scoped_ptr(C *p)
Definition: scoped_ptr.h:210
~scoped_array()
Destructor.
Definition: scoped_ptr.h:243
scoped_ptr(C *p=NULL)
Constructor. Defaults to intializing with NULL. There is no way to create an uninitialized scoped_ptr...
Definition: scoped_ptr.h:71
bool operator==(const C *p) const
Definition: scoped_ptr.h:283