2013-11-28 16:06:01 +01:00
|
|
|
/////////////////////////////////////////////////////////////////////
|
2013-11-06 22:52:00 +01:00
|
|
|
// Utility Collection of Miscellanious Handy Functions
|
2013-11-28 16:06:01 +01:00
|
|
|
//
|
|
|
|
// Created 2013 by Dan Andersson
|
|
|
|
// Edited 2013 by
|
|
|
|
// * Dan Andersson
|
|
|
|
// * Dennis Andersen
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
2013-11-06 22:52:00 +01:00
|
|
|
|
|
|
|
#ifndef UTILITIES_H
|
|
|
|
#define UTILITIES_H
|
|
|
|
|
2014-01-07 10:26:09 +01:00
|
|
|
//warning C4150: deletion of pointer to incomplete type, no destructor called
|
|
|
|
#pragma warning(disable : 4150)
|
|
|
|
|
2013-11-06 22:52:00 +01:00
|
|
|
#include <string>
|
|
|
|
#include <istream>
|
|
|
|
#include <vector>
|
|
|
|
#include <locale>
|
2013-11-09 23:36:38 +01:00
|
|
|
#include <limits>
|
2013-11-27 22:39:49 +01:00
|
|
|
#include <atomic>
|
2013-11-06 22:52:00 +01:00
|
|
|
|
|
|
|
namespace Utility
|
|
|
|
{
|
2013-11-13 12:07:21 +01:00
|
|
|
namespace DynamicMemory
|
2013-11-09 23:36:38 +01:00
|
|
|
{
|
2013-11-28 16:18:20 +01:00
|
|
|
/******************************************************************
|
|
|
|
* If dynamicInstance is not NULL, then delete.
|
|
|
|
******************************************************************/
|
2013-11-13 12:07:21 +01:00
|
|
|
template<typename Type> void SafeDeleteInstance( Type *dynamicInstance );
|
|
|
|
|
2013-11-28 16:18:20 +01:00
|
|
|
/******************************************************************
|
|
|
|
* If dynamicArray is not NULL, then delete [].
|
|
|
|
******************************************************************/
|
2013-11-13 12:07:21 +01:00
|
|
|
template<typename Type> void SafeDeleteArray( Type dynamicArray[] );
|
|
|
|
|
2013-11-29 09:23:08 +01:00
|
|
|
//! 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; }
|
|
|
|
};
|
|
|
|
|
2013-11-25 08:57:47 +01:00
|
|
|
//! Wrapper to safely transfer dynamic ownership/responsibility
|
|
|
|
template<typename Type> struct UniquePointer
|
|
|
|
{
|
2013-11-09 23:36:38 +01:00
|
|
|
public:
|
2013-11-28 16:18:20 +01:00
|
|
|
/******************************************************************
|
|
|
|
* Assigns assignedInstance ownership to this UniquePonter, old owned instance will be deleted.
|
|
|
|
* If NULL is assigned is equivalent with clearing all responsibilities from this UniquePointer.
|
|
|
|
******************************************************************/
|
2013-11-28 16:06:01 +01:00
|
|
|
UniquePointer( Type *assignedInstance = NULL );
|
2013-11-13 12:07:21 +01:00
|
|
|
|
2013-11-28 16:18:20 +01:00
|
|
|
/******************************************************************
|
|
|
|
* Transfers assignedInstance ownership from donor to this UniquePonter, old owned instance will be deleted.
|
|
|
|
* If donor had nothing, is equivalent with clearing all responsibilities from this UniquePointer.
|
|
|
|
******************************************************************/
|
2013-11-26 09:16:51 +01:00
|
|
|
UniquePointer( const UniquePointer<Type> &donor );
|
|
|
|
|
2013-11-28 16:18:20 +01:00
|
|
|
/******************************************************************
|
|
|
|
* Will auto delete assigned dynamic instance.
|
|
|
|
******************************************************************/
|
2013-11-09 23:36:38 +01:00
|
|
|
~UniquePointer();
|
|
|
|
|
2013-11-28 16:18:20 +01:00
|
|
|
/******************************************************************
|
|
|
|
* Transfers assignedInstance ownership from donor to this UniquePonter, old owned instance will be deleted.
|
|
|
|
* If donor had nothing, is equivalent with clearing all responsibilities from this UniquePointer.
|
|
|
|
******************************************************************/
|
2013-11-09 23:36:38 +01:00
|
|
|
UniquePointer<Type> & operator = ( const UniquePointer<Type> &donor );
|
|
|
|
|
2013-11-28 16:18:20 +01:00
|
|
|
/******************************************************************
|
|
|
|
* Access the assigned dynamic instance. Will crash if nothing there
|
|
|
|
******************************************************************/
|
2013-11-09 23:36:38 +01:00
|
|
|
operator Type* ();
|
2013-11-13 12:07:21 +01:00
|
|
|
|
2013-11-28 16:18:20 +01:00
|
|
|
/******************************************************************
|
|
|
|
* Access the assigned dynamic instance. Will crash if nothing there
|
|
|
|
******************************************************************/
|
2013-11-09 23:36:38 +01:00
|
|
|
operator const Type* () const;
|
2013-11-13 12:07:21 +01:00
|
|
|
|
2013-11-28 16:18:20 +01:00
|
|
|
/******************************************************************
|
|
|
|
* Access members of the assigned dynamic instance. Will crash if nothing there
|
|
|
|
******************************************************************/
|
2013-11-09 23:36:38 +01:00
|
|
|
Type * operator -> ();
|
2013-11-13 12:07:21 +01:00
|
|
|
|
2013-11-28 16:18:20 +01:00
|
|
|
/******************************************************************
|
|
|
|
* Access members of the assigned dynamic instance. Will crash if nothing there
|
|
|
|
******************************************************************/
|
2013-11-09 23:36:38 +01:00
|
|
|
const Type * operator -> () const;
|
|
|
|
|
2013-11-28 16:18:20 +01:00
|
|
|
/******************************************************************
|
|
|
|
* @return true if this UniquePointer have a current ownership/responsibility of a dynamic instance.
|
|
|
|
******************************************************************/
|
2013-11-09 23:36:38 +01:00
|
|
|
operator bool () const;
|
|
|
|
|
2013-11-28 16:18:20 +01:00
|
|
|
/******************************************************************
|
|
|
|
* @return true if this ownedInstance matches with stray
|
|
|
|
******************************************************************/
|
2013-11-28 11:15:25 +01:00
|
|
|
bool operator == ( Type *stray ) const;
|
|
|
|
|
2013-11-28 16:18:20 +01:00
|
|
|
/******************************************************************
|
|
|
|
* @return false if this ownedInstance matches with stray
|
|
|
|
******************************************************************/
|
2013-11-28 11:15:25 +01:00
|
|
|
bool operator != ( Type *stray ) const;
|
|
|
|
|
2013-11-28 16:18:20 +01:00
|
|
|
/******************************************************************
|
|
|
|
* This UniquePointer drops all claims of ownership/responsibility and returns the dynamic instance. Now it is your responsibility to delete.
|
|
|
|
******************************************************************/
|
2013-11-09 23:36:38 +01:00
|
|
|
Type* Release();
|
2013-11-13 12:07:21 +01:00
|
|
|
|
2013-11-28 16:18:20 +01:00
|
|
|
/******************************************************************
|
|
|
|
* @return true if this UniquePointer have a current ownership/responsibility of a dynamic instance.
|
|
|
|
* inline of @see operator bool () const
|
|
|
|
******************************************************************/
|
2013-11-13 12:07:21 +01:00
|
|
|
bool HaveOwnership() const;
|
|
|
|
|
2013-11-09 23:36:38 +01:00
|
|
|
private:
|
2013-11-13 12:07:21 +01:00
|
|
|
mutable Type *ownedInstance;
|
2013-11-09 23:36:38 +01:00
|
|
|
};
|
|
|
|
|
2013-11-29 09:23:08 +01:00
|
|
|
//! Wrapper to safely transfer dynamic ownership/responsibility
|
|
|
|
template<typename Type> struct UniqueArray
|
|
|
|
{
|
2013-11-13 12:07:21 +01:00
|
|
|
public:
|
2013-11-28 16:18:20 +01:00
|
|
|
/******************************************************************
|
|
|
|
* Assigns assignedInstance ownership to this UniquePonter, old owned array will be deleted.
|
|
|
|
* If NULL is assigned is equivalent with clearing all responsibilities from this UniqueArray.
|
|
|
|
******************************************************************/
|
2013-11-13 12:07:21 +01:00
|
|
|
UniqueArray( Type assignedArray[] = NULL );
|
|
|
|
|
2013-11-28 16:18:20 +01:00
|
|
|
/******************************************************************
|
|
|
|
* Transfers assignedInstance ownership from donor to this UniquePonter, old owned array will be deleted.
|
|
|
|
* If donor had nothing, is equivalent with clearing all responsibilities from this UniqueArray.
|
|
|
|
******************************************************************/
|
2013-11-26 09:16:51 +01:00
|
|
|
UniqueArray( const UniqueArray<Type> &donor );
|
|
|
|
|
2013-11-28 16:18:20 +01:00
|
|
|
/******************************************************************
|
|
|
|
* Will auto delete assigned dynamic array.
|
|
|
|
******************************************************************/
|
2013-11-13 12:07:21 +01:00
|
|
|
~UniqueArray();
|
|
|
|
|
2013-11-28 16:18:20 +01:00
|
|
|
/******************************************************************
|
|
|
|
* Transfers assignedInstance ownership from donor to this UniquePonter, old owned array will be deleted.
|
|
|
|
* If donor had nothing, is equivalent with clearing all responsibilities from this UniqueArray.
|
|
|
|
******************************************************************/
|
2013-11-13 12:07:21 +01:00
|
|
|
UniqueArray<Type> & operator = ( const UniqueArray<Type> &donor );
|
|
|
|
|
2013-11-28 16:18:20 +01:00
|
|
|
/******************************************************************
|
|
|
|
* Accesses the instance at index i of this UniqeArray's owned dynamic array.
|
|
|
|
* Will crash if out-of-bound or there is no assigned array.
|
|
|
|
******************************************************************/
|
2013-11-13 12:07:21 +01:00
|
|
|
template<typename Index> Type & operator [] ( Index i );
|
|
|
|
|
2013-11-28 16:18:20 +01:00
|
|
|
/******************************************************************
|
|
|
|
* Accesses the instance at index i of this UniqeArray's owned dynamic array.
|
|
|
|
* Will crash if out-of-bound or there is no assigned array.
|
|
|
|
******************************************************************/
|
2013-11-13 12:07:21 +01:00
|
|
|
template<typename Index> const Type & operator [] ( Index i ) const;
|
2013-11-09 23:36:38 +01:00
|
|
|
|
2013-11-28 16:18:20 +01:00
|
|
|
/******************************************************************
|
|
|
|
* @return true if this UniqueArray have a current ownership/responsibility of a dynamic array.
|
|
|
|
******************************************************************/
|
2013-11-13 12:07:21 +01:00
|
|
|
operator bool () const;
|
2013-11-09 23:36:38 +01:00
|
|
|
|
2013-11-28 16:18:20 +01:00
|
|
|
/******************************************************************
|
|
|
|
* @return true if this ownedInstance matches with stray.
|
|
|
|
******************************************************************/
|
2013-11-28 11:15:25 +01:00
|
|
|
bool operator == ( Type *stray ) const;
|
|
|
|
|
2013-11-28 16:18:20 +01:00
|
|
|
/******************************************************************
|
|
|
|
* @return false if this ownedInstance matches with stray.
|
|
|
|
******************************************************************/
|
2013-11-28 11:15:25 +01:00
|
|
|
bool operator != ( Type *stray ) const;
|
|
|
|
|
2013-11-28 16:18:20 +01:00
|
|
|
/******************************************************************
|
|
|
|
* This UniqueArray drops all claims of ownership/responsibility and returns the dynamic array. Now it is your responsibility to delete.
|
|
|
|
******************************************************************/
|
2013-11-13 12:07:21 +01:00
|
|
|
Type* Release();
|
2013-11-09 23:36:38 +01:00
|
|
|
|
2013-11-28 16:18:20 +01:00
|
|
|
/******************************************************************
|
|
|
|
* @return true if this UniqueArray have a current ownership/responsibility of a dynamic array.
|
|
|
|
* inline of @see operator bool () const
|
|
|
|
******************************************************************/
|
2013-11-13 12:07:21 +01:00
|
|
|
bool HaveOwnership() const;
|
2013-11-09 23:36:38 +01:00
|
|
|
|
2013-11-13 12:07:21 +01:00
|
|
|
private:
|
|
|
|
mutable Type *ownedArray;
|
|
|
|
};
|
2013-11-22 10:46:25 +01:00
|
|
|
|
2013-11-29 09:23:08 +01:00
|
|
|
//! Wrapper to manage references on a pointer.
|
|
|
|
template<typename T> struct SmartPointer
|
2013-11-22 10:46:25 +01:00
|
|
|
{
|
|
|
|
private:
|
2013-11-29 09:23:08 +01:00
|
|
|
ReferenceCount *_rc;
|
|
|
|
T *_ptr;
|
|
|
|
|
|
|
|
/** Destroys the pointer and returns the memory allocated. */
|
2013-12-09 22:22:05 +01:00
|
|
|
void Destroy();
|
2013-11-22 10:46:25 +01:00
|
|
|
|
|
|
|
public:
|
2013-11-29 09:23:08 +01:00
|
|
|
SmartPointer();
|
2013-12-06 11:46:47 +01:00
|
|
|
SmartPointer(UniquePointer<T>& up);
|
2013-11-29 09:23:08 +01:00
|
|
|
SmartPointer(T* p);
|
|
|
|
SmartPointer(const SmartPointer& d);
|
|
|
|
virtual~SmartPointer();
|
|
|
|
SmartPointer<T>& operator= (const SmartPointer<T>& p);
|
2013-12-06 11:46:47 +01:00
|
|
|
SmartPointer<T>& operator= (UniquePointer<T>& p);
|
2013-11-29 09:23:08 +01:00
|
|
|
SmartPointer<T>& operator= (T* p);
|
2013-12-06 11:46:47 +01:00
|
|
|
bool operator== (const SmartPointer<T>& d) const;
|
|
|
|
bool operator== (const T& p) const;
|
|
|
|
bool operator!= (const SmartPointer<T>& d) const;
|
|
|
|
bool operator!= (const T& p) const;
|
2013-11-29 09:23:08 +01:00
|
|
|
T& operator* ();
|
2013-12-06 11:46:47 +01:00
|
|
|
const T& operator* () const;
|
2013-11-29 09:23:08 +01:00
|
|
|
T* operator-> ();
|
2013-12-06 11:46:47 +01:00
|
|
|
const T* operator-> () const;
|
|
|
|
operator T* () const;
|
|
|
|
operator const T* () const;
|
|
|
|
operator T& () const;
|
|
|
|
operator bool() const;
|
2013-12-09 10:48:43 +01:00
|
|
|
|
2013-11-29 09:23:08 +01:00
|
|
|
/**
|
|
|
|
* Returns the connected pointer
|
|
|
|
*/
|
|
|
|
T* Get();
|
2013-12-06 11:46:47 +01:00
|
|
|
T* Get() const;
|
2013-11-29 09:23:08 +01:00
|
|
|
|
2013-12-09 22:55:22 +01:00
|
|
|
/**
|
|
|
|
* Releases one reference of the pointer and set value to null, making the current SmartPointer invalid.
|
|
|
|
*/
|
|
|
|
int Release();
|
2013-12-19 12:32:23 +01:00
|
|
|
/**
|
|
|
|
* Only test to release to check reference count.
|
|
|
|
*/
|
|
|
|
int ReleaseDummy();
|
2013-11-29 09:23:08 +01:00
|
|
|
|
|
|
|
/** Checks if the pointer is valid (not NULL)
|
|
|
|
* Returns true for valid, else false.
|
|
|
|
*/
|
2013-12-06 11:46:47 +01:00
|
|
|
bool IsValid() const;
|
2013-11-22 10:46:25 +01:00
|
|
|
};
|
2013-11-09 23:36:38 +01:00
|
|
|
}
|
|
|
|
|
2013-11-06 22:52:00 +01:00
|
|
|
namespace String
|
|
|
|
{
|
2013-11-09 23:36:38 +01:00
|
|
|
// string
|
|
|
|
|
|
|
|
::std::vector<::std::string> & Split( ::std::vector<::std::string> &output, const ::std::string &str, char delim, ::std::string::size_type offset = 0 );
|
|
|
|
::std::vector<::std::string> & Split( ::std::vector<::std::string> &output, const ::std::string &str, const ::std::string &delim, ::std::string::size_type offset = 0 );
|
|
|
|
::std::vector<::std::string> & Split( ::std::vector<::std::string> &output, const ::std::string &str, const ::std::vector<::std::string> &delim, ::std::string::size_type offset = 0 );
|
|
|
|
::std::string Trim( const ::std::string &str );
|
|
|
|
::std::string & ToLowerCase( ::std::string &output, const ::std::string &str );
|
|
|
|
::std::string & ToLowerCase( ::std::string &str );
|
|
|
|
::std::string & ToUpperCase( ::std::string &output, const ::std::string &str );
|
|
|
|
::std::string & ToUpperCase( ::std::string &str );
|
|
|
|
::std::string & ExtractDirPath( ::std::string &output, const ::std::string &file, char dirDelimeter );
|
|
|
|
::std::string & ExtractDirPath( ::std::string &output, const ::std::string &file, const ::std::string &dirDelimeter );
|
|
|
|
::std::string & ReplaceCharacters( ::std::string &str, char characterToReplace, char newCharacter, const ::std::string::size_type &offset = 0, const ::std::string::size_type &end = ::std::string::npos );
|
|
|
|
|
|
|
|
// wstring
|
|
|
|
|
|
|
|
::std::vector<::std::wstring> & Split( ::std::vector<::std::wstring> &output, const ::std::wstring &str, char delim, ::std::wstring::size_type offset = 0 );
|
|
|
|
::std::vector<::std::wstring> & Split( ::std::vector<::std::wstring> &output, const ::std::wstring &str, const ::std::wstring &delim, ::std::wstring::size_type offset = 0 );
|
|
|
|
::std::vector<::std::wstring> & Split( ::std::vector<::std::wstring> &output, const ::std::wstring &str, const ::std::vector<::std::wstring> &delim, ::std::wstring::size_type offset = 0 );
|
2013-11-22 10:46:25 +01:00
|
|
|
::std::wstring & wToLowerCase( ::std::wstring &output, const ::std::wstring &str );
|
|
|
|
::std::wstring & wToLowerCase( ::std::wstring &str );
|
|
|
|
|
|
|
|
//To wstring
|
|
|
|
|
|
|
|
::std::wstring & StringToWstring( const ::std::string &str, ::std::wstring &wstr );
|
|
|
|
::std::string & WStringToString( const ::std::wstring &wstr, ::std::string &str );
|
2013-11-06 22:52:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace Stream
|
|
|
|
{
|
2013-11-09 23:36:38 +01:00
|
|
|
float* ReadFloats( float *output, ::std::istream &input, unsigned int numFloats );
|
2013-11-06 22:52:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace StaticArray
|
|
|
|
{
|
2013-11-09 23:36:38 +01:00
|
|
|
template<typename ScalarType, unsigned int num>
|
|
|
|
inline unsigned int NumElementsOf( const ScalarType(&)[num] )
|
2013-11-06 22:52:00 +01:00
|
|
|
{ return num; }
|
2013-11-09 23:36:38 +01:00
|
|
|
|
|
|
|
template<typename ScalarType, unsigned int num>
|
|
|
|
inline ScalarType & FirstElementOf( ScalarType (&arr)[num] )
|
|
|
|
{ return arr[0]; }
|
|
|
|
|
|
|
|
template<typename ScalarType, unsigned int num>
|
|
|
|
inline ScalarType & LastElementOf( ScalarType (&arr)[num] )
|
|
|
|
{ return arr[num-1]; }
|
2013-11-06 22:52:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace Element
|
|
|
|
{
|
2013-11-09 23:36:38 +01:00
|
|
|
template<typename ScalarType>
|
|
|
|
inline void Swap( ScalarType &elementA, ScalarType &elementB, ScalarType &swapSpace )
|
2013-11-06 22:52:00 +01:00
|
|
|
{ swapSpace = elementA; elementA = elementB; elementB = swapSpace; }
|
|
|
|
|
2013-11-09 23:36:38 +01:00
|
|
|
template<typename ScalarType>
|
|
|
|
inline void Swap( ScalarType &elementA, ScalarType &elementB )
|
|
|
|
{ ScalarType swapSpace; Swap( elementA, elementB, swapSpace ); }
|
2013-11-06 22:52:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace Value
|
|
|
|
{
|
2013-11-09 23:36:38 +01:00
|
|
|
using ::std::numeric_limits;
|
|
|
|
|
2014-02-03 15:48:42 +01:00
|
|
|
template<typename ValueType>
|
|
|
|
inline bool Within( const ValueType &value, const ValueType &lower, const ValueType &upper )
|
|
|
|
{
|
|
|
|
if( value < lower ) return false;
|
|
|
|
if( value > upper ) return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-11-06 22:52:00 +01:00
|
|
|
template<typename ValueType>
|
2013-11-09 23:36:38 +01:00
|
|
|
inline ValueType Abs( const ValueType &value )
|
2013-11-06 22:52:00 +01:00
|
|
|
{ return value < 0 ? value * -1 : value; }
|
|
|
|
|
|
|
|
template<typename ValueType>
|
2013-11-09 23:36:38 +01:00
|
|
|
inline ValueType Max( const ValueType &valueA, const ValueType &valueB )
|
2013-11-06 22:52:00 +01:00
|
|
|
{ return valueA > valueB ? valueA : valueB; }
|
|
|
|
|
|
|
|
template<typename ValueType>
|
2013-11-09 23:36:38 +01:00
|
|
|
inline ValueType Min( const ValueType &valueA, const ValueType &valueB )
|
2013-11-06 22:52:00 +01:00
|
|
|
{ return valueA < valueB ? valueA : valueB; }
|
|
|
|
|
|
|
|
template<typename ValueType>
|
2014-01-20 18:46:08 +01:00
|
|
|
inline ValueType Clamp( const ValueType &value, const ValueType &min, const ValueType &max )
|
2014-02-19 15:47:02 +01:00
|
|
|
{
|
|
|
|
if( value < min ) return min;
|
|
|
|
if( value > max ) return max;
|
|
|
|
return value;
|
|
|
|
}
|
2014-01-20 18:46:08 +01:00
|
|
|
|
|
|
|
template<typename ValueType>
|
2013-11-09 23:36:38 +01:00
|
|
|
inline ValueType Average( const ValueType &valueA, const ValueType &valueB )
|
|
|
|
{ return (valueA + valueB) * 0.5f; }
|
|
|
|
|
|
|
|
template<typename ValueType>
|
|
|
|
inline ValueType AverageWithDelta( const ValueType &origin, const ValueType &delta )
|
|
|
|
{ return origin + (delta * 0.5f); }
|
|
|
|
|
|
|
|
template<typename ValueType>
|
|
|
|
inline ValueType Radian( const ValueType °ree )
|
2013-11-06 22:52:00 +01:00
|
|
|
{ return degree * (3.1415926535897932384626433832795f / 180.0f); }
|
|
|
|
|
|
|
|
template<typename ValueType>
|
2013-11-09 23:36:38 +01:00
|
|
|
inline ValueType Degree( const ValueType &radian )
|
2013-11-06 22:52:00 +01:00
|
|
|
{ return radian * (180.0f / 3.1415926535897932384626433832795f); }
|
2013-11-09 23:36:38 +01:00
|
|
|
|
2013-11-25 08:57:47 +01:00
|
|
|
// SPECIALIZATIONS //!//!//!//!//!//!//!//!//!//!//!//!//!//!
|
2013-11-09 23:36:38 +01:00
|
|
|
|
|
|
|
template<> inline char Average<char>( const char &valueA, const char &valueB )
|
|
|
|
{ return (valueA + valueB) >> 1; }
|
|
|
|
|
|
|
|
template<> inline unsigned char Average<unsigned char>( const unsigned char &valueA, const unsigned char &valueB )
|
|
|
|
{ return (valueA + valueB) >> 1; }
|
|
|
|
|
|
|
|
template<> inline int Average<int>( const int &valueA, const int &valueB )
|
|
|
|
{ return (valueA + valueB) >> 1; }
|
|
|
|
|
|
|
|
template<> inline unsigned int Average<unsigned int>( const unsigned int &valueA, const unsigned int &valueB )
|
|
|
|
{ return (valueA + valueB) >> 1; }
|
|
|
|
|
|
|
|
template<> inline long Average<long>( const long &valueA, const long &valueB )
|
|
|
|
{ return (valueA + valueB) >> 1; }
|
|
|
|
|
|
|
|
template<> inline unsigned long Average<unsigned long>( const unsigned long &valueA, const unsigned long &valueB )
|
|
|
|
{ return (valueA + valueB) >> 1; }
|
|
|
|
|
|
|
|
template<> inline long long Average<long long>( const long long &valueA, const long long &valueB )
|
|
|
|
{ return (valueA + valueB) >> 1; }
|
|
|
|
|
|
|
|
template<> inline unsigned long long Average<unsigned long long>( const unsigned long long &valueA, const unsigned long long &valueB )
|
|
|
|
{ return (valueA + valueB) >> 1; }
|
|
|
|
|
|
|
|
template<> inline char AverageWithDelta<char>( const char &origin, const char &delta )
|
|
|
|
{ return origin + (delta >> 1); }
|
|
|
|
|
|
|
|
template<> inline unsigned char AverageWithDelta<unsigned char>( const unsigned char &origin, const unsigned char &delta )
|
|
|
|
{ return origin + (delta >> 1); }
|
|
|
|
|
|
|
|
template<> inline int AverageWithDelta<int>( const int &origin, const int &delta )
|
|
|
|
{ return origin + (delta >> 1); }
|
|
|
|
|
|
|
|
template<> inline unsigned int AverageWithDelta<unsigned int>( const unsigned int &origin, const unsigned int &delta )
|
|
|
|
{ return origin + (delta >> 1); }
|
|
|
|
|
|
|
|
template<> inline long AverageWithDelta<long>( const long &origin, const long &delta )
|
|
|
|
{ return origin + (delta >> 1); }
|
|
|
|
|
|
|
|
template<> inline unsigned long AverageWithDelta<unsigned long>( const unsigned long &origin, const unsigned long &delta )
|
|
|
|
{ return origin + (delta >> 1); }
|
|
|
|
|
|
|
|
template<> inline long long AverageWithDelta<long long>( const long long &origin, const long long &delta )
|
|
|
|
{ return origin + (delta >> 1); }
|
|
|
|
|
|
|
|
template<> inline unsigned long long AverageWithDelta<unsigned long long>( const unsigned long long &origin, const unsigned long long &delta )
|
|
|
|
{ return origin + (delta >> 1); }
|
2013-11-06 22:52:00 +01:00
|
|
|
}
|
2013-11-29 09:23:08 +01:00
|
|
|
|
|
|
|
namespace Thread
|
|
|
|
{
|
2014-02-04 16:07:10 +01:00
|
|
|
using namespace DynamicMemory;
|
|
|
|
//! Wrapper to manage references on a pointer.
|
|
|
|
template<typename T> struct ThreadSafeSmartPointer
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
std::atomic<ReferenceCount*> _rc;
|
|
|
|
std::atomic<T*> _ptr;
|
|
|
|
|
|
|
|
/** Destroys the pointer and returns the memory allocated. */
|
|
|
|
void Destroy();
|
|
|
|
|
|
|
|
public:
|
|
|
|
ThreadSafeSmartPointer();
|
|
|
|
ThreadSafeSmartPointer(UniquePointer<T>& up);
|
|
|
|
ThreadSafeSmartPointer(T* p);
|
|
|
|
ThreadSafeSmartPointer(const ThreadSafeSmartPointer& d);
|
|
|
|
virtual~ThreadSafeSmartPointer();
|
|
|
|
ThreadSafeSmartPointer<T>& operator= (const ThreadSafeSmartPointer<T>& p);
|
|
|
|
ThreadSafeSmartPointer<T>& operator= (UniquePointer<T>& p);
|
|
|
|
ThreadSafeSmartPointer<T>& operator= (T* p);
|
|
|
|
bool operator== (const ThreadSafeSmartPointer<T>& d) const;
|
|
|
|
bool operator== (const T& p) const;
|
|
|
|
bool operator!= (const ThreadSafeSmartPointer<T>& d) const;
|
|
|
|
bool operator!= (const T& p) const;
|
|
|
|
T& operator* ();
|
|
|
|
const T& operator* () const;
|
|
|
|
T* operator-> ();
|
|
|
|
const T* operator-> () const;
|
|
|
|
operator T* () const;
|
|
|
|
operator const T* () const;
|
|
|
|
operator T& () const;
|
|
|
|
operator bool() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the connected pointer
|
|
|
|
*/
|
|
|
|
T* Get();
|
|
|
|
T* Get() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Releases one reference of the pointer and set value to null, making the current ThreadSafeSmartPointer invalid.
|
|
|
|
*/
|
|
|
|
int Release();
|
|
|
|
/**
|
|
|
|
* Only test to release to check reference count.
|
|
|
|
*/
|
|
|
|
int ReleaseDummy();
|
|
|
|
|
|
|
|
/** Checks if the pointer is valid (not NULL)
|
|
|
|
* Returns true for valid, else false.
|
|
|
|
*/
|
|
|
|
bool IsValid() const;
|
|
|
|
};
|
2013-11-29 09:23:08 +01:00
|
|
|
}
|
2013-11-06 22:52:00 +01:00
|
|
|
}
|
|
|
|
|
2013-11-28 15:45:09 +01:00
|
|
|
#include "Utilities-Impl.h"
|
2013-11-13 13:56:25 +01:00
|
|
|
|
2013-11-06 22:52:00 +01:00
|
|
|
#endif
|