OpenTwin 0.1
OpenTwin
 
Loading...
Searching...
No Matches
Flags.h File Reference

Go to the source code of this file.

Classes

class  ot::Flags< T >
 The Flags class is a wrapper around a enum that allows bitwise operations (flags). OT_ADD_FLAG_FUNCTIONS or custom bitwise operations must be provided for the enum. The type should be an enumeration where every value represents a single bit in a 32/64 bit value. More...
 

Namespaces

namespace  ot
 

Macros

#define OT_ADD_FLAG_FUNCTIONS_IMPL(___prefix, ___castType, ___enumName)
 Adds bitwise operations for the provided enum. Use conveinience OT_ADD_FLAG_FUNCTIONS or OT_ADD_FRIEND_FLAG_FUNCTIONS for most cases.
 
#define OT_ADD_FLAG_FUNCTIONS(___enumName)   OT_ADD_FLAG_FUNCTIONS_IMPL(, long, ___enumName)
 Will add the default bitwise operations for the provided private 32/64 bit bitfield. Use this if the enum is in a public scope.
 
#define OT_ADD_FRIEND_FLAG_FUNCTIONS(___enumName)   OT_ADD_FLAG_FUNCTIONS_IMPL(friend , long, ___enumName)
 Will add the default bitwise operations for the provided private 32/64 bit bitfield. Use this at the same scope inside the class where the enum is defined.
It is not wrong to use OT_ADD_FRIEND_FLAG_FUNCTIONS instead of OT_ADD_FLAG_FUNCTIONS but the scope is important.
 

Detailed Description

Author
Alexander Kuester (alexk95)

Macro Definition Documentation

◆ OT_ADD_FLAG_FUNCTIONS

#define OT_ADD_FLAG_FUNCTIONS ( ___enumName)    OT_ADD_FLAG_FUNCTIONS_IMPL(, long, ___enumName)

Will add the default bitwise operations for the provided private 32/64 bit bitfield. Use this if the enum is in a public scope.

namespace MyNamespace {
    enum MyEnum {
        ...
    };
    typedef ot::Flags<MyEnum> MyFlags;

    // or

    class MyClass {
    private:
        enum MyEnum2 {
            ...
        };
        typedef ot::Flags<MyEnum2> MyFlags2;
    };
}

OT_ADD_FLAG_FUNCTIONS(MyNamespace::MyEnum)
OT_ADD_FLAG_FUNCTIONS(MyClass::MyEnum2)
Note
Only enumration types are allowed.

◆ OT_ADD_FLAG_FUNCTIONS_IMPL

#define OT_ADD_FLAG_FUNCTIONS_IMPL ( ___prefix,
___castType,
___enumName )
Value:
___prefix constexpr ___enumName operator | (___enumName _lhv, ___enumName _rhv) { return static_cast<___enumName>(static_cast<___castType>(_lhv) | static_cast<___castType>(_rhv)); }; \
___prefix constexpr ___enumName operator & (___enumName _lhv, ___enumName _rhv) { return static_cast<___enumName>(static_cast<___castType>(_lhv) & static_cast<___castType>(_rhv)); }; \
___prefix constexpr ___enumName operator ~ (___enumName _lhv) { return static_cast<___enumName>(~(static_cast<___castType>(_lhv))); };

Adds bitwise operations for the provided enum. Use conveinience OT_ADD_FLAG_FUNCTIONS or OT_ADD_FRIEND_FLAG_FUNCTIONS for most cases.

◆ OT_ADD_FRIEND_FLAG_FUNCTIONS

#define OT_ADD_FRIEND_FLAG_FUNCTIONS ( ___enumName)    OT_ADD_FLAG_FUNCTIONS_IMPL(friend , long, ___enumName)

Will add the default bitwise operations for the provided private 32/64 bit bitfield. Use this at the same scope inside the class where the enum is defined.
It is not wrong to use OT_ADD_FRIEND_FLAG_FUNCTIONS instead of OT_ADD_FLAG_FUNCTIONS but the scope is important.

class MyClass {
private:
    enum MyEnum {
        ...
    };
    typedef ot::Flags<MyEnum> MyFlags;

    // Friend is needed since enum is in a private scope.
    OT_ADD_FRIEND_FLAG_FUNCTIONS(MyClass::MyEnum)
};

//  - - - or - - -

class MyClass2 {
public:
    enum MyEnum2 {
        ...
    };
    typedef ot::Flags<MyEnum2> MyFlags2;

    // Friend is not needed but also not wrong.
    // Instead of OT_ADD_FRIEND_FLAG_FUNCTIONS use:
    // OT_ADD_FLAG_FUNCTIONS at the end of the file.
    OT_ADD_FRIEND_FLAG_FUNCTIONS(MyClass2::MyEnum2)
};

//  - - - or - - -

class MyClass3 {
private:
        class MyNestedClass {
        public:
            enum MyEnum3 {
                ...
            };
            typedef ot::Flags<MyEnum3> MyFlags3;

            // Friend is needed since it is a public enum
            // but the parent class is in a private scope.
            OT_ADD_FRIEND_FLAG_FUNCTIONS(MyClass3::MyEnum3)
        };
};

// < Here only OT_ADD_FLAG_FUNCTIONS >
Note
Only enumration types are allowed.