Skip to content

Commit

Permalink
Merge branch 'Neargye:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
jaepil authored Nov 30, 2024
2 parents 7db7827 + 1a1824d commit 4b73790
Show file tree
Hide file tree
Showing 20 changed files with 113 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/bzlmod-archive.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
permissions:
contents: write
steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- run: git archive -o "${{ format('{0}-{1}.tar.gz', github.event.repository.name, github.event.release.tag_name) }}" HEAD
- run: gh release upload ${{ github.event.release.tag_name }} *.tar.gz
env:
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/scorecard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:

steps:
- name: "Checkout code"
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
persist-credentials: false

Expand All @@ -47,14 +47,14 @@ jobs:
# Upload the results as artifacts (optional). Commenting out will disable uploads of run results in SARIF
# format to the repository Actions tab.
- name: "Upload artifact"
uses: actions/upload-artifact@50769540e7f4bd5e21e526ee35c689e35e0d6874 # v4.4.0
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882 # v4.4.3
with:
name: SARIF file
path: results.sarif
retention-days: 5

# Upload the results to GitHub's code scanning dashboard.
- name: "Upload to code-scanning"
uses: github/codeql-action/upload-sarif@e2b3eafc8d227b0241d48be5f425d47c2d750a13 # v3.26.10
uses: github/codeql-action/upload-sarif@662472033e021d55d94146f66f6058822b0b39fd # v3.27.0
with:
sarif_file: results.sarif
4 changes: 2 additions & 2 deletions .github/workflows/ubuntu_test_installed_version.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: actions/cache@v3
- uses: actions/cache@v4
with:
path: "**/cpm_modules"
key: ${{ github.workflow }}-cpm-modules-${{ hashFiles('**/CMakeLists.txt', '**/*.cmake') }}
Expand Down
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.14)

project(
magic_enum
VERSION "0.9.6"
VERSION "0.9.7"
HOMEPAGE_URL "https://github.com/Neargye/magic_enum"
DESCRIPTION
"A library that provides static reflection for enums, work with any enum type without any macro or boilerplate code."
Expand Down Expand Up @@ -81,7 +81,7 @@ if(MAGIC_ENUM_OPT_INSTALL)

install(
DIRECTORY ${PROJECT_SOURCE_DIR}/include/${PROJECT_NAME}
DESTINATION include
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
FILES_MATCHING
PATTERN "${PROJECT_INCLUDE_HEADER_PATTERN}"
)
Expand Down
2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module(
name = "magic_enum",
version = "0.9.6",
version = "0.9.7",
compatibility_level = 0,
)
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@ If you like this project, please consider donating to one of the funds that help

## [Features & Examples](example/)

* Basic

```cpp
#include <magic_enum/magic_enum.hpp>
#include <iostream>

enum class Color : { RED = -10, BLUE = 0, GREEN = 10 };

int main() {
Color c1 = Color::RED;
std::cout << magic_enum::enum_name(c1) << std::endl; // RED
return 0;
}
```
* Enum value to string
```cpp
Expand Down Expand Up @@ -291,7 +306,7 @@ If you like this project, please consider donating to one of the funds that help

## Integration

* You should add the required file [magic_enum.hpp](include/magic_enum.hpp), and optionally other headers from [include dir](include/) or [release archive](https://github.com/Neargye/magic_enum/releases/latest). Alternatively, you can build the library with CMake.
* You should add the required file [magic_enum.hpp](include/magic_enum/magic_enum.hpp), and optionally other headers from [include dir](include/) or [release archive](https://github.com/Neargye/magic_enum/releases/latest). Alternatively, you can build the library with CMake.

* If you are using [vcpkg](https://github.com/Microsoft/vcpkg/) on your project for external dependencies, then you can use the [magic-enum package](https://github.com/microsoft/vcpkg/tree/master/ports/magic-enum).

Expand Down
21 changes: 17 additions & 4 deletions cmake/GenPkgConfig/GenPkgConfig.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,23 @@ function(configure_pkg_config_file_vars TARGET _NAME _INSTALL_LIB_DIR _INSTALL_I
string(REPLACE "," "$<COMMA>" NEEDS_LIBS_ESCAPED "${NEEDS_LIBS}")
string(REPLACE ">" "$<ANGLE-R>" NEEDS_LIBS_ESCAPED "${NEEDS_LIBS_ESCAPED}")

list(APPEND header "prefix=${CMAKE_INSTALL_PREFIX}")
list(APPEND header "$<IF:$<OR:$<BOOL:${PUBLIC_LIBS}>,${NEEDS_LIB_DIR}>,libdir=\${prefix}/${INSTALL_LIB_DIR},>")
list(APPEND header "$<IF:$<BOOL:${PUBLIC_INCLUDES}>,includedir=\${prefix}/${INSTALL_INCLUDE_DIR},>")

# Only use prefix if paths are not absolute like they are with nix
# See also: https://github.com/NixOS/nixpkgs/issues/144170
if(NOT(IS_ABSOLUTE ${INSTALL_LIB_DIR} AND IS_ABSOLUTE ${INSTALL_INCLUDE_DIR}))
list(APPEND header "prefix=${CMAKE_INSTALL_PREFIX}")
endif()

if(IS_ABSOLUTE ${INSTALL_LIB_DIR})
list(APPEND header "$<IF:$<OR:$<BOOL:${PUBLIC_LIBS}>,${NEEDS_LIB_DIR}>,libdir=${INSTALL_LIB_DIR},>")
else()
list(APPEND header "$<IF:$<OR:$<BOOL:${PUBLIC_LIBS}>,${NEEDS_LIB_DIR}>,libdir=\${prefix}/${INSTALL_LIB_DIR},>")
endif()

if(IS_ABSOLUTE ${INSTALL_INCLUDE_DIR})
list(APPEND header "$<IF:$<BOOL:${PUBLIC_INCLUDES}>,includedir=${INSTALL_INCLUDE_DIR},>")
else()
list(APPEND header "$<IF:$<BOOL:${PUBLIC_INCLUDES}>,includedir=\${prefix}/${INSTALL_INCLUDE_DIR},>")
endif()

list(APPEND libSpecific "Name: ${_NAME}")
if(_DESCRIPTION)
Expand Down
4 changes: 2 additions & 2 deletions doc/limitations.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@
```cpp
#define MAGIC_ENUM_RANGE_MIN 0
#define MAGIC_ENUM_RANGE_MAX 256
#include <magic_enum.hpp>
#include <magic_enum/magic_enum.hpp>
```
* If you need a different range for a specific enum type, add the specialization `enum_range` for the enum type. Specializations of `enum_range` must be injected in `namespace magic_enum::customize`.
```cpp
#include <magic_enum.hpp>
#include <magic_enum/magic_enum.hpp>
enum class number { one = 100, two = 200, three = 300 };
Expand Down
52 changes: 46 additions & 6 deletions doc/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
#define MAGIC_ENUM_USING_ALIAS_STRING using string = my_lib::String;
#define MAGIC_ENUM_USING_ALIAS_STRING_VIEW using string_view = my_lib::StringView;
#define MAGIC_ENUM_USING_ALIAS_OPTIONAL template <typename T> using optional = my_lib::Optional<T>;
#include <magic_enum.hpp>
#include <magic_enum/magic_enum.hpp>
```
* Optionally define `MAGIC_ENUM_CONFIG_FILE` i.e., in your build system, with path to header file with defined
Expand Down Expand Up @@ -76,6 +76,8 @@ template <typename E, typename BinaryPredicate>
constexpr optional<E> enum_cast(string_view value, BinaryPredicate p) noexcept(is_nothrow_invocable_v<BinaryPredicate>);
```

* Defined in header `<magic_enum/magic_enum.hpp>`

* Obtains enum value from string or integer.

* Returns `optional<E>`, using `has_value()` to check contains enum value and `value()` to get the enum value.
Expand Down Expand Up @@ -125,6 +127,8 @@ template <typename E, size_t I>
constexpr E enum_value() noexcept;
```
* Defined in header `<magic_enum/magic_enum.hpp>`
* Returns enum value at specified index.
* `enum_value(value)` no bounds checking is performed: the behavior is undefined if `index >= number of enum values`.
* `enum_value<value>()` check if `I >= number of enum values`, occurs the compilation error `magic_enum::enum_value out of range`.
Expand All @@ -149,6 +153,8 @@ template <typename E>
constexpr array<E, N> enum_values() noexcept;
```

* Defined in header `<magic_enum/magic_enum.hpp>`

* Returns `array<E, N>` with all enum values where `N = number of enum values`, sorted by enum value.

* Examples
Expand All @@ -166,6 +172,8 @@ template <typename E>
constexpr size_t enum_count() noexcept;
```

* Defined in header `<magic_enum/magic_enum.hpp>`

* Returns number of enum values.

* Examples
Expand All @@ -185,6 +193,8 @@ template <typename E>
constexpr underlying_type_t<E> enum_underlying(E value) noexcept;
```
* Defined in header `<magic_enum/magic_enum.hpp>`
* Returns integer value from enum value.
* Examples
Expand All @@ -205,6 +215,8 @@ template <auto V>
constexpr string_view enum_name() noexcept;
```
* Defined in header `<magic_enum/magic_enum.hpp>`
* Returns name from enum value as `string_view` with null-terminated string.
* If enum value does not have name or [out of range](limitations.md), `enum_name(value)` returns empty string.
* If enum value does not have name, `enum_name<value>()` occurs the compilation error `magic_enum::enum_name enum value does not have a name`.
Expand Down Expand Up @@ -232,6 +244,8 @@ template <typename E>
constexpr array<string_view, N> enum_names() noexcept;
```

* Defined in header `<magic_enum/magic_enum.hpp>`

* Returns `array<string_view, N>` with all names where `N = number of enum values`, sorted by enum value.

* Examples
Expand All @@ -249,6 +263,8 @@ template <typename E>
constexpr array<pair<E, string_view>, N> enum_entries() noexcept;
```

* Defined in header `<magic_enum/magic_enum.hpp>`

* Returns `array<pair<E, string_view>, N>` with all pairs (value, name) where `N = number of enum values`, sorted by enum value.

* Examples
Expand All @@ -270,6 +286,8 @@ template <auto V>
constexpr size_t enum_index() noexcept;
```
* Defined in header `<magic_enum/magic_enum.hpp>`
* Obtains index in enum values from enum value.
* `enum_index(value)` returns `optional<size_t>` with index.
* `enum_index<value>()` returns index. If enum value does not have a index, occurs the compilation error `magic_enum::enum_index enum value does not have a index`.
Expand Down Expand Up @@ -303,6 +321,8 @@ template <typename E, typename BinaryPredicate>
constexpr bool enum_contains(string_view value, BinaryPredicate p) noexcept(is_nothrow_invocable_v<BinaryPredicate>);
```
* Defined in header `<magic_enum/magic_enum.hpp>`
* Checks whether enum contains enumerator with such value.
* Returns true is enum contains value, otherwise false.
Expand All @@ -327,6 +347,8 @@ template <typename E>
constexpr bool enum_reflected(underlying_type_t<E> value) noexcept;
```
* Defined in header `<magic_enum/magic_enum.hpp>`
* Returns true if the enum value is in the range of values that can be reflected.
## `enum_type_name`
Expand All @@ -336,6 +358,8 @@ template <typename E>
constexpr string_view enum_type_name() noexcept;
```

* Defined in header `<magic_enum/magic_enum.hpp>`

* Returns type name of enum as `string_view` null-terminated string.

* Examples
Expand All @@ -353,7 +377,7 @@ template <typename... Es>
constexpr optional<enum_fuse_t> enum_fuse(Es... values) noexcept;
```
* You should add the required file `<magic_enum_fuse.hpp>`.
* Defined in header `<magic_enum/magic_enum_fuse.hpp>`
* Returns a typesafe bijective mix of several enum values. This can be used to emulate 2D switch/case statements.
Expand Down Expand Up @@ -384,7 +408,7 @@ template <typename Result, typename E, typename Lambda>
constexpr Result enum_switch(Lambda&& lambda, E value, Result&& result);
```
* You should add the required file `<magic_enum_switch.hpp>`.
* Defined in header `<magic_enum/magic_enum_switch.hpp>`
* Examples
Expand All @@ -404,6 +428,8 @@ template <typename E, typename Lambda>
constexpr auto enum_for_each(Lambda&& lambda);
```
* Defined in header `<magic_enum/magic_enum_utility.hpp>`
* Examples
```cpp
Expand Down Expand Up @@ -442,7 +468,7 @@ template <typename E, typename BinaryPredicate>
constexpr bool enum_flags_contains(string_view value, BinaryPredicate p) noexcept(is_nothrow_invocable_v<BinaryPredicate>);
```
* You should add the required file `<magic_enum_flags.hpp>`.
* Defined in header `<magic_enum/magic_enum_flags.hpp>`
* For enum-flags add `is_flags` to specialization `enum_range` for necessary enum type. Specialization of `enum_range` must be injected in `namespace magic_enum::customize`.
```cpp
Expand Down Expand Up @@ -496,6 +522,8 @@ template <typename T>
inline constexpr bool is_unscoped_enum_v = is_unscoped_enum<T>::value;
```

* Defined in header `<magic_enum/magic_enum.hpp>`

* Checks whether type is an [Unscoped enumeration](https://en.cppreference.com/w/cpp/language/enum#Unscoped_enumeration).

* Provides the member constant value which is equal to true, if T is an [Unscoped enumeration](https://en.cppreference.com/w/cpp/language/enum#Unscoped_enumeration) type.</br>
Expand All @@ -521,6 +549,8 @@ template <typename T>
inline constexpr bool is_scoped_enum_v = is_scoped_enum<T>::value;
```

* Defined in header `<magic_enum/magic_enum.hpp>`

* Checks whether type is an [Scoped enumeration](https://en.cppreference.com/w/cpp/language/enum#Scoped_enumerations).

* Provides the member constant value which is equal to true, if T is an [Scoped enumeration](https://en.cppreference.com/w/cpp/language/enum#Scoped_enumerations) type.</br>
Expand All @@ -546,6 +576,8 @@ template <typename T>
using underlying_type_t = typename underlying_type<T>::type;
```

* Defined in header `<magic_enum/magic_enum.hpp>`

* Improved UB-free "SFINAE-friendly" [underlying_type](https://en.cppreference.com/w/cpp/types/underlying_type).

* If T is a complete enumeration type, provides a member typedef type that names the underlying type of T.</br>
Expand All @@ -571,7 +603,7 @@ template <typename Char, typename Traits, typename E>
basic_ostream<Char, Traits>& operator<<(basic_ostream<Char, Traits>& os, optional<E> value);
```

* You should add the required file `<magic_enum_iostream.hpp>`.
* Defined in header `<magic_enum/magic_enum_iostream.hpp>`

* Out-of-the-box ostream operators for all enums.

Expand All @@ -590,7 +622,7 @@ template <typename Char, typename Traits, typename E>
basic_istream<Char, Traits>& operator>>(basic_istream<Char, Traits>& is, E& value);
```

* You should add the required file `<magic_enum_iostream.hpp>`.
* Defined in header `<magic_enum/magic_enum_iostream.hpp>`

* Out-of-the-box istream operators for all enums.

Expand Down Expand Up @@ -627,6 +659,8 @@ template <typename E>
constexpr E& operator^=(E& lhs, E rhs) noexcept;
```

* Defined in header `<magic_enum/magic_enum.hpp>`

* Out-of-the-box bitwise operators for all enums.

* Examples
Expand Down Expand Up @@ -712,6 +746,8 @@ struct array {
}
```

* Defined in header `<magic_enum/magic_enum_containers.hpp>`

* STL like array for all enums.

* Examples
Expand Down Expand Up @@ -824,6 +860,8 @@ class bitset {
}
```
* Defined in header `<magic_enum/magic_enum_containers.hpp>`
* STL like bitset for all enums.
* Examples
Expand Down Expand Up @@ -975,6 +1013,8 @@ class set {
}
```
* Defined in header `<magic_enum/magic_enum_containers.hpp>`
* STL like set for all enums.
* Examples
Expand Down
4 changes: 2 additions & 2 deletions include/magic_enum/magic_enum.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// | | | | (_| | (_| | | (__ | |____| | | | |_| | | | | | | | |____|_| |_|
// |_| |_|\__,_|\__, |_|\___| |______|_| |_|\__,_|_| |_| |_| \_____|
// __/ | https://github.com/Neargye/magic_enum
// |___/ version 0.9.6
// |___/ version 0.9.7
//
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
// SPDX-License-Identifier: MIT
Expand Down Expand Up @@ -34,7 +34,7 @@

#define MAGIC_ENUM_VERSION_MAJOR 0
#define MAGIC_ENUM_VERSION_MINOR 9
#define MAGIC_ENUM_VERSION_PATCH 6
#define MAGIC_ENUM_VERSION_PATCH 7

#ifndef MAGIC_ENUM_USE_STD_MODULE
#include <array>
Expand Down
Loading

0 comments on commit 4b73790

Please sign in to comment.