用于C ++代码生成宏替代 [英] Macro alternative for C++ code generation

查看:94
本文介绍了用于C ++代码生成宏替代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的设置模块有一些redondant代码:

#include <QSettings>

class MySettings
{
public:
    // param1
    void setParam1(QString param1) { _settings.setValue("param1", param1); }
    string param1() { return _settings.value("param1").toString(); }

    // param2
    void setParam2(int param2) { _settings.setValue("param2", param2); }
    int param2() { _settings.value("param2").toInt(); }

    // param3
    void setParam3(int param3) { _settings.setValue("param3", param3); }
    int param3() { _settings.value("param3").toInt(); }

private:
    QSettings _settings;
}

我设法减少了使用宏编写的代码量.以下是 QString 参数类型的示例:

#define INTSETTING(setter, getter) \
    void set##setter(QString getter) { settings.setValue(#getter, getter);} \
    QString getter() {return settings.value(#getter).toString();}

由于我使用的是C ++,所以我知道宏用法很糟糕,因此我正在寻找更清洁的替代方法.

我举了一个Qt示例(QString),但这是一个更普遍的问题.

,这使得上述类的定义更为简单:

class MySettings
{
public:
    STRINGSETTING(Param1, param1)
    INTSETTING(Param2, param2)
    INTSETTING(Param3, param3)

    STRINGSETTING(DefaultTitle, defaultTitle)
    INTSETTING(MaxDocCount, maxDocCount)

private:
    QSettings _settings;
}

解决方案

您可以在宗教的方式无论是回答这个问题,或者你可以回到旧的原则:如果它使你的代码更易读,做

有很多人以虔诚的方式回答这个问题,他们只是讨厌预处理器以及与此有关的一切,并禁止在其代码中使用它.

在另一方面,还有谁常规定义宏做重复任务的人,我曾多次这样做,最经常只是定义一个函数中使用宏(你可以定义的方式使用多在GNU-C的子功能).

我认为,人们对它的思考方式与人们对goto语句的思考方式非常相似:大多数人将其使用妖魔化,其他人则说它具有积极的用途,不应被视为本身就是邪恶的.您需要自己决定.

My settings module have some redondant code:

#include <QSettings>

class MySettings
{
public:
    // param1
    void setParam1(QString param1) { _settings.setValue("param1", param1); }
    string param1() { return _settings.value("param1").toString(); }

    // param2
    void setParam2(int param2) { _settings.setValue("param2", param2); }
    int param2() { _settings.value("param2").toInt(); }

    // param3
    void setParam3(int param3) { _settings.setValue("param3", param3); }
    int param3() { _settings.value("param3").toInt(); }

private:
    QSettings _settings;
}

I managed to reduce the code amout to write by using macro. Here the example for QString parameter type:

#define INTSETTING(setter, getter) \
    void set##setter(QString getter) { settings.setValue(#getter, getter);} \
    QString getter() {return settings.value(#getter).toString();}

Since I'm using C++ I know that macro usage is bad so I'm looking for a cleaner alternative.

I gave a Qt example (QString) but it is a more general question.

Edit:

Which make the definition of the above class much more simpler:

class MySettings
{
public:
    STRINGSETTING(Param1, param1)
    INTSETTING(Param2, param2)
    INTSETTING(Param3, param3)

    STRINGSETTING(DefaultTitle, defaultTitle)
    INTSETTING(MaxDocCount, maxDocCount)

private:
    QSettings _settings;
}

解决方案

You can either answer this in a religious fashion, or you can go back to the old principle: if it makes your code more readable, do it.

There are a lot of people who answer this in a religious way, they just hate the preprocessor and everything that's to do with it, and ban its use from their code.

On the other hand, there are people who routinely define macros to do repetitive task, I have done so on several occasions, most frequently just defining a macro for the use within a single function (used much in the way you can define subfunctions in GNU-C).

I think, the way people think about it is quite similar to the way people think about the goto statement: Most deamonize its use, others say it has its positive uses and should not be viewed as evil in itself. You need to decide this for yourself.

这篇关于用于C ++代码生成宏替代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆