成员变量不同"kinds"的区别
Distinction between different "kinds" of member variables
我正在解析大型 XML 定义文件。
为了实现这一点,我已经开始为 xml 文件中可能遇到的每个 "type" 制作 classes。
<Element Factor="10">Whatever</Element>
目前,我将 XML 文件中的每个 type
放入它自己的 class 中,并带有一些运算符和该类型的成员。
但是,其中一些 types
同时具有 "child elements" 和 "attributes"。
即:
<MySimpleType min="20" max="100">
<name>MyName</name>
<SomeOtherElement>xxxx</SomeOtherElement>
</Mysimpletype>
目前 class 看起来像这样:
class MySimpleType{
std::string MyName;
TSomeOtherElement SomeOtherElement;
int min;
int max;
}
我想知道是否有一种方法可以注释 class 成员,以便清楚地知道所讨论的成员是属性还是元素。
元素可以有不同的类型(可能是另一种自定义 class),属性大多是内置的或 ADT 的。
有没有办法清楚地将成员标记为 'element' 或 'attribute'?
(如果有关系,我使用的是 Visual Studio 2015 和内置编译器,所以我使用的是 c++11)
您可能有一些包装类型来帮助识别:
template <typename T>
struct attribute { T value; };
等等
struct MySimpleType
{
std::string MyName;
TSomeOtherElement SomeOtherElement;
attribute<int> min;
attribute<int> max;
};
您可以丰富包装器 class,使其表现得更像基础类型(operator T&()
、operator = (const T&)
、...)
您可以使用 c++11 属性来标记您的字段:
class MySimpleType {
[[fmashiro::element]] std::string MyName;
[[fmashiro::element]] TSomeOtherElement SomeOtherElement;
[[fmashiro::attribute]] int min;
[[fmashiro::attribute]] int max;
};
虽然这会生成关于未知属性的警告。
另一种常见的方法是使用空宏:
#define FM_ELEMENT
#define FM_ATTRIBUTE
class MySimpleType{
FM_ELEMENT std::string MyName;
FM_ELEMENT TSomeOtherElement SomeOtherElement;
FM_ATTRIBUTE int min;
FM_ATTRIBUTE int max;
};
作为最后一个选项,只需在声明中添加 /* attribute */
注释。
我正在解析大型 XML 定义文件。 为了实现这一点,我已经开始为 xml 文件中可能遇到的每个 "type" 制作 classes。
<Element Factor="10">Whatever</Element>
目前,我将 XML 文件中的每个 type
放入它自己的 class 中,并带有一些运算符和该类型的成员。
但是,其中一些 types
同时具有 "child elements" 和 "attributes"。
即:
<MySimpleType min="20" max="100">
<name>MyName</name>
<SomeOtherElement>xxxx</SomeOtherElement>
</Mysimpletype>
目前 class 看起来像这样:
class MySimpleType{
std::string MyName;
TSomeOtherElement SomeOtherElement;
int min;
int max;
}
我想知道是否有一种方法可以注释 class 成员,以便清楚地知道所讨论的成员是属性还是元素。
元素可以有不同的类型(可能是另一种自定义 class),属性大多是内置的或 ADT 的。
有没有办法清楚地将成员标记为 'element' 或 'attribute'?
(如果有关系,我使用的是 Visual Studio 2015 和内置编译器,所以我使用的是 c++11)
您可能有一些包装类型来帮助识别:
template <typename T>
struct attribute { T value; };
等等
struct MySimpleType
{
std::string MyName;
TSomeOtherElement SomeOtherElement;
attribute<int> min;
attribute<int> max;
};
您可以丰富包装器 class,使其表现得更像基础类型(operator T&()
、operator = (const T&)
、...)
您可以使用 c++11 属性来标记您的字段:
class MySimpleType {
[[fmashiro::element]] std::string MyName;
[[fmashiro::element]] TSomeOtherElement SomeOtherElement;
[[fmashiro::attribute]] int min;
[[fmashiro::attribute]] int max;
};
虽然这会生成关于未知属性的警告。
另一种常见的方法是使用空宏:
#define FM_ELEMENT
#define FM_ATTRIBUTE
class MySimpleType{
FM_ELEMENT std::string MyName;
FM_ELEMENT TSomeOtherElement SomeOtherElement;
FM_ATTRIBUTE int min;
FM_ATTRIBUTE int max;
};
作为最后一个选项,只需在声明中添加 /* attribute */
注释。