Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Help me find the best class design for following problem

Reply
Thread Tools

Help me find the best class design for following problem

 
 
Peter
Guest
Posts: n/a
 
      11-05-2011
Hi,

I have following situation that I'm trying to find the best class design
for:

Base class: Device
Two classes inherit from this Device class, let's call them DeviceA and
DeviceB.

Both Device classes A and B can also be emulated via a file on the HD
instead of actually talking to the device.

However these files come in many flavours. Many are unique to the type of
device, yet some are possible for the two types of devices.
So in one case file X is for device A, but in another it's for device B

So I was first thinking of a template where I inherit from the template
class

template <class T>
class FileDevice : public T

And in this class I put the huge shared code base of the two types possible

Then I create file-type specific classes that inherit from either
FileDevice<DeviceA> or FileDevice<DeviceB>
And for the file type classes that cover both devices I probably best also
make them similar templates and instanciate objects either using
FileDevice<DeviceA> or FileDevice<DeviceB> as base class.

How do you feel about this ?

But then I run into following issues:

In the GUI part of the code there are several places where it makes sense to
see if I'm using a file or an actual device, for display and other user
interface purposes (e.g. loading/unloading a file etc.).
There I would normally test the Device pointer via a dynamic cast. If
(dynamic_cast<FileDevice*>(DevicePtr)) { do stuff ; }
However with a template design this doesn't work, as FileDevice needs a
template class.

Fact of the matter is that both DeviceA and DeviceB are Device classes but
testing this: If (dynamic_cast<FileDevice<Device>*>(DevicePtr)) { do stuff
; }
won't work I assume ?

So how would you do this ?

So I was also thinking of multiple inheritance. Something I've never done
in my code but anyway:

class FileAccess {}

template <class T>
class FileDevice : public T, FileAccess

Doing this I would be able to test:
If (dynamic_cast<FileAccess*>(DevicePtr)) { do stuff ; }
*I assume* ?

However there are other problems then as well, since FileAcces is NOT A
Device class I can't pass this pointer to functions that expect a Device
pointer, while in fact the FileAccess device in use IS in reality a Device
through inheritance.
I suppose a dynamic_cast will help here ? But I'm not sure.

Would this work:

FileAccess *FileDev = new FileTypeXDevice() ; // FileTypeXDevice inherits
from FileAccess AND Device, DeviceA etc.

Function(FileDev) ; // Where the function takes a Device pointer (Function
(Device *MyDevice) // This won't work
Function(dynamic_cast<Device*>(FileDev)) ; // this would compile but would a
the pointer be passed as a Device pointer /

Wow, this post has gotten way longer than I planned


--

Best Regards,
Peter

 
Reply With Quote
 
 
 
 
Peter
Guest
Posts: n/a
 
      11-06-2011
> In a bit less ideal world you would have a function IsDevice() or
> something like that which would return true or false appropriately.


Yes, something I have thought about as well. Implement a function:
IsVirtual() that returns true if it is the file-access variant of the
device.

> Do they expect Device
> interface?


Yes.

Each device (A/B) IS A device and both of them have many common
functionality.
Of course through virtual functions specific IO issues etc. are handled
differently.
But that's exactly the point, because a Read() is a Read(), let the
device(A/B) class itself decide how to handle that Read() best.

There is a lot of code that uses the Device pointer and that code doesn't
need to
know whether its device A or B to perform IO etc.
Or a derived from A or B (e.g. access from file) device for instance

I also need to be able to distinguish a "file"-device from a real
device, and instead of having to do the check twice, to see if it's the file
variant of A or the file variant of B, I would like to get a pointer that
works for both, since the file-devices will also have special functions such
as FileName() etc. that the base class device doesn't have and personally I
don't think it feels right to have the base class device have a virtual
functions that relate only really to the file-access derived classes.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
The Web server reported the following error when attempting to create or open the Web project located at the following URL: 'http://localhost/822319ev1'. 'HTTP/1.1 500 Internal Server Error'. chanmm ASP .Net 2 09-07-2010 07:37 AM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Could you help me to find out what is wrong the following usage of function object PengYu.UT@gmail.com C++ 5 04-16-2005 03:39 PM
Help me find a good typedef name for the following type Eric Lilja C++ 3 03-06-2005 02:58 PM
RE: The Web server reported the following error when attempting to create or open the Web project located at the following URL: <URL> =?Utf-8?B?VHJldm9yIEJlbmVkaWN0IFI=?= ASP .Net 0 06-07-2004 07:36 AM



Advertisments