refactor: rename flag_enum to cenum.
- rename flag_enum to cenum because it not only provide functions related to flag enum, but also make C++ enum used like C enum.
This commit is contained in:
@ -59,7 +59,7 @@ FILES
|
||||
yycc/macro/ptr_size_detector.hpp
|
||||
yycc/macro/class_copy_move.hpp
|
||||
yycc/macro/printf_checker.hpp
|
||||
yycc/flag_enum.hpp
|
||||
yycc/cenum.hpp
|
||||
yycc/string.hpp
|
||||
yycc/string/reinterpret.hpp
|
||||
yycc/string/op.hpp
|
||||
|
||||
@ -1,10 +1,10 @@
|
||||
#include "termcolor.hpp"
|
||||
#include "../flag_enum.hpp"
|
||||
#include "../cenum.hpp"
|
||||
#include "../patch/stream.hpp"
|
||||
#include <stdexcept>
|
||||
#include <bit>
|
||||
|
||||
#define FLAG_ENUM ::yycc::flag_enum
|
||||
#define CENUM ::yycc::cenum
|
||||
|
||||
using namespace std::literals::string_view_literals;
|
||||
using namespace yycc::patch::stream;
|
||||
@ -102,7 +102,7 @@ namespace yycc::carton::termcolor {
|
||||
}
|
||||
|
||||
// Check whether it only has one flag
|
||||
if (!std::has_single_bit(FLAG_ENUM::integer(attr))) {
|
||||
if (!std::has_single_bit(CENUM::integer(attr))) {
|
||||
throw std::invalid_argument("style() only accept single flag attribute");
|
||||
}
|
||||
|
||||
@ -145,7 +145,7 @@ namespace yycc::carton::termcolor {
|
||||
* @return The count of single flag.
|
||||
*/
|
||||
static size_t count_attribute_flags(Attribute attrs) {
|
||||
return static_cast<size_t>(std::popcount(FLAG_ENUM::integer(attrs)));
|
||||
return static_cast<size_t>(std::popcount(CENUM::integer(attrs)));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -163,7 +163,7 @@ namespace yycc::carton::termcolor {
|
||||
*/
|
||||
static void append_styles(std::u8string& s, Attribute attrs) {
|
||||
#define CHECK_ATTR(probe) \
|
||||
if (FLAG_ENUM::has(attrs, probe)) s.append(termcolor::style(probe));
|
||||
if (CENUM::has(attrs, probe)) s.append(termcolor::style(probe));
|
||||
|
||||
if (attrs != Attribute::Default) {
|
||||
CHECK_ATTR(Attribute::Bold);
|
||||
|
||||
@ -8,7 +8,7 @@
|
||||
* But it lack essential logic operations which is commonly used by programmer.
|
||||
* So we create this helper to resolve this issue.
|
||||
*/
|
||||
namespace yycc::flag_enum {
|
||||
namespace yycc::cenum {
|
||||
|
||||
// Reference:
|
||||
// Enum operator overload: https://stackoverflow.com/a/71107019
|
||||
@ -204,4 +204,4 @@ namespace yycc::flag_enum {
|
||||
return static_cast<ut>(e);
|
||||
}
|
||||
|
||||
} // namespace yycc::flag_enum
|
||||
} // namespace yycc::cenum
|
||||
@ -13,7 +13,7 @@ PRIVATE
|
||||
yycc/macro/endian_detector.cpp
|
||||
yycc/macro/ptr_size_detector.cpp
|
||||
yycc/macro/stl_detector.cpp
|
||||
yycc/flag_enum.cpp
|
||||
yycc/cenum.cpp
|
||||
yycc/patch/ptr_pad.cpp
|
||||
yycc/patch/fopen.cpp
|
||||
yycc/patch/stream.cpp
|
||||
|
||||
@ -1,10 +1,10 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <yycc.hpp>
|
||||
#include <yycc/carton/termcolor.hpp>
|
||||
#include <yycc/flag_enum.hpp>
|
||||
#include <yycc/cenum.hpp>
|
||||
|
||||
#define TERMCOLOR ::yycc::carton::termcolor
|
||||
#define FLAG_ENUM ::yycc::flag_enum
|
||||
#define CENUM ::yycc::cenum
|
||||
|
||||
using namespace std::literals::string_view_literals;
|
||||
using Color = TERMCOLOR::Color;
|
||||
@ -23,7 +23,7 @@ namespace yycctest::carton::termcolor {
|
||||
|
||||
EXPECT_EQ(TERMCOLOR::style(Attribute::Default), u8"");
|
||||
EXPECT_EQ(TERMCOLOR::style(Attribute::Italic), u8"\033[3m");
|
||||
EXPECT_EQ(TERMCOLOR::styles(FLAG_ENUM::merge(Attribute::Italic, Attribute::Bold)),
|
||||
EXPECT_EQ(TERMCOLOR::styles(CENUM::merge(Attribute::Italic, Attribute::Bold)),
|
||||
u8"\033[1m"
|
||||
"\033[3m");
|
||||
|
||||
@ -32,7 +32,7 @@ namespace yycctest::carton::termcolor {
|
||||
|
||||
TEST(CartonTermcolor, Highlevel) {
|
||||
EXPECT_EQ(TERMCOLOR::colored(u8"hello"sv), u8"hello\033[0m");
|
||||
EXPECT_EQ(TERMCOLOR::colored(u8"hello"sv, Color::LightWhite, Color::Red, FLAG_ENUM::merge(Attribute::Italic, Attribute::Bold)),
|
||||
EXPECT_EQ(TERMCOLOR::colored(u8"hello"sv, Color::LightWhite, Color::Red, CENUM::merge(Attribute::Italic, Attribute::Bold)),
|
||||
u8"\033[97m"
|
||||
"\033[41m"
|
||||
"\033[1m"
|
||||
|
||||
83
test/yycc/cenum.cpp
Normal file
83
test/yycc/cenum.cpp
Normal file
@ -0,0 +1,83 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <yycc.hpp>
|
||||
#include <yycc/cenum.hpp>
|
||||
#include <cinttypes>
|
||||
|
||||
#include <yycc/prelude.hpp>
|
||||
|
||||
#define CENUM ::yycc::cenum
|
||||
|
||||
namespace yycctest::cenum {
|
||||
|
||||
enum class TestEnum : u8 {
|
||||
Bit1 = 0b00000001,
|
||||
Bit2 = 0b00000010,
|
||||
Bit3 = 0b00000100,
|
||||
Bit4 = 0b00001000,
|
||||
Bit5 = 0b00010000,
|
||||
Bit6 = 0b00100000,
|
||||
Bit7 = 0b01000000,
|
||||
Bit8 = 0b10000000,
|
||||
Empty = 0b00000000,
|
||||
InvBit8 = 0b01111111,
|
||||
MergedBit247 = Bit2 + Bit4 + Bit7,
|
||||
};
|
||||
|
||||
TEST(CEnum, Merge) {
|
||||
EXPECT_EQ(CENUM::merge(TestEnum::Bit2, TestEnum::Bit4, TestEnum::Bit7), TestEnum::MergedBit247);
|
||||
}
|
||||
|
||||
TEST(CEnum, Invert) {
|
||||
EXPECT_EQ(CENUM::invert(TestEnum::Bit8), TestEnum::InvBit8);
|
||||
}
|
||||
|
||||
TEST(CEnum, Mask) {
|
||||
TestEnum src = CENUM::merge(TestEnum::Bit2, TestEnum::Bit4);
|
||||
TestEnum val;
|
||||
|
||||
val = src;
|
||||
CENUM::mask(val, TestEnum::Bit2);
|
||||
EXPECT_EQ(val, TestEnum::Bit2);
|
||||
|
||||
val = src;
|
||||
CENUM::mask(val, TestEnum::Bit4);
|
||||
EXPECT_EQ(val, TestEnum::Bit4);
|
||||
|
||||
val = src;
|
||||
CENUM::mask(val, TestEnum::Bit3);
|
||||
EXPECT_EQ(val, TestEnum::Empty);
|
||||
}
|
||||
|
||||
TEST(CEnum, Add) {
|
||||
TestEnum val = TestEnum::Bit2;
|
||||
CENUM::add(val, TestEnum::Bit4, TestEnum::Bit7);
|
||||
EXPECT_EQ(val, TestEnum::MergedBit247);
|
||||
}
|
||||
|
||||
TEST(CEnum, Remove) {
|
||||
TestEnum val = TestEnum::MergedBit247;
|
||||
CENUM::remove(val, TestEnum::Bit2, TestEnum::Bit7);
|
||||
EXPECT_EQ(val, TestEnum::Bit4);
|
||||
}
|
||||
|
||||
TEST(CEnum, Has) {
|
||||
TestEnum val = TestEnum::MergedBit247;
|
||||
EXPECT_TRUE(CENUM::has(val, TestEnum::Bit2));
|
||||
EXPECT_FALSE(CENUM::has(val, TestEnum::Bit3));
|
||||
EXPECT_TRUE(CENUM::has(val, TestEnum::Bit4));
|
||||
EXPECT_TRUE(CENUM::has(val, TestEnum::Bit7));
|
||||
}
|
||||
|
||||
TEST(CEnum, Boolean) {
|
||||
EXPECT_FALSE(CENUM::boolean(TestEnum::Empty));
|
||||
EXPECT_TRUE(CENUM::boolean(TestEnum::Bit1));
|
||||
EXPECT_TRUE(CENUM::boolean(TestEnum::InvBit8));
|
||||
EXPECT_TRUE(CENUM::boolean(TestEnum::MergedBit247));
|
||||
}
|
||||
|
||||
TEST(CEnum, Integer) {
|
||||
EXPECT_EQ(CENUM::integer(TestEnum::Empty), UINT8_C(0));
|
||||
EXPECT_EQ(CENUM::integer(TestEnum::Bit1), UINT8_C(1));
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,83 +0,0 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <yycc.hpp>
|
||||
#include <yycc/flag_enum.hpp>
|
||||
#include <cinttypes>
|
||||
|
||||
#include <yycc/prelude.hpp>
|
||||
|
||||
#define FLAG_ENUM ::yycc::flag_enum
|
||||
|
||||
namespace yycctest::flag_enum {
|
||||
|
||||
enum class TestEnum : u8 {
|
||||
Bit1 = 0b00000001,
|
||||
Bit2 = 0b00000010,
|
||||
Bit3 = 0b00000100,
|
||||
Bit4 = 0b00001000,
|
||||
Bit5 = 0b00010000,
|
||||
Bit6 = 0b00100000,
|
||||
Bit7 = 0b01000000,
|
||||
Bit8 = 0b10000000,
|
||||
Empty = 0b00000000,
|
||||
InvBit8 = 0b01111111,
|
||||
MergedBit247 = Bit2 + Bit4 + Bit7,
|
||||
};
|
||||
|
||||
TEST(FlagEnum, Merge) {
|
||||
EXPECT_EQ(FLAG_ENUM::merge(TestEnum::Bit2, TestEnum::Bit4, TestEnum::Bit7), TestEnum::MergedBit247);
|
||||
}
|
||||
|
||||
TEST(FlagEnum, Invert) {
|
||||
EXPECT_EQ(FLAG_ENUM::invert(TestEnum::Bit8), TestEnum::InvBit8);
|
||||
}
|
||||
|
||||
TEST(FlagEnum, Mask) {
|
||||
TestEnum src = FLAG_ENUM::merge(TestEnum::Bit2, TestEnum::Bit4);
|
||||
TestEnum val;
|
||||
|
||||
val = src;
|
||||
FLAG_ENUM::mask(val, TestEnum::Bit2);
|
||||
EXPECT_EQ(val, TestEnum::Bit2);
|
||||
|
||||
val = src;
|
||||
FLAG_ENUM::mask(val, TestEnum::Bit4);
|
||||
EXPECT_EQ(val, TestEnum::Bit4);
|
||||
|
||||
val = src;
|
||||
FLAG_ENUM::mask(val, TestEnum::Bit3);
|
||||
EXPECT_EQ(val, TestEnum::Empty);
|
||||
}
|
||||
|
||||
TEST(FlagEnum, Add) {
|
||||
TestEnum val = TestEnum::Bit2;
|
||||
FLAG_ENUM::add(val, TestEnum::Bit4, TestEnum::Bit7);
|
||||
EXPECT_EQ(val, TestEnum::MergedBit247);
|
||||
}
|
||||
|
||||
TEST(FlagEnum, Remove) {
|
||||
TestEnum val = TestEnum::MergedBit247;
|
||||
FLAG_ENUM::remove(val, TestEnum::Bit2, TestEnum::Bit7);
|
||||
EXPECT_EQ(val, TestEnum::Bit4);
|
||||
}
|
||||
|
||||
TEST(FlagEnum, Has) {
|
||||
TestEnum val = TestEnum::MergedBit247;
|
||||
EXPECT_TRUE(FLAG_ENUM::has(val, TestEnum::Bit2));
|
||||
EXPECT_FALSE(FLAG_ENUM::has(val, TestEnum::Bit3));
|
||||
EXPECT_TRUE(FLAG_ENUM::has(val, TestEnum::Bit4));
|
||||
EXPECT_TRUE(FLAG_ENUM::has(val, TestEnum::Bit7));
|
||||
}
|
||||
|
||||
TEST(FlagEnum, Boolean) {
|
||||
EXPECT_FALSE(FLAG_ENUM::boolean(TestEnum::Empty));
|
||||
EXPECT_TRUE(FLAG_ENUM::boolean(TestEnum::Bit1));
|
||||
EXPECT_TRUE(FLAG_ENUM::boolean(TestEnum::InvBit8));
|
||||
EXPECT_TRUE(FLAG_ENUM::boolean(TestEnum::MergedBit247));
|
||||
}
|
||||
|
||||
TEST(FlagEnum, Integer) {
|
||||
EXPECT_EQ(FLAG_ENUM::integer(TestEnum::Empty), UINT8_C(0));
|
||||
EXPECT_EQ(FLAG_ENUM::integer(TestEnum::Bit1), UINT8_C(1));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user