Variadic matrix without __VA_ARGS__

So this is basically what I want to do:

#define RS03(obj, a1, a2, a3) {if (_str1 == #a1) _file >> _##a1; if (_str1 == #a2) _file >> _##a2;if (_str1 == #a3) _file >> _##a3; obj (_##a1, _##a2, _##a3);}

These are three arguments, but I also need:

#define RS04(obj, a1, a2, a3, a4)
#define RS05(obj, a1, a2, a3, a4, a5)
#define RS06(obj, a1, a2, a3, a4, a5, a6)
...

So, a variable macro.

Stackoverflow has a number of questions on this topic, but they do not apply to my case.

In the above code, the three parameters a1, a2 and a3 are used both as a string (in the "if" condition) and as a variable (in assignments and in the constructor), and obj is a class (therefore, the Last command in the macro is a constructor call).

The fact is that: suppose I have twenty different classes, each requiring a different amount of input to build.

The macro gets the class name and argument names needed to build an object of this class.

, (. " " ). , "". ( # ## ) .

, ( , ), .


, , (, 1 , 2 , 3 bool ..).

"run", .

:

1 -

2 - ,

:

car {
    name = model1
    speed = 0.05
}

man {
    name = model2
    speed = 0.03
    male = true
    ageclass = 3
}

...

, .

, .

, , :

car {
    name = pippo
    speed = 0.05
    speed = 0.06
    speed = 0.07
}

( )

, .

(, 4 ).

, :

car {
    name = pippo
    speed = 0.05
}

car {
    name = pluto
}

, .

, , .

:

1 - ( "" ) T ( ) bool ( , )

2 - (, , ), "", (_name, _speed, _male ..)

3 - , , , ( "" ), "=" , , .

( " = 0,03", 0.03 _name ).

( erros, ):

if (car) {
    while (str != "}") {
        if (str == "speed")    { _file >> _speed;     _file >> _str; }
        if (str == "male")     { _file >> _male;      _file >> _str; }
        if (str == "ageclass") { _file >> _ageclass;  _file >> _str; }
        ERROR;
    }
    car (_speed.get (), _male.get (), _ageclass.get ());
}

get() - - "field", , (.. ), ( ).

"" โ†’ , โ†’ true .

, : -)

+4
1

, , , . - .

Properies (), , .

struct Properties
{
    enum Types
    {
        MAN,
        CAR,
        // and more                                                                              
    };

    enum Gender
    {
        MALE, FEMALE
    };

    Types type;
    string name;
    double speed;
    Gender gender;
    int ageClass;
};

, enum, .

Base, , Man, Car ..

class Base
{};

class Man: public Base
{
    string d_name;
    Properties::Gender d_gender;
    int d_ageClass;
    double speed;

public:
    Man(Properties const &properties)
    {
        // Set properties that apply to the "Man"-class                                          
    }
};

class Car: public Base
{
    string d_name;
    double d_speed;

public:
    Car(Properties const &properties)
    {
        // Set properties that apply to the "Car"-class                                          
    }
};

Properties, , . , Car gender, Man .

Loader, . readFile, Base*, . shared_ptr , , .

class Loader
{
public:
    static vector<shared_ptr<Base>> readFile(string const &fileName)
    {
        vector< shared_ptr<Base> > result;
        ifstream file(fileName);
        // Parse the file, creating a "Properties" object, called                                
        // "props" here                                                                          

        while (file) // while EOF not reached.                                                   
        {
            Properties props = parse(file); // implement your parse                              
                                            // routine, returning Properties.                    
            switch (props.type)
            {
            case Properties::CAR:
                result.push_back(shared_ptr<Base>(new Car(props)));
                break;
            case Properties::MAN:
                result.push_back(shared_ptr<Base>(new Man(props)));
                break;
            // etc for all classes derived from Base                                             
            default:
                throw string("error: unknown type");
        }
    }
};

, .

0

Source: https://habr.com/ru/post/1525637/


All Articles