473,395 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

getting rid of dependency of default values

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<typenameclass 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

Sep 26 '06 #1
5 1991
as*********@gmail.com wrote:
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.
See below.
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.
Some existing implementations of smart pointers (e.g.,
std::tr1::shared_ptr aka boost::shared_ptr) accept a deleter argument.
That would suffice in a case such as this:

struct SomeHandle { /*...*/ };
SomeHandle* GetResource();
void FreeResource( SomeHandle* );

std::tr1::shared_ptr<SomeHandlehandle(
GetResource(), FreeResource );

You could also use boost::bind or boost::lambda to reduce the number of
simple policies (probably not a good idea if they are used more than
once).
For the release policy code:
#include <windows.h>

template <typename hType>
struct RegistryPolicy{
void CloseResource(hType& h){
::RegCloseKey(h);
h=NULL; //?
Setting to NULL is probably unnecessary if you're using smart pointers
since the smart pointer is entirely responsible for the resource and
will only call on the release policy when the item is being destroyed
(either on destruction or reset).
};
Semicolons are not necessary after function definitions. You should
also make this function static since there is no member data in play
here.
>
protected: // do I need these or delete those? they are compiler
generated anyway
RegistryPolicy(){};
~RegistryPolicy(){};
They would be generated public by default. You must define them
yourself to override that.
private:
RegistryPolicy(RegistryPolicy&);
RegistryPolicy& operator=(RegistryPolicy&);
The function parameter should be const in both cases.
};

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();}
Dispose?
>
//
//

template <typename hType,
template<typenameclass ReleasePolicy=DeposeObject,
DisposeObject?
hType NULL_VALUE=NULL>//how to generalize NULL_VALUE? makeit
get the value depending on the type?
Use the default value of hType:

template
<
typename hType,
template<typenameclass ReleasePolicy=DisposeObject,
hType NULL_VALUE=hType()
>
class SmartHolder;
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
I'd suggest that, rather than rolling your own, you should switch to
using a tried-and-true RAII mechanism like std::tr1::/boost::shared_ptr
(where you would supply a custom deleter) or even Loki::SmartPtr (where
you would supply a storage policy). No need to reinvent the wheel.

Cheers! --M

Sep 26 '06 #2
Hi, thanks for your feedback. I learnt quite something by reading your
reply. The reason of making this is an exercise for me to learn more on
using template. My C++ is kinda shaky after 4 yrs of idle.
In reply to DisposeObject below. What I intended as to let
SmarterHandle to call the the "Policy template class" such that, the
call will dispatch to the appropriate CleanResource. The idea was to
let the function template DisposeObject right above smarterhandle, then
which in turn defer to the policy class. I note that all the example I
see on theweb have SmarterHandle to inherit from ReleasePolicy to get
access to
CloseResource. But I just don't get why it has to inherit. so instead I
just try to do a usage here. But I can see there is something not going
right with my current arrangement. Can you help me a bit on that. I
don't think my C++ skills is good enough to concretly pinpoint that.
Thanks
mlimber wrote:
as*********@gmail.com wrote:
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.

See below.
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.

Some existing implementations of smart pointers (e.g.,
std::tr1::shared_ptr aka boost::shared_ptr) accept a deleter argument.
That would suffice in a case such as this:

struct SomeHandle { /*...*/ };
SomeHandle* GetResource();
void FreeResource( SomeHandle* );

std::tr1::shared_ptr<SomeHandlehandle(
GetResource(), FreeResource );

You could also use boost::bind or boost::lambda to reduce the number of
simple policies (probably not a good idea if they are used more than
once).
For the release policy code:
#include <windows.h>

template <typename hType>
struct RegistryPolicy{
void CloseResource(hType& h){
::RegCloseKey(h);
h=NULL; //?

Setting to NULL is probably unnecessary if you're using smart pointers
since the smart pointer is entirely responsible for the resource and
will only call on the release policy when the item is being destroyed
(either on destruction or reset).
};

Semicolons are not necessary after function definitions. You should
also make this function static since there is no member data in play
here.

protected: // do I need these or delete those? they are compiler
generated anyway
RegistryPolicy(){};
~RegistryPolicy(){};

They would be generated public by default. You must define them
yourself to override that.
private:
RegistryPolicy(RegistryPolicy&);
RegistryPolicy& operator=(RegistryPolicy&);

The function parameter should be const in both cases.
};

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();}

Dispose?

//
//

template <typename hType,
template<typenameclass ReleasePolicy=DeposeObject,

DisposeObject?
hType NULL_VALUE=NULL>//how to generalize NULL_VALUE? makeit
get the value depending on the type?

Use the default value of hType:

template
<
typename hType,
template<typenameclass ReleasePolicy=DisposeObject,
hType NULL_VALUE=hType()
>
class SmartHolder;
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

I'd suggest that, rather than rolling your own, you should switch to
using a tried-and-true RAII mechanism like std::tr1::/boost::shared_ptr
(where you would supply a custom deleter) or even Loki::SmartPtr (where
you would supply a storage policy). No need to reinvent the wheel.

Cheers! --M
Sep 26 '06 #3

as*********@gmail.com wrote:
I am trying to learn RAII and some template techniques by writer a
I don't fully understand why you need templates here.
I usually write one class for every way I can allocate a system
resource.
E.g. LoadLibrary belongs in the constructor and FreeLibrary into the
destructor.
E.g. RegCreateKey belongs into the constructor and RegCloseKey() into
the destructor.
If allocation fails throw an an exception containing the full error
information -- e.g. GetLastError()
-- what() should then return the system error message -- using
FormatMessage() I think.

The same applies for other operating systems.

Sep 26 '06 #4

as*********@gmail.com wrote:
I am trying to learn RAII and some template techniques by writer a
I don't fully understand why you need templates here.
I usually write one class for every way I can allocate a system
resource.
E.g. LoadLibrary belongs in the constructor and FreeLibrary into the
destructor.
E.g. RegCreateKey belongs into the constructor and RegCloseKey() into
the destructor.
If allocation fails throw an an exception containing the full error
information -- e.g. GetLastError()
-- what() should then return the system error message -- using
FormatMessage() I think.

The same applies for other operating systems.

Sep 26 '06 #5
as*********@gmail.com wrote:
mlimber wrote:
as*********@gmail.com wrote:
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.
See below.
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.
Some existing implementations of smart pointers (e.g.,
std::tr1::shared_ptr aka boost::shared_ptr) accept a deleter argument.
That would suffice in a case such as this:

struct SomeHandle { /*...*/ };
SomeHandle* GetResource();
void FreeResource( SomeHandle* );

std::tr1::shared_ptr<SomeHandlehandle(
GetResource(), FreeResource );

You could also use boost::bind or boost::lambda to reduce the number of
simple policies (probably not a good idea if they are used more than
once).
For the release policy code:
#include <windows.h>
>
template <typename hType>
struct RegistryPolicy{
void CloseResource(hType& h){
::RegCloseKey(h);
h=NULL; //?
Setting to NULL is probably unnecessary if you're using smart pointers
since the smart pointer is entirely responsible for the resource and
will only call on the release policy when the item is being destroyed
(either on destruction or reset).
};
Semicolons are not necessary after function definitions. You should
also make this function static since there is no member data in play
here.
>
protected: // do I need these or delete those? they are compiler
generated anyway
RegistryPolicy(){};
~RegistryPolicy(){};
They would be generated public by default. You must define them
yourself to override that.
private:
RegistryPolicy(RegistryPolicy&);
RegistryPolicy& operator=(RegistryPolicy&);
The function parameter should be const in both cases.
};
>
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();}
Dispose?
>
//
//
>
template <typename hType,
template<typenameclass ReleasePolicy=DeposeObject,
DisposeObject?
hType NULL_VALUE=NULL>//how to generalize NULL_VALUE? makeit
get the value depending on the type?
Use the default value of hType:

template
<
typename hType,
template<typenameclass ReleasePolicy=DisposeObject,
hType NULL_VALUE=hType()
>
class SmartHolder;
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
I'd suggest that, rather than rolling your own, you should switch to
using a tried-and-true RAII mechanism like std::tr1::/boost::shared_ptr
(where you would supply a custom deleter) or even Loki::SmartPtr (where
you would supply a storage policy). No need to reinvent the wheel.

Cheers! --M
First, please don't top-post here (see
http://parashift.com/c++-faq-lite/ho....html#faq-5.4). I fixed it
for you in this reply.
Hi, thanks for your feedback. I learnt quite something by reading your
reply. The reason of making this is an exercise for me to learn more on
using template. My C++ is kinda shaky after 4 yrs of idle.
You should pick up _Accelerated C++_ by Koenig and Moo. It's great for
re-learning C++ the right way. Also, you might be interested in _Modern
C++ Design_ by Alexandrescu. You can see his chapter on smart pointers
here for free:

http://www.informit.com/articles/pri...p?p=25264&rl=1
In reply to DisposeObject below. What I intended as to let
SmarterHandle to call the the "Policy template class" such that, the
call will dispatch to the appropriate CleanResource. The idea was to
let the function template DisposeObject right above smarterhandle, then
which in turn defer to the policy class. I note that all the example I
see on theweb have SmarterHandle to inherit from ReleasePolicy to get
access to
CloseResource. But I just don't get why it has to inherit. so instead I
just try to do a usage here. But I can see there is something not going
right with my current arrangement. Can you help me a bit on that. I
don't think my C++ skills is good enough to concretly pinpoint that.
Inheritance is sometimes required if you want to build an interface
from policies. For instance:

struct A
{
static void f1() { /*...*/ }
void f2() { /*...*/ }
};

struct B
{
static void f3() { /*...*/ }
};

template<class Policy1, class Policy2>
struct C : Policy1
{
C()
{
f1();
f2();
Policy2::f3();
}
};

typedef C<A,BD;

void Foo( D& d )
{
D::f1(); // Ok
d.f2(); // Ok
D::f3(); // Error!
}

Notice that while D has no member functions of its own (other than the
constructor), it inherits some from its Policy1 template parameter and
it can internally use those of its Policy2 template parameter. In
general, you should not inherit unless you need to (cf.
http://www.parashift.com/c++-faq-lit...html#faq-24.3).

Cheers! --M

Sep 27 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jason | last post by:
I have a class that I've written that inherits DataGrid. Inside the constructor I set several properties such as AllowPaging and AllowSorting to true, which is what I want the default values to be....
1
by: Qin Chen | last post by:
I will present very long code, hope someone will read it all, and teach me something like tom_usenet. This question comes to me when i read <<Think in C++>> 2nd, chapter 10 , name control,...
1
by: Rimma Meital via .NET 247 | last post by:
(Type your message here) -------------------------------- From: Rimma Meital I have 2 DLLs (Class Libraries). Both defines single-ton objects - logger object (writes to logger) and...
5
by: Chris | last post by:
I have a project that I have been messing with for a while. I get a message in my task list that says: "The dependency 'ICSharpCode.SharpZipLib' could not be found." I added this library in a...
1
by: martin | last post by:
Hi, I have a page that contain a dropdown list of values. This drop down list rarely changes so I wish to cache the page. However the values in the dropdown box are taken from a database, so if...
4
by: Sean Shanny | last post by:
To all, Running into an out of memory error on our data warehouse server. This occurs only with our data from the 'September' section of a large fact table. The exact same query running over...
9
by: Good Man | last post by:
Hi This is sort of a weird question, perhaps a bit off-topic... I am on the 'edit' screen of a web form, and I have a bunch of variables coming from a database that need to be placed into the...
6
by: Rolf Welskes | last post by:
Hello, I want to partial cache by using a UserControl. Now I have a file dependency. In msdn I see it is not possible to do it the same way as in a page. The only information is to create a...
0
by: das | last post by:
Hello all, I am using the SqlDependency to subscribe to any new inserts into a database table, I enabled the DB to be borker ready and subscrbed to Query notifications on the database. My C#...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.