C++ enum优雅的实现

本文介绍了一种在C++中将枚举类型与字符串进行相互转换的方法,通过定义模板结构体`enumStrings`和相关操作符重载实现。示例中展示了如何为`ReturnCodes`枚举定义字符串映射,并提供了使用示例,如从输入流读取枚举值和将枚举值输出到输出流。
摘要由CSDN通过智能技术生成

 

enum到string相互转换,是必须而经常的

enum_string.h

#pragma once

#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>

// This is the type that will hold all the strings.
// Each enumeration type will declare its own specialization.
// Any enum that does not have a specialization will generate a compiler error
// indicating that there is no definition of this variable (as there should be
// be no definition of a generic version).
template<typename T>
struct enumStrings
{
    static char const* data[];
};

// This is a utility type.
// Created automatically. Should not be used directly.
template<typename T>
struct enumRefHolder
{
    T& enumVal;
    enumRefHolder(T& enumVal) : enumVal(enumVal) {}
};
template<typename T>
struct enumConstRefHolder
{
    T const& enumVal;
    enumConstRefHolder(T const& enumVal) : enumVal(enumVal) {}
};

// The next two functions do the actual work of reading/writing an
// enum as a string.
template<typename T>
std::ostream& operator<<(std::ostream& str, enumConstRefHolder<T> const& data)
{
    return str << enumStrings<T>::data[data.enumVal];
}

template<typename T>
std::istream& operator>>(std::istream& str, enumRefHolder<T> const& data)
{
    std::string value;
    str >> value;

    // These two can be made easier to read in C++11
    // using std::begin() and std::end()
    //  
    static auto begin = std::begin(enumStrings<T>::data);
    static auto end = std::end(enumStrings<T>::data);

    auto find = std::find(begin, end, value);
    if (find != end)
    {
        data.enumVal = static_cast<T>(std::distance(begin, find));
    }
    return str;
}


// This is the public interface:
// use the ability of function to deduce their template type without
// being explicitly told to create the correct type of enumRefHolder<T>

template<typename T>
enumConstRefHolder<T>  enumToString(T const& e) { return enumConstRefHolder<T>(e); }

template<typename T>
enumRefHolder<T>       enumFromString(T& e) { return enumRefHolder<T>(e); }

/*
// Define Enum Like this
enum X {Hi, Lo};
// Then you just need to define their string values.
template<> char const* enumStrings<X>::data[] = {"Hi", "Lo"};

X   a=Hi;

std::cout << enumToString(a) << "\n";

std::stringstream line("Lo");
line >> enumFromString(a);

std::cout << "A: " << a << " : " << enumToString(a) << "\n";

*/

common.h

#pragma once
#include "enum_string.h"
enum ReturnCodes 
{
    OK = 0,
    ERROR,
    ERROR_FILE_NOT_FOUND,
    ERROR_FAILED_TO_CREATE_PATH,
};

template<> char const* enumStrings<ReturnCodes>::data[] =
{
    "OK",
    "ERROR",
    "ERROR_FILE_NOT_FOUND",
    "ERROR_FAILED_TO_CREATE_PATH",
};

 

            return_code = std::min(std::max(return_code, (int)ReturnCodes::OK), (int)ReturnCodes::ERROR_UNKNOWN);
            std::string str(enumStrings<ReturnCodes>::data[return_code]);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值