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,39 +192,42 @@ 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; delete this->_rc;
this->_rc = NULL; this->_rc = NULL;
delete this->_ptr;
//Use default function for memory deallocation.
SafeDeleteInstance<T>(this->_ptr);
this->_ptr = NULL; this->_ptr = NULL;
} }
template<typename T> StdSmartPointer<T>::StdSmartPointer() template<typename T> SmartPointer<T>::SmartPointer()
:_rc(0), _ptr(0) :_rc(0), _ptr(0)
{ } { }
template<typename T> StdSmartPointer<T>::StdSmartPointer(T* p) template<typename T> SmartPointer<T>::SmartPointer(T* p)
:_ptr(p) :_ptr(p)
{ {
this->_rc = new ReferenceCount(); this->_rc = new ReferenceCount();
this->_rc->Incref(); this->_rc->Incref();
} }
template<typename T> StdSmartPointer<T>::StdSmartPointer(const StdSmartPointer& d) template<typename T> SmartPointer<T>::SmartPointer(const SmartPointer& d)
:_ptr(d._ptr), _rc(d._rc) :_ptr(d._ptr), _rc(d._rc)
{ {
if(this->_rc) if(this->_rc)
this->_rc->Incref(); this->_rc->Incref();
} }
template<typename T> StdSmartPointer<T>::~StdSmartPointer() template<typename T> SmartPointer<T>::~SmartPointer()
{ {
if (this->_rc && this->_rc->Decref() == 0) if (this->_rc && this->_rc->Decref() == 0)
{ {
Destroy(); Destroy();
} }
} }
template<typename T> StdSmartPointer<T>& StdSmartPointer<T>::operator= (const StdSmartPointer<T>& p) template<typename T> SmartPointer<T>& SmartPointer<T>::operator= (const SmartPointer<T>& p)
{ {
if (this != &p) if (this != &p)
{ {
@ -240,7 +244,7 @@ namespace Utility
} }
return *this; return *this;
} }
template<typename T> StdSmartPointer<T>& StdSmartPointer<T>::operator= (T* p) template<typename T> SmartPointer<T>& SmartPointer<T>::operator= (T* p)
{ {
if (this->_ptr != p) if (this->_ptr != p)
{ {
@ -262,39 +266,40 @@ namespace Utility
} }
return *this; return *this;
} }
template<typename T> inline bool StdSmartPointer<T>::operator== (const StdSmartPointer<T>& d) template<typename T> inline bool SmartPointer<T>::operator== (const SmartPointer<T>& d)
{ {
return d._ptr == this->_ptr; return d._ptr == this->_ptr;
} }
template<typename T> inline bool StdSmartPointer<T>::operator== (const T& p) template<typename T> inline bool SmartPointer<T>::operator== (const T& p)
{ {
return &p == this->_ptr; return &p == this->_ptr;
} }
template<typename T> inline T& StdSmartPointer<T>::operator* () template<typename T> inline T& SmartPointer<T>::operator* ()
{ {
return *this->_ptr; return *this->_ptr;
} }
template<typename T> inline T* StdSmartPointer<T>::operator-> () template<typename T> inline T* SmartPointer<T>::operator-> ()
{ {
return this->_ptr; return this->_ptr;
} }
template<typename T> inline StdSmartPointer<T>::operator T* () template<typename T> inline SmartPointer<T>::operator T* ()
{ {
return this->_ptr; return this->_ptr;
} }
template<typename T> inline StdSmartPointer<T>::operator bool() template<typename T> inline SmartPointer<T>::operator bool()
{ {
return (this->_ptr != 0); return (this->_ptr != 0);
} }
template<typename T> inline T* StdSmartPointer<T>::Get() template<typename T> inline T* SmartPointer<T>::Get()
{ {
return this->_ptr; return this->_ptr;
} }
template<typename T> inline bool StdSmartPointer<T>::IsValid() template<typename T> inline bool SmartPointer<T>::IsValid()
{ {
return (this->_ptr != NULL) ? true : false; 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,30 +193,8 @@ namespace Utility
mutable Type *ownedArray; mutable Type *ownedArray;
}; };
struct ReferenceCount //! Wrapper to manage references on a pointer.
{ template<typename T> struct SmartPointer
private:
std::atomic<int> count;
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; }
};
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: private:
ReferenceCount *_rc; ReferenceCount *_rc;
@ -210,13 +204,13 @@ namespace Utility
void Destroy(); void Destroy();
public: public:
StdSmartPointer(); SmartPointer();
StdSmartPointer(T* p); SmartPointer(T* p);
StdSmartPointer(const StdSmartPointer& d); SmartPointer(const SmartPointer& d);
virtual~StdSmartPointer(); virtual~SmartPointer();
StdSmartPointer<T>& operator= (const StdSmartPointer<T>& p); SmartPointer<T>& operator= (const SmartPointer<T>& p);
StdSmartPointer<T>& operator= (T* p); SmartPointer<T>& operator= (T* p);
bool operator== (const StdSmartPointer<T>& d); bool operator== (const SmartPointer<T>& d);
bool operator== (const T& p); bool operator== (const T& p);
T& operator* (); T& operator* ();
T* operator-> (); T* operator-> ();
@ -224,18 +218,17 @@ namespace Utility
operator bool(); operator bool();
/** /**
* Returns the connected pointer */ * Returns the connected pointer
*/
T* Get(); T* Get();
/** Checks if the pointer is valid (not NULL) /** Checks if the pointer is valid (not NULL)
Returns true for valid, else false. */ * Returns true for valid, else false.
*/
bool IsValid(); bool IsValid();
}; };
} }
}
namespace String namespace String
{ {
// string // 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"