Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Runtime type checking help.

Runtime type checking help.

Scheduled Pinned Locked Moved C / C++ / MFC
helpregexjsontutorialquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rostfrei
    wrote on last edited by
    #1

    Greetings! I know it's a long shot and please don't mock me if I missed it completely. Suppose I get some config file called objects.txt. It includes lines like: phonenum|1003 colour|red vehicle|truck|mack first string on the line is name of object type and all the rest are parameters. In my program I would like to read file objects.txt line by line and do the following: if "I know-type" phonenum { new phonenum(1003); } else if "I know-type" colour{ new colour(red); } else if "I know-type" vehicle{ new vehicle(truck, mack); } Is that even possible? To check the type at run-time? Before you dissmiss me complitely I'd like to explain what I would like to achieve. Maybe you will know some other solution to the same problem. I'd like to make some kind of save/load, serialize/deserialize framework. It would provide capability to store and load user defined objects. All objects would be serialized and stored in one file (configuration for the whole user defined system). Framework would gather ojects trough some common interface addObj(abstractObj obj*); every user defined object would have to implement methods char* serialize(void); void deserialize(char*); when framework is asked to save objects, it would call "serialize" method of all objects it contains and save them line by line. When framework is asked to load configuration he would read the file line by line and read the type of object and create it, than call its "deserialize" method with the rest of the line characters. I already used the serialize/deserialize pattern before. But in that particular implementation I knew all the types compile time. For example: if (line == "phonenum"){ new phonenum(1002); } ... here I already know what types I expect. I'd like to do implementation when I don't already know all the names infront at compile time. I'd like to read the "name" run-time and check if I know it. This way I'd really have true framework which should not know any user defined types at compile time. Best regards, Rostfrei

    N M 2 Replies Last reply
    0
    • R Rostfrei

      Greetings! I know it's a long shot and please don't mock me if I missed it completely. Suppose I get some config file called objects.txt. It includes lines like: phonenum|1003 colour|red vehicle|truck|mack first string on the line is name of object type and all the rest are parameters. In my program I would like to read file objects.txt line by line and do the following: if "I know-type" phonenum { new phonenum(1003); } else if "I know-type" colour{ new colour(red); } else if "I know-type" vehicle{ new vehicle(truck, mack); } Is that even possible? To check the type at run-time? Before you dissmiss me complitely I'd like to explain what I would like to achieve. Maybe you will know some other solution to the same problem. I'd like to make some kind of save/load, serialize/deserialize framework. It would provide capability to store and load user defined objects. All objects would be serialized and stored in one file (configuration for the whole user defined system). Framework would gather ojects trough some common interface addObj(abstractObj obj*); every user defined object would have to implement methods char* serialize(void); void deserialize(char*); when framework is asked to save objects, it would call "serialize" method of all objects it contains and save them line by line. When framework is asked to load configuration he would read the file line by line and read the type of object and create it, than call its "deserialize" method with the rest of the line characters. I already used the serialize/deserialize pattern before. But in that particular implementation I knew all the types compile time. For example: if (line == "phonenum"){ new phonenum(1002); } ... here I already know what types I expect. I'd like to do implementation when I don't already know all the names infront at compile time. I'd like to read the "name" run-time and check if I know it. This way I'd really have true framework which should not know any user defined types at compile time. Best regards, Rostfrei

      N Offline
      N Offline
      Nibu babu thomas
      wrote on last edited by
      #2

      Will typeid help you? Code sample from MSDN:

      class Base {
      public:
      virtual void vvfunc() {}
      };

      class Derived : public Base {};

      using namespace std;
      int main()
      {
      Derived* pd = new Derived;
      Base* pb = pd;
      Base b;
      cout << typeid( b ).name() << endl; //prints "class Base *"
      cout << typeid( pb ).name() << endl; //prints "class Base *"
      cout << typeid( pb ).name() << endl; //prints "class Derived"
      cout << typeid( pd ).name() << endl; //prints "class Derived *"
      cout << typeid( pd ).name() << endl; //prints "class Derived"
      delete pd;
      }


      Nibu thomas Software Developer

      R 1 Reply Last reply
      0
      • R Rostfrei

        Greetings! I know it's a long shot and please don't mock me if I missed it completely. Suppose I get some config file called objects.txt. It includes lines like: phonenum|1003 colour|red vehicle|truck|mack first string on the line is name of object type and all the rest are parameters. In my program I would like to read file objects.txt line by line and do the following: if "I know-type" phonenum { new phonenum(1003); } else if "I know-type" colour{ new colour(red); } else if "I know-type" vehicle{ new vehicle(truck, mack); } Is that even possible? To check the type at run-time? Before you dissmiss me complitely I'd like to explain what I would like to achieve. Maybe you will know some other solution to the same problem. I'd like to make some kind of save/load, serialize/deserialize framework. It would provide capability to store and load user defined objects. All objects would be serialized and stored in one file (configuration for the whole user defined system). Framework would gather ojects trough some common interface addObj(abstractObj obj*); every user defined object would have to implement methods char* serialize(void); void deserialize(char*); when framework is asked to save objects, it would call "serialize" method of all objects it contains and save them line by line. When framework is asked to load configuration he would read the file line by line and read the type of object and create it, than call its "deserialize" method with the rest of the line characters. I already used the serialize/deserialize pattern before. But in that particular implementation I knew all the types compile time. For example: if (line == "phonenum"){ new phonenum(1002); } ... here I already know what types I expect. I'd like to do implementation when I don't already know all the names infront at compile time. I'd like to read the "name" run-time and check if I know it. This way I'd really have true framework which should not know any user defined types at compile time. Best regards, Rostfrei

        M Offline
        M Offline
        Monty2
        wrote on last edited by
        #3

        yes this can be done using RTTI, what framework are you using?


        C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg

        R 1 Reply Last reply
        0
        • M Monty2

          yes this can be done using RTTI, what framework are you using?


          C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg

          R Offline
          R Offline
          Rostfrei
          wrote on last edited by
          #4

          What do you mean by what framework am I using? You mean developement environment/language? I use VC++ .NET 2003 and also always port the code to gcc for ARM. Rostfrei

          1 Reply Last reply
          0
          • N Nibu babu thomas

            Will typeid help you? Code sample from MSDN:

            class Base {
            public:
            virtual void vvfunc() {}
            };

            class Derived : public Base {};

            using namespace std;
            int main()
            {
            Derived* pd = new Derived;
            Base* pb = pd;
            Base b;
            cout << typeid( b ).name() << endl; //prints "class Base *"
            cout << typeid( pb ).name() << endl; //prints "class Base *"
            cout << typeid( pb ).name() << endl; //prints "class Derived"
            cout << typeid( pd ).name() << endl; //prints "class Derived *"
            cout << typeid( pd ).name() << endl; //prints "class Derived"
            delete pd;
            }


            Nibu thomas Software Developer

            R Offline
            R Offline
            Rostfrei
            wrote on last edited by
            #5

            the problem here is in "new Derived;". When I would do that I would already have to know which derived class it is. Rostfrei

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups