如何使用枚举类作为一组标志? [英] How does one use an enum class as a set of flags?

查看:107
本文介绍了如何使用枚举类作为一组标志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一组标志和类:

  ///< summary>搜索文件。< / summary> 
enum class FindFilesOptions:unsigned char
{
LocalSearch = 0,
RecursiveSearch = 1,
IncludeDotDirectories = 2
}

class FindFiles:boost :: noncopyable
{
/ * ignored * /
public:
FindFiles(std :: wstring const& pattern,FindFilesOptions选项);
/ * omitted * /
}

能够选择多个选项:

  FindFiles句柄(Append(basicRootPath,L*),
FindFilesOptions :: RecursiveSearch | FindFilesOptions :: IncludeDotDirectories);

可以使用C ++ 11以非常类型的方式支持它<$ c $

(我知道调用者可以 static_cast 到底层类型和 static_cast 回,但我不希望调用者必须这样做)

解决方案

当然可以对位图使用枚举类 es。不幸的是,这样做有点痛苦:你需要在你的类型上定义必要的位操作。下面是一个示例如何看起来像。如果枚举类可以派生自可以存在于定义必要的操作符模板代码的合适命名空间中的其他类型,那将是很好的。

  #include  #include< type_traits> 

枚举类位图:unsigned char
{
a = 0x01,
b = 0x02,
c = 0x04
}

位图运算符& (位图x,位图y)
{
typedef std :: underlying_type< bitmap> :: type uchar;
return bitmap(uchar(x)& uchar(y));
}

位图操作符| (位图x,位图y)
{
typedef std :: underlying_type< bitmap> :: type uchar;
return bitmap(uchar(x)| uchar(y));
}

位图操作符^(位图x,位图y)
{
typedef std :: underlying_type< bitmap> :: type uchar;
return bitmap(uchar(x)^ uchar(y));
}

bool test(bitmap x)
{
return std :: underlying_type< bitmap> :: type(x);
}

int main()
{
bitmap v = bitmap :: a | bitmap :: b;
if(test(v& bitmap :: a)){
std :: cout< 一个 ;
}
if(test(v& bitmap :: b)){
std :: cout< b;
}
if(test(v& bitmap :: c)){
std :: cout< C ;
}
std :: cout<< '\\\
';
}


Let's say I have a set of flags and a class like this:

/// <summary>Options controlling a search for files.</summary>
enum class FindFilesOptions : unsigned char
{
    LocalSearch = 0,
    RecursiveSearch = 1,
    IncludeDotDirectories = 2
};

class FindFiles : boost::noncopyable
{
    /* omitted */
public:
    FindFiles(std::wstring const& pattern, FindFilesOptions options);
    /* omitted */
}

and I want a caller to be able to select more than one option:

FindFiles handle(Append(basicRootPath, L"*"),
    FindFilesOptions::RecursiveSearch | FindFilesOptions::IncludeDotDirectories);

Is it possible to support this in a strongly-typed way with C++11 enum class, or do I have to revert to untyped enumerations?

(I know the caller could static_cast to the underlying type and static_cast back, but I don't want the caller to have to do that)

解决方案

It is certainly possible to use enum classes for bitmaps. It is, unfortunately, a bit painful to do so: You need to define the necessary bit operations on your type. Below is an example how this could look like. It would be nice if the enum classes could derive from some other type which could live in a suitable namespace defining the necessary operator boilerplate code.

#include <iostream>
#include <type_traits>

enum class bitmap: unsigned char
{
    a = 0x01,
    b = 0x02,
    c = 0x04
};

bitmap operator& (bitmap x, bitmap y)
{
    typedef std::underlying_type<bitmap>::type uchar;
    return bitmap(uchar(x) & uchar(y));
}

bitmap operator| (bitmap x, bitmap y)
{
    typedef std::underlying_type<bitmap>::type uchar;
    return bitmap(uchar(x) | uchar(y));
}

bitmap operator^ (bitmap x, bitmap y)
{
    typedef std::underlying_type<bitmap>::type uchar;
    return bitmap(uchar(x) ^ uchar(y));
}

bool test(bitmap x)
{
    return std::underlying_type<bitmap>::type(x);
}

int main()
{
    bitmap v = bitmap::a | bitmap::b;
    if (test(v & bitmap::a)) {
        std::cout << "a ";
    }
    if (test(v & bitmap::b)) {
        std::cout << "b ";
    }
    if (test(v & bitmap::c)) {
        std::cout << "c ";
    }
    std::cout << '\n';
}

这篇关于如何使用枚举类作为一组标志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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