繁体   English   中英

对字符串变量使用枚举

[英]Using enum with string variables

我正在尝试做这样的事情,

enum Data_Property
{
 NAME,
 DATETIME,
 TYPE,
};

struct Item_properties
{
  char* Name;
  char* DateTime;
  int Type;  
};

int main() {  
std::string data = "/Name Item_Name /DATETIME [TimeStamp] /TYPE [Integer value]";
std::list <std::string> known_properties;

known_properties.push_back("/Name");
known_properties.push_back("/DATETIME");
known_properties.push_back("/TYPE");

Item_properties item_p = extract_properties(data); //I use a function to get properties
                                                   //in a structure.

//here I want to use a switch-case statement to get the property by enum value
}

我需要知道有什么方法可以使它变得简单吗? 还是我如何将(/ NAME / DATETIME / TYPE)之类的属性键与枚举结合起来,并避免使用std :: list即known_properties?

据我所知,C ++枚举中没有像C#枚举属性那样的东西。 我可能是错的。

鉴于此,我建议您使用地图将data属性映射到item属性。 您只需要构建一次此地图。 您还可以将字符串表示形式与项目和数据属性相关联。 这应该使构建项目属性对象更加容易。 我认为列表不是完成此任务的理想数据结构。

首先,让我说, 总有另一种选择 ,这是我的一种哲学。

让我们看一看大图 您正在尝试从字符串读取项目 项目是基准的容器。 用面向对象的术语来说, Item希望每个数据从字符串中加载其自己的数据。 项目不需要知道如何加载数据,因为它可以从每个数据中请求该数据。

实际上,您的基准面比C ++简单POD类型更为复杂和智能。 (简单的POD类型没有字段名称。)因此,您需要创建类来表示它们(或封装额外的复杂性)。 相信我,从长远来看,这会更好。

这是一个重构 Name成员的简单示例:

struct Name_Field
{
  std::string  value;  // Change your habits to prefer std:string to char *
  void load_from(const std::string& text,
                 const std::string::size_type& starting_position = 0)
  {
     value.clear();
     std::string::size_type position = 0;
     position = text.find("/Name", starting_position);
     if (position != std::string::npos) // the field name was found.
     {
         // Skip the next space
         ++position;

         // Check for end of text
         if (position < text.length())
         {
             std::string::size_t   end_position;
             end_position = text.find_first_not_of(" ", position);
             if (end_position != std::string::npos)
             {
                 value = text.substr(position, end_position - position);
             }
          }
      }
};

您现在可以将load_from添加到Item类:

struct Item
{
    Name_Field   Name;
    char *       DateTime;
    char *       Type;
    void load_from(const std::string& text)
    {
        std::string::size_t  position = 0;

        // Notice, the Name member is responsible for load its data,
        //    relieving Item class from knowing how to do it.
        Name.load_from(text, position);
    }
};

加载项目:

Item new_item;
new_item.load_from(data);

重构时,请注意常用方法。 例如,您可能希望将Name_FieldDateTime_Field之间的通用方法放入基类(父类) Field_Base 这将简化您的代码和设计,并支持可重用性。

您是否看过Boost :: program_options? 这是一些示例代码:

#include "boost\program_options\parsers.hpp"
#include "boost\program_options\variables_map.hpp"
#include <iostream>
using std::cout;
using std::endl;

namespace po = boost::program_options;

int _tmain(int argc, _TCHAR* argv[])
{

    po::options_description desc("Allowed options");
    string taskName;
    desc.add_options()
        ("help", "produce help message")
        ("TaskName", po::value<string>(&taskName), "The project's prefix")
    ;

    po::variables_map vm;
    po::store(po::parse_command_line(argc, argv, desc), vm);
    po::notify(vm);    

    if (vm.count("help")) {
        cout << desc << endl;
        return 1;
    }
    if (!vm.count("TaskName"))
    {
        cout << "Use \"DOTaskTests.exe --help\" for command line options" << endl;
        return 1;
    }
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM