3D Audio GSoC:
[blender.git] / intern / audaspace / intern / AUD_Reference.h
index 3ddeab2eff15524668ae3e06ca8fec1d3ca95b11..3977b22a1dd5ad0d3b18e56ba274b8caa57decc4 100644 (file)
 
 #include <map>
 
+// #define MEM_DEBUG
+
 #ifdef MEM_DEBUG
 #include <iostream>
 #include <typeinfo>
 #endif
 
+/**
+ * This class handles the reference counting.
+ */
 class AUD_ReferenceHandler
 {
 private:
+       /**
+        * Saves the reference counts.
+        */
        static std::map<void*, int> m_references;
 
 public:
+       /**
+        * Reference increment.
+        * \param reference The reference.
+        */
        static inline void incref(void* reference)
        {
                if(!reference)
@@ -60,6 +72,11 @@ public:
                }
        }
 
+       /**
+        * Reference decrement.
+        * \param reference The reference.
+        * \return Whether the reference has to be deleted.
+        */
        static inline bool decref(void* reference)
        {
                if(!reference)
@@ -89,24 +106,33 @@ public:
         * Creates a new reference counter.
         * \param reference The reference.
         */
-       AUD_Reference(T* reference = 0)
+       template <class U>
+       AUD_Reference(U* reference)
        {
-               m_original = m_reference = reference;
-               AUD_ReferenceHandler::incref(reference);
+               m_original = reference;
+               m_reference = dynamic_cast<T*>(reference);
+               AUD_ReferenceHandler::incref(m_original);
 #ifdef MEM_DEBUG
                if(m_reference != 0)
                        std::cerr << "+" << typeid(*m_reference).name() << std::endl;
 #endif
        }
 
+       AUD_Reference()
+       {
+               m_original = 0;
+               m_reference = 0;
+       }
+
        /**
         * Copies an AUD_Reference object.
         * \param ref The AUD_Reference object to copy.
         */
        AUD_Reference(const AUD_Reference& ref)
        {
-               m_original = m_reference = ref.m_reference;
-               AUD_ReferenceHandler::incref(m_reference);
+               m_original = ref.m_original;
+               m_reference = ref.m_reference;
+               AUD_ReferenceHandler::incref(m_original);
 #ifdef MEM_DEBUG
                if(m_reference != 0)
                        std::cerr << "+" << typeid(*m_reference).name() << std::endl;
@@ -182,6 +208,9 @@ public:
                return m_reference;
        }
 
+       /**
+        * Returns the original pointer.
+        */
        inline void* getOriginal() const
        {
                return m_original;