/* -*-c++-*-
*
- * Copyright (C) 2005-2009 Mathias Froehlich
+ * Copyright (C) 2005-2012 Mathias Froehlich
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
template<typename T>
class SGSharedPtr {
public:
+ typedef T element_type;
+
SGSharedPtr(void) : _ptr(0)
{}
SGSharedPtr(T* ptr) : _ptr(ptr)
{ return T::count(_ptr); }
bool valid(void) const
- { return _ptr; }
+ { return _ptr != (T*)0; }
void clear()
{ put(); }
private:
void assign(T* p)
{ get(p); put(); _ptr = p; }
+ void assignNonRef(T* p)
+ { put(); _ptr = p; }
void get(const T* p) const
{ T::get(p); }
void put(void)
- { if (!T::put(_ptr)) { delete _ptr; _ptr = 0; } }
+ { if (!T::put(_ptr)) delete _ptr; _ptr = 0; }
// The reference itself.
T* _ptr;
{
return p.ptr();
}
+
+/**
+ * static_cast for SGSharedPtr
+ */
+template<class T, class U>
+SGSharedPtr<T> static_pointer_cast(SGSharedPtr<U> const & r)
+{
+ return SGSharedPtr<T>( static_cast<T*>(r.get()) );
+}
+
+/**
+ * Compare two SGSharedPtr<T> objects for equality.
+ *
+ * @note Only pointer values are compared, not the actual objects they are
+ * pointing at.
+ */
+template<class T, class U>
+bool operator==(const SGSharedPtr<T>& lhs, const SGSharedPtr<U>& rhs)
+{
+ return lhs.get() == rhs.get();
+}
+
+/**
+ * Compare two SGSharedPtr<T> objects for equality.
+ *
+ * @note Only pointer values are compared, not the actual objects they are
+ * pointing at.
+ */
+template<class T, class U>
+bool operator!=(const SGSharedPtr<T>& lhs, const SGSharedPtr<U>& rhs)
+{
+ return lhs.get() != rhs.get();
+}
+
+/**
+ * Compare two SGSharedPtr<T> objects for weak ordering.
+ *
+ * @note Only pointer values are compared, not the actual objects they are
+ * pointing at.
+ * @note This allows using SGSharedPtr as key in associative containers like for
+ * example std::map and std::set.
+ */
+template<class T, class U>
+bool operator<(const SGSharedPtr<T>& lhs, const SGSharedPtr<U>& rhs)
+{
+ return lhs.get() < rhs.get();
+}
#endif