how to convert node to enumeration type when using yaml-cpp?

Viewed 48

I have a problem, how to convert node to enumeration types. Here is my yaml file named model.yaml. I'm building on Windows 7, with Visual Studio 2017 for win32. The yaml-cpp version is 0.7.0 latest.

# model.yaml
model_base:
    conf:
        -
            type: BLACK
            value: 0.1
        -
            type: YELLOW
            value: 0.2
        -
            type: BLUE
            value: 0.3
        -
            type: WHITE
            value: 0.4

And the key codes are as follows:

typedef enum color_type_tag {
  BLACK = 0,
  YELLOW,
  BLUE,
  WHITE,
  SUPPORT_COLOR_NUM
}color_type_t, *pcolor_type_t;

YAML::Node yaml_config = YAML::LoadFile("model.yaml");
color_type_t car_color[SUPPORT_COLOR_NUM];
YAML::Node conf = yaml_config["model_base"]["conf"];
auto i = 0;
for (const auto& it : conf)
{
    for (const auto& itt : it)
    {
        if(itt.first.as<std::string>() == "type")
        {
             // I want to parse the type, and get color_type_t like "BLACK"、“YELLOW”、“BLUE”、“WHITE” directly.
            car_color[i] = itt.second.as<color_type_t>();
        }
    }
    i++;
}

I want to parse the type, and get enumeration type "color_type_t" like "BLACK"、“YELLOW”、“BLUE”、“WHITE” directly using car_color[i] = itt.second.as<color_type_t>();, could someone give me some advice, thanks.

0 Answers
Related