Misc - Added more fetures to Smart pointers
This commit is contained in:
parent
f0b766e37c
commit
597891acfe
|
@ -1,73 +0,0 @@
|
|||
/////////////////////////////////////////////////////////////////////
|
||||
// Created by [Dennis Andersen] [2013]
|
||||
/////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "..\OResource.h"
|
||||
#include "..\..\Utilities.h"
|
||||
|
||||
#include <fstream>
|
||||
|
||||
using namespace Oyster::Resource;
|
||||
|
||||
|
||||
OResource* OResource::CustomLoader(const wchar_t filename[], CustomLoadFunction fnc)
|
||||
{
|
||||
<<<<<<< HEAD
|
||||
CustomData &data = fnc(filename);
|
||||
|
||||
if(!data.loadedData) return 0;
|
||||
if(!data.resourceUnloadFnc) return 0;
|
||||
OHRESOURCE n = (OHRESOURCE)data.loadedData;
|
||||
OResource *resource = new OResource(n, ResourceType_UNKNOWN, 0, 0, filename);
|
||||
|
||||
resource->customData = new CustomResourceData();
|
||||
resource->customData->unloadingFunction = data.resourceUnloadFnc;
|
||||
//resource->resourceData = (OHRESOURCE)data.loadedData;
|
||||
=======
|
||||
CustomData data;
|
||||
memset(&data, 0, sizeof(CustomData));
|
||||
|
||||
fnc(filename, data);
|
||||
|
||||
if(!data.loadedData)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if(!data.resourceUnloadFnc)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
/** For some wierd reason that i don't understand when trying to send data.loadedData directly as a
|
||||
* parameter to OResource constructor, the value is changed when it arrives in the constructor.
|
||||
* Doing it like this, storing in a temporary variable, the value stays correct. (What the fuck! I must be overloking something...)*/
|
||||
//OHRESOURCE temp = data.loadedData;
|
||||
OResource *resource = new OResource(data.loadedData, ResourceType_UNKNOWN, 0, 0, filename);
|
||||
|
||||
resource->customData = new CustomResourceData();
|
||||
resource->customData->unloadingFunction = data.resourceUnloadFnc;
|
||||
>>>>>>> d08644e8e1ecc56f4d9dfa6a9aa33df94d9e655a
|
||||
resource->customData->loadingFunction = fnc;
|
||||
|
||||
return resource;
|
||||
}
|
||||
void OResource::CustomUnloader()
|
||||
{
|
||||
this->customData->unloadingFunction(this->resourceData);
|
||||
}
|
||||
OResource* OResource::CustomReloader()
|
||||
{
|
||||
CustomUnloader();
|
||||
|
||||
CustomData data;
|
||||
memset(&data, 0, sizeof(CustomData));
|
||||
|
||||
this->customData->loadingFunction(this->resourceFilename.c_str(), data);
|
||||
this->resourceData = (OHRESOURCE)data.loadedData;
|
||||
|
||||
if(data.resourceUnloadFnc)
|
||||
{
|
||||
this->customData->unloadingFunction = data.resourceUnloadFnc;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
|
@ -1,162 +0,0 @@
|
|||
/////////////////////////////////////////////////////////////////////
|
||||
// Created by [Dennis Andersen] [2013]
|
||||
/////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef MISC_OYSTER_RESOURCE_H
|
||||
#define MISC_OYSTER_RESOURCE_H
|
||||
|
||||
|
||||
namespace Oyster
|
||||
{
|
||||
namespace Resource
|
||||
{
|
||||
struct CustomData;
|
||||
/** A Resource handle representing various resources */
|
||||
typedef void* OHRESOURCE;
|
||||
/** Typedef on a fuction required for custom unloading */
|
||||
typedef void(*CustomUnloadFunction)(void* loadedData);
|
||||
/** Typedef on a fuction required for custom loading */
|
||||
<<<<<<< HEAD
|
||||
typedef CustomData&(*CustomLoadFunction)(const wchar_t filename[]);
|
||||
=======
|
||||
typedef void(*CustomLoadFunction)(const wchar_t filename[], CustomData& outData);
|
||||
>>>>>>> d08644e8e1ecc56f4d9dfa6a9aa33df94d9e655a
|
||||
|
||||
/** An enum class representing all avalible resources that is supported. */
|
||||
enum ResourceType
|
||||
{
|
||||
//Byte
|
||||
ResourceType_Byte_Raw, /**< Handle can be interpeted as char[] or char* */
|
||||
ResourceType_Byte_ANSI, /**< Handle can be interpeted as char[] or char* */
|
||||
ResourceType_Byte_UTF8, /**< Handle can be interpeted as char[] or char* */
|
||||
ResourceType_Byte_UNICODE, /**< Handle can be interpeted as char[] or char* */
|
||||
ResourceType_Byte_UTF16LE, /**< Handle can be interpeted as char[] or char* */
|
||||
|
||||
ResourceType_COUNT, /**< Not used. */
|
||||
|
||||
ResourceType_UNKNOWN = -1, /**< Handle can be interpeted as void* */
|
||||
ResourceType_INVALID = -2, /**< Invalid or non existing resource */
|
||||
};
|
||||
|
||||
/** A struct to fill when doing a custom resource Load. */
|
||||
struct CustomData
|
||||
{
|
||||
void* loadedData; //<! The loaded resource interpeted as a void*.
|
||||
CustomUnloadFunction resourceUnloadFnc; //<! The function that will be used to free the resource when needed.
|
||||
};
|
||||
|
||||
/** A resource handler interface to interact with when loading resources.
|
||||
* The resource handler uses the filename to make resources unuiqe.
|
||||
*/
|
||||
class OysterResource
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Load a resource given a type.
|
||||
* @param filename The path to the resource.
|
||||
* @param type The resource type to load.
|
||||
* @param force If set to true, the resource will be reloaded if it already exists. If it does not, nothing happens.
|
||||
* @return If function suceeds, a handle to the resource will be returned. If failed 0 is returned.
|
||||
*/
|
||||
static OHRESOURCE LoadResource(const wchar_t filename[], ResourceType type, int customId = -1, bool force = false);
|
||||
|
||||
/**
|
||||
* Load a resource with a custom loading function
|
||||
* @param filename The path to the resource.
|
||||
* @param loadFnc If set, this gives you the right to do custom resource loading if your recource type is not supported.
|
||||
* @param customId A custom ID that can be used.
|
||||
* @param force If set to true, the resource will be reloaded even if exists.
|
||||
* @return If function suceeds, a handle to the resource will be returned. If failed 0 is returned.
|
||||
*/
|
||||
static OHRESOURCE LoadResource(const wchar_t filename[], CustomLoadFunction loadFnc = 0, int customId = -1, bool force = false);
|
||||
|
||||
/**
|
||||
* Reload a resource
|
||||
* @param filename The path to the resource.
|
||||
* @return If function suceeds, a handle to the resource will be returned. If failed 0 is returned.
|
||||
*/
|
||||
static OHRESOURCE ReloadResource(const wchar_t filename[]);
|
||||
|
||||
/**
|
||||
* Reload a resource
|
||||
* @param filename The path to the resource.
|
||||
* @return If function suceeds, a handle to the resource will be returned. If failed 0 is returned.
|
||||
*/
|
||||
static OHRESOURCE ReloadResource(OHRESOURCE resource);
|
||||
|
||||
/**
|
||||
* Releases all resources loaded by the resource handler.
|
||||
* @return Nothing
|
||||
*/
|
||||
static void Clean();
|
||||
|
||||
/**
|
||||
* Release a reference to the resource handle
|
||||
* @param resource The handle to release.
|
||||
* @return Nothing
|
||||
*/
|
||||
static void ReleaseResource(const OHRESOURCE& resource);
|
||||
|
||||
/**
|
||||
* Release a reference to the resource handle
|
||||
* @param resource The resource filename to release reference.
|
||||
* @return Nothing
|
||||
*/
|
||||
static void ReleaseResource(const wchar_t filename[]);
|
||||
|
||||
/** Set a user defined ID
|
||||
* @param resource A handle to accociate the id with.
|
||||
* @param id A user defined identifier that the resource handler does not touch.
|
||||
*/
|
||||
static void SetResourceId(const OHRESOURCE& resource, unsigned int id);
|
||||
|
||||
/** Set a user defined ID
|
||||
* If the resource is not loaded the id will not be set.
|
||||
* @param resource A filename to accociate the id with.
|
||||
* @param id A user defined identifier that the resource handler does not touch.
|
||||
*/
|
||||
static void SetResourceId(const wchar_t filename[], unsigned int id);
|
||||
|
||||
/** Get a resource type given a OHRESOURCE handle
|
||||
* @param resource The handle to check
|
||||
* @return Returns the resource type of the handle
|
||||
*/
|
||||
static ResourceType GetResourceType(const OHRESOURCE& resource);
|
||||
|
||||
/** Get a resource type given a filename
|
||||
* If the resource is not loaded the id will not be set.
|
||||
* @param resource The filename to check
|
||||
* @return Returns the resource type of the handle
|
||||
*/
|
||||
static ResourceType GetResourceType (const wchar_t filename[]);
|
||||
|
||||
/** Get a resource filename given a OHRESOURCE handle
|
||||
* @param resource The handle to check
|
||||
* @return Returns the accociated filename
|
||||
*/
|
||||
static const wchar_t* GetResourceFilename(const OHRESOURCE& resource);
|
||||
|
||||
/** Get a resource handle given a filename
|
||||
* If the resource is not loaded function returns 0.
|
||||
* @param resource The filename to check
|
||||
* @return Returns the accociated handle
|
||||
*/
|
||||
static OHRESOURCE GetResourceHandle(const wchar_t filename[]);
|
||||
|
||||
/** Get a user defined ID accociated with a handle
|
||||
* @param resource The handle to check
|
||||
* @return Returns the accociated ID
|
||||
*/
|
||||
static int GetResourceId(const OHRESOURCE& resource);
|
||||
|
||||
/** Get a user defined ID accociated with a filename
|
||||
* @param resource The filename to check
|
||||
* @return Returns the accociated ID
|
||||
*/
|
||||
static int GetResourceId(const wchar_t filename[]);
|
||||
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
|
@ -208,6 +208,12 @@ namespace Utility
|
|||
template<typename T> SmartPointer<T>::SmartPointer()
|
||||
:_rc(0), _ptr(0)
|
||||
{ }
|
||||
template<typename T> SmartPointer<T>::SmartPointer(UniquePointer<T>& p)
|
||||
:_ptr(p.Release())
|
||||
{
|
||||
this->_rc = new ReferenceCount();
|
||||
this->_rc->Incref();
|
||||
}
|
||||
template<typename T> SmartPointer<T>::SmartPointer(T* p)
|
||||
:_ptr(p)
|
||||
{
|
||||
|
@ -244,6 +250,30 @@ namespace Utility
|
|||
}
|
||||
return *this;
|
||||
}
|
||||
template<typename T> SmartPointer<T>& SmartPointer<T>::operator= (UniquePointer<T>& p)
|
||||
{
|
||||
//Last to go?
|
||||
if(this->_rc)
|
||||
{
|
||||
if(this->_rc->Decref() == 0)
|
||||
{
|
||||
//Call child specific
|
||||
Destroy();
|
||||
this->_rc = new ReferenceCount();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if(p) this->_rc = new ReferenceCount();
|
||||
}
|
||||
|
||||
if(this->_rc)
|
||||
this->_rc->Incref();
|
||||
|
||||
this->_ptr = p.Release();
|
||||
|
||||
return *this;
|
||||
}
|
||||
template<typename T> SmartPointer<T>& SmartPointer<T>::operator= (T* p)
|
||||
{
|
||||
if (this->_ptr != p)
|
||||
|
@ -266,19 +296,19 @@ namespace Utility
|
|||
}
|
||||
return *this;
|
||||
}
|
||||
template<typename T> inline bool SmartPointer<T>::operator== (const SmartPointer<T>& d)
|
||||
template<typename T> inline bool SmartPointer<T>::operator== (const SmartPointer<T>& d) const
|
||||
{
|
||||
return d._ptr == this->_ptr;
|
||||
}
|
||||
template<typename T> inline bool SmartPointer<T>::operator== (const T& p)
|
||||
template<typename T> inline bool SmartPointer<T>::operator== (const T& p) const
|
||||
{
|
||||
return &p == this->_ptr;
|
||||
}
|
||||
template<typename T> inline bool SmartPointer<T>::operator!= (const SmartPointer<T>& d)
|
||||
template<typename T> inline bool SmartPointer<T>::operator!= (const SmartPointer<T>& d) const
|
||||
{
|
||||
return d._ptr != this->_ptr;
|
||||
}
|
||||
template<typename T> inline bool SmartPointer<T>::operator!= (const T& p)
|
||||
template<typename T> inline bool SmartPointer<T>::operator!= (const T& p) const
|
||||
{
|
||||
return &p != this->_ptr;
|
||||
}
|
||||
|
@ -286,15 +316,31 @@ namespace Utility
|
|||
{
|
||||
return *this->_ptr;
|
||||
}
|
||||
template<typename T> inline const T& SmartPointer<T>::operator* () const
|
||||
{
|
||||
return *this->_ptr;
|
||||
}
|
||||
template<typename T> inline T* SmartPointer<T>::operator-> ()
|
||||
{
|
||||
return this->_ptr;
|
||||
}
|
||||
template<typename T> inline SmartPointer<T>::operator T* ()
|
||||
template<typename T> inline const T* SmartPointer<T>::operator-> () const
|
||||
{
|
||||
return this->_ptr;
|
||||
}
|
||||
template<typename T> inline SmartPointer<T>::operator bool()
|
||||
template<typename T> inline SmartPointer<T>::operator T* () const
|
||||
{
|
||||
return this->_ptr;
|
||||
}
|
||||
template<typename T> inline SmartPointer<T>::operator const T* () const
|
||||
{
|
||||
return this->_ptr;
|
||||
}
|
||||
template<typename T> inline SmartPointer<T>::operator T& () const
|
||||
{
|
||||
return *this->_ptr;
|
||||
}
|
||||
template<typename T> inline SmartPointer<T>::operator bool() const
|
||||
{
|
||||
return (this->_ptr != 0);
|
||||
}
|
||||
|
@ -302,7 +348,7 @@ namespace Utility
|
|||
{
|
||||
return this->_ptr;
|
||||
}
|
||||
template<typename T> inline bool SmartPointer<T>::IsValid()
|
||||
template<typename T> inline bool SmartPointer<T>::IsValid() const
|
||||
{
|
||||
return (this->_ptr != NULL) ? true : false;
|
||||
}
|
||||
|
|
|
@ -205,29 +205,36 @@ namespace Utility
|
|||
|
||||
public:
|
||||
SmartPointer();
|
||||
SmartPointer(UniquePointer<T>& up);
|
||||
SmartPointer(T* p);
|
||||
SmartPointer(const SmartPointer& d);
|
||||
virtual~SmartPointer();
|
||||
SmartPointer<T>& operator= (const SmartPointer<T>& p);
|
||||
SmartPointer<T>& operator= (UniquePointer<T>& p);
|
||||
SmartPointer<T>& operator= (T* p);
|
||||
bool operator== (const SmartPointer<T>& d);
|
||||
bool operator== (const T& p);
|
||||
bool operator!= (const SmartPointer<T>& d);
|
||||
bool operator!= (const T& p);
|
||||
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;
|
||||
T& operator* ();
|
||||
const T& operator* () const;
|
||||
T* operator-> ();
|
||||
operator T* ();
|
||||
operator bool();
|
||||
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;
|
||||
|
||||
/** Checks if the pointer is valid (not NULL)
|
||||
* Returns true for valid, else false.
|
||||
*/
|
||||
bool IsValid();
|
||||
bool IsValid() const;
|
||||
};
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue