扩展枚举类型 [英] Extending enum types

查看:127
本文介绍了扩展枚举类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

在C ++中扩展枚举?

它自己的一组错误作为枚举类型。

I am using a library which defines its own set of errors as an enum type.


枚举error_type {...}

enum error_type {...}

库还有一个函数,它接受该枚举类型,并输出错误。

The library also has a function which takes that enum type, and prints the error.


void set_status(error_type new_error)

void set_status(error_type new_error)

如果我想定义自己的错误并赋予set_status函数,

If I want to define my own error, and give it to the set_status function, is it possible to extend the error_type enum somehow, or maybe override it?

推荐答案

可能有一些尖锐的边缘,但我认为这应该工作为您:

There might be some sharp edges, but I think this should work for you:

#include<iostream>

// Helper struct

template<typename T, int N, typename... LIST>
struct whereInType {
  static const int index = -1;
};

template<typename T, int N, typename HEAD, typename... TAIL>
struct whereInType<T,N,HEAD,TAIL...> {
  static const int index = whereInType<T, N+1, TAIL...>::index;
};

template<typename T, int N, typename... TAIL>
struct whereInType<T,N,T,TAIL...> {
  static const int index = N;
};

// The actual union type

template<typename... ENUMS>
class enum_union {
public:
  template<typename ENUM>
  constexpr enum_union(ENUM val) :
    which_enum(whereInType<ENUM,0,ENUMS...>::index),
    value(val) {}
  constexpr operator long long(){
    return static_cast<long long>(which_enum)<<32 | value;
  }
  template<typename ENUM, int IGNORE=0>
  constexpr bool operator==(const ENUM& e) {
    return *this == enum_union<ENUMS...>(e);
  }
  template<int IGNORE=0>
  constexpr bool operator==(const enum_union<ENUMS...>& oth) {
    return which_enum==oth.which_enum && value==oth.value;
  }
  template<typename T>
  constexpr bool operator!=(const T& oth) {
    return !(*this == oth);
  }
private:
  int which_enum;
  int value;
};

// An example usage

enum normal_errors {
  E_OUTOFMEMORY,
  E_IOERROR
};

enum weird_errors {
  E_OUTOFAARDVARKS,
  E_DIVIDEBYCUCUMBER
};

typedef enum_union<normal_errors, weird_errors> any_error;

// Some tests

void print(any_error e) {
  switch(e) {
  case any_error(E_OUTOFMEMORY):
    std::cout << "Out of Memory\n";
    break;
  case any_error(E_IOERROR):
    std::cout << "I/O Error\n";
    break;
  case any_error(E_OUTOFAARDVARKS):
    std::cout << "WE NEED AARDVARKS!!!  NOW!!!!!\n";
    break;
  case any_error(E_DIVIDEBYCUCUMBER):
    std::cout << "please reinstall universe\n";
    break;
  }
}  

main(){
  print(E_OUTOFMEMORY);
  print(E_IOERROR);
  print(E_OUTOFAARDVARKS);
  print(E_DIVIDEBYCUCUMBER);
  if (any_error(E_OUTOFMEMORY) == E_OUTOFAARDVARKS) {
    std::cout<<"bad\n";
  }else{
    std::cout<<"good\n";
  }
  if (any_error(E_OUTOFMEMORY) != E_OUTOFAARDVARKS) {
    std::cout<<"good\n";
  }else{
    std::cout<<"bad\n";
  }
  if (any_error(E_OUTOFMEMORY) == E_OUTOFMEMORY) {
    std::cout<<"good\n";
  }else{
    std::cout<<"bad\n";
  }
  if (any_error(E_OUTOFMEMORY) != E_OUTOFMEMORY) {
    std::cout<<"bad\n";
  }else{
    std::cout<<"good\n";
  }
}

这篇关于扩展枚举类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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