Compare commits

..

27 Commits

Author SHA1 Message Date
Anton Bachin
520d8ee390 Update copyright year 2024-01-07 15:20:03 +03:00
Chris Heyes
d65550f378
Turn off clang-format (#118)
Turns off clang-format for easier usage in projects using it.
2024-01-07 15:18:17 +03:00
Anton Bachin
1e8f499ddf Update copyright year 2021-09-09 14:18:02 +03:00
Anton Bachin
f3ff0a6917 Remove version link from README
[skip ci]
2020-11-08 08:37:41 +03:00
Anton Bachin
68ca02d1f5 Replace tab by spaces
[skip ci]
2020-11-08 08:28:09 +03:00
Anton Bachin
517387fba0 Docs: rely on GitHub issues only for contact
[skip ci]
2020-10-19 09:11:14 +03:00
Anton Bachin
fb8b398b02 README: remove link to magic_enum
There are now several alternative enum libraries, so the README no
longer seems to be a convenient place for linking them. At this point,
they should be quite discoverable on their own.

See https://github.com/aantron/better-enums/issues/89#issuecomment-706870187.
Closes #78.

[skip ci]
2020-10-19 09:05:45 +03:00
Anton Bachin
c35576bed0 Docs: update version number
[skip ci]
2020-10-19 08:45:29 +03:00
Anton Bachin
2422dd6f26 Docs: remove Wandbox link
[skip ci]
2020-10-19 08:45:17 +03:00
Anton Bachin
894eab5284 Fix some code formatting
[skip ci]
2020-10-19 08:42:50 +03:00
Anton Bachin
fd646ae537 Document BETTER_ENUMS_CLASS_ATTRIBUTE
Follow-on to #80.

[skip ci]
2020-10-19 08:33:38 +03:00
Anton Bachin
abb693d4ef Document BETTER_ENUMS_DECLARE_STD_HASH
Follow-on to #77.

[skip ci]
2020-10-19 08:26:14 +03:00
Anton Bachin
2f1fcaae7a Docs: fix rendering of <Enum>
[skip ci]
2020-10-19 08:12:51 +03:00
Anton Bachin
3e115c0c17 Rename argument of _to_index, _from_index
Follow-on to #59.
2020-10-19 08:09:55 +03:00
Anton Bachin
c730fc8ae8 Document _to_index, _from_index, etc.
Follow-on to #59.

[skip ci]
2020-10-19 08:07:45 +03:00
Anton Bachin
f480b7ef1c Remove Patreon link
[skip ci]
2020-10-18 22:32:01 +03:00
Anton Bachin
1a3c4f9e5a Tweak note about magic_enum
Resolves #88.

[skip ci]
2020-10-08 21:50:05 +03:00
Anton Bachin
21c374914c Try GitHub Sponsors
[skip ci]
2020-01-23 01:53:00 +03:00
Jan-Gerd Tenberge
dcbd3c0554 Add BETTER_ENUMS_IGNORE_ATTRIBUTES_* for clang (#81) 2020-01-13 15:40:59 +03:00
Anton Bachin
89bc057a63 Update copyright year
[skip ci]
2020-01-01 18:16:19 +03:00
CJ Smith
5dcd59bd29 Ignores -Wattributes for GCC 7 and 8 to mask compiler bug. (#79)
See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89325
This bug produces massive amounts of spurious warnings and is present
in the version of g++ currently available in RHEL devtoolset-7 and devtoolset-8.
2019-12-07 08:15:47 +03:00
Michael Ernst
fa39f5c89e Add macro BETTER_ENUMS_CLASS_ATTRIBUTE for adding attributes to enum class (#80)
When not set it defaults to empty macro.
2019-12-06 23:23:25 +03:00
Anton Bachin
4b76e7783b Recommend magic_enum
[skip ci]
2019-08-17 23:06:40 +03:00
Anton Bachin
1ea7f04bb9 Move LICENSE so that GitHub recognizes it
[skip ci]
2019-07-25 20:16:35 +03:00
Anton Bachin
e1756cdb07 Docs: remove unnecessary scroll bars
[skip ci]
2019-07-25 20:14:08 +03:00
Sven Scharmentke
5a677ac72b Support using better-enum as key in dictionaries and maps (#77)
This enabled hashing so that unordered_map are supported.
2019-07-16 12:36:16 +03:00
Felipe Lema
8a0d376b53 fix -wold-style-cast (#74) 2019-06-19 12:47:57 -05:00
15 changed files with 202 additions and 140 deletions

1
.github/FUNDING.yml vendored Normal file
View File

@ -0,0 +1 @@
github: aantron

23
LICENSE.md Normal file
View File

@ -0,0 +1,23 @@
Copyright (c) 2012-2024, Anton Bachin
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View File

@ -1,7 +1,5 @@
# Better Enums &nbsp; [![version 0.11.2][version]][releases] [![Try online][wandbox-img]][wandbox] [![Travis status][travis-img]][travis] [![AppVeyor status][appveyor-img]][appveyor] [![BSD license][license-img]][license]
# Better Enums &nbsp; [![Try online][wandbox-img]][wandbox] [![Travis status][travis-img]][travis] [![AppVeyor status][appveyor-img]][appveyor]
[version]: https://img.shields.io/badge/version-0.11.2-blue.svg
[releases]: https://github.com/aantron/better-enums/releases
[wandbox]: http://melpon.org/wandbox/permlink/2QCi3cwQnplAToge
[wandbox-img]: https://img.shields.io/badge/try%20it-online-blue.svg
[appveyor]: https://ci.appveyor.com/project/aantron/better-enums/branch/master
@ -98,27 +96,8 @@ a comparison:
<br/>
## Contact and development
Don't hesitate to contact me about features or bugs:
[antonbachin@yahoo.com][email], or open an issue on GitHub.
If you'd like to help develop Better Enums, see [`CONTRIBUTING`][contributing].
One area that could use fresh ideas is finding a compile-time data structure
that both compiles quickly and allows lookup in sub-linear time.
[email]: mailto:antonbachin@yahoo.com
[contributing]: https://github.com/aantron/better-enums/blob/master/doc/CONTRIBUTING.md
<br/>
## License and history
Better Enums is released under the BSD 2-clause license. See
[`LICENSE`][license].
## History
The original version of the library was developed by the author in the winter of
2012-2013 at Hudson River Trading, as a replacement for an older generator
called `BETTER_ENUM`.
[license]: https://github.com/aantron/better-enums/blob/master/doc/LICENSE

View File

@ -228,7 +228,7 @@ If the given string is the exact name of a declared constant, returns the
constant. Otherwise, throws `std::runtime_error`. Running time is linear in the
number of declared constants multiplied by the length of the longest constant.
#### static constexpr optional<Enum> <em>_from_string_nothrow</em>(const char*)
#### static constexpr optional&lt;Enum&gt; <em>_from_string_nothrow</em>(const char*)
Same as [`_from_string`](#_from_string), but does not throw an exception on
failure. Returns an [optional value](#StructBetter_enumsoptional) instead.
@ -238,7 +238,7 @@ failure. Returns an [optional value](#StructBetter_enumsoptional) instead.
Same as [`_from_string`](#_from_string), but comparison is up to case, in the
usual sense in the Latin-1 encoding.
#### static constexpr optional<Enum> <em>_from_string_nocase_nothrow</em>(const char*)
#### static constexpr optional&lt;Enum&gt; <em>_from_string_nocase_nothrow</em>(const char*)
Is to [`_from_string_nocase`](#_from_string_nocase) as
[`_from_string_nothrow`](#_from_string_nothrow) is to
@ -342,7 +342,7 @@ of one of the declared constants.
<em>Enum::_from_integral</em>(<em>2</em>); // Enum::C
<em>Enum::_from_integral</em>(<em>42</em>); // std::runtime_error
#### static constexpr optional<Enum> <em>_from_integral_nothrow</em>(_integral)
#### static constexpr optional&lt;Enum&gt; <em>_from_integral_nothrow</em>(_integral)
Checked conversion as [`_from_integral`](#_from_integral), but does not throw an
exception on failure. Returns an [optional value](#StructBetter_enumsoptional)
@ -370,6 +370,34 @@ constants.
### Index lookup
#### member constexpr std::size_t <em>_to_index</em>() const
Returns the index of a Better Enum value within its enum declaration. The index
is determined from the value only; if two constants in the declaration have the
same value, this function may return the index of either constant.
If the value does not correspond to any constant in the declaration (for
example, if it was obtained using an unchecked conversion or a cast), then the
behavior of `value._to_index` is undefined.
#### static constexpr Enum <em>_from_index</em>(size_t)
Returns the value of the constant with the given index. Throws
`std::runtime_error` if not given the index of one of the constants.
#### static constexpr Enum <em>_from_index_unchecked</em>(size_t)
Returns the value of the constant with the given index. If not given one of the
constants in the declaration of the enum, the returned value is undefined.
#### static constexpr optional&lt;Enum&gt; <em>_from_index_nothrow</em>(size_t)
Returns the value of the constant with the given index.
### Stream operators
#### non-member std::ostream& <em>operator <<</em>(std::ostream&, const Enum&)
@ -385,6 +413,21 @@ as [`_from_string`](#_from_string). In case of failure, sets the stream's
### Hashing
#### macro <em>BETTER_ENUMS_DECLARE_STD_HASH</em>(Enum)
Use this outside namespace scope to declare a specialization of `std::hash` for
the type `Enum`. For example:
// This declaration might be inside a namespace.
<em>BETTER_ENUM</em>(<em>Channel</em>, int, Red, Green, Blue)
// Later, outside the namespace:
<em>BETTER_ENUMS_DECLARE_STD_HASH</em>(<em>Channel</em>)
%% class = api
%% description = Detailed description of the Better Enums API.

View File

@ -1,15 +0,0 @@
## Contact
- Send me an email: [antonbachin@yahoo.com](mailto:antonbachin@yahoo.com).
- Visit the [GitHub]($repo) project to open an issue or get a development
version.
I'm happy to hear any feedback. If you have any trouble using the library or
parsing the documentation, please don't hesitate to let me know. I'd also be
interested to hear about your use case, if you are willing to share :)
And, if you find this library helpful, give it a star on GitHub to let me know
you use it :)
%% description =
Contact information for Better Enums bugs, issues, support, and feedback.

View File

@ -1,68 +0,0 @@
Better Enums is distributed under the terms of the 2-clause BSD license. Its
text is given below.
Copyright (c) 2012-2019, Anton Bachin
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Better Enums uses the mistune library as part of its documentation generator.
Its web address and license are given below.
http://mistune.readthedocs.org/en/latest/
Copyright (c) 2014 - 2015, Hsiaoming Yang
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
* Neither the name of the creator nor the names of its contributors may be used
to endorse or promote products derived from this software without specific prior
written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View File

@ -88,3 +88,15 @@ Here they are:
%% description = Optional Better Enums features, disabled by default for
performance or compatibility reasons.
### Injecting tokens
You can define `BETTER_ENUMS_CLASS_ATTRIBUTE` before declaring a Better Enum, to
inject tokens into the class declaration. For example, in
#define <em>BETTER_ENUMS_CLASS_ATTRIBUTE</em> <em>__attribute__(foo)</em>
<em>BETTER_ENUM</em>(<em>Channel</em>, int, Red, Green, Blue)
The resulting enum declaration will begin with
class <em>__attribute__(foo)</em> <em>Channel</em> {

View File

@ -19,7 +19,7 @@ pre {
background-color: #477093;
padding: 1.5em 20px;
border-radius: 5px;
overflow: scroll;
overflow: auto;
color: rgba(255, 255, 255, 0.6);
font-size: 78%;
max-width: 84ex;

View File

@ -69,10 +69,7 @@ It's what built-in enums ought to support. Better Enums simply adds the missing
features. And, it is based on the best known techniques, thoroughly tested,
fast, portable, and documented exhaustively.
To use it, just include <code>enum.h</code>.
Try it live online in
[Wandbox](http://melpon.org/wandbox/permlink/6kFervewqeDsNAGu), or begin the
To use it, just include <code>enum.h</code> and begin the
[tutorial](${prefix}tutorial/HelloWorld.html)!
<div class="hack"></div>

View File

@ -3,7 +3,7 @@
<footer>
<div class="container">
Copyright &copy; 2015-2019 Anton Bachin. Released under the BSD 2-clause
Copyright &copy; 2015-2024 Anton Bachin. Released under the BSD 2-clause
license. See
<a href="https://github.com/aantron/better-enums/blob/$ref/doc/LICENSE">
LICENSE</a>.

View File

@ -25,7 +25,6 @@ $ga
<a href="${prefix}index.html">Home</a>
<a href="${prefix}tutorial/HelloWorld.html">Tutorial</a>
<a href="${prefix}ApiReference.html">Reference</a>
<a href="${prefix}Contact.html">Contact</a>
</div>
</nav>

View File

@ -1 +1 @@
0.11.2
0.11.3

View File

@ -1 +1 @@
0.11.2
0.11.3

80
enum.h
View File

@ -1,6 +1,7 @@
// This file is part of Better Enums, released under the BSD 2-clause license.
// See doc/LICENSE for details, or visit http://github.com/aantron/better-enums.
// See LICENSE.md for details, or visit http://github.com/aantron/better-enums.
// clang-format off
#pragma once
#ifndef BETTER_ENUMS_ENUM_H
@ -14,6 +15,48 @@
#include <stdexcept>
// in-line, non-#pragma warning handling
// not supported in very old compilers (namely gcc 4.4 or less)
#ifdef __GNUC__
# ifdef __clang__
# define BETTER_ENUMS_IGNORE_OLD_CAST_HEADER _Pragma("clang diagnostic push")
# define BETTER_ENUMS_IGNORE_OLD_CAST_BEGIN _Pragma("clang diagnostic ignored \"-Wold-style-cast\"")
# define BETTER_ENUMS_IGNORE_OLD_CAST_END _Pragma("clang diagnostic pop")
# define BETTER_ENUMS_IGNORE_ATTRIBUTES_HEADER
# define BETTER_ENUMS_IGNORE_ATTRIBUTES_BEGIN
# define BETTER_ENUMS_IGNORE_ATTRIBUTES_END
# else
# define BETTER_ENUMS_GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100)
# if BETTER_ENUMS_GCC_VERSION > 40400
# define BETTER_ENUMS_IGNORE_OLD_CAST_HEADER _Pragma("GCC diagnostic push")
# define BETTER_ENUMS_IGNORE_OLD_CAST_BEGIN _Pragma("GCC diagnostic ignored \"-Wold-style-cast\"")
# define BETTER_ENUMS_IGNORE_OLD_CAST_END _Pragma("GCC diagnostic pop")
# if (BETTER_ENUMS_GCC_VERSION >= 70300)
# define BETTER_ENUMS_IGNORE_ATTRIBUTES_HEADER _Pragma("GCC diagnostic push")
# define BETTER_ENUMS_IGNORE_ATTRIBUTES_BEGIN _Pragma("GCC diagnostic ignored \"-Wattributes\"")
# define BETTER_ENUMS_IGNORE_ATTRIBUTES_END _Pragma("GCC diagnostic pop")
# else
# define BETTER_ENUMS_IGNORE_ATTRIBUTES_HEADER
# define BETTER_ENUMS_IGNORE_ATTRIBUTES_BEGIN
# define BETTER_ENUMS_IGNORE_ATTRIBUTES_END
# endif
# else
# define BETTER_ENUMS_IGNORE_OLD_CAST_HEADER
# define BETTER_ENUMS_IGNORE_OLD_CAST_BEGIN
# define BETTER_ENUMS_IGNORE_OLD_CAST_END
# define BETTER_ENUMS_IGNORE_ATTRIBUTES_HEADER
# define BETTER_ENUMS_IGNORE_ATTRIBUTES_BEGIN
# define BETTER_ENUMS_IGNORE_ATTRIBUTES_END
# endif
# endif
#else // empty definitions for compilers that don't support _Pragma
# define BETTER_ENUMS_IGNORE_OLD_CAST_HEADER
# define BETTER_ENUMS_IGNORE_OLD_CAST_BEGIN
# define BETTER_ENUMS_IGNORE_OLD_CAST_END
# define BETTER_ENUMS_IGNORE_ATTRIBUTES_HEADER
# define BETTER_ENUMS_IGNORE_ATTRIBUTES_BEGIN
# define BETTER_ENUMS_IGNORE_ATTRIBUTES_END
#endif
// Feature detection.
@ -364,6 +407,8 @@ continue_with(T, U value) { return value; }
// Values array declaration helper.
//! Get intrinsic value of an (Enum::value) by taking advantage of
// C-conversion's parentheses priority
template <typename EnumType>
struct _eat_assign {
explicit BETTER_ENUMS_CONSTEXPR_ _eat_assign(EnumType value) : _value(value)
@ -583,6 +628,10 @@ constexpr const char *_final_ ## index = \
#endif
#ifndef BETTER_ENUMS_CLASS_ATTRIBUTE
# define BETTER_ENUMS_CLASS_ATTRIBUTE
#endif
#define BETTER_ENUMS_TYPE(SetUnderlyingType, SwitchType, GenerateSwitchType, \
GenerateStrings, ToStringConstexpr, \
DeclareInitialize, DefineInitialize, CallInitialize, \
@ -594,7 +643,7 @@ BETTER_ENUMS_ID(GenerateSwitchType(Underlying, __VA_ARGS__)) \
\
} \
\
class Enum { \
class BETTER_ENUMS_CLASS_ATTRIBUTE Enum { \
private: \
typedef ::better_enums::optional<Enum> _optional; \
typedef ::better_enums::optional<std::size_t> _optional_index; \
@ -624,12 +673,12 @@ class Enum { \
\
BETTER_ENUMS_CONSTEXPR_ std::size_t _to_index() const; \
BETTER_ENUMS_IF_EXCEPTIONS( \
BETTER_ENUMS_CONSTEXPR_ static Enum _from_index(std::size_t value); \
BETTER_ENUMS_CONSTEXPR_ static Enum _from_index(std::size_t index); \
) \
BETTER_ENUMS_CONSTEXPR_ static Enum \
_from_index_unchecked(std::size_t value); \
_from_index_unchecked(std::size_t index); \
BETTER_ENUMS_CONSTEXPR_ static _optional \
_from_index_nothrow(std::size_t value); \
_from_index_nothrow(std::size_t index); \
\
ToStringConstexpr const char* _to_string() const; \
BETTER_ENUMS_IF_EXCEPTIONS( \
@ -690,19 +739,25 @@ static ::better_enums::_initialize_at_program_start<Enum> \
\
enum _putNamesInThisScopeAlso { __VA_ARGS__ }; \
\
BETTER_ENUMS_IGNORE_OLD_CAST_HEADER \
BETTER_ENUMS_IGNORE_OLD_CAST_BEGIN \
BETTER_ENUMS_CONSTEXPR_ const Enum _value_array[] = \
{ BETTER_ENUMS_ID(BETTER_ENUMS_EAT_ASSIGN(Enum, __VA_ARGS__)) }; \
BETTER_ENUMS_IGNORE_OLD_CAST_END \
\
BETTER_ENUMS_ID(GenerateStrings(Enum, __VA_ARGS__)) \
\
} \
\
BETTER_ENUMS_IGNORE_ATTRIBUTES_HEADER \
BETTER_ENUMS_IGNORE_ATTRIBUTES_BEGIN \
BETTER_ENUMS_UNUSED BETTER_ENUMS_CONSTEXPR_ \
inline const Enum \
operator +(Enum::_enumerated enumerated) \
{ \
return static_cast<Enum>(enumerated); \
} \
BETTER_ENUMS_IGNORE_ATTRIBUTES_END \
\
BETTER_ENUMS_CONSTEXPR_ inline Enum::_optional_index \
Enum::_from_value_loop(Enum::_integral value, std::size_t index) \
@ -873,6 +928,8 @@ ToStringConstexpr inline Enum::_name_iterable Enum::_names() \
\
DefineInitialize(Enum) \
\
BETTER_ENUMS_IGNORE_ATTRIBUTES_HEADER \
BETTER_ENUMS_IGNORE_ATTRIBUTES_BEGIN \
BETTER_ENUMS_UNUSED BETTER_ENUMS_CONSTEXPR_ \
inline bool operator ==(const Enum &a, const Enum &b) \
{ return a._to_integral() == b._to_integral(); } \
@ -896,6 +953,7 @@ inline bool operator >(const Enum &a, const Enum &b) \
BETTER_ENUMS_UNUSED BETTER_ENUMS_CONSTEXPR_ \
inline bool operator >=(const Enum &a, const Enum &b) \
{ return a._to_integral() >= b._to_integral(); } \
BETTER_ENUMS_IGNORE_ATTRIBUTES_END \
\
\
template <typename Char, typename Traits> \
@ -1255,4 +1313,16 @@ BETTER_ENUMS_CONSTEXPR_ map<Enum, T> make_map(T (*f)(Enum))
}
#define BETTER_ENUMS_DECLARE_STD_HASH(type) \
namespace std { \
template <> struct hash<type> \
{ \
size_t operator()(const type &x) const \
{ \
return std::hash<size_t>()(x._to_integral()); \
} \
}; \
}
#endif // #ifndef BETTER_ENUMS_ENUM_H
// clang-format on

View File

@ -31,7 +31,10 @@ BETTER_ENUM(Namespaced, short, One, Two)
// be changed to be more precise in the future.
#ifdef BETTER_ENUMS_HAVE_CONSTEXPR_
BETTER_ENUMS_DECLARE_STD_HASH(Channel)
#include <type_traits>
#include <functional>
// Type properties.
static_assert_1(std::is_class<Channel>());
@ -161,6 +164,24 @@ static_assert_1(same_string(Depth::_names()[0], "HighColor"));
// Run-time testing.
class HashTests : public CxxTest::TestSuite {
public:
void test_same_values()
{
#ifdef _ENUM_HAVE_CONSTEXPR
TS_ASSERT_EQUALS(
std::hash<Channel>().operator()(Channel::Red),
std::hash<int>().operator()(0));
TS_ASSERT_EQUALS(
std::hash<Channel>().operator()(Channel::Green),
std::hash<int>().operator()(1));
TS_ASSERT_EQUALS(
std::hash<Channel>().operator()(Channel::Blue),
std::hash<int>().operator()(2));
#endif // #ifdef _ENUM_HAVE_CONSTEXPR
}
};
class EnumTests : public CxxTest::TestSuite {
public:
void test_constant_values()