handles in C++

C

Carter

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.
 
G

Gianni Mariani

Carter said:
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) );
 
C

Carter

Thanks for the thoughts. I ended up doing this with a auto_ptr<
shared_ptr<T> >.

Regards,

Carter.

Carter said:
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) );
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top