Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   does rtti of gcc support "get object by name at runtime" ? (http://www.velocityreviews.com/forums/t459867-does-rtti-of-gcc-support-get-object-by-name-at-runtime.html)

William 01-17-2007 02:31 AM

does rtti of gcc support "get object by name at runtime" ?
 

for example, I have a global object:
extern Object myobj;

Can gcc get this object by using string "myobj" at runtime??
I know C++ rtti doesnt support this,
but I dont know if gcc can ,

thanks in advance.

Victor Bazarov 01-17-2007 04:26 AM

Re: does rtti of gcc support "get object by name at runtime" ?
 
William wrote:
> for example, I have a global object:
> extern Object myobj;
>
> Can gcc get this object by using string "myobj" at runtime??
> I know C++ rtti doesnt support this,
> but I dont know if gcc can ,


Please ask in 'gnu.g++.help'. Compiler-specific questions are
off topic in c.l.c++.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask



Dizzy 01-17-2007 10:38 AM

Re: does rtti of gcc support "get object by name at runtime" ?
 
William wrote:

>
> for example, I have a global object:
> extern Object myobj;
>
> Can gcc get this object by using string "myobj" at runtime??
> I know C++ rtti doesnt support this,
> but I dont know if gcc can ,


Can't you implement a system that registers objects and their types (thus
calling something like TypeRegistry.registerType(myobj, "myobj")) and then
at runtime query for it ? (obviously you will need to know/presume the type
when you "get it", but C++ RTTI can actually help you to at least throw an
exception if the type presumed to get does not match the type registered).
A solution like this at least should be ISO C++.

I think you should detail some more exactly what are you trying to do (ex.
if you try to load a dynamic library and lookup for symbols in it there is
specific platform dependent API for this).

--
Dizzy
http://dizzy.roedu.net


=?iso-8859-1?q?Erik_Wikstr=F6m?= 01-17-2007 11:00 AM

Re: does rtti of gcc support "get object by name at runtime" ?
 
On Jan 17, 11:38 am, Dizzy <d...@roedu.net> wrote:
> William wrote:
>
> > for example, I have a global object:
> > extern Object myobj;

>
> > Can gcc get this object by using string "myobj" at runtime??
> > I know C++ rtti doesnt support this,
> > but I dont know if gcc can ,

>
> Can't you implement a system that registers objects and their types (thus
> calling something like TypeRegistry.registerType(myobj, "myobj")) and then
> at runtime query for it ? (obviously you will need to know/presume the type
> when you "get it", but C++ RTTI can actually help you to at least throw an
> exception if the type presumed to get does not match the type registered).
> A solution like this at least should be ISO C++.


How? I'm not saying that it's impossible but I don't know how, and if
it's possible I'd like to know how. My problem is that I can't figure
out how to store the name/type register, all data-structures I know of
can only store instances of objects not a type itself.

--
Erik Wikström


Dizzy 01-17-2007 11:46 AM

Re: does rtti of gcc support "get object by name at runtime" ?
 
Erik Wikström wrote:

> On Jan 17, 11:38 am, Dizzy <d...@roedu.net> wrote:
>> William wrote:
>>
>> > for example, I have a global object:
>> > extern Object myobj;

>>
>> > Can gcc get this object by using string "myobj" at runtime??
>> > I know C++ rtti doesnt support this,
>> > but I dont know if gcc can ,

>>
>> Can't you implement a system that registers objects and their types (thus
>> calling something like TypeRegistry.registerType(myobj, "myobj")) and
>> then at runtime query for it ? (obviously you will need to know/presume
>> the type when you "get it", but C++ RTTI can actually help you to at
>> least throw an exception if the type presumed to get does not match the
>> type registered). A solution like this at least should be ISO C++.

>
> How? I'm not saying that it's impossible but I don't know how, and if
> it's possible I'd like to know how. My problem is that I can't figure
> out how to store the name/type register, all data-structures I know of
> can only store instances of objects not a type itself.


You don't need to store the "type" itself (that's abstract information
available at compile time and I already said that when you query/get the
object you will have to presume/know the type to get it) but store some
RTTI (typeid().name) string to help you at runtime at least throw if you
are trying to read it back as another type (so it's runtime type safe).

Example:

template<typename T>
void
registerObject(T& obj, std::string const& name) {
someMap.insert(std::make_pair(name, Object(obj)));
}

template<typename T>
T&
get(std::string const& name) {
SomeMap::const_iterator cit(someMap.find(name));
if (name == someMap.end())
throw // type not found
return cit->get<T>();
}

class Object
{
template<typename T>
explicit Object(T& obj)
: m_ptr(&obj), m_rtti(typeid(T).name) {}

template<typename T>
T& get() const {
if (m_rtti != typeid(T).name)
throw std::bad_cast;
return *static_cast<T*>(m_ptr);
}

private:
void* m_ptr;
std::string const m_rtti;
};

I have not tried to compile the code but I know it the principle behind it
works because I used something similar for some serialization code. Also
note that when using get the called must presume the type as in:
SomeType& obj(get<SomeType>("SomeType"));

But at least if the type presumed is wrong you get an exception. Obviously
this cannot be solved at compile time if you decide at runtime what
types/objects to register and for what names.

--
Dizzy
http://dizzy.roedu.net


William 01-18-2007 01:37 AM

Re: does rtti of gcc support "get object by name at runtime" ?
 
On Wed, 17 Jan 2007 19:46:44 +0800, Dizzy <dizzy@roedu.net> wrote:

> Erik Wikström wrote:
>
>> On Jan 17, 11:38 am, Dizzy <d...@roedu.net> wrote:
>>> William wrote:
>>>
>>> > for example, I have a global object:
>>> > extern Object myobj;
>>>
>>> > Can gcc get this object by using string "myobj" at runtime??
>>> > I know C++ rtti doesnt support this,
>>> > but I dont know if gcc can ,
>>>
>>> Can't you implement a system that registers objects and their types
>>> (thus
>>> calling something like TypeRegistry.registerType(myobj, "myobj")) and
>>> then at runtime query for it ? (obviously you will need to know/presume
>>> the type when you "get it", but C++ RTTI can actually help you to at
>>> least throw an exception if the type presumed to get does not match the
>>> type registered). A solution like this at least should be ISO C++.

>>
>> How? I'm not saying that it's impossible but I don't know how, and if
>> it's possible I'd like to know how. My problem is that I can't figure
>> out how to store the name/type register, all data-structures I know of
>> can only store instances of objects not a type itself.

>
> You don't need to store the "type" itself (that's abstract information
> available at compile time and I already said that when you query/get the
> object you will have to presume/know the type to get it) but store some
> RTTI (typeid().name) string to help you at runtime at least throw if you
> are trying to read it back as another type (so it's runtime type safe).
>
> Example:
>
> template<typename T>
> void
> registerObject(T& obj, std::string const& name) {
> someMap.insert(std::make_pair(name, Object(obj)));
> }
>
> template<typename T>
> T&
> get(std::string const& name) {
> SomeMap::const_iterator cit(someMap.find(name));
> if (name == someMap.end())
> throw // type not found
> return cit->get<T>();
> }
>
> class Object
> {
> template<typename T>
> explicit Object(T& obj)
> : m_ptr(&obj), m_rtti(typeid(T).name) {}
>
> template<typename T>
> T& get() const {
> if (m_rtti != typeid(T).name)
> throw std::bad_cast;
> return *static_cast<T*>(m_ptr);
> }
>
> private:
> void* m_ptr;
> std::string const m_rtti;
> };
>
> I have not tried to compile the code but I know it the principle behind
> it
> works because I used something similar for some serialization code. Also
> note that when using get the called must presume the type as in:
> SomeType& obj(get<SomeType>("SomeType"));
>
> But at least if the type presumed is wrong you get an exception.
> Obviously
> this cannot be solved at compile time if you decide at runtime what
> types/objects to register and for what names.
>



in fact, i have a static lib that contains lots of global variables,
and i want to get them only by variable name.
anyway, your method gives me a way , thanks.





William 01-18-2007 01:40 AM

Re: does rtti of gcc support "get object by name at runtime" ?
 
On Wed, 17 Jan 2007 12:26:52 +0800, Victor Bazarov
<v.Abazarov@comAcast.net> wrote:

> William wrote:
>> for example, I have a global object:
>> extern Object myobj;
>>
>> Can gcc get this object by using string "myobj" at runtime??
>> I know C++ rtti doesnt support this,
>> but I dont know if gcc can ,

>
> Please ask in 'gnu.g++.help'. Compiler-specific questions are
> off topic in c.l.c++.
>
> V


OK, I'll post in that group.



All times are GMT. The time now is 09:20 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.