Loading...
Searching...
No Matches
flags.h
Go to the documentation of this file.
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Copyright (C) 2020, Google Inc.
*
* Type-safe enum-based bitfields
*/
#pragma once
#include <type_traits>
namespace libcamera {
template<typename E>
class Flags
{
public:
static_assert(std::is_enum<E>::value,
"Flags<> template parameter must be an enum");
using Type = std::underlying_type_t<E>;
constexpr Flags()
: value_(0)
{
}
constexpr Flags(E flag)
: value_(static_cast<Type>(flag))
{
}
constexpr Flags &operator&=(E flag)
{
value_ &= static_cast<Type>(flag);
return *this;
}
constexpr Flags &operator&=(Flags other)
{
value_ &= other.value_;
return *this;
}
constexpr Flags &operator|=(E flag)
{
value_ |= static_cast<Type>(flag);
return *this;
}
constexpr Flags &operator|=(Flags other)
{
value_ |= other.value_;
return *this;
}
constexpr Flags &operator^=(E flag)
{
value_ ^= static_cast<Type>(flag);
return *this;
}
constexpr Flags &operator^=(Flags other)
{
value_ ^= other.value_;
return *this;
}
constexpr bool operator==(E flag)
{
return value_ == static_cast<Type>(flag);
}
constexpr bool operator==(Flags other)
{
return value_ == static_cast<Type>(other);
}
constexpr bool operator!=(E flag)
{
return value_ != static_cast<Type>(flag);
}
constexpr bool operator!=(Flags other)
{
return value_ != static_cast<Type>(other);
}
constexpr explicit operator Type() const
{
return value_;
}
constexpr explicit operator bool() const
{
return !!value_;
}
constexpr Flags operator&(E flag) const
{
return Flags(static_cast<E>(value_ & static_cast<Type>(flag)));
}
constexpr Flags operator&(Flags other) const
{
return Flags(static_cast<E>(value_ & other.value_));
}
constexpr Flags operator|(E flag) const
{
return Flags(static_cast<E>(value_ | static_cast<Type>(flag)));
}
constexpr Flags operator|(Flags other) const
{
return Flags(static_cast<E>(value_ | other.value_));
}
constexpr Flags operator^(E flag) const
{
return Flags(static_cast<E>(value_ ^ static_cast<Type>(flag)));
}
constexpr Flags operator^(Flags other) const
{
return Flags(static_cast<E>(value_ ^ other.value_));
}
constexpr Flags operator~() const
{
return Flags(static_cast<E>(~value_));
}
constexpr bool operator!() const
{
return !value_;
}
private:
Type value_;
};
#ifndef __DOXYGEN__
template<typename E>
struct flags_enable_operators {
static const bool enable = false;
};
template<typename E>
std::enable_if_t<flags_enable_operators<E>::enable, Flags<E>>
operator|(E lhs, E rhs)
{
using type = std::underlying_type_t<E>;
return Flags<E>(static_cast<E>(static_cast<type>(lhs) | static_cast<type>(rhs)));
}
template<typename E>
std::enable_if_t<flags_enable_operators<E>::enable, Flags<E>>
operator&(E lhs, E rhs)
{
using type = std::underlying_type_t<E>;
return Flags<E>(static_cast<E>(static_cast<type>(lhs) & static_cast<type>(rhs)));
}
template<typename E>
std::enable_if_t<flags_enable_operators<E>::enable, Flags<E>>
operator^(E lhs, E rhs)
{
using type = std::underlying_type_t<E>;
return Flags<E>(static_cast<E>(static_cast<type>(lhs) ^ static_cast<type>(rhs)));
}
template<typename E>
std::enable_if_t<flags_enable_operators<E>::enable, Flags<E>>
operator~(E rhs)
{
using type = std::underlying_type_t<E>;
return Flags<E>(static_cast<E>(~static_cast<type>(rhs)));
}
#define LIBCAMERA_FLAGS_ENABLE_OPERATORS(_enum) \
template<> \
struct flags_enable_operators<_enum> { \
static const bool enable = true; \
};
#else /* __DOXYGEN__ */
#define LIBCAMERA_FLAGS_ENABLE_OPERATORS(_enum)
#endif /* __DOXYGEN__ */
} /* namespace libcamera */
libcamera::Flags
Type-safe container for enum-based bitfields.
Definition flags.h:16
libcamera::Flags::operator|=
constexpr Flags & operator|=(Flags other)
Store the bitwise OR of this Flags and the other Flags in this Flags.
Definition flags.h:51
libcamera::Flags::operator==
constexpr bool operator==(E flag)
Compare flags for equality.
Definition flags.h:69
libcamera::Flags::operator&=
constexpr Flags & operator&=(Flags other)
Store the bitwise AND of this Flags and the other Flags in this Flags.
Definition flags.h:39
libcamera::Flags::operator&=
constexpr Flags & operator&=(E flag)
Store the bitwise AND of this Flags and the flag in this Flags.
Definition flags.h:33
libcamera::Flags::operator^=
constexpr Flags & operator^=(Flags other)
Store the bitwise XOR of this Flags and the other Flags in this Flags.
Definition flags.h:63
libcamera::Flags< MapFlag >::Type
std::underlying_type_t< MapFlag > Type
Definition flags.h:21
libcamera::Flags::operator|
constexpr Flags operator|(E flag) const
Compute the bitwise OR of this Flags and the flag.
Definition flags.h:109
libcamera::Flags::Flags
constexpr Flags(E flag)
Construct a Flags instance storing the flag.
Definition flags.h:28
libcamera::Flags::operator^=
constexpr Flags & operator^=(E flag)
Store the bitwise XOR of this Flags and the flag in this Flags.
Definition flags.h:57
libcamera::Flags::operator&
constexpr Flags operator&(Flags other) const
Compute the bitwise AND of this Flags and the other Flags.
Definition flags.h:104
libcamera::Flags::operator!=
constexpr bool operator!=(Flags other)
Compare flags for non-equality.
Definition flags.h:84
libcamera::Flags::operator!
constexpr bool operator!() const
Check if flags are set.
Definition flags.h:134
libcamera::Flags::operator^
constexpr Flags operator^(E flag) const
Compute the bitwise XOR of this Flags and the flag.
Definition flags.h:119
libcamera::Flags::operator|
constexpr Flags operator|(Flags other) const
Compute the bitwise OR of this Flags and the other Flags.
Definition flags.h:114
libcamera::Flags::operator^
constexpr Flags operator^(Flags other) const
Compute the bitwise XOR of this Flags and the other Flags.
Definition flags.h:124
libcamera::Flags::operator~
constexpr Flags operator~() const
Compute the bitwise NOT of this Flags.
Definition flags.h:129
libcamera::Flags::operator==
constexpr bool operator==(Flags other)
Compare flags for equality.
Definition flags.h:74
libcamera::Flags::operator|=
constexpr Flags & operator|=(E flag)
Store the bitwise OR of this Flags and the flag in this Flags.
Definition flags.h:45
libcamera::Flags::operator&
constexpr Flags operator&(E flag) const
Compute the bitwise AND of this Flags and the flag.
Definition flags.h:99
libcamera::Flags::operator!=
constexpr bool operator!=(E flag)
Compare flags for non-equality.
Definition flags.h:79
libcamera::Flags::Flags
constexpr Flags()
Construct a Flags instance with a zero value.
Definition flags.h:23
libcamera
Top-level libcamera namespace.
Definition backtrace.h:17
libcamera::operator~
constexpr Transform operator~(Transform t)
Return the transform with all the bits inverted individually.
Definition transform.h:66
libcamera::operator|
constexpr Transform operator|(Transform t0, Transform t1)
Apply bitwise OR operator between the bits in the two transforms.
Definition transform.h:32
libcamera::operator^
constexpr Transform operator^(Transform t0, Transform t1)
Apply bitwise XOR operator between the bits in the two transforms.
Definition transform.h:37
libcamera::operator&
constexpr Transform operator&(Transform t0, Transform t1)
Apply bitwise AND operator between the bits in the two transforms.
Definition transform.h:27