1
0
Fork 0
mirror of https://github.com/Neargye/magic_enum.git synced 2026-01-09 23:34:23 +00:00

add constexpr containers (#187)

This commit is contained in:
Bela Schaum 2023-01-17 15:59:37 +01:00 committed by GitHub
parent 2a7658d084
commit 533c9509ef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 2609 additions and 7 deletions

View file

@ -42,6 +42,9 @@ Header-only C++17 library provides static reflection for enums, work with any en
* `underlying_type` improved UB-free "SFINAE-friendly" [underlying_type](https://en.cppreference.com/w/cpp/types/underlying_type). * `underlying_type` improved UB-free "SFINAE-friendly" [underlying_type](https://en.cppreference.com/w/cpp/types/underlying_type).
* `ostream_operators` ostream operators for enums. * `ostream_operators` ostream operators for enums.
* `bitwise_operators` bitwise operators for enums. * `bitwise_operators` bitwise operators for enums.
* `containers::array` array container for enums.
* `containers::bitset` bitset container for enums.
* `containers::set` set container for enums.
## Documentation ## Documentation
@ -222,6 +225,40 @@ Header-only C++17 library provides static reflection for enums, work with any en
// color_name -> "BLUE" // color_name -> "BLUE"
``` ```
* `containers::array` array container for enums.
```cpp
magic_enum::containers::array<Color, RGB> color_rgb_array {};
color_rgb_array[Color::RED] = {255, 0, 0};
color_rgb_array[Color::GREEN] = {0, 255, 0};
color_rgb_array[Color::BLUE] = {0, 0, 255};
std::get<Color::BLUE>(color_rgb_array) // -> RGB{0, 0, 255}
```
* `containers::bitset` bitset container for enums.
```cpp
constexpr magic_enum::containers::bitset<Color> color_bitset_red_green {Color::RED|Color::GREEN};
bool all = color_bitset_red_green.all();
// all -> false
// Color::BLUE is missing
bool test = color_bitset_red_green.test(Color::RED);
// test -> true
```
* `containers::set` set container for enums.
```cpp
auto color_set = magic_enum::containers::set<Color>();
bool empty = color_set.empty();
// empty -> true
color_set.insert(Color::GREEN);
color_set.insert(Color::BLUE);
color_set.insert(Color::RED);
std::size_t size = color_set.size();
// size -> 3
```
## Remarks ## Remarks
* `magic_enum` does not pretend to be a silver bullet for reflection for enums, it was originally designed for small enum. * `magic_enum` does not pretend to be a silver bullet for reflection for enums, it was originally designed for small enum.

View file

@ -20,6 +20,9 @@
* [`underlying_type` improved UB-free "SFINAE-friendly" underlying_type.](#underlying_type) * [`underlying_type` improved UB-free "SFINAE-friendly" underlying_type.](#underlying_type)
* [`ostream_operators` ostream operators for enums.](#ostream_operators) * [`ostream_operators` ostream operators for enums.](#ostream_operators)
* [`bitwise_operators` bitwise operators for enums.](#bitwise_operators) * [`bitwise_operators` bitwise operators for enums.](#bitwise_operators)
* [`containers::array` array container for enums.](#containersarray)
* [`containers::bitset` bitset container for enums.](#containersbitset)
* [`containers::set` set container for enums.](#containersset)
## Synopsis ## Synopsis
@ -325,7 +328,7 @@ constexpr string_view enum_type_name() noexcept;
```cpp ```cpp
Color color = Color::RED; Color color = Color::RED;
auto type_name = magic_enum::enum_type_name<decltype(color)>(); auto type_name = magic_enum::enum_type_name<decltype(color)>();
// color_name -> "Color" // type_name -> "Color"
``` ```
## `enum_fuse` ## `enum_fuse`
@ -427,7 +430,7 @@ constexpr optional<E> enum_flags_contains(string_view value, BinaryPredicate p)
```cpp ```cpp
auto directions_name = magic_enum::enum_flags_name(Directions::Up | Directions::Right); auto directions_name = magic_enum::enum_flags_name(Directions::Up | Directions::Right);
// directions_name -> "Directions::Up | Directions::Right" // directions_name -> "Directions::Up|Directions::Right"
``` ```
@ -562,3 +565,362 @@ constexpr E& operator^=(E& lhs, E rhs) noexcept;
// Support operators: ~, |, &, ^, |=, &=, ^=. // Support operators: ~, |, &, ^, |=, &=, ^=.
Flags flags = Flags::A | Flags::B & ~Flags::C; Flags flags = Flags::A | Flags::B & ~Flags::C;
``` ```
## `containers::array`
```cpp
template<typename E, typename V, typename Index = default_indexing<E>>
struct array {
constexpr reference at(E pos);
constexpr const_reference at(E pos) const;
constexpr reference operator[](E pos) noexcept;
constexpr const_reference operator[](E pos) const noexcept;
constexpr reference front() noexcept;
constexpr const_reference front() const noexcept;
constexpr reference back() noexcept;
constexpr const_reference back() const noexcept;
constexpr pointer data() noexcept;
constexpr const_pointer data() const noexcept;
constexpr iterator begin() noexcept;
constexpr const_iterator begin() const noexcept;
constexpr const_iterator cbegin() const noexcept;
constexpr iterator end() noexcept;
constexpr const_iterator end() const noexcept;
constexpr const_iterator cend() const noexcept;
constexpr iterator rbegin() noexcept;
constexpr const_iterator rbegin() const noexcept;
constexpr const_iterator crbegin() const noexcept;
constexpr iterator rend() noexcept;
constexpr const_iterator rend() const noexcept;
constexpr const_iterator crend() const noexcept;
constexpr bool empty() const noexcept;
constexpr size_type size() const noexcept;
constexpr size_type max_size() const noexcept;
constexpr void fill( const V& value );
constexpr void swap(array& other) noexcept(std::is_nothrow_swappable_v<V>);
friend constexpr bool operator==(const array& a1, const array& a2);
friend constexpr bool operator!=(const array& a1, const array& a2);
friend constexpr bool operator<(const array& a1, const array& a2);
friend constexpr bool operator<=(const array& a1, const array& a2);
friend constexpr bool operator>(const array& a1, const array& a2);
friend constexpr bool operator>=(const array& a1, const array& a2);
}
```
* STL like array for all enums.
* Examples
```cpp
constexpr magic_enum::containers::array<Color, RGB> color_rgb_array {{{{255, 0, 0}, {0, 255, 0}, {0, 0, 255}}}};
```
```cpp
magic_enum::containers::array<Color, RGB> color_rgb_array {};
color_rgb_array[Color::RED] = {255, 0, 0};
color_rgb_array[Color::GREEN] = {0, 255, 0};
color_rgb_array[Color::BLUE] = {0, 0, 255};
std::get<Color::BLUE>(color_rgb_array) // -> RGB{0, 0, 255}
```
## `containers::bitset`
```cpp
template<typename E, typename Index = default_indexing<E>>
class bitset {
constexpr explicit bitset(detail::raw_access_t = raw_access) noexcept;
constexpr explicit bitset(detail::raw_access_t, unsigned long long val);
constexpr explicit bitset(detail::raw_access_t,
string_view sv,
string_view::size_type pos = 0,
string_view::size_type n = string_view::npos,
char zero = '0',
char one = '1');
constexpr explicit bitset(detail::raw_access_t,
const char* str,
std::size_t n = ~std::size_t{},
char zero = '0',
char one = '1');
constexpr bitset(std::initializer_list<E> starters);
template<typename V = E>
constexpr explicit bitset(std::enable_if_t<magic_enum::detail::is_flags_v<V>, E> starter);
template<typename Cmp = std::equal_to<>>
constexpr explicit bitset(string_view sv,
Cmp&& cmp = {},
char sep = '|');
friend constexpr bool operator==( const bitset& lhs, const bitset& rhs ) noexcept;
friend constexpr bool operator!=( const bitset& lhs, const bitset& rhs ) noexcept;
constexpr bool operator[](E pos) const noexcept;
constexpr reference operator[](E pos) noexcept;
constexpr bool test(E pos) const;
constexpr bool all() const noexcept;
constexpr bool any() const noexcept;
constexpr bool none() const noexcept;
constexpr std::size_t count() const noexcept;
constexpr std::size_t size() const noexcept;
constexpr std::size_t max_size() const noexcept;
constexpr bitset& operator&= (const bitset& other) noexcept;
constexpr bitset& operator|= (const bitset& other) noexcept;
constexpr bitset& operator^= (const bitset& other) noexcept;
constexpr bitset operator~() const noexcept;
constexpr bitset& set() noexcept;
constexpr bitset& set(E pos, bool value = true);
constexpr bitset& reset() noexcept;
constexpr bitset& reset(E pos);
constexpr bitset& flip() noexcept;
friend constexpr bitset operator&(const bitset& lhs, const bitset& rhs) noexcept;
friend constexpr bitset operator|(const bitset& lhs, const bitset& rhs) noexcept;
friend constexpr bitset operator^(const bitset& lhs, const bitset& rhs) noexcept;
template<typename V = E>
constexpr explicit operator std::enable_if_t<magic_enum::detail::is_flags_v<V>, E>() const;
string to_string(char sep = '|') const;
string to_string(detail::raw_access_t,
char zero = '0',
char one = '1') const;
constexpr unsigned long long to_ullong(detail::raw_access_t raw) const;
constexpr unsigned long long to_ulong(detail::raw_access_t raw) const;
friend std::ostream& operator<<(std::ostream& o, const bitset& bs);
friend std::istream& operator>>(std::istream& i, bitset& bs);
}
```
* STL like bitset for all enums.
* Examples
```cpp
constexpr magic_enum::containers::bitset<Color> color_bitset_red_green {Color::RED|Color::GREEN};
bool all = color_bitset_red_green.all();
// all -> false
// Color::BLUE is missing
bool test = color_bitset_red_green.test(Color::RED);
// test -> true
```
```cpp
auto color_bitset = magic_enum::containers::bitset<Color>();
color_bitset.set(Color::GREEN);
color_bitset.set(Color::BLUE);
std::string to_string = color_bitset.to_string();
// to_string -> "GREEN|BLUE"
```
## `containers::set`
```cpp
template<typename E, typename CExprLess = std::less<E>>
class set {
constexpr set() noexcept = default;
template<typename InputIt>
constexpr set(InputIt first, InputIt last);
constexpr set(std::initializer_list<E> ilist);
template<typename V = E>
constexpr explicit set(std::enable_if_t<magic_enum::detail::is_flags_v<V>, E> starter);
constexpr set(const set&) noexcept = default;
constexpr set(set&&) noexcept = default;
constexpr set& operator=(const set&) noexcept = default;
constexpr set& operator=(set&&) noexcept = default;
constexpr set& operator=(std::initializer_list<E> ilist);
constexpr const_iterator begin() const noexcept;
constexpr const_iterator end() const noexcept;
constexpr const_iterator cbegin() const noexcept;
constexpr const_iterator cend() const noexcept;
constexpr const_reverse_iterator rbegin() const noexcept;
constexpr const_reverse_iterator rend() const noexcept;
constexpr const_reverse_iterator crbegin() const noexcept;
constexpr const_reverse_iterator crend() const noexcept;
constexpr bool empty() const noexcept;
constexpr size_type size() const noexcept;
constexpr size_type max_size() const noexcept;
constexpr void clear() noexcept;
constexpr std::pair<iterator,bool> insert(const value_type& value) noexcept;
constexpr std::pair<iterator,bool> insert(value_type&& value) noexcept;
constexpr iterator insert(const_iterator, const value_type& value) noexcept;
constexpr iterator insert(const_iterator hint, value_type&& value) noexcept;
template< class InputIt >
constexpr void insert(InputIt first, InputIt last) noexcept;
constexpr void insert(std::initializer_list<value_type> ilist) noexcept;
template<class... Args>
constexpr std::pair<iterator,bool> emplace(Args&&... args) noexcept;
template<class... Args>
constexpr iterator emplace_hint(const_iterator, Args&&... args) noexcept;
constexpr iterator erase(const_iterator pos) noexcept;
constexpr iterator erase(const_iterator first, const_iterator last) noexcept;
constexpr size_type erase(const key_type& key) noexcept;
template<class K, typename KC = key_compare>
constexpr std::enable_if_t<detail::is_transparent_v<KC>, size_type> erase(K&& x) noexcept;
void swap(set& other) noexcept;
constexpr size_type count(const key_type& key) const noexcept;
template<typename K, typename KC = key_compare>
constexpr std::enable_if_t<detail::is_transparent_v<KC>, size_type> count(const K& x) const;
constexpr const_iterator find(const key_type & key) const noexcept;
template<class K, typename KC = key_compare>
constexpr std::enable_if_t<detail::is_transparent_v<KC>, const_iterator> find(const K& x) const;
constexpr bool contains(const key_type& key) const noexcept;
template<typename K, typename KC = key_compare>
constexpr std::enable_if_t<detail::is_transparent_v<KC>, bool> contains(const K& x) const noexcept;
constexpr std::pair<const_iterator,const_iterator> equal_range(const key_type& key) const noexcept;
template<typename K, typename KC = key_compare>
constexpr std::enable_if_t<detail::is_transparent_v<KC>, std::pair<const_iterator,const_iterator>> equal_range(const K& x) const noexcept;
constexpr const_iterator lower_bound(const key_type& key) const noexcept;
template<typename K, typename KC = key_compare>
constexpr std::enable_if_t<detail::is_transparent_v<KC>, const_iterator> lower_bound(const K& x) const noexcept;
constexpr const_iterator upper_bound(const key_type& key) const noexcept;
template<typename K, typename KC = key_compare>
constexpr std::enable_if_t<detail::is_transparent_v<KC>, const_iterator> upper_bound(const K& x) const noexcept;
constexpr key_compare key_comp() const;
constexpr value_compare value_comp() const;
constexpr friend bool operator==(const set& lhs, const set& rhs) noexcept;
constexpr friend bool operator!=(const set& lhs, const set& rhs) noexcept;
constexpr friend bool operator<(const set& lhs, const set& rhs) noexcept;
constexpr friend bool operator<=(const set& lhs, const set& rhs) noexcept;
constexpr friend bool operator>(const set& lhs, const set& rhs) noexcept;
constexpr friend bool operator>=(const set& lhs, const set& rhs) noexcept;
template<typename Pred>
size_type erase_if(Pred pred);
}
```
* STL like set for all enums.
* Examples
```cpp
constexpr magic_enum::containers::set color_set_filled = {Color::RED, Color::GREEN, Color::BLUE};
```
```cpp
auto color_set = magic_enum::containers::set<Color>();
bool empty = color_set.empty();
// empty -> true
color_set.insert(Color::GREEN);
color_set.insert(Color::BLUE);
color_set.insert(Color::RED);
std::size_t size = color_set.size();
// size -> 3
```

View file

@ -19,6 +19,9 @@ endfunction()
make_example(example) make_example(example)
make_example(enum_flag_example) make_example(enum_flag_example)
make_example(example_containers_array)
make_example(example_containers_bitset)
make_example(example_containers_set)
make_example(example_custom_name) make_example(example_custom_name)
make_example(example_switch) make_example(example_switch)
if(MAGIC_ENUM_OPT_ENABLE_NONASCII) if(MAGIC_ENUM_OPT_ENABLE_NONASCII)

View file

@ -0,0 +1,61 @@
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
// SPDX-License-Identifier: MIT
// Copyright (c) 2019 - 2022 Daniil Goncharov <neargye@gmail.com>.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include <iostream>
#include <magic_enum_containers.hpp>
enum class Color { RED = 1, GREEN = 2, BLUE = 4 };
struct RGB {
std::uint8_t r {};
std::uint8_t g {};
std::uint8_t b {};
[[nodiscard]] constexpr bool empty() { return std::equal_to{}(r, g) && std::equal_to{}(g, b) && std::equal_to{}(b, 0); }
[[nodiscard]] constexpr bool operator==(RGB rgb) const noexcept { return std::equal_to{}(r, rgb.r) && std::equal_to{}(g, rgb.g) && std::equal_to{}(b, rgb.b); }
friend std::ostream& operator<<(std::ostream& ostream, RGB rgb) {
ostream << "R=" << static_cast<std::uint32_t>(rgb.r) << " G=" << static_cast<std::uint32_t>(rgb.g) << " B=" << static_cast<std::uint32_t>(rgb.b);
return ostream;
}
};
constexpr std::uint8_t color_max = std::numeric_limits<std::uint8_t>::max();
int main() {
constexpr magic_enum::containers::array<Color, RGB> color_rgb_initializer {{{{color_max, 0, 0}, {0, color_max, 0}, {0, 0, color_max}}}};
std::cout << std::get<0>(color_rgb_initializer) << std::endl; // R=255 G=0 B=0
std::cout << std::get<1>(color_rgb_initializer) << std::endl; // R=0 G=255 B=0
std::cout << std::get<2>(color_rgb_initializer) << std::endl; // R=0 G=0 B=255
std::cout << std::get<Color::RED>(color_rgb_initializer) << std::endl; // R=255 G=0 B=0
std::cout << std::get<Color::GREEN>(color_rgb_initializer) << std::endl; // R=0 G=255 B=0
std::cout << std::get<Color::BLUE>(color_rgb_initializer) << std::endl; // R=0 G=0 B=255
return 0;
}

View file

@ -0,0 +1,51 @@
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
// SPDX-License-Identifier: MIT
// Copyright (c) 2019 - 2022 Daniil Goncharov <neargye@gmail.com>.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#if defined(_MSC_VER)
# pragma warning(push)
# pragma warning(disable : 4244) // warning C4244: 'argument': conversion from 'const T' to 'unsigned int', possible loss of data.
#endif
#include <iostream>
#include <magic_enum_containers.hpp>
enum class Color { RED = 1, GREEN = 2, BLUE = 4 };
int main() {
auto color_bitset = magic_enum::containers::bitset<Color>();
color_bitset.set(Color::GREEN);
color_bitset.set(Color::BLUE);
std::cout << std::boolalpha;
std::cout << color_bitset.size() << std::endl; // 3 == magic_enum::enum_count<Color>()
std::cout << color_bitset.all() << std::endl; // false
std::cout << color_bitset.any() << std::endl; // true
std::cout << color_bitset.none() << std::endl; // false
std::cout << color_bitset.count() << std::endl; // 2
std::cout << color_bitset.test(Color::RED) << std::endl; // false
std::cout << color_bitset.test(Color::GREEN) << std::endl; // true
std::cout << color_bitset.test(Color::BLUE) << std::endl; // true
return 0;
}

View file

@ -0,0 +1,55 @@
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
// SPDX-License-Identifier: MIT
// Copyright (c) 2019 - 2022 Daniil Goncharov <neargye@gmail.com>.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#if defined(_MSC_VER)
# pragma warning(push)
# pragma warning(disable : 4244) // warning C4244: 'argument': conversion from 'const T' to 'unsigned int', possible loss of data.
#endif
#ifdef _WIN32
#define _ITERATOR_DEBUG_LEVEL 0
#endif
#include <iostream>
#include <magic_enum_containers.hpp>
enum class Color { RED = 1, GREEN = 2, BLUE = 4 };
int main() {
std::cout << std::boolalpha;
magic_enum::containers::set color_set {Color::RED, Color::GREEN, Color::BLUE};
std::cout << color_set.empty() << std::endl; // false
std::cout << color_set.size() << std::endl; // 3
color_set.clear();
std::cout << color_set.empty() << std::endl; // true
std::cout << color_set.size() << std::endl; // 0
color_set.insert(Color::GREEN);
color_set.insert(Color::BLUE);
std::cout << color_set.empty() << std::endl; // false
std::cout << color_set.size() << std::endl; // 2
return 0;
}

View file

@ -38,8 +38,8 @@
#include <array> #include <array>
#include <cassert> #include <cassert>
#include <cstdint>
#include <cstddef> #include <cstddef>
#include <cstdint>
#include <limits> #include <limits>
#include <type_traits> #include <type_traits>
#include <utility> #include <utility>
@ -281,6 +281,7 @@ constexpr string_view pretty_name(string_view name) noexcept {
return {}; // Invalid name. return {}; // Invalid name.
} }
template<typename Op = std::equal_to<>>
class case_insensitive { class case_insensitive {
static constexpr char to_lower(char c) noexcept { static constexpr char to_lower(char c) noexcept {
return (c >= 'A' && c <= 'Z') ? static_cast<char>(c + ('a' - 'A')) : c; return (c >= 'A' && c <= 'Z') ? static_cast<char>(c + ('a' - 'A')) : c;
@ -293,7 +294,7 @@ class case_insensitive {
static_assert(always_false_v<L, R>, "magic_enum::case_insensitive not supported Non-ASCII feature."); static_assert(always_false_v<L, R>, "magic_enum::case_insensitive not supported Non-ASCII feature.");
return false; return false;
#else #else
return to_lower(lhs) == to_lower(rhs); return Op{}(to_lower(lhs), to_lower(rhs));
#endif #endif
} }
}; };
@ -1107,7 +1108,7 @@ template <typename E>
// Obtains index in enum values from enum value. // Obtains index in enum values from enum value.
// Returns optional with index. // Returns optional with index.
template <typename E> template <typename E>
[[nodiscard]] constexpr auto enum_index(E value) noexcept -> detail::enable_if_t<E, optional<std::size_t>> { [[nodiscard]] constexpr auto enum_index([[maybe_unused]] E value) noexcept -> detail::enable_if_t<E, optional<std::size_t>> {
using D = std::decay_t<E>; using D = std::decay_t<E>;
using U = underlying_type_t<D>; using U = underlying_type_t<D>;
@ -1216,7 +1217,7 @@ template <detail::value_type VT, typename E>
// Returns name from enum-flags value. // Returns name from enum-flags value.
// If enum-flags value does not have name or value out of range, returns empty string. // If enum-flags value does not have name or value out of range, returns empty string.
template <typename E> template <typename E>
[[nodiscard]] auto enum_flags_name(E value) -> detail::enable_if_t<E, string> { [[nodiscard]] auto enum_flags_name(E value, [[maybe_unused]] char sep = '|') -> detail::enable_if_t<E, string> {
using D = std::decay_t<E>; using D = std::decay_t<E>;
static_assert(detail::is_flags_v<D>, "magic_enum::enum_flags_name requires enum-flags type."); static_assert(detail::is_flags_v<D>, "magic_enum::enum_flags_name requires enum-flags type.");
@ -1236,7 +1237,7 @@ template <typename E>
} }
// Allows you to write magic_enum::enum_cast<foo>("bar", magic_enum::case_insensitive); // Allows you to write magic_enum::enum_cast<foo>("bar", magic_enum::case_insensitive);
inline constexpr auto case_insensitive = detail::case_insensitive{}; inline constexpr auto case_insensitive = detail::case_insensitive<>{};
// Obtains enum value from integer value. // Obtains enum value from integer value.
// Returns optional with enum value. // Returns optional with enum value.

File diff suppressed because it is too large Load diff

View file

@ -41,21 +41,25 @@ endfunction()
make_test(test.cpp test-cpp17 c++17) make_test(test.cpp test-cpp17 c++17)
make_test(test_flags.cpp test_flags-cpp17 c++17) make_test(test_flags.cpp test_flags-cpp17 c++17)
make_test(test_aliases.cpp test_aliases-cpp17 c++17) make_test(test_aliases.cpp test_aliases-cpp17 c++17)
make_test(test_containers.cpp test_containers-cpp17 c++17)
if(HAS_CPP20_FLAG) if(HAS_CPP20_FLAG)
make_test(test.cpp test-cpp20 c++20) make_test(test.cpp test-cpp20 c++20)
make_test(test_flags.cpp test_flags-cpp20 c++20) make_test(test_flags.cpp test_flags-cpp20 c++20)
make_test(test_aliases.cpp test_aliases-cpp20 c++20) make_test(test_aliases.cpp test_aliases-cpp20 c++20)
make_test(test_containers.cpp test_containers-cpp20 c++20)
endif() endif()
if(HAS_CPP23_FLAG) if(HAS_CPP23_FLAG)
make_test(test.cpp test-cpp23 c++23) make_test(test.cpp test-cpp23 c++23)
make_test(test_flags.cpp test_flags-cpp23 c++23) make_test(test_flags.cpp test_flags-cpp23 c++23)
make_test(test_aliases.cpp test_aliases-cpp23 c++23) make_test(test_aliases.cpp test_aliases-cpp23 c++23)
make_test(test_containers.cpp test_containers-cpp23 c++23)
endif() endif()
if(HAS_CPPLATEST_FLAG) if(HAS_CPPLATEST_FLAG)
make_test(test.cpp test-cpplatest c++latest) make_test(test.cpp test-cpplatest c++latest)
make_test(test_flags.cpp test_flags-cpplatest c++latest) make_test(test_flags.cpp test_flags-cpplatest c++latest)
make_test(test_aliases.cpp test_aliases-cpplatest c++latest) make_test(test_aliases.cpp test_aliases-cpplatest c++latest)
make_test(test_containers.cpp test_containers-cpplatest c++latest)
endif() endif()

318
test/test_containers.cpp Normal file
View file

@ -0,0 +1,318 @@
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
// SPDX-License-Identifier: MIT
// Copyright (c) 2019 - 2022 Daniil Goncharov <neargye@gmail.com>.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#if defined(__clang__)
# pragma clang diagnostic push
#elif defined(__GNUC__)
# pragma GCC diagnostic push
#elif defined(_MSC_VER)
# pragma warning(push)
# pragma warning(disable : 4244) // warning C4244: 'argument': conversion from 'const T' to 'unsigned int', possible loss of data.
#endif
#ifdef _WIN32
#define _ITERATOR_DEBUG_LEVEL 0
#endif
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>
#include <magic_enum_containers.hpp>
#include <functional>
enum class Color { RED = 1, GREEN = 2, BLUE = 4 };
enum class Empty {};
struct RGB {
std::uint8_t r {};
std::uint8_t g {};
std::uint8_t b {};
[[nodiscard]] constexpr bool empty() { return std::equal_to{}(r, g) && std::equal_to{}(g, b) && std::equal_to{}(b, 0); }
[[nodiscard]] constexpr bool operator==(RGB rgb) const noexcept { return std::equal_to{}(r, rgb.r) && std::equal_to{}(g, rgb.g) && std::equal_to{}(b, rgb.b); }
friend std::ostream& operator<<(std::ostream& ostream, RGB rgb) {
ostream << "R=" << static_cast<std::uint32_t>(rgb.r) << " G=" << static_cast<std::uint32_t>(rgb.g) << " B=" << static_cast<std::uint32_t>(rgb.b);
return ostream;
}
};
template <typename T> bool check_const([[maybe_unused]]T& element) { return false; }
template <typename T> bool check_const([[maybe_unused]]T const& element) { return true; }
constexpr std::uint8_t color_max = std::numeric_limits<std::uint8_t>::max();
TEST_CASE("containers_array") {
using namespace magic_enum::bitwise_operators;
using namespace magic_enum::ostream_operators;
constexpr magic_enum::containers::array<Color, RGB> color_rgb_initializer {{{{color_max, 0, 0}, {0, color_max, 0}, {0, 0, color_max}}}};
REQUIRE(color_rgb_initializer.at(Color::RED) == RGB{color_max, 0, 0});
REQUIRE(color_rgb_initializer.at(Color::GREEN) == RGB{0, color_max, 0});
REQUIRE(color_rgb_initializer.at(Color::BLUE) == RGB{0, 0, color_max});
/* BUG: a sort will not survive the data integration */
magic_enum::containers::array<Color, std::uint8_t> color_rgb_container_int{{1U, 4U, 2U}};
// ENC: Direct convert to std::array
// std::array compare_before {1U, 4U, 2U};
constexpr magic_enum::containers::array<Color, std::uint8_t> compare_before{{1U, 4U, 2U}};
REQUIRE(color_rgb_container_int == compare_before);
constexpr auto colors = magic_enum::enum_values<Color>();
std::ignore = std::get<0>(compare_before);
std::ignore = std::get<1>(compare_before);
std::ignore = std::get<2>(compare_before);
std::ignore = std::get<Color::RED>(compare_before);
std::ignore = std::get<Color::GREEN>(compare_before);
std::ignore = std::get<Color::BLUE>(compare_before);
REQUIRE(std::make_pair(colors[0], color_rgb_container_int[colors[0]]) == std::make_pair<Color, std::uint8_t>(Color::RED, 1U));
REQUIRE(std::make_pair(colors[1], color_rgb_container_int[colors[1]]) == std::make_pair<Color, std::uint8_t>(Color::GREEN, 4U));
REQUIRE(std::make_pair(colors[2], color_rgb_container_int[colors[2]]) == std::make_pair<Color, std::uint8_t>(Color::BLUE, 2U));
std::sort(std::begin(color_rgb_container_int), std::end(color_rgb_container_int));
// Missing: Direct convert to std::array
// std::array compare_after {1U, 2U, 4U};
constexpr magic_enum::containers::array<Color, std::uint8_t> compare_after{{1U, 2U, 4U}};
REQUIRE(color_rgb_container_int == compare_after);
std::ignore = std::get<0>(compare_after);
std::ignore = std::get<1>(compare_after);
std::ignore = std::get<2>(compare_after);
std::ignore = std::get<Color::RED>(compare_after);
std::ignore = std::get<Color::GREEN>(compare_after);
std::ignore = std::get<Color::BLUE>(compare_after);
REQUIRE(std::make_pair(colors[0], color_rgb_container_int[colors[0]]) == std::make_pair<Color, std::uint8_t>(Color::RED, 1U));
REQUIRE(std::make_pair(colors[1], color_rgb_container_int[colors[1]]) == std::make_pair<Color, std::uint8_t>(Color::GREEN, 2U));
REQUIRE(std::make_pair(colors[2], color_rgb_container_int[colors[2]]) == std::make_pair<Color, std::uint8_t>(Color::BLUE, 4U));
auto empty = magic_enum::containers::array<Empty, std::nullptr_t>();
REQUIRE(empty.empty());
REQUIRE(empty.size() == 0);
REQUIRE(magic_enum::enum_count<Empty>() == empty.size());
auto color_rgb_container = magic_enum::containers::array<Color, RGB>();
REQUIRE_FALSE(color_rgb_container.empty());
REQUIRE(color_rgb_container.size() == 3);
REQUIRE(magic_enum::enum_count<Color>() == color_rgb_container.size());
REQUIRE(color_rgb_container.at(Color::RED).empty());
REQUIRE(color_rgb_container.at(Color::GREEN).empty());
REQUIRE(color_rgb_container.at(Color::BLUE).empty());
REQUIRE_THROWS(color_rgb_container.at(Color::BLUE|Color::GREEN).empty());
color_rgb_container[Color::RED] = {color_max, 0, 0};
color_rgb_container[Color::GREEN] = {0, color_max, 0};
color_rgb_container[Color::BLUE] = {0, 0, color_max};
REQUIRE(color_rgb_container.at(Color::RED) == RGB{color_max, 0, 0});
REQUIRE(color_rgb_container.at(Color::GREEN) == RGB{0, color_max, 0});
REQUIRE(color_rgb_container.at(Color::BLUE) == RGB{0, 0, color_max});
REQUIRE(color_rgb_container.front() == RGB{color_max, 0, 0});
REQUIRE(color_rgb_container.back() == RGB{0, 0, color_max});
REQUIRE(std::get<Color::RED>(color_rgb_container) == RGB{color_max, 0, 0});
REQUIRE(std::get<Color::GREEN>(color_rgb_container) == RGB{0, color_max, 0});
REQUIRE(std::get<Color::BLUE>(color_rgb_container) == RGB{0, 0, color_max});
auto iterator = color_rgb_container.begin();
REQUIRE_FALSE(check_const(iterator));
REQUIRE(check_const(color_rgb_container.begin()));
REQUIRE(check_const(color_rgb_container.cbegin()));
auto color_rgb_container_compare = magic_enum::containers::array<Color, RGB>();
color_rgb_container_compare.fill({color_max, color_max, color_max});
REQUIRE_FALSE(color_rgb_container == color_rgb_container_compare);
color_rgb_container_compare[Color::RED] = {color_max, 0, 0};
color_rgb_container_compare[Color::GREEN] = {0, color_max, 0};
color_rgb_container_compare[Color::BLUE] = {0, 0, color_max};
REQUIRE(color_rgb_container == color_rgb_container_compare);
constexpr auto from_to_array = magic_enum::containers::to_array<Color, RGB>({{color_max, 0, 0}, {0, color_max, 0}, {0, 0, color_max}});
REQUIRE(from_to_array.at(Color::RED) == RGB{color_max, 0, 0});
REQUIRE(from_to_array.at(Color::GREEN) == RGB{0, color_max, 0});
REQUIRE(from_to_array.at(Color::BLUE) == RGB{0, 0, color_max});
}
TEST_CASE("containers_bitset") {
using namespace magic_enum::bitwise_operators;
auto color_bitset = magic_enum::containers::bitset<Color>();
REQUIRE(color_bitset.to_string().empty());
REQUIRE(color_bitset.size() == 3);
REQUIRE(magic_enum::enum_count<Color>() == color_bitset.size());
REQUIRE_FALSE(color_bitset.all());
REQUIRE_FALSE(color_bitset.any());
REQUIRE(color_bitset.none());
REQUIRE(color_bitset.count() == 0);
color_bitset.set(Color::GREEN);
REQUIRE_FALSE(color_bitset.all());
REQUIRE(color_bitset.any());
REQUIRE_FALSE(color_bitset.none());
REQUIRE(color_bitset.count() == 1);
REQUIRE_FALSE(color_bitset.test(Color::RED));
REQUIRE(color_bitset.test(Color::GREEN));
REQUIRE_FALSE(color_bitset.test(Color::BLUE));
color_bitset.set(Color::BLUE);
REQUIRE_FALSE(color_bitset.all());
REQUIRE(color_bitset.any());
REQUIRE_FALSE(color_bitset.none());
REQUIRE(color_bitset.count() == 2);
REQUIRE_FALSE(color_bitset.test(Color::RED));
REQUIRE(color_bitset.test(Color::GREEN));
REQUIRE(color_bitset.test(Color::BLUE));
color_bitset.set(Color::RED);
REQUIRE(color_bitset.all());
REQUIRE(color_bitset.any());
REQUIRE_FALSE(color_bitset.none());
REQUIRE(color_bitset.count() == 3);
REQUIRE(color_bitset.test(Color::RED));
REQUIRE(color_bitset.test(Color::GREEN));
REQUIRE(color_bitset.test(Color::BLUE));
color_bitset.reset();
REQUIRE_FALSE(color_bitset.all());
REQUIRE_FALSE(color_bitset.any());
REQUIRE(color_bitset.none());
REQUIRE(color_bitset.count() == 0);
REQUIRE_FALSE(color_bitset.test(Color::RED));
REQUIRE_FALSE(color_bitset.test(Color::GREEN));
REQUIRE_FALSE(color_bitset.test(Color::BLUE));
color_bitset.set(Color::RED);
REQUIRE(color_bitset.test(Color::RED));
REQUIRE_FALSE(color_bitset.test(Color::GREEN));
REQUIRE_FALSE(color_bitset.test(Color::BLUE));
color_bitset.flip();
REQUIRE_FALSE(color_bitset.test(Color::RED));
REQUIRE(color_bitset.test(Color::GREEN));
REQUIRE(color_bitset.test(Color::BLUE));
constexpr magic_enum::containers::bitset<Color> color_bitset_all {Color::RED|Color::GREEN|Color::BLUE};
REQUIRE(color_bitset_all.to_string() == "RED|GREEN|BLUE");
REQUIRE(color_bitset_all.to_string( {}, '0', '1' ) == "111");
REQUIRE(color_bitset_all.to_ulong( {} ) == 7);
REQUIRE(color_bitset_all.to_ullong( {} ) == 7);
REQUIRE(color_bitset_all.all());
REQUIRE(color_bitset_all.any());
REQUIRE_FALSE(color_bitset_all.none());
constexpr magic_enum::containers::bitset<Color> color_bitset_red_green {Color::RED|Color::GREEN};
REQUIRE(color_bitset_red_green.to_string() == "RED|GREEN");
REQUIRE(color_bitset_red_green.to_string( {}, '0', '1' ) == "110");
REQUIRE(color_bitset_red_green.to_ulong( {} ) == 3);
REQUIRE(color_bitset_red_green.to_ullong( {} ) == 3);
REQUIRE_FALSE(color_bitset_red_green.all());
REQUIRE(color_bitset_red_green.any());
REQUIRE_FALSE(color_bitset_red_green.none());
}
TEST_CASE("containers_set") {
using namespace magic_enum::bitwise_operators;
using namespace magic_enum::ostream_operators;
auto color_set = magic_enum::containers::set<Color>();
REQUIRE(color_set.empty());
REQUIRE(color_set.size() == 0);
REQUIRE_FALSE(magic_enum::enum_count<Color>() == color_set.size());
color_set.insert(Color::RED);
std::ignore = color_set.insert(Color::RED);
color_set.insert(Color::GREEN);
color_set.insert(Color::BLUE);
color_set.insert(Color::RED);
color_set.insert(Color::RED|Color::GREEN);
color_set.insert(Color::RED|Color::BLUE);
color_set.insert(Color::GREEN|Color::BLUE);
color_set.insert(Color::RED|Color::GREEN|Color::BLUE);
REQUIRE_FALSE(color_set.empty());
REQUIRE(color_set.size() == 3);
REQUIRE(magic_enum::enum_count<Color>() == color_set.size());
auto color_set_compare = magic_enum::containers::set<Color>();
color_set_compare.insert(Color::BLUE);
color_set_compare.insert(Color::RED);
color_set_compare.insert(Color::GREEN);
constexpr magic_enum::containers::set color_set_filled = {Color::RED, Color::GREEN, Color::BLUE};
REQUIRE_FALSE(color_set_filled.empty());
REQUIRE(color_set_filled.size() == 3);
REQUIRE(magic_enum::enum_count<Color>() == color_set_filled.size());
magic_enum::containers::set color_set_not_const {Color::RED, Color::GREEN, Color::BLUE};
REQUIRE_FALSE(color_set_not_const.empty());
REQUIRE(color_set_not_const.size() == 3);
REQUIRE(magic_enum::enum_count<Color>() == color_set_not_const.size());
color_set_not_const.clear();
REQUIRE(color_set_not_const.empty());
REQUIRE(color_set_not_const.size() == 0);
REQUIRE_FALSE(magic_enum::enum_count<Color>() == color_set_not_const.size());
}
TEST_CASE("containers_flat_set") {
// constexpr magic_enum::containers::flat_set color_flat_set_filled = {Color::RED, Color::GREEN, Color::BLUE};
// REQUIRE_FALSE(color_flat_set_filled.empty());
// REQUIRE(color_flat_set_filled.size() == 3);
// REQUIRE(magic_enum::enum_count<Color>() == color_flat_set_filled.size());
}
TEST_CASE("map_like_container") {
using namespace magic_enum::ostream_operators;
std::vector<std::pair<Color, RGB>> map {{Color::GREEN, {0, color_max, 0}}, {Color::BLUE, {0, 0, color_max}}, {Color::RED, {color_max, 0, 0}}};
for (auto [key, value] : map) {
std::cout << "Key=" << key << " Value=" << value << std::endl;
}
auto compare = [](std::pair<Color, RGB>& lhs,
std::pair<Color, RGB>& rhs) {
return static_cast<std::int32_t>(lhs.first) < static_cast<std::int32_t>(rhs.first);
};
std::sort(std::begin(map), std::end(map), compare);
for (auto [key, value] : map) {
std::cout << "Key=" << key << " Value=" << value << std::endl;
}
}