mirror of
https://github.com/aantron/better-enums.git
synced 2025-12-07 09:16:44 +08:00
These modifications ensure enum.h can be used in a wider selection of end user projects without triggering warnings. GCC 4.9.2 was used with the following warning flags set: -Wall -Wextra -Wshadow -Weffc++ -Wno-unused-parameter -Wno-unused-local-typedefs -Wno-long-long -Wstrict-aliasing -Werror -pedantic -std=c++1y -Wformat=2 -Wmissing-include-dirs -Wsync-nand -Wuninitialized -Wconditionally-supported -Wconversion -Wuseless-cast -Wzero-as-null-pointer-constant This commit includes the modifications required to enable successful use of enum.h via both the "test" and "example" directories.
65 lines
1.5 KiB
C++
65 lines
1.5 KiB
C++
// Usage with STL containers.
|
|
|
|
#include <iostream>
|
|
#include <map>
|
|
#include <vector>
|
|
|
|
#include <enum.h>
|
|
|
|
ENUM(Channel, int, Red, Green, Blue)
|
|
|
|
int main()
|
|
{
|
|
// Vectors of enums.
|
|
std::vector<Channel> vector = {Channel::Red, Channel::Green};
|
|
|
|
vector.push_back(Channel::Red);
|
|
vector.push_back(Channel::Blue);
|
|
vector.push_back(Channel::Blue);
|
|
vector.push_back(Channel::Red);
|
|
|
|
for (Channel channel : vector)
|
|
std::cout << channel.to_string() << " ";
|
|
std::cout << std::endl;
|
|
|
|
|
|
|
|
// Maps. Lack of a default constructor in the current version means that
|
|
// std::map::operator[] usage is complicated. Insertion can still be done
|
|
// with ::insert, and access with ::find.
|
|
std::map<const char*, Channel> map = {{"first", Channel::Blue}};
|
|
map.insert({"second", Channel::Green});
|
|
|
|
for (Channel channel : Channel::_values)
|
|
map.insert({channel.to_string(), channel});
|
|
|
|
bool first = true;
|
|
for (auto item : map) {
|
|
if (first)
|
|
first = false;
|
|
else
|
|
std::cout << ", ";
|
|
|
|
std::cout
|
|
<< item.first
|
|
<< " -> "
|
|
<< item.second.to_string();
|
|
}
|
|
std::cout << std::endl;
|
|
|
|
|
|
|
|
// Enums as map keys.
|
|
std::map<Channel, const char*> descriptions =
|
|
{{Channel::Red, "the red channel"},
|
|
{Channel::Green, "the green channel"},
|
|
{Channel::Blue, "the blue channel"}};
|
|
|
|
for (auto item : descriptions)
|
|
std::cout << item.second << std::endl;
|
|
|
|
|
|
|
|
return 0;
|
|
}
|