A
asianmuscle
I am trying to learn RAII and some template techniques by writer a
smarter pointer class to manage the resource management. Since I find
that a lot of the resource management is kinda the same, I try to mimic
some other examples outther then tailor to my needs. My case is for
windows handle like hkey, dll library, file handle, etc. But the code
largely uses standard C++. Only the Clean up action, I put windows api
call in there.
In general, I want to archieve one thing: the template will instantiate
when I give it the inpout type, then call the CloseResource eventually
to clean up on exit of scope.
But I have a hard time to make the class to find out what the
NULL_VALUE is. NULL_VALUE is a value that depends on the type. or is
this a worthwhile effort to get rid of that dependency?I defaulted
evertyhing to NULL now but not all type will default to NULL, if I take
extendsibility into account.
Also I notice I end up with a lot of small policy classes. Is there a
way to reduce that number? Because all it depends is the windows API
call. I think I am asking somethign circular here. Thanks. See below
for code:
For the release policy code:
#include <windows.h>
template <typename hType>
struct RegistryPolicy{
void CloseResource(hType& h){
::RegCloseKey(h);
h=NULL; //?
};
protected: // do I need these or delete those? they are compiler
generated anyway
RegistryPolicy(){};
~RegistryPolicy(){};
private:
RegistryPolicy(RegistryPolicy&);
RegistryPolicy& operator=(RegistryPolicy&);
};
template <typename hType>
struct LibraryPolicy{
void CloseResource(hType& h){
::FreeLibrary(h);
h=NULL; //?
};
protected:
LibraryPolicy(){};
~LibraryPolicy(){};
private:
explicit LibraryPolicy(LibraryPolicy&);
LibraryPolicy& operator=(LibraryPolicy&);
};
template <typename hType>
struct CloseViewOfFile{
void CloseResource(hType& h){
::UnmapViewOfFile(h);
h=NULL; //?
};
protected:
explicit CloseViewOfFile(){};
~CloseViewOfFile(){};
private:
CloseViewOfFile(CloseViewOfFile&);
CloseViewOfFile& operator=(CloseViewOfFile&);
};
///////////////////////////// my smarter pointer code in below
//////////////////////////////
template <class T>
void DestroyObject(T object) {object->Destroy();}
template <class T>
void DesposeObject(T object) {object->CloseResource();}
//
//
template <typename hType,
template<typename> class ReleasePolicy=DeposeObject,
hType NULL_VALUE=NULL> //how to generalize NULL_VALUE? make it
get the value depending on the type?
class SmarterHolder{
private:
hType mhandle;
void CleanUp(){
if (NULL_VALUE!= mhandle)
{
ReleasePolicy(mhandle);
mhandle = NULL_VALUE;
}
}
hType operator=(hType);
SmarterHolder(SmarterHolder&);
public:
explicit SmarterHolder():mhandle(NULL_VALUE){};
explicit SmarterHolder(hType h): mhandle(h){};
~SmarterHolder(){ ReleasePolicy(); };
operator hType () const { return mhandle; }
bool IsValidHandle() const { return NULL_VALUE != mhandle; }
};
#endif
smarter pointer class to manage the resource management. Since I find
that a lot of the resource management is kinda the same, I try to mimic
some other examples outther then tailor to my needs. My case is for
windows handle like hkey, dll library, file handle, etc. But the code
largely uses standard C++. Only the Clean up action, I put windows api
call in there.
In general, I want to archieve one thing: the template will instantiate
when I give it the inpout type, then call the CloseResource eventually
to clean up on exit of scope.
But I have a hard time to make the class to find out what the
NULL_VALUE is. NULL_VALUE is a value that depends on the type. or is
this a worthwhile effort to get rid of that dependency?I defaulted
evertyhing to NULL now but not all type will default to NULL, if I take
extendsibility into account.
Also I notice I end up with a lot of small policy classes. Is there a
way to reduce that number? Because all it depends is the windows API
call. I think I am asking somethign circular here. Thanks. See below
for code:
For the release policy code:
#include <windows.h>
template <typename hType>
struct RegistryPolicy{
void CloseResource(hType& h){
::RegCloseKey(h);
h=NULL; //?
};
protected: // do I need these or delete those? they are compiler
generated anyway
RegistryPolicy(){};
~RegistryPolicy(){};
private:
RegistryPolicy(RegistryPolicy&);
RegistryPolicy& operator=(RegistryPolicy&);
};
template <typename hType>
struct LibraryPolicy{
void CloseResource(hType& h){
::FreeLibrary(h);
h=NULL; //?
};
protected:
LibraryPolicy(){};
~LibraryPolicy(){};
private:
explicit LibraryPolicy(LibraryPolicy&);
LibraryPolicy& operator=(LibraryPolicy&);
};
template <typename hType>
struct CloseViewOfFile{
void CloseResource(hType& h){
::UnmapViewOfFile(h);
h=NULL; //?
};
protected:
explicit CloseViewOfFile(){};
~CloseViewOfFile(){};
private:
CloseViewOfFile(CloseViewOfFile&);
CloseViewOfFile& operator=(CloseViewOfFile&);
};
///////////////////////////// my smarter pointer code in below
//////////////////////////////
template <class T>
void DestroyObject(T object) {object->Destroy();}
template <class T>
void DesposeObject(T object) {object->CloseResource();}
//
//
template <typename hType,
template<typename> class ReleasePolicy=DeposeObject,
hType NULL_VALUE=NULL> //how to generalize NULL_VALUE? make it
get the value depending on the type?
class SmarterHolder{
private:
hType mhandle;
void CleanUp(){
if (NULL_VALUE!= mhandle)
{
ReleasePolicy(mhandle);
mhandle = NULL_VALUE;
}
}
hType operator=(hType);
SmarterHolder(SmarterHolder&);
public:
explicit SmarterHolder():mhandle(NULL_VALUE){};
explicit SmarterHolder(hType h): mhandle(h){};
~SmarterHolder(){ ReleasePolicy(); };
operator hType () const { return mhandle; }
bool IsValidHandle() const { return NULL_VALUE != mhandle; }
};
#endif