S
Simon Elliott
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?
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?