72 Commits

Author SHA1 Message Date
Anton Bachin
b4be7537b6 Fixed bug with missing constructor deletion, removed reference to nullptr. 2015-05-27 09:05:10 -05:00
Anton Bachin
1ad787f94a Renamed some constants and pp_map_gen.py. 2015-05-27 09:05:10 -05:00
Anton Bachin
e784f0c860 Made enum class (strict) conversion opt-in on a global basis.
This makes C++98 and C++11 Better Enums fully compatible by default. If the user
defines BETTER_ENUMS_FORCE_STRICT_CONVERSION before including enum.h, it is
necessary to prefix enum constants in switch cases with '+', but Better Enums
are not implicitly convertible to integers.
2015-05-27 09:02:27 -05:00
Anton Bachin
5ce9537d66 Made all-constexpr (slow) enums an opt-in feature. 2015-05-27 09:02:27 -05:00
Anton Bachin
156b9d9b04 Used explicit inline functions to simplify type hierarchy, also simplified iterables names. 2015-05-27 09:00:19 -05:00
Anton Bachin
d0e4d9ffaa Fixed problem with multiple compilation units under C++98. 2015-05-27 08:57:45 -05:00
Anton Bachin
6bcca9bc8c Refactored using more higher-order macros. 2015-05-27 08:54:50 -05:00
Anton Bachin
96c274a708 Made comparison operators global to simplify them. 2015-05-27 08:47:18 -05:00
Anton Bachin
d01aacd454 Prefixed .to_* methods with underscores to avoid name conflicts. 2015-05-27 08:47:17 -05:00
Anton Bachin
5d27fd83cf Fixed incorrect definition of optional::operator ->. 2015-05-27 08:47:17 -05:00
Anton Bachin
a2d738a913 Renamed remaining uppercased types in public interface to lowercase. 2015-05-27 08:47:17 -05:00
Ben Alex
832dad561f Modifications to support aggressive compiler warning levels.
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.
2015-05-27 14:18:12 +10:00
Anton Bachin
f74de74604 Port to C++98 with variadic macros.
enum.h tries to automatically detect whether it is running with C++11 support.
If not, it emits alternative code that is supposed to work on compilers
supporting C++98 and variadic macros. This code is largely interface-compatible
with the C++11 code, with the following semantic differences:

- No compile-time stringization. This is done upon first use of a function other
  than to_integral.
- Implicit conversion to integral types. This is due to the lack of enum class
  support.
- The values _name, _names, _values are replaced with functions _name_, _names_,
  _values_.
2015-05-19 16:22:06 -05:00
Anton Bachin
1b3d1cc784 Forbade nearly all implicit conversions to integral types.
Each Better Enum now has an internal enum class type to which it is convertible,
instead of being convertible to the regular enum that defines its constants.
switch statements are compiled at the enum class type. This comes at the price
of the user having to type +Enum::Constant instead of Enum::Constant in cases,
in order to trigger an explicit promotion of the pre-C++11 enum to Better Enum,
so it can then be implicitly converted to the enum class.

The remaining "hole" is that direct references to constants (Enum::Constant) are
still implicitly convertible to integral types, because they have naked
pre-C++11 enum type.
2015-05-18 19:56:17 -05:00
Anton Bachin
08dbe47edd Added non-throwing versions of enum introduction functions.
These return values of an optional type better_enums::optional<T>. This type is
defined in the spirit of boost::optional<T> and std::optional<T>, but is easy to
manipulate at compile time. Two additional macros BETTER_ENUMS_USE_OPTIONAL and
BETTER_ENUMS_EXTRA_INCLUDE are honored, whose intent is for the user to be able
to inject an alternative option type. However, there are currently no viable
alternatives. boost::optional<T> does not play well with constexpr, and I failed
to make the code compile with std::optional<T>. I did not try very hard. I
intend to support std::optional<T> in the future. Perhaps it will be the
default, when available.
2015-05-18 17:15:38 -05:00
Anton Bachin
d9bd109172 Removed range properties.
They can now be easily computed using the random access iterators. There appears
to be a slight performance improvement.
2015-05-18 09:29:35 -05:00
Anton Bachin
1cfe53e827 Option to opt in to implicit conversion to enum class instead of enum.
A Better Enum is normally implicitly convertible to its internal enum type,
which makes it then implicitly convertible to an integer as well. The former
conversion is necessary for Better Enums to be usable in switch statements.
This change makes it possible to define BETTER_ENUMS_SAFER_SWITCH, which makes
Better Enums convert to an enum class, preventing the implicit conversion to
integers. The drawback is that switch cases have to be written as

    case Enum::_Case::A:

instead of

    case Enum::A:
2015-05-18 00:53:00 -05:00
Anton Bachin
6c60edcc46 Subscript operator for iterables and tests for constexpr iterators. 2015-05-17 22:13:44 -05:00
Anton Bachin
14bc834f7d Made to_string conversion constexpr and removed the last of the weak symbols.
The interface is now uniformly constexpr, including to_string and the _names
iterable. Without the weak symbol, the remaining code is also entirely standard
C++.

The compile-time string trimming code in this commit has a negative impact on
performance. The performance test is now twice as slow as including <iostream>,
whereas before it was faster. That test declares an excessive number of enums,
though, so perhaps in typical usage, and with some future optimizations, the
impact will not be so significant.

There may be other ways to solve this, such as providing a version of the macro
that does not trim strings at compile time, but only checks if they need
trimming. If some string does need trimming, that macro would fail a
static_assert and ask the user to use the slow macro.
2015-05-17 21:29:26 -05:00
Anton Bachin
1465901bc1 Removed most weak symbols. Iterators should now be random access.
The remaining weak symbol will be removed when string conversions become
constexpr. Iterator are random access because they are now pointers.
2015-05-17 18:47:51 -05:00
Anton Bachin
460bb806a3 Eliminated separate map macro file and inlined its contents into enum.h. 2015-05-17 18:44:39 -05:00
Anton Bachin
dd606fd450 Initial release. 2015-05-11 17:38:41 -04:00