Click here to Skip to main content
15,903,523 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: how to implement three indepedent random number generators in the same subroutine/function Pin
Adam Roderick J10-Apr-10 20:52
Adam Roderick J10-Apr-10 20:52 
GeneralRe: how to implement three indepedent random number generators in the same subroutine/function Pin
mrby12314-Apr-10 7:21
mrby12314-Apr-10 7:21 
GeneralRe: how to implement three indepedent random number generators in the same subroutine/function Pin
mrby12314-Apr-10 7:20
mrby12314-Apr-10 7:20 
Questionpost mouse move, wheel, RClick messages, (captured by CView) to the started thread Pin
m_code9-Apr-10 11:46
m_code9-Apr-10 11:46 
AnswerRe: post mouse move, wheel, RClick messages, (captured by CView) to the started thread Pin
Adam Roderick J9-Apr-10 18:11
Adam Roderick J9-Apr-10 18:11 
GeneralRe: post mouse move, wheel, RClick messages, (captured by CView) to the started thread Pin
m_code9-Apr-10 18:43
m_code9-Apr-10 18:43 
GeneralRe: post mouse move, wheel, RClick messages, (captured by CView) to the started thread Pin
Adam Roderick J9-Apr-10 19:12
Adam Roderick J9-Apr-10 19:12 
QuestionC++ class hierarchy design problem Pin
kylur9-Apr-10 9:00
kylur9-Apr-10 9:00 
Fellow coders,

RE: a C++ design problem.

I am attempting to construct a C++ class hierarchy on top of a C legacy system. The result
will be then placed into an embedded controller. The controller communicates with a Windows
program via a series of 'software pipes' to a supplied DLL and then calls in the Windows
program to fetch information from the DLL. The data in a given pipe will be of a specific type.

In order to generalize this process the third party library uses the following header:

typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef unsigned long DWORD;

typedef union gen_scalar {
    double  _double;
    __int64 _i64;
    float   _float;
    long    _i32;
    short   _i16;
    DWORD   _long;
    WORD    _word;
    BYTE    _byte;
} GENERIC_SCALAR;

And then, for example, reading a long from a pipe would be:

long Get(PIPE* pipe)
{
    GENERIC_SCALAR v;
    pipe_value_get(pipe, &v);
    Return v._i32;
};

So, the hierarchy starts with a base class:

class Pipe
{
public:
    Pipe(PIPE* pipe)
        : pipe_(pipe)
    {};
    virtual ~Pipe()
    {};

protected:
    PIPE*   pipe_;
};

where a PIPE* is just a handle to the communication pipe.

Then an input class is needed:

class IPipe : public Pipe
{
public:
    IPipe(PIPE* pipe)
        : Pipe(pipe)
    {
        pipe_open(pipe_, P_READ);
    };
};


There would also be a corresponding output class OPipe.

And now the part I need help with!

Suppose I know a pipe will contain a series of 'shorts'. Then I could create:

class ShortIPipe : public IPipe
{
public:
    ShortIPipe(IPIPE* pipe)
        : IPipe(pipe)
    {};

    short Get() const
    {
        GENERIC_SCALAR v;
        pipe_value_get(pipe_, &v);
        return v._i16;
    };
};

And so on for a long, double, float, etc, pipe(s).

However, the only difference between all these classes is the return value, that is,
v._i16 or v._i32, etc.

I'd prefer to create a single class which can handle them all. Maybe a template class?
For example:

template <typename T>
class IPipe : public Pipe
{
public:
    IPipe(PIPE* pipe)
        : Pipe(pipe)
    {
        pipe_open(pipe_, P_READ);
    };


    T Get() const
    {
        GENERIC_SCALAR v;
        pipe_value_get(pipe_, &v);
        return v._???;// depends on T
    };
};

Used like:
IPipe<short> spipe(...);
short s = spipe.Get();

And so, (finally) the question is how, within the Get() member, do I define the return value?
Note: the embedded controller cannot support RTTI. And I'd prefer not to have a big switch.

Any help would be much appreciated!
Kylur.
AnswerRe: C++ class hierarchy design problem Pin
Jonathan Davies9-Apr-10 10:28
Jonathan Davies9-Apr-10 10:28 
GeneralRe: C++ class hierarchy design problem Pin
kylur12-Apr-10 9:47
kylur12-Apr-10 9:47 
AnswerRe: C++ class hierarchy design problem Pin
Bernódus Kristinsson9-Apr-10 11:04
Bernódus Kristinsson9-Apr-10 11:04 
GeneralRe: C++ class hierarchy design problem Pin
kylur12-Apr-10 10:25
kylur12-Apr-10 10:25 
AnswerRe: C++ class hierarchy design problem Pin
cmk9-Apr-10 14:52
cmk9-Apr-10 14:52 
GeneralRe: C++ class hierarchy design problem Pin
kylur12-Apr-10 10:09
kylur12-Apr-10 10:09 
GeneralRe: C++ class hierarchy design problem Pin
cmk12-Apr-10 12:45
cmk12-Apr-10 12:45 
AnswerRe: C++ class hierarchy design problem Pin
code_slashxx9-Apr-10 17:38
code_slashxx9-Apr-10 17:38 
GeneralRe: C++ class hierarchy design problem Pin
kylur12-Apr-10 10:10
kylur12-Apr-10 10:10 
QuestionCan PostThreadMessage Post to a Thread in a Different Process Pin
ForNow9-Apr-10 7:50
ForNow9-Apr-10 7:50 
AnswerRe: Can PostThreadMessage Post to a Thread in a Different Process Pin
Code-o-mat9-Apr-10 10:48
Code-o-mat9-Apr-10 10:48 
GeneralRe: Can PostThreadMessage Post to a Thread in a Different Process Pin
ForNow9-Apr-10 11:03
ForNow9-Apr-10 11:03 
GeneralRe: Can PostThreadMessage Post to a Thread in a Different Process Pin
Code-o-mat9-Apr-10 11:11
Code-o-mat9-Apr-10 11:11 
GeneralRe: Can PostThreadMessage Post to a Thread in a Different Process Pin
Adam Roderick J9-Apr-10 19:25
Adam Roderick J9-Apr-10 19:25 
AnswerRe: Can PostThreadMessage Post to a Thread in a Different Process Pin
cmk9-Apr-10 15:15
cmk9-Apr-10 15:15 
AnswerRe: Can PostThreadMessage Post to a Thread in a Different Process [modified] Pin
Adam Roderick J9-Apr-10 18:58
Adam Roderick J9-Apr-10 18:58 
QuestionDestructElements() and destructor not getting called Pin
David Crow9-Apr-10 6:34
David Crow9-Apr-10 6:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.