Fixed some structure issues and commented

This commit is contained in:
dean11 2013-11-29 09:23:08 +01:00
parent a3ae340a30
commit de01d23d1d
5 changed files with 153 additions and 151 deletions

View File

@ -152,7 +152,6 @@
<ClCompile Include="Resource\OResource.cpp" /> <ClCompile Include="Resource\OResource.cpp" />
<ClCompile Include="Thread\OysterMutex.cpp" /> <ClCompile Include="Thread\OysterMutex.cpp" />
<ClCompile Include="Thread\OysterThread_Impl.cpp" /> <ClCompile Include="Thread\OysterThread_Impl.cpp" />
<ClCompile Include="Thread\OysterThread_Impl.cpp" />
<ClCompile Include="Utilities.cpp" /> <ClCompile Include="Utilities.cpp" />
<ClCompile Include="WinTimer.cpp" /> <ClCompile Include="WinTimer.cpp" />
</ItemGroup> </ItemGroup>
@ -162,7 +161,6 @@
<ClInclude Include="Thread\IThreadObject.h" /> <ClInclude Include="Thread\IThreadObject.h" />
<ClInclude Include="Thread\OysterMutex.h" /> <ClInclude Include="Thread\OysterMutex.h" />
<ClInclude Include="Thread\OysterThread.h" /> <ClInclude Include="Thread\OysterThread.h" />
<ClInclude Include="Thread\OysterThread.h" />
<ClInclude Include="Utilities-Impl.h" /> <ClInclude Include="Utilities-Impl.h" />
<ClInclude Include="Utilities.h" /> <ClInclude Include="Utilities.h" />
<ClInclude Include="WinTimer.h" /> <ClInclude Include="WinTimer.h" />

View File

@ -61,7 +61,7 @@ namespace Oyster
static OResource* Reload (OResource* resource); static OResource* Reload (OResource* resource);
static bool Release (OResource* resource); static bool Release (OResource* resource);
Utility::DynamicMemory::RefCount resourceRef; Utility::DynamicMemory::ReferenceCount resourceRef;
private: private:
static OResource* ByteLoader (const wchar_t filename[], ResourceType type, OResource* old = 0); static OResource* ByteLoader (const wchar_t filename[], ResourceType type, OResource* old = 0);

View File

@ -10,14 +10,14 @@
#include <atomic> #include <atomic>
using namespace Oyster::Thread; using namespace Oyster::Thread;
using namespace Utility::DynamicMemory::SmartPointer; using namespace Utility::DynamicMemory;
#pragma region Declerations #pragma region Declerations
struct ThreadData; struct ThreadData;
/** A typical Oyster thread function */ /** A typical Oyster thread function */
typedef void (*ThreadFunction)(StdSmartPointer<ThreadData>&); typedef void (*ThreadFunction)(SmartPointer<ThreadData>&);
enum OYSTER_THREAD_STATE enum OYSTER_THREAD_STATE
{ {
@ -32,12 +32,13 @@ using namespace Utility::DynamicMemory::SmartPointer;
struct ThreadData struct ThreadData
{ {
std::atomic<OYSTER_THREAD_STATE> state; //<! The current thread state. std::atomic<OYSTER_THREAD_STATE> state; //<! The current thread state.
//OYSTER_THREAD_STATE state; //<! The current thread state. SmartPointer<std::thread> workerThread; //<! The worker thread.
StdSmartPointer<std::thread> workerThread; //<! The worker thread.
std::thread::id callingThread; //<! The owner thread. std::thread::id callingThread; //<! The owner thread.
IThreadObject *owner; //<! The owner of the thread as IThread. IThreadObject *owner; //<! The owner of the thread as IThread.
std::atomic<int> msec; //<! A timer in miliseconds. std::atomic<int> msec; //<! A timer in miliseconds.
//OysterMutex mutexLock; //<! The lock, locking the member variabls. //OysterMutex mutexLock; //<! The lock, locking the member variabls.
//OYSTER_THREAD_STATE state; //<! The current thread state.
ThreadData() {} ThreadData() {}
~ThreadData() {} ~ThreadData() {}
@ -45,7 +46,7 @@ using namespace Utility::DynamicMemory::SmartPointer;
}; };
struct OysterThread::PrivateData struct OysterThread::PrivateData
{ {
StdSmartPointer<ThreadData> threadData; SmartPointer<ThreadData> threadData;
PrivateData() PrivateData()
:threadData(new ThreadData()) :threadData(new ThreadData())
@ -76,11 +77,11 @@ using namespace Utility::DynamicMemory::SmartPointer;
int tempId = 0; int tempId = 0;
std::vector<int> IDS; std::vector<int> IDS;
static void ThreadingFunction(StdSmartPointer<ThreadData> &origin) static void ThreadingFunction(SmartPointer<ThreadData> &origin)
{ {
bool shouldContinue; bool shouldContinue;
StdSmartPointer<ThreadData> w = origin; SmartPointer<ThreadData> w = origin;
theBegining: theBegining:

View File

@ -35,6 +35,7 @@ namespace Utility
} }
} }
#pragma region UnuiqePointer
template<typename Type> template<typename Type>
UniquePointer<Type>::UniquePointer( Type *assignedInstance ) UniquePointer<Type>::UniquePointer( Type *assignedInstance )
{ {
@ -191,110 +192,114 @@ namespace Utility
{ {
return this->operator bool(); return this->operator bool();
} }
#pragma endregion
namespace SmartPointer #pragma region SmartPointer
template<typename T> void SmartPointer<T>::Destroy()
{ {
template<typename T> void StdSmartPointer<T>::Destroy() delete this->_rc;
{ this->_rc = NULL;
delete this->_rc;
this->_rc = NULL; //Use default function for memory deallocation.
delete this->_ptr; SafeDeleteInstance<T>(this->_ptr);
this->_ptr = NULL;
} this->_ptr = NULL;
template<typename T> StdSmartPointer<T>::StdSmartPointer() }
:_rc(0), _ptr(0) template<typename T> SmartPointer<T>::SmartPointer()
{ } :_rc(0), _ptr(0)
template<typename T> StdSmartPointer<T>::StdSmartPointer(T* p) { }
:_ptr(p) template<typename T> SmartPointer<T>::SmartPointer(T* p)
{ :_ptr(p)
this->_rc = new ReferenceCount(); {
this->_rc = new ReferenceCount();
this->_rc->Incref();
}
template<typename T> SmartPointer<T>::SmartPointer(const SmartPointer& d)
:_ptr(d._ptr), _rc(d._rc)
{
if(this->_rc)
this->_rc->Incref(); this->_rc->Incref();
} }
template<typename T> StdSmartPointer<T>::StdSmartPointer(const StdSmartPointer& d) template<typename T> SmartPointer<T>::~SmartPointer()
:_ptr(d._ptr), _rc(d._rc) {
if (this->_rc && this->_rc->Decref() == 0)
{ {
if(this->_rc) Destroy();
this->_rc->Incref();
} }
template<typename T> StdSmartPointer<T>::~StdSmartPointer() }
template<typename T> SmartPointer<T>& SmartPointer<T>::operator= (const SmartPointer<T>& 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(); Destroy();
} }
this->_ptr = p._ptr;
this->_rc = p._rc;
this->_rc->Incref();
} }
template<typename T> StdSmartPointer<T>& StdSmartPointer<T>::operator= (const StdSmartPointer<T>& p) return *this;
}
template<typename T> SmartPointer<T>& SmartPointer<T>::operator= (T* p)
{
if (this->_ptr != p)
{ {
if (this != &p) //Last to go?
if(this->_rc)
{ {
//Last to go? if(this->_rc->Decref() == 0)
if(this->_rc && this->_rc->Decref() == 0)
{ {
//Call child specific //Call child specific
Destroy(); Destroy();
}
this->_ptr = p._ptr;
this->_rc = p._rc;
this->_rc->Incref();
}
return *this;
}
template<typename T> StdSmartPointer<T>& StdSmartPointer<T>::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->_rc = new ReferenceCount();
}
this->_ptr = p;
this->_rc->Incref();
} }
return *this; else
} this->_rc = new ReferenceCount();
template<typename T> inline bool StdSmartPointer<T>::operator== (const StdSmartPointer<T>& d)
{ this->_ptr = p;
return d._ptr == this->_ptr; this->_rc->Incref();
}
template<typename T> inline bool StdSmartPointer<T>::operator== (const T& p)
{
return &p == this->_ptr;
}
template<typename T> inline T& StdSmartPointer<T>::operator* ()
{
return *this->_ptr;
}
template<typename T> inline T* StdSmartPointer<T>::operator-> ()
{
return this->_ptr;
}
template<typename T> inline StdSmartPointer<T>::operator T* ()
{
return this->_ptr;
}
template<typename T> inline StdSmartPointer<T>::operator bool()
{
return (this->_ptr != 0);
}
template<typename T> inline T* StdSmartPointer<T>::Get()
{
return this->_ptr;
}
template<typename T> inline bool StdSmartPointer<T>::IsValid()
{
return (this->_ptr != NULL) ? true : false;
} }
return *this;
} }
template<typename T> inline bool SmartPointer<T>::operator== (const SmartPointer<T>& d)
{
return d._ptr == this->_ptr;
}
template<typename T> inline bool SmartPointer<T>::operator== (const T& p)
{
return &p == this->_ptr;
}
template<typename T> inline T& SmartPointer<T>::operator* ()
{
return *this->_ptr;
}
template<typename T> inline T* SmartPointer<T>::operator-> ()
{
return this->_ptr;
}
template<typename T> inline SmartPointer<T>::operator T* ()
{
return this->_ptr;
}
template<typename T> inline SmartPointer<T>::operator bool()
{
return (this->_ptr != 0);
}
template<typename T> inline T* SmartPointer<T>::Get()
{
return this->_ptr;
}
template<typename T> inline bool SmartPointer<T>::IsValid()
{
return (this->_ptr != NULL) ? true : false;
}
#pragma endregion
} }
} }

View File

@ -31,6 +31,22 @@ namespace Utility
******************************************************************/ ******************************************************************/
template<typename Type> void SafeDeleteArray( Type dynamicArray[] ); template<typename Type> 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 //! Wrapper to safely transfer dynamic ownership/responsibility
template<typename Type> struct UniquePointer template<typename Type> struct UniquePointer
{ {
@ -108,9 +124,9 @@ namespace Utility
mutable Type *ownedInstance; mutable Type *ownedInstance;
}; };
template<typename Type> //! Wrapper to safely transfer dynamic ownership/responsibility
struct UniqueArray template<typename Type> struct UniqueArray
{ //! Wrapper to safely transfer dynamic ownership/responsibility {
public: public:
/****************************************************************** /******************************************************************
* Assigns assignedInstance ownership to this UniquePonter, old owned array will be deleted. * Assigns assignedInstance ownership to this UniquePonter, old owned array will be deleted.
@ -177,63 +193,40 @@ namespace Utility
mutable Type *ownedArray; mutable Type *ownedArray;
}; };
struct ReferenceCount //! Wrapper to manage references on a pointer.
template<typename T> struct SmartPointer
{ {
private: private:
std::atomic<int> count; ReferenceCount *_rc;
T *_ptr;
/** Destroys the pointer and returns the memory allocated. */
void Destroy();
public: public:
ReferenceCount() :count(0) { } SmartPointer();
ReferenceCount(const ReferenceCount& o) { count.store(o.count); } SmartPointer(T* p);
inline const ReferenceCount& operator=(const ReferenceCount& o) { count.store(o.count); return *this;} SmartPointer(const SmartPointer& d);
inline void Incref() { this->count++; } virtual~SmartPointer();
inline void Incref(int c) { this->count += c; } SmartPointer<T>& operator= (const SmartPointer<T>& p);
inline int Decref() { return --this->count;} SmartPointer<T>& operator= (T* p);
inline void Reset() { this->count = 0; } bool operator== (const SmartPointer<T>& 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<typename T>
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<T>& operator= (const StdSmartPointer<T>& p);
StdSmartPointer<T>& operator= (T* p);
bool operator== (const StdSmartPointer<T>& 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 namespace String
@ -379,6 +372,11 @@ namespace Utility
template<> inline unsigned long long AverageWithDelta<unsigned long long>( const unsigned long long &origin, const unsigned long long &delta ) template<> inline unsigned long long AverageWithDelta<unsigned long long>( const unsigned long long &origin, const unsigned long long &delta )
{ return origin + (delta >> 1); } { return origin + (delta >> 1); }
} }
namespace Thread
{
//Utilities for threading
}
} }
#include "Utilities-Impl.h" #include "Utilities-Impl.h"