473,400 Members | 2,163 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,400 software developers and data experts.

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 1513
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Rohit | last post by:
Hi, Threads in the .NET Framework 1.1 (and possibly in 1.0 also) leak "Event" handles, by Event handles I mean Win32 Event handles which can be monitored using the ProcessExplorer from...
1
by: Darren Wooding | last post by:
Hello, can anyone offer any advice, this one is driving me up the wall! 1. I am using a webform datagrid. 2. The columns are all dynamic and defined using templates (see code below). 3. I have...
6
by: enki | last post by:
I have been reading Ruminations on C++, a very interesting book. They are going into handles. I think what they are doing is very intersting and very confusing. I have to read each paragraph...
5
by: LBT | last post by:
I have a window service written in VB.NET to scan directory and process file. During directory scanning (scheduled by timer using System.IO.Directory.GetFiles method), I can observe that the column...
3
by: Brett | last post by:
I'm using a third part component in a certain app. The component has methods and events. I noticed one particular event was not firing after a while but it should have been. The problem was the...
12
by: johnb41 | last post by:
This question is regarding the "Handles" part of a routine, for example: Handles Button1.Click On Form2 (a User Control) I have a button. Here's it's code: Public Sub Button1_Click(ByVal...
7
by: Simon Verona | last post by:
I have a problem in my application which I believe is due to open handles.. . The symptom that users report is that after they have been using the application for a while, it will randomly just...
6
by: antonyliu2002 | last post by:
Right now I put my VB code in the aspx pages. I would prefer to use codebehind, especially many of my VB functions may be shared across multiple aspx pages. I've tried a few pages with...
2
by: sck10 | last post by:
Hello, Is there an equivalent to using "Handles" in the codebehind for c#? Or do I have to declare every event in the GridView control like the following: <asp:GridView ID="gvSearchList"...
7
by: Jurgen Haan | last post by:
Hi there. Recently, one of our application servers is moaning about its handles. (no more handles available). I'm guessing it has to do with some bug in our software where it does not release...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.