57 Commits

Author SHA1 Message Date
Anuradha Dissanayake
6988e0509e Fix /W4 level warnings in MSVC++ 2015 (#41)
Remove unused variable names and use underlying class for enum (VS 2012
and above support strongly typed enums and will warn about incorrect
usage).
2017-07-02 10:30:01 -05:00
cheparukhin
37d8f987ca Add unused attribute to free functions (#27) 2016-07-19 00:18:33 +01:00
Zsolt Parragi
ab4c8583db Enabled constepxr support for clang-cl. 2016-07-09 14:50:19 +02:00
cheparukhin
d94b21a7e0 Resolve clashing global and nested namespace names
Using better_enums as a nested namespace name along a global better_enums namespace resulted in name clashes.
2016-06-22 15:52:35 +01:00
cheparukhin
9e522eaf3c Move stream I/O operators to enum namespace
Stream I/O operators defined in a global namespace caused name resolution errors in some cases.
2016-06-22 15:47:29 +01:00
Mitsutaka Takeda
e6e8e24f5c Fixed old-style cast warnings. 2016-03-12 20:33:33 -06:00
Anton Bachin
cc9cce28ae Enabled more warnings during testing.
To avoid problems with warnings in CxxTest, the extra warnings are
enabled during one test (linking) that does not use CxxTest.

Resolves #16.
2016-02-27 00:53:35 -06:00
Mikhail Ovchinnikov
76ad2256dd Removed double underscores from macro names.
Names containing (and not only beginning with) double underscores are
reserved in C++.

Resolves #15.
2016-02-24 12:24:56 -06:00
Anton Bachin
e9b6792922 Reordered some member functions.
Microsoft's incomplete constexpr implementation does not currently
allow constexpr use of constexpr functions that are defined out of line
below their point of use. The reordering in this commit is a
workaround.

While this still doesn't give MSVC constexpr support due to additional
bugs in Microsoft's implementation, maintaining the member functions in
this order makes it easier to begin each attempt to work around the
remaining compiler bugs.
2015-10-05 13:15:04 -05:00
Anton Bachin
fc0b0de647 Made default constructor customizable by a macro.
By default, Better Enums will generate with an inaccessible (private or
deleted) default constructor. However, if the user defines
BETTER_ENUMS_DEFAULT_CONSTRUCTOR(Enum), the expansion of that macro
will be used instead. The macro approach was chosen because the
expansion can include access modifiers and fragments such as
"= default".

Resolves #10.
2015-10-04 19:03:21 -05:00
Anton Bachin
9a2389cd15 Eliminated non-integral underlying types.
This was an experimental feature. Removing it to simplify maintenance.
2015-10-04 18:34:57 -05:00
Anton Bachin
e1e237a4ea Renamed top-level macro ENUM to BETTER_ENUM.
To reduce name clashes.

Fixes #11.
2015-10-04 11:03:08 -05:00
Anton Bachin
eac6afacdc Bidirectional maps between enums and any types.
Also added a C++14 configuration to testing, but using it only for
bidrectional map testing at the moment.
2015-07-11 19:32:28 -05:00
Anton Bachin
6c3d02b951 Fixed bug that disallowed some constant names.
The user-supplied constant names are used to declare an internal enum
type. They end up sharing a namespace with some internal library
values. Those internal values had names not prefixed with underscores.
As a result, it was impossible to declare enum constants with these
names.

Prefixed the internal names with underscores.
2015-07-11 10:23:34 -05:00
Anton Bachin
9273051e07 Updated documentation and appearance. 2015-07-09 00:38:45 -05:00
Anton Bachin
6278793a0b Added workarounds for VC2008.
VC2008 has two quirks. It generates linking errors if a copy
constructor is not explicitly defined on a Better Enum, and it has a
buggy interaction between the ternary operator and throw. This change
detects VC2008 and generates alternative code for that compiler.

Having an explicitly-defined copy constructor in a literal type appears
to cause an internal compiler error in g++4.7, and causes a spurious
compilation failure in g++4.8. For this reason, the copy constructor
generation is conditioned on the compiler.

The replacement code for the ternary operator is also generated
conditionally, because it uses an if-statement. The normal code has to
compile in a constexpr context, and so has to use the ternary operator
instead of the if-statement.

Resolves #6.
2015-07-07 11:22:40 -05:00
Anton Bachin
535f7f151b Fixed char16_t and char32_t detection for clang.
These are now only assumed in C++11 mode. long long is also assumed
only in C++11 mode for clang, which may make some programs that rely on
long long as an extension in C++98 fail with Better Enums. I will solve
that at a later date if it becomes a problem.
2015-07-01 15:11:29 -05:00
Anton Bachin
d90bfd6f18 Fixed some warnings with strict flags. 2015-06-20 11:50:26 -05:00
Anton Bachin
9810dd07ce Changed _size to a function.
An alternative constant _size_constant is provided for use in C++98,
for example for declaring arrays.

Also renamed underlying_traits to integral_mapping.
2015-06-19 17:05:33 -05:00
Anton Bachin
aa34aad468 Simplified underlying type traits.
Removed the function are_equal. Comparison is now done by converting
operands to their integral representation, and comparing those. Also
restored ordering of enum values along the same lines (according to
integral representation).
2015-06-15 19:29:39 -05:00
Anton Bachin
a493e90ac4 Initialization now always completed before main.
Before this change, in C++98 and C++11 "fast" mode, initializer
trimming was done "lazily" the first time _to_string or _names was
called. To make performance more "predictable", an object with static
storage is now used to force initializaton during program start-up,
when static object constructors are called.

The benefit of this change is very debatable. I had to give the static
object static linkage to avoid duplicate symbols, so there is a copy
now in each translation unit. I hope this does not increase code size
too much in realistic scenarios.

Lazy initialization checks are still performed and cannot be removed,
because other objects with static storage may try to use an enum from
their constructors before the enum's initialization is forced.
2015-06-15 18:32:57 -05:00
Anton Bachin
fc609e58aa Internal clean-up. 2015-06-15 10:48:05 -05:00
Anton Bachin
74b3a66284 Eliminated dynamic allocation.
When compile-time stringized constant name trimming is disabled (off by
default), trimming happens "lazily" - the first time the user calls a
function such as _to_string, the function allocates space for trimmed
constant names and trims them there.

With this change, space is reserved statically in a writeable char
array, and trimming happens in that array instead.
2015-06-13 16:22:07 -05:00
Anton Bachin
98232ee4fb Made enum.h build with exceptions disabled.
Throwing functions are simply omitted.
2015-06-12 17:17:41 -05:00
Anton Bachin
7502ae3c18 Included enum type names in exception messages. 2015-06-12 14:06:59 -05:00
Anton Bachin
11a1c26494 Internal improvements to stream operators. 2015-06-12 12:56:51 -05:00
Anton Bachin
0f63667106 Overloaded stream operators.
To avoid paying the huge penalty of including iostream and string for
users that don't need those headers, and to avoid creating a second,
optional header file, I resorted to defining the operators as templates
to prevent type checking until the user tries to actually use them. The
stream types and strings are wrapped in a metafunction that depends on
the template parameter. This is basically a hack, but it seems to work.
2015-06-11 23:05:46 -05:00
Anton Bachin
4314ad3fd3 Experimental generalization of underlying types.
With this change, the underlying type can be a non-integral type that
provides conversions to and from an integral type. See the test at
test/cxxtest/underlying.h for some examples - though they are more
verbose than strictly necessary, for testing needs.

Move constructors in underlying types are not supported. It has been
difficult so far to get constexpr code not to select the move
constructor, which is generally not constexpr, for various operations.
2015-06-11 20:39:46 -05:00
Anton Bachin
5edcb3e121 Made ENUM usable in namespaces. 2015-06-07 17:05:31 -05:00
Anton Bachin
41508fb114 Eliminated underscored internal macro names.
Also made a few documentation changes.
2015-06-05 19:20:18 -05:00
Anton Bachin
b24d155b7b Updated and improved documentation. 2015-06-05 13:01:28 -05:00
Anton Bachin
3a316e6f79 Made the test script use only the system compiler by default and extended some
support to VC++.

The unit test is currently not being run on VC++ due to a problem with CxxTest,
Cygwin, and paths. However, the examples are being compiled and having their
output checked, and the multiple translation unit test is being run.

Running "(cd test ; ./test.py)" should now run tests only using the default
compiler, on a Unix-like system. test.py --all runs tests on the full array of
compilers that I have installed and symlinked on my development machines.
2015-05-27 18:40:39 -05:00
Anton Bachin
ccc7858f14 Ported to Microsoft Visual Studio.
Worked around a bug with vararg macro expansion in VC++ and tested with Visual
Studio 2013. This commit does not include exhaustive tests for that compiler as
for clang and gcc. They are coming in a follow-on commit.

https://connect.microsoft.com/VisualStudio/feedback/details/521844/variadic-macro-treating-va-args-as-a-single-parameter-for-other-macros
2015-05-27 13:20:14 -05:00
Anton Bachin
2acb5743fa Complete documentation and testing overhaul.
The documentation is now generated from markdown. Samples are generated from the
tutorial pages. Testing is done by a Python script which runs the tests for a
large number of compilers.

This version is not very developer-friendly - the Python scripts need ways of
limiting what compilers they try to run. If you don't have 15 compilers
installed, you won't be able to run the tests in this commit. Fix coming soon.
2015-05-27 09:58:34 -05:00
Anton Bachin
a9ddc59808 Simplified enum.h.
This patch contains several minor changes.
- Eliminated the use of a deleted constructor in C++11. C++98 private default
  constructor is sufficient.
- Eliminated old namespace _enum and merged it with namespace better_enums.
- Prefixed size_t with std:: to comply with standards more strictly.
- Shortened feature control macros by deleting the word "FORCE".

Also moved make_macros.py.
2015-05-27 09:12:41 -05:00
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