473,698 Members | 2,551 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"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_t o_cast = typeid(type_to_ cast);
type_to_cast *casted =
really_dynamic_ cast<info_of_ty pe_to_cast>(my_ data_ptr);

I expect to work as:
type_to_cast *casted = dynamic_cast<ty pe_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 6019
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_t o_cast = typeid(type_to_ cast);
type_to_cast *casted =
really_dynamic_ cast<info_of_ty pe_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<ty pe_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_t o_cast = typeid(type_to_ cast);
type_to_cast *casted =
really_dynamic_ cast<info_of_ty pe_to_cast>(my_ data_ptr);

I expect to work as:
type_to_cast *casted = dynamic_cast<ty pe_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_t o_cast = typeid(type_to_ cast);
type_to_cast *casted =
really_dynamic_ cast<info_of_ty pe_to_cast>(my_ data_ptr);

I expect to work as:
type_to_cast *casted = dynamic_cast<ty pe_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(nointerfa ce,
bad_typeid) = 0;

a trivial implementation is:

void* CMyImpl::cast_t o(const type_info &ti)
{
if(ti == typeid(IIntefac e1*))
return static_cast<IIn terface1*>(this );
if(ti == typeid(IIntefac e2*))
return static_cast<IIn terface2*>(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_t o(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<vo id*>() !!

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::Query Interface() 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_t o(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_t o(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.co m> wrote in message news:<sl******* ***********@pan ix2.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_t o(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_t o(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************ ************@po sting.google.co m>, Jordi Vilar wrote:
Donovan Rebbechi <ab***@aol.co m> wrote in message news:<sl******* ***********@pan ix2.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
1169
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 test values from text boxes using isNaN() and also checking if they are still the same after parseInt or parseFloat are performed on them. The second test is because isNaN() will return false (is a number) for the string "3a". The curious...
11
4229
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 environment which should look like Delphi / Kylix, Borland's C++ builder or Allegro Common Lisp. I have found a plug-in named "Visual Python" and this name naturally maked me happy. But is it really "Visual" and does it provide a WYSIWYG rapid development...
1
1936
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 DW logical/physical model. He pointed out that the work and risk of making a database change will be reduced by using the "base view". Since this base view is just a selection of all user table's columns, as DBA, I don't see any reasons for...
7
1975
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 http://video.google.com/videoplay?docid=-7750532340306101329 There is no mention of this building in 911 Omission Report. Can fire make a building come crashing down at free fall speed?
4
4527
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 child apps of an IIS application and can be used by multiple users during the application life cycle and for multiple page loads for the same or different page under a root application. What I don't understand and need to know is whether that...
0
8680
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9169
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9030
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7738
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6528
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2335
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.