How to create variables by using macros

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 2di
    New Member
    • Mar 2008
    • 4

    How to create variables by using macros

    Hi every one,
    I got this problem, i need to create classes in c++ by using macros.
    Thats what i want:

    #define createObject(ty pe) type a;

    main(){
    string className = "int";
    createObject(cl assName);
    }

    unfortunately it doesnt works with strings i can do it only if i will use actual data type:

    createObject(in t);
    _______________ _______________ _____
    currently i am using VS2005, well of course in theory i can use a lot of if statements or switch, but buy using this method i dont need to add code each time i want to add compatability for some new class.

    Thank you very much !
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    You could also use templates/generic programming. They are finicky, though.

    Comment

    • 2di
      New Member
      • Mar 2008
      • 4

      #3
      I dont think tempaltes will solve all my problems.
      I am sorry i didnt mentioned it at first time,
      there is another thing i need to do:
      beside just creating variables of different type, i need to access member variables of some classes by using strings.

      here is example of what i need:

      Code:
      class ClassA
      {
      public: 
      	ClassA(){}
      	~ClassA(){}
      	int positionX;
      	int positionY;
      };
      
      #define M_test(A)  myClass.A  = 10;
      
      
      int _tmain(int argc, _TCHAR* argv[])
      {
      
      	ClassA myClass;
      	M_test("positionX"); 
      
      	cout<<myClass.positionX;
      	system("PAUSE");
      	return 0;
      }
      M_test("positio nX"); -> working only in this way -> M_test(position X);
      but i have all my classes and member function/variables stored as a string, so i need some how to convert ("string") into (string) ;(

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Originally posted by 2di
        but i have all my classes and member function/variables stored as a string
        Why? Or may be more pertinently where did this data come from?

        If this is the result of some external generation process that you are having to import then I would suggest you need to create a type (class) that can do the work for you.

        I map of instances of the class will easily enable you to translate from "variable Name" to instance of variable. In turn the class could contain a map of a class representing the members of the class. This class could be a virtual base class and the actual types can be classes derived from this type.

        In this way you can map variable names in strings and member names in strings to actual instances of data.

        However I would only even think about/examine the possibilities of this approach if you are dealing with data generated by an external source that is like to change regularly.

        If you are not dealing with data of this type start by looking at how you may reformat the data in these strings.

        Oh and BTW any solution that relies on macros in C++ is almost certainly poorly designed in most cases.

        Comment

        • 2di
          New Member
          • Mar 2008
          • 4

          #5
          Quote:
          Originally Posted by 2di
          but i have all my classes and member function/variables stored as a string
          Why? Or may be more pertinently where did this data come from?
          Ok the story is, I writing my scripting language, At a moment langage is mostly completed, so i working on the c++ classes support inside of the scripting language, U create your classes in C++, and using them inside of ur script.

          I have special class ObjectContainer which can store pointers to different Objects
          It looks like that:
          Code:
          	void* _class;
          	ClassType _type;
          	string _name;
          	string _className;
          Virtual machine has list of ObjectContainer s, where each element represents some object which been created inside of the script;

          every time its executes script line like that:

          Code:
          player.positionX = 10;
          1)it searches ObjectContainer List for the "player" object
          2)casting void* p into PlayerClass*
          3)changing value to 10

          This is a code i have at a moment

          Code:
          void AssignToken(void* vClassContainer, string dataM, Token* token)
          	{
          		ClassContainer* container = reinterpret_cast<ClassContainer*>(vClassContainer);
          		if (container->_type == ct_Player)
          		{
          			Player* object = reinterpret_cast<Player*>(container->_class);
          			if (dataM == "positionX")
          			{
          				object->PositionX = token->intData();
          			}
          		}
          	}
          where:
          vClassContainer pointerto ObjectContainer ;
          dataM = "positionX"
          token = pointer to a token with value (10);
          this means that every time i am going to add new class, i need to write all this stupid if statements.

          And this is what i want:

          Code:
          #define M_cast(className)  className* object = reinterpret_cast<className*>(container->_class);
          #define M_assignToken(dataMemberName) object->dataMemberName = token->intData();
          
          void AssignToken(void* vClassContainer, string dataM, Token* token)
          	{
          
          		ClassContainer* container = reinterpret_cast<ClassContainer*>(vClassContainer);
          		string className = container->_className;
          
          		M_cast(vClassContainer->_className);
          		M_assignToken(dataM);
          	}
          Target of this scripting language is a AI engine, so this language has to be easily integrated. So all i am trying to do is to make integration of new classes as simple as possible.
          classes data, wont be genereated more than once, for one particular project.

          Thanks for the idea about maps, i am thinking about it at a moment.

          Comment

          • 2di
            New Member
            • Mar 2008
            • 4

            #6
            I map of instances of the class will easily enable you to translate from "variable Name" to instance of variable. In turn the class could contain a map of a class representing the members of the class. This class could be a virtual base class and the actual types can be classes derived from this type.
            could you please show me some code examples of this one ?

            Comment

            Working...