mirror of
https://github.com/aantron/better-enums.git
synced 2025-12-06 16:56:42 +08:00
Prefixed .to_* methods with underscores to avoid name conflicts.
This commit is contained in:
parent
5d27fd83cf
commit
d01aacd454
8
enum.h
8
enum.h
@ -555,7 +555,7 @@ class EnumType : public _ENUM_NS(EnumType)::_Base { \
|
||||
constexpr EnumType(_enumerated value) : _Base(value) { } \
|
||||
constexpr EnumType(_ENUM_NS(EnumType)::_Base value) : _Base(value) { } \
|
||||
\
|
||||
constexpr _integral to_integral() const \
|
||||
constexpr _integral _to_integral() const \
|
||||
{ \
|
||||
return _value; \
|
||||
} \
|
||||
@ -579,7 +579,7 @@ class EnumType : public _ENUM_NS(EnumType)::_Base { \
|
||||
return (_enumerated)value; \
|
||||
} \
|
||||
\
|
||||
constexpr const char* to_string() const \
|
||||
constexpr const char* _to_string() const \
|
||||
{ \
|
||||
return \
|
||||
_enum::_or_throw( \
|
||||
@ -710,7 +710,7 @@ class EnumType : public _ENUM_NS(EnumType)::_Base { \
|
||||
\
|
||||
static const size_t _size = _ENUM_NS(EnumType)::_size; \
|
||||
\
|
||||
_integral to_integral() const \
|
||||
_integral _to_integral() const \
|
||||
{ \
|
||||
return _value; \
|
||||
} \
|
||||
@ -738,7 +738,7 @@ class EnumType : public _ENUM_NS(EnumType)::_Base { \
|
||||
return (_enumerated)value; \
|
||||
} \
|
||||
\
|
||||
const char* to_string() const \
|
||||
const char* _to_string() const \
|
||||
{ \
|
||||
_optional_index index = _value_to_index(_value); \
|
||||
if (!index) \
|
||||
|
||||
@ -11,9 +11,9 @@ void print_channel(Channel channel)
|
||||
{
|
||||
std::cout
|
||||
<< "channel \'"
|
||||
<< channel.to_string()
|
||||
<< channel._to_string()
|
||||
<< "\' has value "
|
||||
<< channel.to_integral()
|
||||
<< channel._to_integral()
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
@ -85,10 +85,10 @@ int main()
|
||||
// values of type Channel, but of type Channel::_Enumerated, and the
|
||||
// compiler isn't always able to implicitly promote the latter to the
|
||||
// former. + is used to force the promotion.
|
||||
std::cout << (+Channel::Green).to_string() << std::endl;
|
||||
std::cout << (+Channel::Green)._to_string() << std::endl;
|
||||
|
||||
// This will not work.
|
||||
// std::cout << (Channel::Green).to_string() << std::endl;
|
||||
// std::cout << (Channel::Green)._to_string() << std::endl;
|
||||
|
||||
|
||||
|
||||
|
||||
@ -9,7 +9,7 @@ int main()
|
||||
{
|
||||
// Listing declared values. Output is 3 4 0.
|
||||
for (Channel channel : Channel::_values)
|
||||
std::cout << channel.to_integral() << " ";
|
||||
std::cout << channel._to_integral() << " ";
|
||||
std::cout << std::endl;
|
||||
|
||||
// Listing declared names. Output is Red Green Blue.
|
||||
|
||||
@ -17,8 +17,8 @@ constexpr Channel channel_2 = Channel::_from_string("Blue");
|
||||
constexpr Channel channel_3 = Channel::_from_string_nocase("gReEn");
|
||||
|
||||
// Conversions to integers and strings.
|
||||
constexpr int channel_1_representation = channel_1.to_integral();
|
||||
constexpr const char *channel_1_name = channel_1.to_string();
|
||||
constexpr int channel_1_representation = channel_1._to_integral();
|
||||
constexpr const char *channel_1_name = channel_1._to_string();
|
||||
|
||||
// Validity checks (including against strings).
|
||||
constexpr bool should_be_valid_1 = Channel::_is_valid(2);
|
||||
@ -39,7 +39,7 @@ constexpr const char *name_through_subscript = Channel::_names[2];
|
||||
constexpr auto name = Channel::_name;
|
||||
|
||||
// Explicit promotion.
|
||||
constexpr int converted = (+Channel::Green).to_integral();
|
||||
constexpr int converted = (+Channel::Green)._to_integral();
|
||||
|
||||
|
||||
|
||||
@ -50,7 +50,7 @@ void print_channel(int number, Channel channel)
|
||||
<< "channel_"
|
||||
<< number
|
||||
<< " is "
|
||||
<< channel.to_string()
|
||||
<< channel._to_string()
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
|
||||
@ -19,7 +19,7 @@ int main()
|
||||
vector.push_back(Channel::Red);
|
||||
|
||||
for (Channel channel : vector)
|
||||
std::cout << channel.to_string() << " ";
|
||||
std::cout << channel._to_string() << " ";
|
||||
std::cout << std::endl;
|
||||
|
||||
|
||||
@ -31,7 +31,7 @@ int main()
|
||||
map.insert({"second", Channel::Green});
|
||||
|
||||
for (Channel channel : Channel::_values)
|
||||
map.insert({channel.to_string(), channel});
|
||||
map.insert({channel._to_string(), channel});
|
||||
|
||||
bool first = true;
|
||||
for (auto item : map) {
|
||||
@ -43,7 +43,7 @@ int main()
|
||||
std::cout
|
||||
<< item.first
|
||||
<< " -> "
|
||||
<< item.second.to_string();
|
||||
<< item.second._to_string();
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
|
||||
@ -41,10 +41,10 @@ int main()
|
||||
// default value is still declared in one place, not all over the program
|
||||
// code.
|
||||
Depth depth = default_<Depth>();
|
||||
std::cout << depth.to_string() << std::endl;
|
||||
std::cout << depth._to_string() << std::endl;
|
||||
|
||||
std::cout << default_<Channel>().to_string() << std::endl;
|
||||
std::cout << default_<Depth>().to_string() << std::endl;
|
||||
std::cout << default_<Channel>()._to_string() << std::endl;
|
||||
std::cout << default_<Depth>()._to_string() << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -19,7 +19,7 @@ ENUM(Channel, int, Red, Green, Blue)
|
||||
|
||||
int main()
|
||||
{
|
||||
using ChannelSet = std::bitset<maximum<Channel>().to_integral() + 1>;
|
||||
using ChannelSet = std::bitset<maximum<Channel>()._to_integral() + 1>;
|
||||
|
||||
ChannelSet red_only;
|
||||
red_only.set(Channel::Red);
|
||||
@ -31,9 +31,9 @@ int main()
|
||||
|
||||
for (Channel channel : Channel::_values) {
|
||||
std::cout
|
||||
<< channel.to_string()
|
||||
<< channel._to_string()
|
||||
<< " bit is set to "
|
||||
<< red_and_blue[channel.to_integral()]
|
||||
<< red_and_blue[channel._to_integral()]
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
|
||||
@ -75,11 +75,11 @@ static_assert_1(!(std::is_convertible<decltype(Channel::_values[0]),
|
||||
|
||||
|
||||
// Constant values.
|
||||
static_assert_1((+Channel::Red).to_integral() == 0);
|
||||
static_assert_1((+Channel::Green).to_integral() == 1);
|
||||
static_assert_1((+Channel::Blue).to_integral() == 2);
|
||||
static_assert_1((+Depth::HighColor).to_integral() == 40);
|
||||
static_assert_1((+Depth::TrueColor).to_integral() == 20);
|
||||
static_assert_1((+Channel::Red)._to_integral() == 0);
|
||||
static_assert_1((+Channel::Green)._to_integral() == 1);
|
||||
static_assert_1((+Channel::Blue)._to_integral() == 2);
|
||||
static_assert_1((+Depth::HighColor)._to_integral() == 40);
|
||||
static_assert_1((+Depth::TrueColor)._to_integral() == 20);
|
||||
|
||||
|
||||
|
||||
@ -126,7 +126,7 @@ static_assert_1(Channel::_values[2] == Channel::Blue);
|
||||
|
||||
static_assert_1(Channel::_names.size() == Channel::_size);
|
||||
// The next one is a little janky, but actually the pointers should be the same.
|
||||
static_assert_1(*Channel::_names.begin() == (+Channel::Red).to_string());
|
||||
static_assert_1(*Channel::_names.begin() == (+Channel::Red)._to_string());
|
||||
|
||||
|
||||
|
||||
@ -146,8 +146,8 @@ class EnumTests : public CxxTest::TestSuite {
|
||||
|
||||
void test_string_conversions()
|
||||
{
|
||||
TS_ASSERT_EQUALS(strcmp((+Channel::Green).to_string(), "Green"), 0);
|
||||
TS_ASSERT_EQUALS(strcmp((+Depth::HighColor).to_string(),
|
||||
TS_ASSERT_EQUALS(strcmp((+Channel::Green)._to_string(), "Green"), 0);
|
||||
TS_ASSERT_EQUALS(strcmp((+Depth::HighColor)._to_string(),
|
||||
"HighColor"), 0);
|
||||
|
||||
TS_ASSERT_THROWS(Channel::_from_string("green"), std::runtime_error);
|
||||
|
||||
@ -3,5 +3,5 @@
|
||||
|
||||
void print(Channel channel)
|
||||
{
|
||||
std::cout << Channel::_name << "::" << channel.to_string() << std::endl;
|
||||
std::cout << Channel::_name << "::" << channel._to_string() << std::endl;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user