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

W

William

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.
 
V

Victor Bazarov

William said:
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
 
D

Dizzy

William said:
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).
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

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.
 
D

Dizzy

Erik said:
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.
 
W

William

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top