Blame cairomm/refptr.h

Packit 908522
// -*- c++ -*-
Packit 908522
#ifndef _cairo_REFPTR_H
Packit 908522
#define _cairo_REFPTR_H
Packit 908522
Packit 908522
/* $Id: refptr.h,v 1.6 2006-09-27 18:38:57 murrayc Exp $ */
Packit 908522
Packit 908522
/* Copyright 2005 The cairomm Development Team
Packit 908522
 *
Packit 908522
 * This library is free software; you can redistribute it and/or
Packit 908522
 * modify it under the terms of the GNU Library General Public
Packit 908522
 * License as published by the Free Software Foundation; either
Packit 908522
 * version 2 of the License, or (at your option) any later version.
Packit 908522
 *
Packit 908522
 * This library is distributed in the hope that it will be useful,
Packit 908522
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 908522
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 908522
 * Library General Public License for more details.
Packit 908522
 *
Packit 908522
 * You should have received a copy of the GNU Library General Public
Packit 908522
 * License along with this library; if not, write to the Free Software
Packit 908522
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
Packit 908522
 * 02110-1301, USA.
Packit 908522
 */
Packit 908522
Packit 908522
#include <utility>
Packit 908522
Packit 908522
namespace Cairo
Packit 908522
{
Packit 908522
Packit 908522
//TODO: Use std::shared_ptr<> instead when we can break ABI.
Packit 908522
/** RefPtr<> is a reference-counting shared smartpointer.
Packit 908522
 *
Packit 908522
 * Reference counting means that a shared reference count is incremented each
Packit 908522
 * time a RefPtr is copied, and decremented each time a RefPtr is destroyed,
Packit 908522
 * for instance when it leaves its scope. When the reference count reaches
Packit 908522
 * zero, the contained object is deleted
Packit 908522
 *
Packit 908522
 * cairomm uses RefPtr so that you don't need to remember
Packit 908522
 * to delete the object explicitly, or know when a method expects you to delete 
Packit 908522
 * the object that it returns, and to prevent any need to manually  reference 
Packit 908522
 * and unreference() cairo objects.
Packit 908522
 */
Packit 908522
template <class T_CppObject>
Packit 908522
class RefPtr
Packit 908522
{
Packit 908522
public:
Packit 908522
  // Let the cast constructors and assignement operators access private data.
Packit 908522
  template <typename T_CastFrom>
Packit 908522
  friend class RefPtr;
Packit 908522
Packit 908522
  /** Default constructor
Packit 908522
   *
Packit 908522
   * Afterwards it will be null and use of -> will cause a segmentation fault.
Packit 908522
   */
Packit 908522
  inline RefPtr() noexcept;
Packit 908522
  
Packit 908522
  /// Destructor - decrements reference count.
Packit 908522
  inline ~RefPtr() noexcept;
Packit 908522
Packit 908522
  /** For use only in the internal implementation of cairomm, gtkmm, etc.
Packit 908522
   *
Packit 908522
   * This takes ownership of @a pCppObject, so it will be deleted when the 
Packit 908522
   * last RefPtr is deleted, for instance when it goes out of scope.
Packit 908522
   *
Packit 908522
   * This assumes that @a pCppObject already has a starting reference for its underlying cairo object,
Packit 908522
   * so that destruction of @a @pCppObject will cause a corresponding unreference of its underlying 
Packit 908522
   * cairo object. For instance, a cairo_*_create() function usually provides a starting reference, 
Packit 908522
   * but a cairo_*_get_*() function requires the caller to manually reference the returned object.
Packit 908522
   * In this case, you should call reference() on @a pCppObject before passing it to this constructor.
Packit 908522
   */
Packit 908522
  explicit inline RefPtr(T_CppObject* pCppObject) noexcept;
Packit 908522
Packit 908522
  ///  For use only in the internal implementation of sharedptr.
Packit 908522
  explicit inline RefPtr(T_CppObject* pCppObject, int* refcount) noexcept;
Packit 908522
Packit 908522
  /** Move constructor
Packit 908522
   */
Packit 908522
  inline RefPtr(RefPtr&& src) noexcept;
Packit 908522
Packit 908522
  /** Move constructor (from different, but castable type).
Packit 908522
   */
Packit 908522
  template <class T_CastFrom>
Packit 908522
  inline RefPtr(RefPtr<T_CastFrom>&& src) noexcept;
Packit 908522
Packit 908522
  /** Copy constructor
Packit 908522
   *
Packit 908522
   * This increments the shared reference count.
Packit 908522
   */
Packit 908522
  inline RefPtr(const RefPtr<T_CppObject>& src) noexcept;
Packit 908522
Packit 908522
  /** Copy constructor (from different, but castable type).
Packit 908522
   *
Packit 908522
   * Increments the reference count.
Packit 908522
   */
Packit 908522
  template <class T_CastFrom>
Packit 908522
  inline RefPtr(const RefPtr<T_CastFrom>& src) noexcept;
Packit 908522
Packit 908522
  /** Swap the contents of two RefPtr<>.
Packit 908522
   * This method swaps the internal pointers to T_CppObject.  This can be
Packit 908522
   * done safely without involving a reference/unreference cycle and is
Packit 908522
   * therefore highly efficient.
Packit 908522
   */
Packit 908522
  inline void swap(RefPtr<T_CppObject>& other) noexcept;
Packit 908522
Packit 908522
  /// Copy from another RefPtr:
Packit 908522
  inline RefPtr<T_CppObject>& operator=(const RefPtr<T_CppObject>& src) noexcept;
Packit 908522
Packit 908522
  /** Copy from different, but castable type).
Packit 908522
   *
Packit 908522
   * Increments the reference count.
Packit 908522
   */
Packit 908522
  template <class T_CastFrom>
Packit 908522
  inline RefPtr<T_CppObject>& operator=(const RefPtr<T_CastFrom>& src) noexcept;
Packit 908522
Packit 908522
  /// Move assignment operator:
Packit 908522
  inline RefPtr& operator=(RefPtr&& src) noexcept;
Packit 908522
Packit 908522
  /// Move assignment operator (from different, but castable type):
Packit 908522
  template <class T_CastFrom>
Packit 908522
  inline RefPtr& operator=(RefPtr<T_CastFrom>&& src) noexcept;
Packit 908522
Packit 908522
  /// Tests whether the RefPtr<> point to the same underlying instance.
Packit 908522
  inline bool operator==(const RefPtr<T_CppObject>& src) const noexcept;
Packit 908522
  
Packit 908522
  /// See operator==().
Packit 908522
  inline bool operator!=(const RefPtr<T_CppObject>& src) const noexcept;
Packit 908522
Packit 908522
  /** Dereferencing.
Packit 908522
   *
Packit 908522
   * Use the methods of the underlying instance like so:
Packit 908522
   * refptr->memberfun().
Packit 908522
   */
Packit 908522
  inline T_CppObject* operator->() const noexcept;
Packit 908522
Packit 908522
  /** Test whether the RefPtr<> points to any underlying instance.
Packit 908522
   *
Packit 908522
   * Mimics usage of ordinary pointers:
Packit 908522
   * @code
Packit 908522
   *   if (ptr)
Packit 908522
   *     do_something();
Packit 908522
   * @endcode
Packit 908522
   */
Packit 908522
  inline operator bool() const noexcept;
Packit 908522
Packit 908522
  /// Set underlying instance to 0, decrementing reference count of existing instance appropriately.
Packit 908522
  inline void clear() noexcept;
Packit 908522
Packit 908522
Packit 908522
  /** Dynamic cast to derived class.
Packit 908522
   *
Packit 908522
   * The RefPtr can't be cast with the usual notation so instead you can use
Packit 908522
   * @code
Packit 908522
   *   ptr_derived = RefPtr<Derived>::cast_dynamic(ptr_base);
Packit 908522
   * @endcode
Packit 908522
   */
Packit 908522
  template <class T_CastFrom>
Packit 908522
  static inline RefPtr<T_CppObject> cast_dynamic(const RefPtr<T_CastFrom>& src) noexcept;
Packit 908522
Packit 908522
  /** Static cast to derived class.
Packit 908522
   *
Packit 908522
   * Like the dynamic cast; the notation is 
Packit 908522
   * @code
Packit 908522
   *   ptr_derived = RefPtr<Derived>::cast_static(ptr_base);
Packit 908522
   * @endcode
Packit 908522
   */
Packit 908522
  template <class T_CastFrom>
Packit 908522
  static inline RefPtr<T_CppObject> cast_static(const RefPtr<T_CastFrom>& src) noexcept;
Packit 908522
Packit 908522
  /** Cast to non-const.
Packit 908522
   *
Packit 908522
   * The RefPtr can't be cast with the usual notation so instead you can use
Packit 908522
   * @code
Packit 908522
   *   ptr_unconst = RefPtr<UnConstType>::cast_const(ptr_const);
Packit 908522
   * @endcode
Packit 908522
   */
Packit 908522
  template <class T_CastFrom>
Packit 908522
  static inline RefPtr<T_CppObject> cast_const(const RefPtr<T_CastFrom>& src) noexcept;
Packit 908522
Packit 908522
Packit 908522
#ifndef DOXYGEN_IGNORE_THIS
Packit 908522
Packit 908522
  // Warning: This is for internal use only.  Do not manually modify the
Packit 908522
  // reference count with this pointer.
Packit 908522
  inline int* refcount_() const noexcept { return pCppRefcount_; }
Packit 908522
Packit 908522
#endif // DOXYGEN_IGNORE_THIS
Packit 908522
Packit 908522
private:
Packit 908522
  void unref() noexcept;
Packit 908522
Packit 908522
  T_CppObject* pCppObject_;
Packit 908522
  mutable int* pCppRefcount_;
Packit 908522
};
Packit 908522
Packit 908522
Packit 908522
#ifndef DOXYGEN_IGNORE_THIS
Packit 908522
Packit 908522
// RefPtr<>::operator->() comes first here since it's used by other methods.
Packit 908522
// If it would come after them it wouldn't be inlined.
Packit 908522
Packit 908522
template <class T_CppObject> inline
Packit 908522
T_CppObject* RefPtr<T_CppObject>::operator->() const noexcept
Packit 908522
{
Packit 908522
  return pCppObject_;
Packit 908522
}
Packit 908522
Packit 908522
template <class T_CppObject> inline
Packit 908522
RefPtr<T_CppObject>::RefPtr() noexcept
Packit 908522
:
Packit 908522
  pCppObject_(nullptr),
Packit 908522
  pCppRefcount_(0)
Packit 908522
{}
Packit 908522
Packit 908522
template <class T_CppObject> inline
Packit 908522
RefPtr<T_CppObject>::~RefPtr() noexcept
Packit 908522
{
Packit 908522
  unref();
Packit 908522
}
Packit 908522
Packit 908522
template <class T_CppObject> inline
Packit 908522
void RefPtr<T_CppObject>::unref() noexcept
Packit 908522
{
Packit 908522
  if(pCppRefcount_)
Packit 908522
  {
Packit 908522
    --(*pCppRefcount_);
Packit 908522
Packit 908522
    if(*pCppRefcount_ == 0)
Packit 908522
    {
Packit 908522
      if(pCppObject_)
Packit 908522
      {
Packit 908522
        delete pCppObject_;
Packit 908522
        pCppObject_ = nullptr;
Packit 908522
      }
Packit 908522
Packit 908522
      delete pCppRefcount_;
Packit 908522
      pCppRefcount_ = nullptr;
Packit 908522
    }
Packit 908522
  }
Packit 908522
}
Packit 908522
Packit 908522
Packit 908522
template <class T_CppObject> inline
Packit 908522
RefPtr<T_CppObject>::RefPtr(T_CppObject* pCppObject) noexcept
Packit 908522
:
Packit 908522
  pCppObject_(pCppObject),
Packit 908522
  pCppRefcount_(0)
Packit 908522
{
Packit 908522
  if(pCppObject)
Packit 908522
  {
Packit 908522
    pCppRefcount_ = new int;
Packit 908522
    *pCppRefcount_ = 1; //This will be decremented in the destructor.
Packit 908522
  }
Packit 908522
}
Packit 908522
Packit 908522
//Used by cast_*() implementations:
Packit 908522
template <class T_CppObject> inline
Packit 908522
RefPtr<T_CppObject>::RefPtr(T_CppObject* pCppObject, int* refcount) noexcept
Packit 908522
:
Packit 908522
  pCppObject_(pCppObject),
Packit 908522
  pCppRefcount_(refcount)
Packit 908522
{
Packit 908522
  if(pCppObject_ && pCppRefcount_)
Packit 908522
    ++(*pCppRefcount_);
Packit 908522
}
Packit 908522
Packit 908522
template <class T_CppObject> inline
Packit 908522
RefPtr<T_CppObject>::RefPtr(const RefPtr<T_CppObject>& src) noexcept
Packit 908522
:
Packit 908522
  pCppObject_ (src.pCppObject_),
Packit 908522
  pCppRefcount_(src.pCppRefcount_)
Packit 908522
{
Packit 908522
  if(pCppObject_ && pCppRefcount_)
Packit 908522
    ++(*pCppRefcount_);
Packit 908522
}
Packit 908522
Packit 908522
template <class T_CppObject> inline
Packit 908522
RefPtr<T_CppObject>::RefPtr(RefPtr&& src) noexcept
Packit 908522
:
Packit 908522
  pCppObject_ (src.pCppObject_),
Packit 908522
  pCppRefcount_ (src.pCppRefcount_)
Packit 908522
{
Packit 908522
  src.pCppObject_ = nullptr;
Packit 908522
  src.pCppRefcount_ = nullptr;
Packit 908522
}
Packit 908522
Packit 908522
template <class T_CppObject>
Packit 908522
  template <class T_CastFrom>
Packit 908522
inline
Packit 908522
RefPtr<T_CppObject>::RefPtr(RefPtr<T_CastFrom>&& src) noexcept
Packit 908522
:
Packit 908522
  pCppObject_ (src.pCppObject_),
Packit 908522
  pCppRefcount_ (src.pCppRefcount_)
Packit 908522
{
Packit 908522
  src.pCppObject_ = nullptr;
Packit 908522
  src.pCppRefcount_ = nullptr;
Packit 908522
}
Packit 908522
Packit 908522
// The templated ctor allows copy construction from any object that's
Packit 908522
// castable.  Thus, it does downcasts:
Packit 908522
//   base_ref = derived_ref
Packit 908522
template <class T_CppObject>
Packit 908522
  template <class T_CastFrom>
Packit 908522
inline
Packit 908522
RefPtr<T_CppObject>::RefPtr(const RefPtr<T_CastFrom>& src) noexcept
Packit 908522
:
Packit 908522
  // Without the friend delaration,
Packit 908522
  // a different RefPtr<> will not allow us access to pCppObject_.  We need
Packit 908522
  // to add a get_underlying() for this, but that would encourage incorrect
Packit 908522
  // use, so we use the less well-known operator->() accessor:
Packit 908522
  pCppObject_ (src.operator->()),
Packit 908522
  pCppRefcount_(src.refcount_())
Packit 908522
{
Packit 908522
  if(pCppObject_ && pCppRefcount_)
Packit 908522
    ++(*pCppRefcount_);
Packit 908522
}
Packit 908522
Packit 908522
template <class T_CppObject> inline
Packit 908522
void RefPtr<T_CppObject>::swap(RefPtr<T_CppObject>& other) noexcept
Packit 908522
{
Packit 908522
  T_CppObject *const temp = pCppObject_;
Packit 908522
  int* temp_count = pCppRefcount_; 
Packit 908522
Packit 908522
  pCppObject_ = other.pCppObject_;
Packit 908522
  pCppRefcount_ = other.pCppRefcount_;
Packit 908522
Packit 908522
  other.pCppObject_ = temp;
Packit 908522
  other.pCppRefcount_ = temp_count;
Packit 908522
}
Packit 908522
Packit 908522
template <class T_CppObject> inline
Packit 908522
RefPtr<T_CppObject>& RefPtr<T_CppObject>::operator=(const RefPtr<T_CppObject>& src) noexcept
Packit 908522
{
Packit 908522
  // In case you haven't seen the swap() technique to implement copy
Packit 908522
  // assignment before, here's what it does:
Packit 908522
  //
Packit 908522
  // 1) Create a temporary RefPtr<> instance via the copy ctor, thereby
Packit 908522
  //    increasing the reference count of the source object.
Packit 908522
  //
Packit 908522
  // 2) Swap the internal object pointers of *this and the temporary
Packit 908522
  //    RefPtr<>.  After this step, *this already contains the new pointer,
Packit 908522
  //    and the old pointer is now managed by temp.
Packit 908522
  //
Packit 908522
  // 3) The destructor of temp is executed, thereby unreferencing the
Packit 908522
  //    old object pointer.
Packit 908522
  //
Packit 908522
  // This technique is described in Herb Sutter's "Exceptional C++", and
Packit 908522
  // has a number of advantages over conventional approaches:
Packit 908522
  //
Packit 908522
  // - Code reuse by calling the copy ctor.
Packit 908522
  // - Strong exception safety for free.
Packit 908522
  // - Self assignment is handled implicitely.
Packit 908522
  // - Simplicity.
Packit 908522
  // - It just works and is hard to get wrong; i.e. you can use it without
Packit 908522
  //   even thinking about it to implement copy assignment whereever the
Packit 908522
  //   object data is managed indirectly via a pointer, which is very common.
Packit 908522
Packit 908522
  RefPtr<T_CppObject> temp (src);
Packit 908522
  this->swap(temp);
Packit 908522
  return *this;
Packit 908522
}
Packit 908522
Packit 908522
template <class T_CppObject> inline
Packit 908522
RefPtr<T_CppObject>& RefPtr<T_CppObject>::operator=(RefPtr&& src) noexcept
Packit 908522
{
Packit 908522
  RefPtr<T_CppObject> temp (std::move(src));
Packit 908522
  this->swap(temp);
Packit 908522
  src.pCppObject_ = nullptr;
Packit 908522
  src.pCppRefcount_ = nullptr;
Packit 908522
Packit 908522
  return *this;
Packit 908522
}
Packit 908522
Packit 908522
template <class T_CppObject>
Packit 908522
  template <class T_CastFrom>
Packit 908522
inline
Packit 908522
RefPtr<T_CppObject>& RefPtr<T_CppObject>::operator=(RefPtr<T_CastFrom>&& src) noexcept
Packit 908522
{
Packit 908522
  RefPtr<T_CppObject> temp (std::move(src));
Packit 908522
  this->swap(temp);
Packit 908522
  src.pCppObject_ = nullptr;
Packit 908522
  src.pCppRefcount_ = nullptr;
Packit 908522
Packit 908522
  return *this;
Packit 908522
}
Packit 908522
Packit 908522
template <class T_CppObject>
Packit 908522
  template <class T_CastFrom>
Packit 908522
inline
Packit 908522
RefPtr<T_CppObject>& RefPtr<T_CppObject>::operator=(const RefPtr<T_CastFrom>& src) noexcept
Packit 908522
{
Packit 908522
  RefPtr<T_CppObject> temp (src);
Packit 908522
  this->swap(temp);
Packit 908522
  return *this;
Packit 908522
}
Packit 908522
Packit 908522
template <class T_CppObject> inline
Packit 908522
bool RefPtr<T_CppObject>::operator==(const RefPtr<T_CppObject>& src) const noexcept
Packit 908522
{
Packit 908522
  return (pCppObject_ == src.pCppObject_);
Packit 908522
}
Packit 908522
Packit 908522
template <class T_CppObject> inline
Packit 908522
bool RefPtr<T_CppObject>::operator!=(const RefPtr<T_CppObject>& src) const noexcept
Packit 908522
{
Packit 908522
  return (pCppObject_ != src.pCppObject_);
Packit 908522
}
Packit 908522
Packit 908522
template <class T_CppObject> inline
Packit 908522
RefPtr<T_CppObject>::operator bool() const noexcept
Packit 908522
{
Packit 908522
  return (pCppObject_ != 0);
Packit 908522
}
Packit 908522
Packit 908522
template <class T_CppObject> inline
Packit 908522
void RefPtr<T_CppObject>::clear() noexcept
Packit 908522
{
Packit 908522
  RefPtr<T_CppObject> temp; // swap with an empty RefPtr<> to clear *this
Packit 908522
  this->swap(temp);
Packit 908522
}
Packit 908522
Packit 908522
template <class T_CppObject>
Packit 908522
  template <class T_CastFrom>
Packit 908522
inline
Packit 908522
RefPtr<T_CppObject> RefPtr<T_CppObject>::cast_dynamic(const RefPtr<T_CastFrom>& src) noexcept
Packit 908522
{
Packit 908522
  T_CppObject *const pCppObject = dynamic_cast<T_CppObject*>(src.operator->());
Packit 908522
Packit 908522
  if(pCppObject) //Check whether dynamic_cast<> succeeded so we don't pass a null object with a used refcount:
Packit 908522
    return RefPtr<T_CppObject>(pCppObject, src.refcount_());
Packit 908522
  else
Packit 908522
    return RefPtr<T_CppObject>();
Packit 908522
}
Packit 908522
Packit 908522
template <class T_CppObject>
Packit 908522
  template <class T_CastFrom>
Packit 908522
inline
Packit 908522
RefPtr<T_CppObject> RefPtr<T_CppObject>::cast_static(const RefPtr<T_CastFrom>& src) noexcept
Packit 908522
{
Packit 908522
  T_CppObject *const pCppObject = static_cast<T_CppObject*>(src.operator->());
Packit 908522
Packit 908522
  return RefPtr<T_CppObject>(pCppObject, src.refcount_());
Packit 908522
}
Packit 908522
Packit 908522
template <class T_CppObject>
Packit 908522
  template <class T_CastFrom>
Packit 908522
inline
Packit 908522
RefPtr<T_CppObject> RefPtr<T_CppObject>::cast_const(const RefPtr<T_CastFrom>& src) noexcept
Packit 908522
{
Packit 908522
  T_CppObject *const pCppObject = const_cast<T_CppObject*>(src.operator->());
Packit 908522
Packit 908522
  return RefPtr<T_CppObject>(pCppObject, src.refcount_());
Packit 908522
}
Packit 908522
Packit 908522
#endif /* DOXYGEN_IGNORE_THIS */
Packit 908522
Packit 908522
/** @relates Glib::RefPtr */
Packit 908522
template <class T_CppObject> inline
Packit 908522
void swap(RefPtr<T_CppObject>& lhs, RefPtr<T_CppObject>& rhs) noexcept
Packit 908522
{
Packit 908522
  lhs.swap(rhs);
Packit 908522
}
Packit 908522
Packit 908522
} // namespace Cairo
Packit 908522
Packit 908522
Packit 908522
#endif /* _cairo_REFPTR_H */
Packit 908522
Packit 908522
// vim: ts=2 sw=2 et