Fixed some structure issues and commented
This commit is contained in:
parent
a3ae340a30
commit
de01d23d1d
|
@ -152,7 +152,6 @@
|
|||
<ClCompile Include="Resource\OResource.cpp" />
|
||||
<ClCompile Include="Thread\OysterMutex.cpp" />
|
||||
<ClCompile Include="Thread\OysterThread_Impl.cpp" />
|
||||
<ClCompile Include="Thread\OysterThread_Impl.cpp" />
|
||||
<ClCompile Include="Utilities.cpp" />
|
||||
<ClCompile Include="WinTimer.cpp" />
|
||||
</ItemGroup>
|
||||
|
@ -162,7 +161,6 @@
|
|||
<ClInclude Include="Thread\IThreadObject.h" />
|
||||
<ClInclude Include="Thread\OysterMutex.h" />
|
||||
<ClInclude Include="Thread\OysterThread.h" />
|
||||
<ClInclude Include="Thread\OysterThread.h" />
|
||||
<ClInclude Include="Utilities-Impl.h" />
|
||||
<ClInclude Include="Utilities.h" />
|
||||
<ClInclude Include="WinTimer.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);
|
||||
|
|
|
@ -10,14 +10,14 @@
|
|||
#include <atomic>
|
||||
|
||||
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<ThreadData>&);
|
||||
typedef void (*ThreadFunction)(SmartPointer<ThreadData>&);
|
||||
|
||||
enum OYSTER_THREAD_STATE
|
||||
{
|
||||
|
@ -32,12 +32,13 @@ using namespace Utility::DynamicMemory::SmartPointer;
|
|||
struct ThreadData
|
||||
{
|
||||
std::atomic<OYSTER_THREAD_STATE> state; //<! The current thread state.
|
||||
//OYSTER_THREAD_STATE state; //<! The current thread state.
|
||||
StdSmartPointer<std::thread> workerThread; //<! The worker thread.
|
||||
SmartPointer<std::thread> workerThread; //<! The worker thread.
|
||||
std::thread::id callingThread; //<! The owner thread.
|
||||
IThreadObject *owner; //<! The owner of the thread as IThread.
|
||||
std::atomic<int> msec; //<! A timer in miliseconds.
|
||||
|
||||
//OysterMutex mutexLock; //<! The lock, locking the member variabls.
|
||||
//OYSTER_THREAD_STATE state; //<! The current thread state.
|
||||
|
||||
ThreadData() {}
|
||||
~ThreadData() {}
|
||||
|
@ -45,7 +46,7 @@ using namespace Utility::DynamicMemory::SmartPointer;
|
|||
};
|
||||
struct OysterThread::PrivateData
|
||||
{
|
||||
StdSmartPointer<ThreadData> threadData;
|
||||
SmartPointer<ThreadData> threadData;
|
||||
|
||||
PrivateData()
|
||||
:threadData(new ThreadData())
|
||||
|
@ -76,11 +77,11 @@ using namespace Utility::DynamicMemory::SmartPointer;
|
|||
|
||||
int tempId = 0;
|
||||
std::vector<int> IDS;
|
||||
static void ThreadingFunction(StdSmartPointer<ThreadData> &origin)
|
||||
static void ThreadingFunction(SmartPointer<ThreadData> &origin)
|
||||
{
|
||||
|
||||
bool shouldContinue;
|
||||
StdSmartPointer<ThreadData> w = origin;
|
||||
SmartPointer<ThreadData> w = origin;
|
||||
|
||||
theBegining:
|
||||
|
||||
|
|
|
@ -35,6 +35,7 @@ namespace Utility
|
|||
}
|
||||
}
|
||||
|
||||
#pragma region UnuiqePointer
|
||||
template<typename Type>
|
||||
UniquePointer<Type>::UniquePointer( Type *assignedInstance )
|
||||
{
|
||||
|
@ -191,110 +192,114 @@ namespace Utility
|
|||
{
|
||||
return this->operator bool();
|
||||
}
|
||||
|
||||
namespace SmartPointer
|
||||
#pragma endregion
|
||||
|
||||
#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->_ptr;
|
||||
this->_ptr = NULL;
|
||||
}
|
||||
template<typename T> StdSmartPointer<T>::StdSmartPointer()
|
||||
:_rc(0), _ptr(0)
|
||||
{ }
|
||||
template<typename T> StdSmartPointer<T>::StdSmartPointer(T* p)
|
||||
:_ptr(p)
|
||||
{
|
||||
this->_rc = new ReferenceCount();
|
||||
delete this->_rc;
|
||||
this->_rc = NULL;
|
||||
|
||||
//Use default function for memory deallocation.
|
||||
SafeDeleteInstance<T>(this->_ptr);
|
||||
|
||||
this->_ptr = NULL;
|
||||
}
|
||||
template<typename T> SmartPointer<T>::SmartPointer()
|
||||
:_rc(0), _ptr(0)
|
||||
{ }
|
||||
template<typename T> SmartPointer<T>::SmartPointer(T* p)
|
||||
:_ptr(p)
|
||||
{
|
||||
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();
|
||||
}
|
||||
template<typename T> StdSmartPointer<T>::StdSmartPointer(const StdSmartPointer& d)
|
||||
:_ptr(d._ptr), _rc(d._rc)
|
||||
}
|
||||
template<typename T> SmartPointer<T>::~SmartPointer()
|
||||
{
|
||||
if (this->_rc && this->_rc->Decref() == 0)
|
||||
{
|
||||
if(this->_rc)
|
||||
this->_rc->Incref();
|
||||
Destroy();
|
||||
}
|
||||
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();
|
||||
}
|
||||
|
||||
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 && 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<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->_ptr = p;
|
||||
this->_rc->Incref();
|
||||
}
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
template<typename T> inline bool StdSmartPointer<T>::operator== (const StdSmartPointer<T>& d)
|
||||
{
|
||||
return d._ptr == this->_ptr;
|
||||
}
|
||||
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;
|
||||
else
|
||||
this->_rc = new ReferenceCount();
|
||||
|
||||
this->_ptr = p;
|
||||
this->_rc->Incref();
|
||||
}
|
||||
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
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -31,6 +31,22 @@ namespace Utility
|
|||
******************************************************************/
|
||||
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
|
||||
template<typename Type> struct UniquePointer
|
||||
{
|
||||
|
@ -108,9 +124,9 @@ namespace Utility
|
|||
mutable Type *ownedInstance;
|
||||
};
|
||||
|
||||
template<typename Type>
|
||||
struct UniqueArray
|
||||
{ //! Wrapper to safely transfer dynamic ownership/responsibility
|
||||
//! Wrapper to safely transfer dynamic ownership/responsibility
|
||||
template<typename Type> 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<typename T> struct SmartPointer
|
||||
{
|
||||
private:
|
||||
std::atomic<int> 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<T>& operator= (const SmartPointer<T>& p);
|
||||
SmartPointer<T>& operator= (T* p);
|
||||
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
|
||||
|
@ -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 )
|
||||
{ return origin + (delta >> 1); }
|
||||
}
|
||||
|
||||
namespace Thread
|
||||
{
|
||||
//Utilities for threading
|
||||
}
|
||||
}
|
||||
|
||||
#include "Utilities-Impl.h"
|
||||
|
|
Loading…
Reference in New Issue