473,386 Members | 1,712 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,386 software developers and data experts.

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.getvaluefrombar(my_handle, "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::FindBar(const std::string& key)
{
TsomeXMLcontainer* elementBar FindBar(elementMain,key);
return(elementBar); // casts TsomeXMLcontainer* to void*
}

std::string foo::fooImpl::getvaluefrombar
(barHandle handle, const std::string& key)
{
TsomeXMLcontainer* elementBar =
static_cast<TsomeXMLcontainer*>(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 1211

"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.getvaluefrombar(my_handle, "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::FindBar(const std::string& key)
{
TsomeXMLcontainer* elementBar FindBar(elementMain,key);
return(elementBar); // casts TsomeXMLcontainer* to void*
}

std::string foo::fooImpl::getvaluefrombar
(barHandle handle, const std::string& key)
{
TsomeXMLcontainer* elementBar =
static_cast<TsomeXMLcontainer*>(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
TsomeXMLcontainer* 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
TsomeXMLcontainer* 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
TsomeXMLcontainer* with TsomeSQLresource*.

--
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
TsomeXMLcontainer* 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
TsomeXMLcontainer* with TsomeSQLresource*.

--
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
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...
3
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...
3
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;...
3
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...
2
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...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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
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,...
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...

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.