473,704 Members | 6,472 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Handles and pointers

I have a data management class which uses a handle:

typedef void* barHandle;

class foo
{
private:
class fooImpl;
fooImpl* pimpl_;
public:
foo(void);
virtual ~foo(void);
barHandle FindBar(const std::string& key);
std::string getvaluefrombar (barHandle handle, const std::string& key);
};

This would be used as follows:

std::string my_value;
barHandle my_handle = my_foo.Findbar( "some_bar") ;
if (my_handle)
{
my_value = my_foo.getvalue frombar(my_hand le, "some_value ");
}

I want the implementation to be opaque to the user; it could be using
XML, an SQL database or whatever. Currently it's using XML, and the
implementation has code along these lines:

barHandle foo::fooImpl::F indBar(const std::string& key)
{
TsomeXMLcontain er* elementBar FindBar(element Main,key);
return(elementB ar); // casts TsomeXMLcontain er* to void*
}

std::string foo::fooImpl::g etvaluefrombar
(barHandle handle, const std::string& key)
{
TsomeXMLcontain er* elementBar =
static_cast<Tso meXMLcontainer* >(barHandle);
elementBar->GoAndFindValue (key);
...

Fine as far as it goes, but not very type safe. Any recommendations for
improving this by replacing the void* with something else?

--
Simon Elliott http://www.ctsn.co.uk
Jul 22 '05 #1
3 1221

"Simon Elliott" <Simon at ctsn.co.uk> wrote in message
news:41******** *************** @news.gradwell. net...
I have a data management class which uses a handle:

typedef void* barHandle;
This is just a pointer. I'm not sure if there's any "standard" for the term
"handle", but in the code I work on, a handle is a pointer to a pointer.
(This is true both in Windows and Mac, so at least it's common, if not
"standard". )

class foo
{
private:
class fooImpl;
fooImpl* pimpl_;
public:
foo(void);
virtual ~foo(void);
barHandle FindBar(const std::string& key);
std::string getvaluefrombar (barHandle handle, const std::string& key);
};

This would be used as follows:

std::string my_value;
barHandle my_handle = my_foo.Findbar( "some_bar") ;
if (my_handle)
{
my_value = my_foo.getvalue frombar(my_hand le, "some_value ");
}

I want the implementation to be opaque to the user; it could be using
XML, an SQL database or whatever. Currently it's using XML, and the
implementation has code along these lines:

barHandle foo::fooImpl::F indBar(const std::string& key)
{
TsomeXMLcontain er* elementBar FindBar(element Main,key);
return(elementB ar); // casts TsomeXMLcontain er* to void*
}

std::string foo::fooImpl::g etvaluefrombar
(barHandle handle, const std::string& key)
{
TsomeXMLcontain er* elementBar =
static_cast<Tso meXMLcontainer* >(barHandle);
elementBar->GoAndFindValue (key);
...

Fine as far as it goes, but not very type safe. Any recommendations for
improving this by replacing the void* with something else?


I don't see why you need to cast to anything. Why not just use a
TsomeXMLcontain er* pointer wherever you need it? Casting to void* is, as
you've said, not type safe. Hiding implementation details doesn't
neccessarily mean hiding data types. If the name's too combersome, you can
always use a typedef for that pointer (or even a #define, for simple
symbolic replacement). Casting back and forth to void* is just a waste of
time, and dangerous. Unless you have some third-party API/SDK that requires
a void* parameter somewhere, don't bother. Just use the pointer type you
need. (Even in the case where you *do* need a void*, it's best to cast it
to that at the point of use, not carry around a void* that can get abused in
other code.) Just my 2 cents worth...

-Howard


Jul 22 '05 #2
On 22/09/2004, Howard wrote:

I don't see why you need to cast to anything. Why not just use a
TsomeXMLcontain er* pointer wherever you need it? Casting to void*
is, as you've said, not type safe. Hiding implementation details
doesn't neccessarily mean hiding data types.


In this case the data type exposes the underlying implementation too
much. One day the users might want to replace the XML based
implementation with something else, perhaps an SQL database. They would
then need to change their source code, replacing every instance of
TsomeXMLcontain er* with TsomeSQLresourc e*.

--
Simon Elliott http://www.ctsn.co.uk
Jul 22 '05 #3

"Simon Elliott" <Simon at ctsn.co.uk> wrote in message
news:41******** *************** @news.gradwell. net...
On 22/09/2004, Howard wrote:

I don't see why you need to cast to anything. Why not just use a
TsomeXMLcontain er* pointer wherever you need it? Casting to void*
is, as you've said, not type safe. Hiding implementation details
doesn't neccessarily mean hiding data types.


In this case the data type exposes the underlying implementation too
much. One day the users might want to replace the XML based
implementation with something else, perhaps an SQL database. They would
then need to change their source code, replacing every instance of
TsomeXMLcontain er* with TsomeSQLresourc e*.

--
Simon Elliott http://www.ctsn.co.uk


That's where using a typedef or #define can help. Define a new symbol in
the header for the class that implements those functions, and when you
change the underlying functionality, you can change the definition to match.

A better approach might be to use another class in between, which hides the
passing of this pointer around entirely. If the user has no need to know
what the actual variable type is, then they probably also don't actually
need to *touch* that variable. (After all, you can't do anything useful
with a variable if you don't know what it is, right? :-)) Instead of
returning a pointer from a function and then passing that pointer to another
function, just call one (or two) functions in this new class to do the work
for you, hiding the pointer entirely within that wrapper class. That's much
more in the OO-style of doing things, I think.

-Howard
Jul 22 '05 #4

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

Similar topics

6
1896
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 several times to understand. I would like to understnad hgandles and find good uses for them or better understand how they can make my programs better.
3
2031
by: Alex | last post by:
I'm having a problem porting an ASP solution to ASPX. In the ASP solution I'm accessing a DCOM server, create sub DCOM objects and call functions from VB script on the ASP pages. The DCOM object handles are stored in session variables. This works fine without a problem. Ported it to ASPX, accessing the same DCOM server from code behind pages. Still, usually no problems. However sometimes I'm seeing an error stating that the DCOM handle...
3
1127
by: mwindham | last post by:
How does one make a gc class that contains an array of handles to an array of X-type? // comments out what I am trying to do: public ref class c0 { private: static System::UInt32 c0_uid; System::Int32 ndx; array<System::UInt32> ^ c01 ; // array<System::UInt32^> ^ c02 ;
3
1628
by: sajohn | last post by:
I'm fairly new to c programming and am not very clear on pointers and handles. Currently, I'm writting a wrapper script and I need to interface with another application. I have decided that the wrapper script could use some data structures and have defined a few. The problem is that the application has defined some pointers that I could use for my structures that are defined as unsigned long. I thought I could write some code as follows ...
2
1522
by: Carter | last post by:
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*...
0
8763
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9266
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9127
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9020
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
4435
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3128
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2467
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2073
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.