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

Discussion in 'C++' started by William, Jan 17, 2007.

  1. William

    William Guest

    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.
     
    William, Jan 17, 2007
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Jan 17, 2007
    #2
    1. Advertising

  3. William

    Dizzy Guest

    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
     
    Dizzy, Jan 17, 2007
    #3
  4. On Jan 17, 11:38 am, Dizzy <> 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
     
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Jan 17, 2007
    #4
  5. William

    Dizzy Guest

    Erik Wikström wrote:

    > On Jan 17, 11:38 am, Dizzy <> 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
     
    Dizzy, Jan 17, 2007
    #5
  6. William

    William Guest

    On Wed, 17 Jan 2007 19:46:44 +0800, Dizzy <> wrote:

    > Erik Wikström wrote:
    >
    >> On Jan 17, 11:38 am, Dizzy <> 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, Jan 18, 2007
    #6
  7. William

    William Guest

    On Wed, 17 Jan 2007 12:26:52 +0800, Victor Bazarov
    <> 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.
     
    William, Jan 18, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    8
    Views:
    464
  2. Kevin P. Fleming

    C99 structure initialization in gcc-2.95.3 vs gcc-3.3.1

    Kevin P. Fleming, Nov 6, 2003, in forum: C Programming
    Replies:
    2
    Views:
    682
    Kevin P. Fleming
    Nov 6, 2003
  3. Replies:
    5
    Views:
    391
    Nathan Addy
    Sep 17, 2005
  4. ashnin

    GCC 3.4.3 and GCC 4.1.2

    ashnin, Jul 7, 2008, in forum: C++
    Replies:
    1
    Views:
    589
    Michael DOUBEZ
    Jul 7, 2008
  5. kas
    Replies:
    1
    Views:
    344
    red floyd
    Apr 22, 2010
Loading...

Share This Page