Adobe Source Libraries 1.49.0
A collection of C++ libraries.
Loading...
Searching...
No Matches
Typesafe Integers and Bit Fields (enums)

Description

enum_ops provides optional typesafe bitset and arithmetic operations for enumeration types. Without these typesafe operations, the compiler will promote the operand(s) to the appropriate integral type, and the result will be an integral type. When the typesafe operations have been defined for an enumeration type, E, the result will be of type E exactly when the operand(s) are of type E.

ADOBE_DEFINE_BITSET_OPS(E) or auto adobe_enable_bitmask_enum(E) -> std::true_type; enables the bitset operations ~, |, &, ^, |=, &=, ^= for enumeration type E.

ADOBE_DEFINE_ARITHMETIC_OPS(E) or auto adobe_enable_arithmetic_enum(E) -> std::true_type; enables the typesafe arithmetic operations +, -, *, /, %, +=, *=, -=, /=, %= for enumeration type E.

Definition

Defined in adobe/enum_ops.hpp

Example

The following is an example of code that will compile:

// start_of_example
#include <boost/detail/lightweight_test.hpp>
enum Foo { foo_4 = 1 << 2, foo_8 = 1 << 3 };
enum class Views : int { None = 0, Text = 1 << 0, Icon = 1 << 1, Preview = 1 << 2 };
auto stlab_enable_bitmask_enum(E) -> std::true_type;
int main() {
Foo a(foo_4);
Foo b(foo_8);
Foo c(a | b);
BOOST_TEST(a == 4L);
BOOST_TEST(b == 8L);
BOOST_TEST(c == 12L);
Views x;
x = Views::Text | Views::Icon;
BOOST_TEST((x & Views::Text) == Views::Text);
BOOST_TEST((x & Views::Preview) != Views::Preview);
return boost::report_errors();
}
// end_of_example
#define ADOBE_DEFINE_BITSET_OPS(EnumType)
Definition enum_ops.hpp:90

The following is contains an example of code that will not compile since the typesafe operators have not been defined.

// start_of_example
#include <boost/detail/lightweight_test.hpp>
enum Foo { foo_4 = 1 << 2, foo_8 = 1 << 3 };
enum Bar { bar_4 = 1 << 2, bar_8 = 1 << 3 };
enum Baz { baz_4 = 1 << 2, baz_8 = 1 << 3 };
// ADOBE_DEFINE_ARITHMETIC_OPS is not defined for Foo and Bar
int main() {
Foo a(foo_4);
Bar b(bar_8);
Foo c(a + b); // error! a and b are different enum types, so result is integral
Baz d(baz_4);
Baz e(baz_8);
Baz f(d + e); // Ok, ADOBE_DEFINE_ARITHMETIC_OPS(Baz) is defined, so d + e has type Baz
BOOST_TEST(c);
BOOST_TEST(f);
return boost::report_errors();
}
// end_of_example
#define ADOBE_DEFINE_ARITHMETIC_OPS(EnumType)
Definition enum_ops.hpp:141