#ifndef POINTER_H
#define POINTER_H


#include "Namespace.h"
BEGIN_NAMESPACE


#ifdef HAVE_CPP11

template <typename T>
class SharedPtr<T> : public shared_ptr<T> { };
template <typename T>
class WeakPtr<T> : public weak_ptr<T> { };

#else

/*
// If *a_ptr is the same as a_comparand, then *a_ptr is assigned a_val
void AtomicCompareAndSwap32(int32_t* a_ptr, int32_t a_comparand, int32_t a_val)
{
#if defined(IL_PLATFORM_WINDOWS)
  InterlockedCompareExchange(a_ptr, a_val, a_comparand);
#elif defined(IL_OPERATING_SYSTEM_MACOSX)
  OSAtomicCompareAndSwap32(a_comparand, a_val, a_ptr);
#elif defined(IL_PLATFORM_UNIX)
  __sync_val_compare_and_swap(a_ptr, a_comparand, a_val);
#endif
}
*/

template <typename T>
class Atomic
{
	// TODO: actual implementation needed and fill in gaps and test
public:
											// Currently implementation is not atomic, just place holder code
	Atomic()                               { m_value = 0; }
	Atomic(T a_val)                        { m_value = a_val; }
	Atomic<T> const& operator=(T a_val)    { m_value = a_val; return *this; }
	Atomic<T> const& operator++()          { m_value++; return *this; }
	Atomic<T> const& operator--()          { m_value--; return *this; }
	Atomic<T> operator++(int)              { m_value++; return *this; }
	Atomic<T> operator--(int)              { m_value--; return *this; }
	operator T const& ()                   { return m_value; }
	T m_value;
};


template <typename T>
class SharedPtrPrivate
{
public:
	T* m_ptr;
	Atomic<int32_t> m_refCount;
};


template <typename T>
class SharedPtr
{
	// TODO: test and fill in gaps
public:
    SharedPtr(T* ptr = 0)                                    { init(ptr); }
    SharedPtr(const SharedPtr<T> &a_other)                   { copy(a_other); }
	//explicit SharedPtr(const WeakPtr<T>& a_other)               { copy(a_other); }
    ~SharedPtr()                                             { unref(); }

	SharedPtr<T> &operator=(const SharedPtr<T>& a_other)     { if (m_data != a_other.m_data) { unref(); copy(a_other); } return *this; }
    SharedPtr<T> &operator=(T* ptr)                          { unref(); init(ptr); return *this; }

    T &operator*() const                                     { return *m_data->m_ptr;     }
    T *operator->() const                                    { return m_data->m_ptr;      }
	
	//template <typename ReturnType>
	//const ReturnType operator->*(ReturnType (T::*pmf)()) const { return m_data->m_ptr->pmf(); }

    bool operator!() const                                   { return m_data->m_ptr == 0; }
    operator bool() const                                    { return m_data->m_ptr != 0; }
	
	// Is every combo needed here? Just added all of them for good measure
	bool operator==(const SharedPtr<T>& p) const             { return m_data->m_ptr == p.m_data->p_ptr; }
	bool operator==(const T* p) const                        { return m_data->m_ptr == p;               }
	friend bool operator==(const T *p, const SharedPtr &sp)  { return p == sp.m_data->m_ptr;            }
	bool operator!=(const SharedPtr<T>& p) const             { return m_data->m_ptr != p.m_data->p_ptr; }
	bool operator!=(const T* p) const                        { return m_data->m_ptr != p;               }
	friend bool operator!=(const T *p, const SharedPtr &sp)  { return p != sp.m_data->m_ptr;            }
	bool operator< (const SharedPtr<T>& p) const             { return m_data->m_ptr <  p.m_data->p_ptr; }
	bool operator< (const T* p) const                        { return m_data->m_ptr <  p;               }
	friend bool operator< (const T *p, const SharedPtr &sp)  { return p <  sp.m_data->m_ptr;            }
	bool operator<=(const SharedPtr<T>& p) const             { return m_data->m_ptr <= p.m_data->p_ptr; }
	bool operator<=(const T* p) const                        { return m_data->m_ptr <= p;               }
	friend bool operator<=(const T *p, const SharedPtr &sp)  { return p <= sp.m_data->m_ptr;            }
	bool operator> (const SharedPtr<T>& p) const             { return m_data->m_ptr >  p.m_data->p_ptr; }
	bool operator> (const T* p) const                        { return m_data->m_ptr >  p;               }
	friend bool operator> (const T *p, const SharedPtr &sp)  { return p >  sp.m_data->m_ptr;            }
	bool operator>=(const SharedPtr<T>& p) const             { return m_data->m_ptr >= p.m_data->p_ptr; }
	bool operator>=(const T* p) const                        { return m_data->m_ptr >= p;               }
	friend bool operator>=(const T *p, const SharedPtr &sp)  { return p >= sp.m_data->m_ptr;            }

private:
	void unref() {
		m_data->m_refCount--;
		if (m_data->m_refCount == 0) { 
			delete m_data->m_ptr;
			delete m_data;
		}
	}
	void copy(const SharedPtr<T>& a_other) {
		m_data = a_other.m_data;
		m_data->m_refCount++;
	}
	void init(T* a_pointer) { 
		m_data = new SharedPtrPrivate<T>;
		m_data->m_ptr = a_pointer;
		m_data->m_refCount = 1;
	}

	SharedPtrPrivate<T>* m_data;
};


template <typename T>
class WeakPtr : public SharedPtr<T>
{
public:
    WeakPtr(T* ptr = 0) : SharedPtr<T>(ptr) {}
	
	// TODO
};



// TODO : really need lots of unit tests


/*

UNIT_TEST(SharedPtrTests, 0)
{
	A *a = new A();
	A *b = new B();
	A *d = new D();
}

*/




/*

Someones lame one that is GPL that doesn't use an atomic refcount
Probably the API is complete though, and note the interaction of the weak_ptr in the shared_ptr API
The refcount is a pointer to int and not associated with the ptr.

template <class T> class weak_ptr;
    
template <class T>
class shared_ptr {
    private:
        T* ptr;
        unsigned* count; //

        // special case, null pointer (nil-code)
        static unsigned* nil() { static unsigned nil_counter(1); return &nil_counter; }
        
        void decref() { if (--(*count) == 0) { delete ptr; delete count; }} 
        void incref() { ++(*count); }
        
        friend class weak_ptr<T>;
        
    public:
        
        shared_ptr() : ptr(0), count(nil()) { incref(); }
        ~shared_ptr() { decref(); }
        
        shared_ptr(const shared_ptr<T>& o) : ptr(o.ptr), count(o.count) { incref(); }
        shared_ptr(T* p) : ptr(p), count(new unsigned(1)) {}
        explicit shared_ptr(const weak_ptr<T>& w) : ptr(w.ptr), count(w.count) { incref(); }
        
        shared_ptr<T>& operator=(const shared_ptr<T>& o) {
            if (ptr == o.ptr) return *this;
            decref();
            ptr = o.ptr;
            count = o.count;
            incref();
            return *this;
        }
        
        T* get() { return ptr; }
        T* operator->() { return ptr; }
        T& operator*()  { return *ptr; }

        const T* get() const { return ptr; }
        const T* operator->() const { return ptr; }
        const T& operator*() const { return *ptr; }

        bool operator==(const shared_ptr<T>& o) const { return ptr == o.ptr; }
        bool operator!=(const shared_ptr<T>& o) const { return ptr != o.ptr; }
        bool operator<(const shared_ptr<T>& o) const { return ptr < o.ptr; }    

        unsigned refcount() const { return *count; }
};

template <class T>
class weak_ptr {
        T* ptr;
        unsigned* count;
        
        friend class shared_ptr<T>;
        
    public:
        
        weak_ptr() : ptr(0), count(shared_ptr<T>::nil()) {}
        explicit weak_ptr( const shared_ptr<T>& s) : ptr(s.ptr), count(s.count) {}
        
        shared_ptr<T> lock() const { return shared_ptr<T>(*this); }

        
        T* get() { return ptr; }
        T* operator->() { return ptr; }
        T& operator*()  { return *ptr; }

        const T* get() const { return ptr; }
        const T* operator->() const { return ptr; }
        const T& operator*() const { return *ptr; }

        bool operator==(const shared_ptr<T>& o) const { return ptr == o.ptr; }
        bool operator!=(const shared_ptr<T>& o) const { return ptr != o.ptr; }
        bool operator<(const shared_ptr<T>& o) const { return ptr < o.ptr; }    
        
        unsigned refcount() const { return *count; }
        
};


*/

#endif


END_NAMESPACE


#endif // POINTER_H
