Blame include/ceres/internal/scoped_ptr.h

Packit ea1746
// Ceres Solver - A fast non-linear least squares minimizer
Packit ea1746
// Copyright 2015 Google Inc. All rights reserved.
Packit ea1746
// http://ceres-solver.org/
Packit ea1746
//
Packit ea1746
// Redistribution and use in source and binary forms, with or without
Packit ea1746
// modification, are permitted provided that the following conditions are met:
Packit ea1746
//
Packit ea1746
// * Redistributions of source code must retain the above copyright notice,
Packit ea1746
//   this list of conditions and the following disclaimer.
Packit ea1746
// * Redistributions in binary form must reproduce the above copyright notice,
Packit ea1746
//   this list of conditions and the following disclaimer in the documentation
Packit ea1746
//   and/or other materials provided with the distribution.
Packit ea1746
// * Neither the name of Google Inc. nor the names of its contributors may be
Packit ea1746
//   used to endorse or promote products derived from this software without
Packit ea1746
//   specific prior written permission.
Packit ea1746
//
Packit ea1746
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit ea1746
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit ea1746
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit ea1746
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
Packit ea1746
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Packit ea1746
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Packit ea1746
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit ea1746
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Packit ea1746
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Packit ea1746
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Packit ea1746
// POSSIBILITY OF SUCH DAMAGE.
Packit ea1746
//
Packit ea1746
// Author: jorg@google.com (Jorg Brown)
Packit ea1746
//
Packit ea1746
// This is an implementation designed to match the anticipated future TR2
Packit ea1746
// implementation of the scoped_ptr class, and its closely-related brethren,
Packit ea1746
// scoped_array, scoped_ptr_malloc, and make_scoped_ptr.
Packit ea1746
Packit ea1746
#ifndef CERES_PUBLIC_INTERNAL_SCOPED_PTR_H_
Packit ea1746
#define CERES_PUBLIC_INTERNAL_SCOPED_PTR_H_
Packit ea1746
Packit ea1746
#include <assert.h>
Packit ea1746
#include <stdlib.h>
Packit ea1746
#include <cstddef>
Packit ea1746
#include <algorithm>
Packit ea1746
Packit ea1746
namespace ceres {
Packit ea1746
namespace internal {
Packit ea1746
Packit ea1746
template <class C> class scoped_ptr;
Packit ea1746
template <class C, class Free> class scoped_ptr_malloc;
Packit ea1746
template <class C> class scoped_array;
Packit ea1746
Packit ea1746
template <class C>
Packit ea1746
scoped_ptr<C> make_scoped_ptr(C *);
Packit ea1746
Packit ea1746
// A scoped_ptr<T> is like a T*, except that the destructor of
Packit ea1746
// scoped_ptr<T> automatically deletes the pointer it holds (if
Packit ea1746
// any). That is, scoped_ptr<T> owns the T object that it points
Packit ea1746
// to. Like a T*, a scoped_ptr<T> may hold either NULL or a pointer to
Packit ea1746
// a T object. Also like T*, scoped_ptr<T> is thread-compatible, and
Packit ea1746
// once you dereference it, you get the threadsafety guarantees of T.
Packit ea1746
//
Packit ea1746
// The size of a scoped_ptr is small: sizeof(scoped_ptr<C>) == sizeof(C*)
Packit ea1746
template <class C>
Packit ea1746
class scoped_ptr {
Packit ea1746
 public:
Packit ea1746
  // The element type
Packit ea1746
  typedef C element_type;
Packit ea1746
Packit ea1746
  // Constructor.  Defaults to intializing with NULL.
Packit ea1746
  // There is no way to create an uninitialized scoped_ptr.
Packit ea1746
  // The input parameter must be allocated with new.
Packit ea1746
  explicit scoped_ptr(C* p = NULL) : ptr_(p) { }
Packit ea1746
Packit ea1746
  // Destructor.  If there is a C object, delete it.
Packit ea1746
  // We don't need to test ptr_ == NULL because C++ does that for us.
Packit ea1746
  ~scoped_ptr() {
Packit ea1746
    enum { type_must_be_complete = sizeof(C) };
Packit ea1746
    delete ptr_;
Packit ea1746
  }
Packit ea1746
Packit ea1746
  // Reset.  Deletes the current owned object, if any.
Packit ea1746
  // Then takes ownership of a new object, if given.
Packit ea1746
  // this->reset(this->get()) works.
Packit ea1746
  void reset(C* p = NULL) {
Packit ea1746
    if (p != ptr_) {
Packit ea1746
      enum { type_must_be_complete = sizeof(C) };
Packit ea1746
      delete ptr_;
Packit ea1746
      ptr_ = p;
Packit ea1746
    }
Packit ea1746
  }
Packit ea1746
Packit ea1746
  // Accessors to get the owned object.
Packit ea1746
  // operator* and operator-> will assert() if there is no current object.
Packit ea1746
  C& operator*() const {
Packit ea1746
    assert(ptr_ != NULL);
Packit ea1746
    return *ptr_;
Packit ea1746
  }
Packit ea1746
  C* operator->() const  {
Packit ea1746
    assert(ptr_ != NULL);
Packit ea1746
    return ptr_;
Packit ea1746
  }
Packit ea1746
  C* get() const { return ptr_; }
Packit ea1746
Packit ea1746
  // Comparison operators.
Packit ea1746
  // These return whether a scoped_ptr and a raw pointer refer to
Packit ea1746
  // the same object, not just to two different but equal objects.
Packit ea1746
  bool operator==(const C* p) const { return ptr_ == p; }
Packit ea1746
  bool operator!=(const C* p) const { return ptr_ != p; }
Packit ea1746
Packit ea1746
  // Swap two scoped pointers.
Packit ea1746
  void swap(scoped_ptr& p2) {
Packit ea1746
    C* tmp = ptr_;
Packit ea1746
    ptr_ = p2.ptr_;
Packit ea1746
    p2.ptr_ = tmp;
Packit ea1746
  }
Packit ea1746
Packit ea1746
  // Release a pointer.
Packit ea1746
  // The return value is the current pointer held by this object.
Packit ea1746
  // If this object holds a NULL pointer, the return value is NULL.
Packit ea1746
  // After this operation, this object will hold a NULL pointer,
Packit ea1746
  // and will not own the object any more.
Packit ea1746
  C* release() {
Packit ea1746
    C* retVal = ptr_;
Packit ea1746
    ptr_ = NULL;
Packit ea1746
    return retVal;
Packit ea1746
  }
Packit ea1746
Packit ea1746
 private:
Packit ea1746
  C* ptr_;
Packit ea1746
Packit ea1746
  // google3 friend class that can access copy ctor (although if it actually
Packit ea1746
  // calls a copy ctor, there will be a problem) see below
Packit ea1746
  friend scoped_ptr<C> make_scoped_ptr<C>(C *p);
Packit ea1746
Packit ea1746
  // Forbid comparison of scoped_ptr types.  If C2 != C, it totally doesn't
Packit ea1746
  // make sense, and if C2 == C, it still doesn't make sense because you should
Packit ea1746
  // never have the same object owned by two different scoped_ptrs.
Packit ea1746
  template <class C2> bool operator==(scoped_ptr<C2> const& p2) const;
Packit ea1746
  template <class C2> bool operator!=(scoped_ptr<C2> const& p2) const;
Packit ea1746
Packit ea1746
  // Disallow evil constructors
Packit ea1746
  scoped_ptr(const scoped_ptr&);
Packit ea1746
  void operator=(const scoped_ptr&);
Packit ea1746
};
Packit ea1746
Packit ea1746
// Free functions
Packit ea1746
template <class C>
Packit ea1746
inline void swap(scoped_ptr<C>& p1, scoped_ptr<C>& p2) {
Packit ea1746
  p1.swap(p2);
Packit ea1746
}
Packit ea1746
Packit ea1746
template <class C>
Packit ea1746
inline bool operator==(const C* p1, const scoped_ptr<C>& p2) {
Packit ea1746
  return p1 == p2.get();
Packit ea1746
}
Packit ea1746
Packit ea1746
template <class C>
Packit ea1746
inline bool operator==(const C* p1, const scoped_ptr<const C>& p2) {
Packit ea1746
  return p1 == p2.get();
Packit ea1746
}
Packit ea1746
Packit ea1746
template <class C>
Packit ea1746
inline bool operator!=(const C* p1, const scoped_ptr<C>& p2) {
Packit ea1746
  return p1 != p2.get();
Packit ea1746
}
Packit ea1746
Packit ea1746
template <class C>
Packit ea1746
inline bool operator!=(const C* p1, const scoped_ptr<const C>& p2) {
Packit ea1746
  return p1 != p2.get();
Packit ea1746
}
Packit ea1746
Packit ea1746
template <class C>
Packit ea1746
scoped_ptr<C> make_scoped_ptr(C *p) {
Packit ea1746
  // This does nothing but to return a scoped_ptr of the type that the passed
Packit ea1746
  // pointer is of.  (This eliminates the need to specify the name of T when
Packit ea1746
  // making a scoped_ptr that is used anonymously/temporarily.)  From an
Packit ea1746
  // access control point of view, we construct an unnamed scoped_ptr here
Packit ea1746
  // which we return and thus copy-construct.  Hence, we need to have access
Packit ea1746
  // to scoped_ptr::scoped_ptr(scoped_ptr const &).  However, it is guaranteed
Packit ea1746
  // that we never actually call the copy constructor, which is a good thing
Packit ea1746
  // as we would call the temporary's object destructor (and thus delete p)
Packit ea1746
  // if we actually did copy some object, here.
Packit ea1746
  return scoped_ptr<C>(p);
Packit ea1746
}
Packit ea1746
Packit ea1746
// scoped_array<C> is like scoped_ptr<C>, except that the caller must allocate
Packit ea1746
// with new [] and the destructor deletes objects with delete [].
Packit ea1746
//
Packit ea1746
// As with scoped_ptr<C>, a scoped_array<C> either points to an object
Packit ea1746
// or is NULL.  A scoped_array<C> owns the object that it points to.
Packit ea1746
// scoped_array<T> is thread-compatible, and once you index into it,
Packit ea1746
// the returned objects have only the threadsafety guarantees of T.
Packit ea1746
//
Packit ea1746
// Size: sizeof(scoped_array<C>) == sizeof(C*)
Packit ea1746
template <class C>
Packit ea1746
class scoped_array {
Packit ea1746
 public:
Packit ea1746
  // The element type
Packit ea1746
  typedef C element_type;
Packit ea1746
Packit ea1746
  // Constructor.  Defaults to intializing with NULL.
Packit ea1746
  // There is no way to create an uninitialized scoped_array.
Packit ea1746
  // The input parameter must be allocated with new [].
Packit ea1746
  explicit scoped_array(C* p = NULL) : array_(p) { }
Packit ea1746
Packit ea1746
  // Destructor.  If there is a C object, delete it.
Packit ea1746
  // We don't need to test ptr_ == NULL because C++ does that for us.
Packit ea1746
  ~scoped_array() {
Packit ea1746
    enum { type_must_be_complete = sizeof(C) };
Packit ea1746
    delete[] array_;
Packit ea1746
  }
Packit ea1746
Packit ea1746
  // Reset. Deletes the current owned object, if any.
Packit ea1746
  // Then takes ownership of a new object, if given.
Packit ea1746
  // this->reset(this->get()) works.
Packit ea1746
  void reset(C* p = NULL) {
Packit ea1746
    if (p != array_) {
Packit ea1746
      enum { type_must_be_complete = sizeof(C) };
Packit ea1746
      delete[] array_;
Packit ea1746
      array_ = p;
Packit ea1746
    }
Packit ea1746
  }
Packit ea1746
Packit ea1746
  // Get one element of the current object.
Packit ea1746
  // Will assert() if there is no current object, or index i is negative.
Packit ea1746
  C& operator[](std::ptrdiff_t i) const {
Packit ea1746
    assert(i >= 0);
Packit ea1746
    assert(array_ != NULL);
Packit ea1746
    return array_[i];
Packit ea1746
  }
Packit ea1746
Packit ea1746
  // Get a pointer to the zeroth element of the current object.
Packit ea1746
  // If there is no current object, return NULL.
Packit ea1746
  C* get() const {
Packit ea1746
    return array_;
Packit ea1746
  }
Packit ea1746
Packit ea1746
  // Comparison operators.
Packit ea1746
  // These return whether a scoped_array and a raw pointer refer to
Packit ea1746
  // the same array, not just to two different but equal arrays.
Packit ea1746
  bool operator==(const C* p) const { return array_ == p; }
Packit ea1746
  bool operator!=(const C* p) const { return array_ != p; }
Packit ea1746
Packit ea1746
  // Swap two scoped arrays.
Packit ea1746
  void swap(scoped_array& p2) {
Packit ea1746
    C* tmp = array_;
Packit ea1746
    array_ = p2.array_;
Packit ea1746
    p2.array_ = tmp;
Packit ea1746
  }
Packit ea1746
Packit ea1746
  // Release an array.
Packit ea1746
  // The return value is the current pointer held by this object.
Packit ea1746
  // If this object holds a NULL pointer, the return value is NULL.
Packit ea1746
  // After this operation, this object will hold a NULL pointer,
Packit ea1746
  // and will not own the object any more.
Packit ea1746
  C* release() {
Packit ea1746
    C* retVal = array_;
Packit ea1746
    array_ = NULL;
Packit ea1746
    return retVal;
Packit ea1746
  }
Packit ea1746
Packit ea1746
 private:
Packit ea1746
  C* array_;
Packit ea1746
Packit ea1746
  // Forbid comparison of different scoped_array types.
Packit ea1746
  template <class C2> bool operator==(scoped_array<C2> const& p2) const;
Packit ea1746
  template <class C2> bool operator!=(scoped_array<C2> const& p2) const;
Packit ea1746
Packit ea1746
  // Disallow evil constructors
Packit ea1746
  scoped_array(const scoped_array&);
Packit ea1746
  void operator=(const scoped_array&);
Packit ea1746
};
Packit ea1746
Packit ea1746
// Free functions
Packit ea1746
template <class C>
Packit ea1746
inline void swap(scoped_array<C>& p1, scoped_array<C>& p2) {
Packit ea1746
  p1.swap(p2);
Packit ea1746
}
Packit ea1746
Packit ea1746
template <class C>
Packit ea1746
inline bool operator==(const C* p1, const scoped_array<C>& p2) {
Packit ea1746
  return p1 == p2.get();
Packit ea1746
}
Packit ea1746
Packit ea1746
template <class C>
Packit ea1746
inline bool operator==(const C* p1, const scoped_array<const C>& p2) {
Packit ea1746
  return p1 == p2.get();
Packit ea1746
}
Packit ea1746
Packit ea1746
template <class C>
Packit ea1746
inline bool operator!=(const C* p1, const scoped_array<C>& p2) {
Packit ea1746
  return p1 != p2.get();
Packit ea1746
}
Packit ea1746
Packit ea1746
template <class C>
Packit ea1746
inline bool operator!=(const C* p1, const scoped_array<const C>& p2) {
Packit ea1746
  return p1 != p2.get();
Packit ea1746
}
Packit ea1746
Packit ea1746
// This class wraps the c library function free() in a class that can be
Packit ea1746
// passed as a template argument to scoped_ptr_malloc below.
Packit ea1746
class ScopedPtrMallocFree {
Packit ea1746
 public:
Packit ea1746
  inline void operator()(void* x) const {
Packit ea1746
    free(x);
Packit ea1746
  }
Packit ea1746
};
Packit ea1746
Packit ea1746
}  // namespace internal
Packit ea1746
}  // namespace ceres
Packit ea1746
Packit ea1746
#endif  // CERES_PUBLIC_INTERNAL_SCOPED_PTR_H_