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="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" />

View File

@ -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);

View File

@ -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:

View File

@ -35,6 +35,7 @@ namespace Utility
}
}
#pragma region UnuiqePointer
template<typename Type>
UniquePointer<Type>::UniquePointer( Type *assignedInstance )
{
@ -191,39 +192,42 @@ namespace Utility
{
return this->operator bool();
}
#pragma endregion
namespace SmartPointer
{
template<typename T> void StdSmartPointer<T>::Destroy()
#pragma region SmartPointer
template<typename T> void SmartPointer<T>::Destroy()
{
delete this->_rc;
this->_rc = NULL;
delete this->_ptr;
//Use default function for memory deallocation.
SafeDeleteInstance<T>(this->_ptr);
this->_ptr = NULL;
}
template<typename T> StdSmartPointer<T>::StdSmartPointer()
template<typename T> SmartPointer<T>::SmartPointer()
:_rc(0), _ptr(0)
{ }
template<typename T> StdSmartPointer<T>::StdSmartPointer(T* p)
template<typename T> SmartPointer<T>::SmartPointer(T* p)
:_ptr(p)
{
this->_rc = new ReferenceCount();
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)
{
if(this->_rc)
this->_rc->Incref();
}
template<typename T> StdSmartPointer<T>::~StdSmartPointer()
template<typename T> SmartPointer<T>::~SmartPointer()
{
if (this->_rc && this->_rc->Decref() == 0)
{
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)
{
@ -240,7 +244,7 @@ namespace Utility
}
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)
{
@ -262,39 +266,40 @@ namespace Utility
}
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;
}
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;
}
template<typename T> inline T& StdSmartPointer<T>::operator* ()
template<typename T> inline T& SmartPointer<T>::operator* ()
{
return *this->_ptr;
}
template<typename T> inline T* StdSmartPointer<T>::operator-> ()
template<typename T> inline T* SmartPointer<T>::operator-> ()
{
return this->_ptr;
}
template<typename T> inline StdSmartPointer<T>::operator T* ()
template<typename T> inline SmartPointer<T>::operator T* ()
{
return this->_ptr;
}
template<typename T> inline StdSmartPointer<T>::operator bool()
template<typename T> inline SmartPointer<T>::operator bool()
{
return (this->_ptr != 0);
}
template<typename T> inline T* StdSmartPointer<T>::Get()
template<typename T> inline T* SmartPointer<T>::Get()
{
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;
}
}
#pragma endregion
}
}

View File

@ -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,30 +193,8 @@ namespace Utility
mutable Type *ownedArray;
};
struct ReferenceCount
{
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
//! Wrapper to manage references on a pointer.
template<typename T> struct SmartPointer
{
private:
ReferenceCount *_rc;
@ -210,13 +204,13 @@ namespace Utility
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);
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-> ();
@ -224,18 +218,17 @@ namespace Utility
operator bool();
/**
* Returns the connected pointer */
* Returns the connected pointer
*/
T* Get();
/** Checks if the pointer is valid (not NULL)
Returns true for valid, else false. */
* Returns true for valid, else false.
*/
bool IsValid();
};
}
}
namespace 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 )
{ return origin + (delta >> 1); }
}
namespace Thread
{
//Utilities for threading
}
}
#include "Utilities-Impl.h"