473,408 Members | 2,888 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,408 software developers and data experts.

"really" dynamic cast

Hi All,

Is there a way to dynamic_cast a pointer to a type defined by a
type_info*?

something like:
type_info *info_of_type_to_cast = typeid(type_to_cast);
type_to_cast *casted =
really_dynamic_cast<info_of_type_to_cast>(my_data_ ptr);

I expect to work as:
type_to_cast *casted = dynamic_cast<type_to_cast>(my_data_ptr);

Of course it should raise a bad_cast exception if types are
unrelated, and all other standard behavior of dynamic_cast...

thanks,

Jordi Vilar
Jul 22 '05 #1
6 5996
Jordi Vilar wrote:
Hi All,

Is there a way to dynamic_cast a pointer to a type defined by a
type_info*?

something like:
type_info *info_of_type_to_cast = typeid(type_to_cast);
type_to_cast *casted =
really_dynamic_cast<info_of_type_to_cast>(my_data_ ptr);
What would that be good for? You already know the target type at compile
time, since you created the 'casted' pointer with it.
I expect to work as:
type_to_cast *casted = dynamic_cast<type_to_cast>(my_data_ptr);

Of course it should raise a bad_cast exception if types are
unrelated, and all other standard behavior of dynamic_cast...


dynamic_cast doesn't throw at all when used with pointers.

Jul 22 '05 #2
In article <5b**************************@posting.google.com >, Jordi Vilar wrote:
Hi All,

Is there a way to dynamic_cast a pointer to a type defined by a
type_info*?

something like:
type_info *info_of_type_to_cast = typeid(type_to_cast);
type_to_cast *casted =
really_dynamic_cast<info_of_type_to_cast>(my_data_ ptr);

I expect to work as:
type_to_cast *casted = dynamic_cast<type_to_cast>(my_data_ptr);

Of course it should raise a bad_cast exception if types are
unrelated, and all other standard behavior of dynamic_cast...


I agree with Rolf's response -- what's the motivation for this ?

The question as presented doesn't make sense -- the point of dynamic_cast
is to attempt convert a type not known at compile to type known at compile.

Or to put it another way, how does a derived class pointer where you don't
know the type of pointer at compile time better than a base class pointer ? To
me, it appears that the latter is C++'s way of modelling the former.

Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 22 '05 #3
Hi All again,
Hi All,

Is there a way to dynamic_cast a pointer to a type defined by a
type_info*?

something like:
type_info *info_of_type_to_cast = typeid(type_to_cast);
type_to_cast *casted =
really_dynamic_cast<info_of_type_to_cast>(my_data_ ptr);

I expect to work as:
type_to_cast *casted = dynamic_cast<type_to_cast>(my_data_ptr);

Of course it should raise a bad_cast exception if types are
unrelated, and all other standard behavior of dynamic_cast...
I agree with Rolf's response -- what's the motivation for this ?


I'm currently developing an implementation of an interface containing
a method:

virtual void* cast_to(const type_info &ti) throw(nointerface,
bad_typeid) = 0;

a trivial implementation is:

void* CMyImpl::cast_to(const type_info &ti)
{
if(ti == typeid(IInteface1*))
return static_cast<IInterface1*>(this);
if(ti == typeid(IInteface2*))
return static_cast<IInterface2*>(this);
...
throw nointerface();
}

but I'm looking for a simpler and less error prone implementation. I
found __RTDynamicCast in MS VC++ that just does it (VC++ implements
dynamic_cast using this undocumented helper function)

void* CMyImpl::cast_to(const type_info &ti)
{
void *coerced = __RTDynamicCast(this, 0, typeid(CMyImpl*), ti, 0);
if(coerced == NULL)
throw nointerface();
return coerced;
}

This resolves all casts as dynamic_cast does, but this is not
standard, so I don't want it (I need a portable code)
The question as presented doesn't make sense -- the point of dynamic_cast
is to attempt convert a type not known at compile to type known at compile.

Or to put it another way, how does a derived class pointer where you don't
know the type of pointer at compile time better than a base class pointer ? To
me, it appears that the latter is C++'s way of modelling the former.
I agree with you, you cannot use data of an unknown type! so this is
not very useful and you can obtain the same functionality with
dynamic_cast (if you know the type to cast to in compile-time). But
the interface I NEED to implement requires such semantics to be
supported.

It also enables to query for a type known in another layer. The bad
thing is the void*, but it also happens with dynamic_cast<void*>() !!

The only interesting benefit of this scheme is the control of the cast
process, so you can control the interfaces you want to expose or hide,
beyond the public/protected/private scheme. Also you can resolve
ambiguities (what dynamic_cast doest when requesting a cast to a base
interface of a two implemented interfaces (multiple inheritance of
interfaces deriving from a common base interface)?) And achieving this
hiding at all the implementation class and using pure virtual
interfaces.

This is a C++ replacement of the discovering services in other systems
(reflection in Java/.NET, or IUnknown::QueryInterface() in COM, or the
equivalent in CORBA, etc.)

Maybe this is not possible with the current standard, so bad luck...
:(
Cheers,


Thanks for your comments.

Regards,

Jordi Vilar
Jul 22 '05 #4
In article <5b**************************@posting.google.com >, Jordi Vilar wrote:

[snip]

OK reading the text at the bottom, I have some idea what you're trying to
do.
void* CMyImpl::cast_to(const type_info &ti)
{
void *coerced = __RTDynamicCast(this, 0, typeid(CMyImpl*), ti, 0);
if(coerced == NULL)
throw nointerface();
return coerced;
}

This resolves all casts as dynamic_cast does, but this is not
standard, so I don't want it (I need a portable code)


OK. I see what it's doing. It attempts to safely cast the current object to
one of type ti and returns the appropriate pointer if the cast worked.

One thing worth thinking about is making your own more powerful objects for
representing types, and in particular, make the type object responsible for
handling the conversions.

e.g. here's a really simple example:

struct type
{
public:
virtual void* convert (const type& x) = 0;
};

template <typename T>
struct type_impl : type
{
public:
virtual void* convert (const type & x)
{
return dynamic_cast<T*>(&x);
}
};

void* CMyImpl::cast_to(const type &ti)
{
return ti.convert(this);
}

Though this looks static (because you have templates), you could actually compile your
templated objects into instantion modules, and load them dynamically (IOW add types to
a running system).

Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 22 '05 #5
Donovan Rebbechi <ab***@aol.com> wrote in message news:<sl******************@panix2.panix.com>...

Hi Donovan,

Your proposal is very interesting! The only cost of your approach is
to define a new type class to handle conversions. This sounds great
except the interface I need to implement uses the standad type_info.
The solution maybe is to create a map (type_info -> type) and create a
single, global instance of type_impl for each type. C'tor of type_impl
can then insert itself into the map.

One question: why type::convert needs a 'const type &' argument
instead of void*? this forces to derive always from 'type'...

Thanks,

Jordi Vilar
In article <5b**************************@posting.google.com >, Jordi Vilar wrote:

[snip]

OK reading the text at the bottom, I have some idea what you're trying to
do.
void* CMyImpl::cast_to(const type_info &ti)
{
void *coerced = __RTDynamicCast(this, 0, typeid(CMyImpl*), ti, 0);
if(coerced == NULL)
throw nointerface();
return coerced;
}

This resolves all casts as dynamic_cast does, but this is not
standard, so I don't want it (I need a portable code)


OK. I see what it's doing. It attempts to safely cast the current object to
one of type ti and returns the appropriate pointer if the cast worked.

One thing worth thinking about is making your own more powerful objects for
representing types, and in particular, make the type object responsible for
handling the conversions.

e.g. here's a really simple example:

struct type
{
public:
virtual void* convert (const type& x) = 0;
};

template <typename T>
struct type_impl : type
{
public:
virtual void* convert (const type & x)
{
return dynamic_cast<T*>(&x);
}
};

void* CMyImpl::cast_to(const type &ti)
{
return ti.convert(this);
}

Though this looks static (because you have templates), you could actually compile your
templated objects into instantion modules, and load them dynamically (IOW add types to
a running system).

Cheers,

Jul 22 '05 #6
In article <5b************************@posting.google.com>, Jordi Vilar wrote:
Donovan Rebbechi <ab***@aol.com> wrote in message news:<sl******************@panix2.panix.com>...

Hi Donovan,

Your proposal is very interesting! The only cost of your approach is
to define a new type class to handle conversions. This sounds great
except the interface I need to implement uses the standad type_info.
The solution maybe is to create a map (type_info -> type) and create a
single, global instance of type_impl for each type. C'tor of type_impl
can then insert itself into the map.
Yes, something like that.
One question: why type::convert needs a 'const type &' argument
instead of void*? this forces to derive always from 'type'...


Sorry, yes of course you're right. It should be void*

Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jeff Sandler | last post by:
I have a web page. It uses JavaScript to test the user's input before sending it to the server. It frequently tests using isNaN() with some very interesting results. The statements in question...
11
by: Tolga | last post by:
After a very rapid entrance into the Python, I have immediately looked for a good IDE. Komodo and Wing IDE look very good and I think they are enough. But now, I am searching for a Pyhton...
1
by: wxqun | last post by:
Our company is now trying to make a "standard" of creating a base view for each user table. This initiative is suggested as a good practice by a data modeling consultant who is helping us to build...
7
by: schoenfeld.one | last post by:
Most people don't know that there were actually 3 buildings which came crashing down on the day of 9/11. The third building, WTC 7, can be seen here ...
4
by: Dave | last post by:
I have a global.asax file with Application_Start defined and create some static data there and in another module used in the asp.net application and I realize that static data is shared amongst...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.