diff --git a/Code/Misc/Misc.vcxproj b/Code/Misc/Misc.vcxproj index 82888770..beb55abe 100644 --- a/Code/Misc/Misc.vcxproj +++ b/Code/Misc/Misc.vcxproj @@ -152,7 +152,6 @@ - @@ -162,7 +161,6 @@ - diff --git a/Code/Misc/Resource/OResource.h b/Code/Misc/Resource/OResource.h index 3b0d0c17..a0573c92 100644 --- a/Code/Misc/Resource/OResource.h +++ b/Code/Misc/Resource/OResource.h @@ -61,7 +61,7 @@ namespace Oyster static OResource* Reload (OResource* resource); static bool Release (OResource* resource); - Utility::DynamicMemory::RefCount resourceRef; + Utility::DynamicMemory::ReferenceCount resourceRef; private: static OResource* ByteLoader (const wchar_t filename[], ResourceType type, OResource* old = 0); diff --git a/Code/Misc/Thread/OysterThread_Impl.cpp b/Code/Misc/Thread/OysterThread_Impl.cpp index 46b889fe..9605dd78 100644 --- a/Code/Misc/Thread/OysterThread_Impl.cpp +++ b/Code/Misc/Thread/OysterThread_Impl.cpp @@ -10,14 +10,14 @@ #include using namespace Oyster::Thread; -using namespace Utility::DynamicMemory::SmartPointer; +using namespace Utility::DynamicMemory; #pragma region Declerations struct ThreadData; /** A typical Oyster thread function */ - typedef void (*ThreadFunction)(StdSmartPointer&); + typedef void (*ThreadFunction)(SmartPointer&); enum OYSTER_THREAD_STATE { @@ -32,12 +32,13 @@ using namespace Utility::DynamicMemory::SmartPointer; struct ThreadData { std::atomic state; // workerThread; // workerThread; // msec; // threadData; + SmartPointer threadData; PrivateData() :threadData(new ThreadData()) @@ -76,11 +77,11 @@ using namespace Utility::DynamicMemory::SmartPointer; int tempId = 0; std::vector IDS; -static void ThreadingFunction(StdSmartPointer &origin) +static void ThreadingFunction(SmartPointer &origin) { bool shouldContinue; - StdSmartPointer w = origin; + SmartPointer w = origin; theBegining: diff --git a/Code/Misc/Utilities-Impl.h b/Code/Misc/Utilities-Impl.h index 655f06c5..cc82c959 100644 --- a/Code/Misc/Utilities-Impl.h +++ b/Code/Misc/Utilities-Impl.h @@ -35,6 +35,7 @@ namespace Utility } } +#pragma region UnuiqePointer template UniquePointer::UniquePointer( Type *assignedInstance ) { @@ -191,110 +192,114 @@ namespace Utility { return this->operator bool(); } - - namespace SmartPointer +#pragma endregion + +#pragma region SmartPointer + template void SmartPointer::Destroy() { - template void StdSmartPointer::Destroy() - { - delete this->_rc; - this->_rc = NULL; - delete this->_ptr; - this->_ptr = NULL; - } - template StdSmartPointer::StdSmartPointer() - :_rc(0), _ptr(0) - { } - template StdSmartPointer::StdSmartPointer(T* p) - :_ptr(p) - { - this->_rc = new ReferenceCount(); + delete this->_rc; + this->_rc = NULL; + + //Use default function for memory deallocation. + SafeDeleteInstance(this->_ptr); + + this->_ptr = NULL; + } + template SmartPointer::SmartPointer() + :_rc(0), _ptr(0) + { } + template SmartPointer::SmartPointer(T* p) + :_ptr(p) + { + this->_rc = new ReferenceCount(); + this->_rc->Incref(); + } + template SmartPointer::SmartPointer(const SmartPointer& d) + :_ptr(d._ptr), _rc(d._rc) + { + if(this->_rc) this->_rc->Incref(); - } - template StdSmartPointer::StdSmartPointer(const StdSmartPointer& d) - :_ptr(d._ptr), _rc(d._rc) + } + template SmartPointer::~SmartPointer() + { + if (this->_rc && this->_rc->Decref() == 0) { - if(this->_rc) - this->_rc->Incref(); + Destroy(); } - template StdSmartPointer::~StdSmartPointer() + } + template SmartPointer& SmartPointer::operator= (const SmartPointer& p) + { + if (this != &p) { - if (this->_rc && this->_rc->Decref() == 0) + //Last to go? + if(this->_rc && this->_rc->Decref() == 0) { + //Call child specific Destroy(); } + + this->_ptr = p._ptr; + this->_rc = p._rc; + this->_rc->Incref(); } - template StdSmartPointer& StdSmartPointer::operator= (const StdSmartPointer& p) + return *this; + } + template SmartPointer& SmartPointer::operator= (T* p) + { + if (this->_ptr != p) { - if (this != &p) + //Last to go? + if(this->_rc) { - //Last to go? - if(this->_rc && this->_rc->Decref() == 0) + if(this->_rc->Decref() == 0) { //Call child specific Destroy(); - } - - this->_ptr = p._ptr; - this->_rc = p._rc; - this->_rc->Incref(); - } - return *this; - } - template StdSmartPointer& StdSmartPointer::operator= (T* p) - { - if (this->_ptr != p) - { - //Last to go? - if(this->_rc) - { - if(this->_rc->Decref() == 0) - { - //Call child specific - Destroy(); - this->_rc = new ReferenceCount(); - } - } - else this->_rc = new ReferenceCount(); - - this->_ptr = p; - this->_rc->Incref(); + } } - return *this; - } - template inline bool StdSmartPointer::operator== (const StdSmartPointer& d) - { - return d._ptr == this->_ptr; - } - template inline bool StdSmartPointer::operator== (const T& p) - { - return &p == this->_ptr; - } - template inline T& StdSmartPointer::operator* () - { - return *this->_ptr; - } - template inline T* StdSmartPointer::operator-> () - { - return this->_ptr; - } - template inline StdSmartPointer::operator T* () - { - return this->_ptr; - } - template inline StdSmartPointer::operator bool() - { - return (this->_ptr != 0); - } - template inline T* StdSmartPointer::Get() - { - return this->_ptr; - } - template inline bool StdSmartPointer::IsValid() - { - return (this->_ptr != NULL) ? true : false; + else + this->_rc = new ReferenceCount(); + + this->_ptr = p; + this->_rc->Incref(); } + return *this; } + template inline bool SmartPointer::operator== (const SmartPointer& d) + { + return d._ptr == this->_ptr; + } + template inline bool SmartPointer::operator== (const T& p) + { + return &p == this->_ptr; + } + template inline T& SmartPointer::operator* () + { + return *this->_ptr; + } + template inline T* SmartPointer::operator-> () + { + return this->_ptr; + } + template inline SmartPointer::operator T* () + { + return this->_ptr; + } + template inline SmartPointer::operator bool() + { + return (this->_ptr != 0); + } + template inline T* SmartPointer::Get() + { + return this->_ptr; + } + template inline bool SmartPointer::IsValid() + { + return (this->_ptr != NULL) ? true : false; + } +#pragma endregion + } } diff --git a/Code/Misc/Utilities.h b/Code/Misc/Utilities.h index 6f5cf3ab..ec8b0229 100644 --- a/Code/Misc/Utilities.h +++ b/Code/Misc/Utilities.h @@ -31,6 +31,22 @@ namespace Utility ******************************************************************/ template void SafeDeleteArray( Type dynamicArray[] ); + //! A simple reference counter with some extra functionality + struct ReferenceCount + { + private: + int count; + + public: + ReferenceCount() :count(0) { } + ReferenceCount(const ReferenceCount& o) { count = o.count; } + inline const ReferenceCount& operator=(const ReferenceCount& o) { count = o.count; return *this;} + inline void Incref() { this->count++; } + inline void Incref(int c) { this->count += c; } + inline int Decref() { return --this->count;} + inline void Reset() { this->count = 0; } + }; + //! Wrapper to safely transfer dynamic ownership/responsibility template struct UniquePointer { @@ -108,9 +124,9 @@ namespace Utility mutable Type *ownedInstance; }; - template - struct UniqueArray - { //! Wrapper to safely transfer dynamic ownership/responsibility + //! Wrapper to safely transfer dynamic ownership/responsibility + template struct UniqueArray + { public: /****************************************************************** * Assigns assignedInstance ownership to this UniquePonter, old owned array will be deleted. @@ -177,63 +193,40 @@ namespace Utility mutable Type *ownedArray; }; - struct ReferenceCount + //! Wrapper to manage references on a pointer. + template struct SmartPointer { private: - std::atomic count; + ReferenceCount *_rc; + T *_ptr; + + /** Destroys the pointer and returns the memory allocated. */ + void Destroy(); public: - ReferenceCount() :count(0) { } - ReferenceCount(const ReferenceCount& o) { count.store(o.count); } - inline const ReferenceCount& operator=(const ReferenceCount& o) { count.store(o.count); return *this;} - inline void Incref() { this->count++; } - inline void Incref(int c) { this->count += c; } - inline int Decref() { return --this->count;} - inline void Reset() { this->count = 0; } + SmartPointer(); + SmartPointer(T* p); + SmartPointer(const SmartPointer& d); + virtual~SmartPointer(); + SmartPointer& operator= (const SmartPointer& p); + SmartPointer& operator= (T* p); + bool operator== (const SmartPointer& d); + bool operator== (const T& p); + T& operator* (); + T* operator-> (); + operator T* (); + operator bool(); + + /** + * Returns the connected pointer + */ + T* Get(); + + /** Checks if the pointer is valid (not NULL) + * Returns true for valid, else false. + */ + bool IsValid(); }; - - namespace SmartPointer - { - //! Smart pointer for a regular object. - /** - * Regular objects, objects that is deleted normaly (ie not COM objects, or array pointers) - * can use this class to easy the use of dynamic memory - */ - template - struct StdSmartPointer - { - private: - ReferenceCount *_rc; - T *_ptr; - - /** Destroys the pointer and returns the memory allocated. */ - void Destroy(); - - public: - StdSmartPointer(); - StdSmartPointer(T* p); - StdSmartPointer(const StdSmartPointer& d); - virtual~StdSmartPointer(); - StdSmartPointer& operator= (const StdSmartPointer& p); - StdSmartPointer& operator= (T* p); - bool operator== (const StdSmartPointer& d); - bool operator== (const T& p); - T& operator* (); - T* operator-> (); - operator T* (); - operator bool(); - - /** - * Returns the connected pointer */ - T* Get(); - - /** Checks if the pointer is valid (not NULL) - Returns true for valid, else false. */ - bool IsValid(); - }; - } - - } namespace String @@ -379,6 +372,11 @@ namespace Utility template<> inline unsigned long long AverageWithDelta( const unsigned long long &origin, const unsigned long long &delta ) { return origin + (delta >> 1); } } + + namespace Thread + { + //Utilities for threading + } } #include "Utilities-Impl.h"