stlab.adobe.com Adobe Systems Incorporated

Typesafe Integers and Bit Fields (enums)
[Utility]

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) enables the bitset operations ~, |, &, ^, |=, &=, ^= for enumeration type E.

ADOBE_DEFINE_ARITHMETIC_OPS(E) 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>
#include <adobe/enum_ops.hpp>

enum Foo
    {
    foo_4 = 1 << 2,
    foo_8 = 1 << 3
    };

ADOBE_DEFINE_BITSET_OPS(Foo)

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);

    return boost::report_errors();
    }

// end_of_example

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>
#include <adobe/enum_ops.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(Baz)

//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

Copyright © 2006-2007 Adobe Systems Incorporated.

Use of this website signifies your agreement to the Terms of Use and Online Privacy Policy.

Search powered by Google