Storing a type for dynamic casts... 
Author Message
 Storing a type for dynamic casts...

Ok, here's the problem.  I want to be able to use a number of controls whom
all have the same interface is an incredibly generic way.  The idea is to
store the type in a variable on the class that holds info on each of these
individual controls and then use that type to cast them back from void* when
I want to access their methods.  Some example code to show what I mean.

Creation Method
------------------
this->m_SomeClass.pv = (void*) new SomeOtherClass;
this->m_SomeClass.Type = (SomeOtherClass*);

Usage Method
---------------
( (this->m_SomeClass.Type) this->m_SomeClass.pv )->SomeMethod();

I have thus far done a lot of research and tried a lot of things to
accomplish this, but I will not go into detail as to not have someone not
post a possible answer because I've already tried it.  The reason for being
this general is that we are constantly going to be adding new controls for
new devices to this main control and I want to be able to have only one new
'if' check for the initial 'new' operation and that's it.  After that all
code would be able to be identical because all of our wrapper controls will
have the exact same interfaces.  (Generalized interfaces for non-general
devices.)  Thanks in advance (TIA) for any help you can offer.

- Jon



Sun, 12 Oct 2003 21:18:32 GMT  
 Storing a type for dynamic casts...
Maybe I misunderstood what you said but it seems to me that you want to do
the following:

class Base
{
   Foo ()

Quote:
}

class Derived1 : public Base
{
   Foo()
Quote:
}

...

and later call Foo() given any object derived from Base.  If Foo is virtual
and you are using reference or pointer objects then the call will work as
you expect.  You don't have to keep track of the object type.  The compiler
generates code to look up the correct function at runtime.  Note that this
is also where COM is normally used.

If you want to call a method that is specific to a derived class then you
would have to know that the object you have supports it first.  So I don't
really see any easy way to avoid adding object-specific code in this case.

If you want to determine the type of an object then use typeof().  It
returns a constant type_info& that you can store for later use and do
comparisons elsewhere.   It might be possible to pass a type_info object as
the argument to dynamic_cast<> but I sort of doubt it.

Hope it helps,
--
Michael Taylor,MCP
DCS Corp.


Quote:
> Ok, here's the problem.  I want to be able to use a number of controls
whom
> all have the same interface is an incredibly generic way.  The idea is to
> store the type in a variable on the class that holds info on each of these
> individual controls and then use that type to cast them back from void*
when
> I want to access their methods.  Some example code to show what I mean.

> Creation Method
> ------------------
> this->m_SomeClass.pv = (void*) new SomeOtherClass;
> this->m_SomeClass.Type = (SomeOtherClass*);

> Usage Method
> ---------------
> ( (this->m_SomeClass.Type) this->m_SomeClass.pv )->SomeMethod();

> I have thus far done a lot of research and tried a lot of things to
> accomplish this, but I will not go into detail as to not have someone not
> post a possible answer because I've already tried it.  The reason for
being
> this general is that we are constantly going to be adding new controls for
> new devices to this main control and I want to be able to have only one
new
> 'if' check for the initial 'new' operation and that's it.  After that all
> code would be able to be identical because all of our wrapper controls
will
> have the exact same interfaces.  (Generalized interfaces for non-general
> devices.)  Thanks in advance (TIA) for any help you can offer.

> - Jon



Sun, 12 Oct 2003 21:30:05 GMT  
 Storing a type for dynamic casts...
If your objects are derived from CObject you may be able to use
obj.IsKindOf(xxx)

See -
    IsKindOf(...)
    RUNTIME_CLASS( x )

Darryl

--
Software Engineer
Telspec Turope Ltd

Quote:
> Ok, here's the problem.  I want to be able to use a number of controls
whom
> all have the same interface is an incredibly generic way.  The idea is to
> store the type in a variable on the class that holds info on each of these
> individual controls and then use that type to cast them back from void*
when
> I want to access their methods.  Some example code to show what I mean.

> Creation Method
> ------------------
> this->m_SomeClass.pv = (void*) new SomeOtherClass;
> this->m_SomeClass.Type = (SomeOtherClass*);

> Usage Method
> ---------------
> ( (this->m_SomeClass.Type) this->m_SomeClass.pv )->SomeMethod();

> I have thus far done a lot of research and tried a lot of things to
> accomplish this, but I will not go into detail as to not have someone not
> post a possible answer because I've already tried it.  The reason for
being
> this general is that we are constantly going to be adding new controls for
> new devices to this main control and I want to be able to have only one
new
> 'if' check for the initial 'new' operation and that's it.  After that all
> code would be able to be identical because all of our wrapper controls
will
> have the exact same interfaces.  (Generalized interfaces for non-general
> devices.)  Thanks in advance (TIA) for any help you can offer.

> - Jon



Sun, 12 Oct 2003 22:19:19 GMT  
 Storing a type for dynamic casts...
What you are describing sounds like virtual functions. Is that right?

One problem is that MFC supports only one CWnd-derived base class for
Multiple Inheritance. Multiple Inheritance is when a class is derived from
more than one class. I think you will need to create a class derived from
CWnd with your overrides and then create a class derived from the various
controls. The problem though is that you cannot do that because then you
would need to derive each of of your control's classes from both of the
other classes.

So anothr possibility is templates, but I do not know much about them.


Quote:
> Ok, here's the problem.  I want to be able to use a number of controls
whom
> all have the same interface is an incredibly generic way.  The idea is to
> store the type in a variable on the class that holds info on each of these
> individual controls and then use that type to cast them back from void*
when
> I want to access their methods.  Some example code to show what I mean.

> Creation Method
> ------------------
> this->m_SomeClass.pv = (void*) new SomeOtherClass;
> this->m_SomeClass.Type = (SomeOtherClass*);

> Usage Method
> ---------------
> ( (this->m_SomeClass.Type) this->m_SomeClass.pv )->SomeMethod();

> I have thus far done a lot of research and tried a lot of things to
> accomplish this, but I will not go into detail as to not have someone not
> post a possible answer because I've already tried it.  The reason for
being
> this general is that we are constantly going to be adding new controls for
> new devices to this main control and I want to be able to have only one
new
> 'if' check for the initial 'new' operation and that's it.  After that all
> code would be able to be identical because all of our wrapper controls
will
> have the exact same interfaces.  (Generalized interfaces for non-general
> devices.)  Thanks in advance (TIA) for any help you can offer.

> - Jon



Sun, 12 Oct 2003 22:05:24 GMT  
 Storing a type for dynamic casts...
That's sort of it, only I am using ActiveX controls dynamically, not classes
with a base class and virtual functions (which is an option I had
considered).  The reason for using the ActiveX controls is that the machine
on which we install this main ActiveX control (the ones including the
smaller 'hardware wrapper' controls), may or may not have the hardware and
supporting files for that hardware installed on it.  Hence the dynamic
loading of the ActiveX controls.  If you explicitly tell it to include them
and the driver files are not installed, it won't register.  Also we are
trying to cut down on how much the main ActiveX control grows with each
added piece of hardware.  With dynamically included controls this is very
simple.  One extra 'if' check theoretically which will add negligable new
size.  So if it were as simple as standard C++ classes I'd indeed use
virtual functions.  Instead I am stuck with an ActiveX control dynamically
including other ActiveX controls.  Still, thanks for the answers thus far.
:)  Any more ideas?

- Jon



Sun, 12 Oct 2003 23:02:27 GMT  
 Storing a type for dynamic casts...
It sounds like you really want each of your ActiveX controls to implement a
specific interface.

Then your Creation does a QueryInterface for that interface...

Or are you saying you are using off the shelf ActiveX controls, and you want
to create a C++ Wrapper for each. In which case it sounds like a common base
class...

Just a thought
Jay


Quote:
> Ok, here's the problem.  I want to be able to use a number of controls
whom
> all have the same interface is an incredibly generic way.  The idea is to
> store the type in a variable on the class that holds info on each of these
> individual controls and then use that type to cast them back from void*
when
> I want to access their methods.  Some example code to show what I mean.

> Creation Method
> ------------------
> this->m_SomeClass.pv = (void*) new SomeOtherClass;
> this->m_SomeClass.Type = (SomeOtherClass*);

> Usage Method
> ---------------
> ( (this->m_SomeClass.Type) this->m_SomeClass.pv )->SomeMethod();

> I have thus far done a lot of research and tried a lot of things to
> accomplish this, but I will not go into detail as to not have someone not
> post a possible answer because I've already tried it.  The reason for
being
> this general is that we are constantly going to be adding new controls for
> new devices to this main control and I want to be able to have only one
new
> 'if' check for the initial 'new' operation and that's it.  After that all
> code would be able to be identical because all of our wrapper controls
will
> have the exact same interfaces.  (Generalized interfaces for non-general
> devices.)  Thanks in advance (TIA) for any help you can offer.

> - Jon



Tue, 14 Oct 2003 07:54:59 GMT  
 Storing a type for dynamic casts...
That's awfully simple.  Use a COM object as a COM object.  I was too caught
up on the casting to remember QI and Invoke.  So anyway, along those lines I
am storing the IID for the control in the same class I store the pointer to
the control, and then I will (am working on this now) QI with the IID and
then Invoke with the DispID - which each control can have the same DispID
for the same functions since I am writing them as well (we are wrapping up
either a DLL or an OCX, depending on what the board manufacturer provides us
with).  So then if I want to call Initialize() (custom method) on ControlA
or ControlB, as long as their DispIDs are the same for Initialize(), and I
have the IID, I should be able to Invoke with no problem.  Does that sound
feasible?  Thanks for the help, by the way Jay.  Got me looking at it a
different way - just what I was in need of.  :)

- Jon



Wed, 15 Oct 2003 01:40:13 GMT  
 Storing a type for dynamic casts...
Jon,
 Glad to help...

DispID being the same does not really matter... If the controls stick with
IDispatch (you did mention Invoke) you can use IDispatch::GetIDsOfNames to
get the dispID. If you are writing the controls, you can ensure that the are
'constant'...

I was actually thinking you would 'publish' a brand new interface. Each of
the wrapper controls (you are writing?) would then implement this new
interface. Similar to how it implements IOleControl & IDispatch now...

You then QI on this new interface, and invoke the methods on it...

Hope this helps
Jay


Quote:
> That's awfully simple.  Use a COM object as a COM object.  I was too
caught
> up on the casting to remember QI and Invoke.  So anyway, along those lines
I
> am storing the IID for the control in the same class I store the pointer
to
> the control, and then I will (am working on this now) QI with the IID and
> then Invoke with the DispID - which each control can have the same DispID
> for the same functions since I am writing them as well (we are wrapping up
> either a DLL or an OCX, depending on what the board manufacturer provides
us
> with).  So then if I want to call Initialize() (custom method) on ControlA
> or ControlB, as long as their DispIDs are the same for Initialize(), and I
> have the IID, I should be able to Invoke with no problem.  Does that sound
> feasible?  Thanks for the help, by the way Jay.  Got me looking at it a
> different way - just what I was in need of.  :)

> - Jon



Wed, 15 Oct 2003 07:04:20 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Cast to a dynamic type?

2. dynamic casting (type known at runtime)

3. dynamic type casting

4. Dynamic Type Casting at Runtime

5. Type Casting between Base and Derived Type (Serialization of objects with Type members)

6. type casting to / from pointers to incomplete data types

7. type casting to / from pointers to incomplete data types

8. cast cstring type to integer type

9. Casting to/from STL types to MFC types

10. Why can't overload type cast to base type (object)

11. Strong typing....(static/dynamic typing)

12. Dynamic Casting

 

 
Powered by phpBB® Forum Software