470,848 Members | 1,197 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,848 developers. It's quick & easy.

handles in C++

Hi,

I am currently working on a project in C++ but I am somewhat unsure
about how to implement the equivalent of C/Java style handles. I am
curious about the correct idiomatic way to do this. My problem is as
follows I have a set of elements which I want to update to a new value
all at the same time. I figure I would use a handle for this.

In C this might be a T** handle. and possibly an array/linked list
holding the pointers T* array[N]. What is the best way to do something
like this in C++?

Regards,

Carter.
Jun 27 '08 #1
2 1297
Carter wrote:
Hi,

I am currently working on a project in C++ but I am somewhat unsure
about how to implement the equivalent of C/Java style handles. I am
curious about the correct idiomatic way to do this. My problem is as
follows I have a set of elements which I want to update to a new value
all at the same time. I figure I would use a handle for this.

In C this might be a T** handle. and possibly an array/linked list
holding the pointers T* array[N]. What is the best way to do something
like this in C++?
There are many C++ ways of doing this. The technique I like the most is
the one that gives you the greatest type safety. A handle can usually
refer to different types of "things" being handled so you try to build a
class heirarchy.

class HandleBase
{
protected:
HandleType m_handle;

public:
HandleBase(HandleType i_handle)
: m_handle( i_handle )
{
}

virtual void Close()
{
... close handle ...
}

virtual ~HandleBase()
{
Close();
}

// no copying ...
private:
HanldeBase(const HanldeBase &);
HanldeBase operator=(const HanldeBase &);
};
Now, the question is, how do you manage HandleBase which is a simple
matter of defining how you want them to behave. I like managing them
with shared pointers which means when all references to a handle go
away, the handle is magically closed.

e.g.

std::vector<shared_ptr<HanldeBase handles;

handles.push_back( new HandleBase(handle) );
Jun 27 '08 #2
Thanks for the thoughts. I ended up doing this with a auto_ptr<
shared_ptr<T.

Regards,

Carter.

On May 12, 5:56 am, Gianni Mariani <gi4nos...@mariani.wswrote:
Carter wrote:
Hi,
I am currently working on a project in C++ but I am somewhat unsure
about how to implement the equivalent of C/Java style handles. I am
curious about the correct idiomatic way to do this. My problem is as
follows I have a set of elements which I want to update to a new value
all at the same time. I figure I would use a handle for this.
In C this might be a T** handle. and possibly an array/linked list
holding the pointers T* array[N]. What is the best way to do something
like this in C++?

There are many C++ ways of doing this. The technique I like the most is
the one that gives you the greatest type safety. A handle can usually
refer to different types of "things" being handled so you try to build a
class heirarchy.

class HandleBase
{
protected:
HandleType m_handle;

public:
HandleBase(HandleType i_handle)
: m_handle( i_handle )
{
}

virtual void Close()
{
... close handle ...
}

virtual ~HandleBase()
{
Close();
}

// no copying ...
private:
HanldeBase(const HanldeBase &);
HanldeBase operator=(const HanldeBase &);

};

Now, the question is, how do you manage HandleBase which is a simple
matter of defining how you want them to behave. I like managing them
with shared pointers which means when all references to a handle go
away, the handle is magically closed.

e.g.

std::vector<shared_ptr<HanldeBase handles;

handles.push_back( new HandleBase(handle) );
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Darren Wooding | last post: by
6 posts views Thread by enki | last post: by
5 posts views Thread by LBT | last post: by
3 posts views Thread by Brett | last post: by
12 posts views Thread by johnb41 | last post: by
6 posts views Thread by antonyliu2002 | last post: by
2 posts views Thread by sck10 | last post: by
7 posts views Thread by Jurgen Haan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.