Loading...
Searching...
No Matches
flags.h
Go to the documentation of this file.
/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2020 John Wellbelove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/
#ifndef ETL_FLAGS_INCLUDED
#define ETL_FLAGS_INCLUDED
#include "platform.h"
#include "algorithm.h"
#include "initializer_list.h"
#include "integral_limits.h"
#include "static_assert.h"
#include "type_traits.h"
#include <stddef.h>
namespace etl
{
//*************************************************************************
//*************************************************************************
template <typename T, T MASK = etl::integral_limits<T>::max>
class flags
{
public:
ETL_STATIC_ASSERT(etl::is_integral<T>::value&& etl::is_unsigned<T>::value, "Unsigned integral values only");
typedef T value_type;
static ETL_CONSTANT value_type ALL_SET = etl::integral_limits<value_type>::max & MASK;
static ETL_CONSTANT value_type ALL_CLEAR = 0;
static ETL_CONSTANT size_t NBITS = etl::integral_limits<value_type>::bits;
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR flags() ETL_NOEXCEPT
: data(value_type(0))
{
}
ETL_CONSTEXPR flags(value_type pattern) ETL_NOEXCEPT
: data(pattern & MASK)
{
}
ETL_CONSTEXPR flags(const flags<T, MASK>& pattern) ETL_NOEXCEPT
: data(pattern.value())
{
}
//*************************************************************************
//*************************************************************************
template <value_type pattern>
ETL_CONSTEXPR bool test() const ETL_NOEXCEPT
{
return (data & pattern) != value_type(0);
}
//*******************************************
ETL_CONSTEXPR bool test(value_type pattern) const ETL_NOEXCEPT
{
return (data & pattern) != value_type(0);
}
//*************************************************************************
//*************************************************************************
template <value_type pattern, bool value>
ETL_CONSTEXPR14 flags<T, MASK>& set() ETL_NOEXCEPT
{
value ? data |= (pattern & MASK) : data &= static_cast<value_type>(~pattern & MASK);
return *this;
}
//*******************************************
template <value_type pattern>
ETL_CONSTEXPR14 flags<T, MASK>& set(bool value) ETL_NOEXCEPT
{
value ? data |= (pattern & MASK) : data &= static_cast<value_type>(~pattern & MASK);
return *this;
}
//*******************************************
template <value_type pattern>
ETL_CONSTEXPR14 flags<T, MASK>& set() ETL_NOEXCEPT
{
data |= (pattern & MASK);
return *this;
}
//*******************************************
ETL_CONSTEXPR14 flags<T, MASK>& set(value_type pattern) ETL_NOEXCEPT
{
data |= (pattern & MASK);
return *this;
}
//*******************************************
ETL_CONSTEXPR14 flags<T, MASK>& set(value_type pattern, bool value) ETL_NOEXCEPT
{
value ? data |= (pattern & MASK) : data &= static_cast<value_type>(~pattern & MASK);
return *this;
}
//*************************************************************************
ETL_CONSTEXPR14 flags<T, MASK>& clear() ETL_NOEXCEPT
{
data = ALL_CLEAR;
return *this;
}
//*************************************************************************
//*************************************************************************
template <value_type pattern>
ETL_CONSTEXPR14 flags<T, MASK>& reset() ETL_NOEXCEPT
{
data &= static_cast<value_type>(~pattern);
return *this;
}
//*******************************************
ETL_CONSTEXPR14 flags<T, MASK>& reset(value_type pattern) ETL_NOEXCEPT
{
data &= static_cast<value_type>(~pattern);
return *this;
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR14 flags<T, MASK>& flip() ETL_NOEXCEPT
{
data = static_cast<value_type>(~data & MASK);
return *this;
}
//*******************************************
template <value_type pattern>
ETL_CONSTEXPR14 flags<T, MASK>& flip() ETL_NOEXCEPT
{
data ^= pattern & MASK;
return *this;
}
//*******************************************
ETL_CONSTEXPR14 flags<T, MASK>& flip(value_type pattern) ETL_NOEXCEPT
{
data ^= pattern & MASK;
return *this;
}
//*************************************************************************
// Are all the bits sets?
//*************************************************************************
ETL_CONSTEXPR bool all() const ETL_NOEXCEPT
{
return data == MASK;
}
//*******************************************
template <value_type pattern>
ETL_CONSTEXPR bool all_of() const ETL_NOEXCEPT
{
return (data & (pattern & MASK)) == (pattern & MASK);
}
//*******************************************
ETL_CONSTEXPR bool all_of(value_type pattern) const ETL_NOEXCEPT
{
return (data & (pattern & MASK)) == (pattern & MASK);
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR bool none() const ETL_NOEXCEPT
{
return (data & MASK) == ALL_CLEAR;
}
//*******************************************
template <value_type pattern>
ETL_CONSTEXPR bool none_of() const ETL_NOEXCEPT
{
return !any_of(pattern);
}
//*******************************************
ETL_CONSTEXPR bool none_of(value_type pattern) const ETL_NOEXCEPT
{
return !any_of(pattern);
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR bool any() const ETL_NOEXCEPT
{
return (data & MASK) != value_type(0);
}
//*******************************************
template <value_type pattern>
ETL_CONSTEXPR bool any_of() const ETL_NOEXCEPT
{
return (data & (pattern & MASK)) != value_type(0);
}
//*******************************************
ETL_CONSTEXPR bool any_of(value_type pattern) const
{
return (data & (pattern & MASK)) != value_type(0);
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR value_type value() const ETL_NOEXCEPT
{
return data;
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR14 flags<T, MASK>& value(value_type pattern) ETL_NOEXCEPT
{
data = pattern & MASK;
return *this;
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR operator value_type() const ETL_NOEXCEPT
{
return data;
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR14 flags<T, MASK>& operator&=(value_type pattern) ETL_NOEXCEPT
{
data &= pattern;
return *this;
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR14 flags<T, MASK>& operator|=(value_type pattern) ETL_NOEXCEPT
{
data |= (pattern & MASK);
return *this;
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR14 flags<T, MASK>& operator^=(value_type pattern) ETL_NOEXCEPT
{
data ^= (pattern & MASK);
return *this;
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR14 flags<T, MASK>& operator=(flags<T, MASK> other) ETL_NOEXCEPT
{
data = other.data;
return *this;
}
//*************************************************************************
//*************************************************************************
ETL_CONSTEXPR14 flags<T, MASK>& operator=(value_type pattern) ETL_NOEXCEPT
{
data = (pattern & MASK);
return *this;
}
//*************************************************************************
//*************************************************************************
void swap(flags<T, MASK>& other) ETL_NOEXCEPT
{
using ETL_OR_STD::swap;
swap(data, other.data);
}
private:
value_type data;
};
template <typename T, T MASK>
ETL_CONSTANT typename flags<T, MASK>::value_type flags<T, MASK>::ALL_SET;
template <typename T, T MASK>
ETL_CONSTANT typename flags<T, MASK>::value_type flags<T, MASK>::ALL_CLEAR;
template <typename T, T MASK>
ETL_CONSTANT size_t flags<T, MASK>::NBITS;
//***************************************************************************
//***************************************************************************
template <typename T, T MASK>
ETL_CONSTEXPR bool operator==(flags<T, MASK> lhs, flags<T, MASK> rhs) ETL_NOEXCEPT
{
return lhs.value() == rhs.value();
}
//***************************************************************************
//***************************************************************************
template <typename T, T MASK>
ETL_CONSTEXPR bool operator!=(flags<T, MASK> lhs, flags<T, MASK> rhs) ETL_NOEXCEPT
{
return !(lhs == rhs);
}
//*************************************************************************
//*************************************************************************
template <typename T, T MASK>
void swap(etl::flags<T, MASK>& lhs, etl::flags<T, MASK>& rhs) ETL_NOEXCEPT
{
lhs.swap(rhs);
}
} // namespace etl
#endif
algorithm.h
etl::flags
Definition flags.h:51
etl::flags::operator=
ETL_CONSTEXPR14 flags< T, MASK > & operator=(value_type pattern) ETL_NOEXCEPT
operator =
Definition flags.h:327
etl::flags::set
ETL_CONSTEXPR14 flags< T, MASK > & set() ETL_NOEXCEPT
Set the bits.
Definition flags.h:100
etl::flags::operator^=
ETL_CONSTEXPR14 flags< T, MASK > & operator^=(value_type pattern) ETL_NOEXCEPT
operator ^=
Definition flags.h:307
etl::flags::operator|=
ETL_CONSTEXPR14 flags< T, MASK > & operator|=(value_type pattern) ETL_NOEXCEPT
operator |=
Definition flags.h:297
etl::flags::flags
ETL_CONSTEXPR flags() ETL_NOEXCEPT
Constructor.
Definition flags.h:66
etl::flags::flip
ETL_CONSTEXPR14 flags< T, MASK > & flip() ETL_NOEXCEPT
Flip bits.
Definition flags.h:171
etl::flags::reset
ETL_CONSTEXPR14 flags< T, MASK > & reset() ETL_NOEXCEPT
Reset the bit at the pattern.
Definition flags.h:153
etl::flags::value
ETL_CONSTEXPR14 flags< T, MASK > & value(value_type pattern) ETL_NOEXCEPT
Set the value of the flags.
Definition flags.h:269
etl::flags::operator&=
ETL_CONSTEXPR14 flags< T, MASK > & operator&=(value_type pattern) ETL_NOEXCEPT
operator &=
Definition flags.h:287
etl::flags::none
ETL_CONSTEXPR bool none() const ETL_NOEXCEPT
Are none of the bits set?
Definition flags.h:219
etl::flags::any
ETL_CONSTEXPR bool any() const ETL_NOEXCEPT
Are any of the bits set?
Definition flags.h:240
etl::flags::swap
void swap(flags< T, MASK > &other) ETL_NOEXCEPT
swap
Definition flags.h:337
etl::flags::test
ETL_CONSTEXPR bool test() const ETL_NOEXCEPT
Tests bits.
Definition flags.h:85
etl::flags::value
ETL_CONSTEXPR value_type value() const ETL_NOEXCEPT
Return the value of the flags.
Definition flags.h:261
etl::flags::operator=
ETL_CONSTEXPR14 flags< T, MASK > & operator=(flags< T, MASK > other) ETL_NOEXCEPT
operator =
Definition flags.h:317
etl::any_of
ETL_NODISCARD ETL_CONSTEXPR14 bool any_of(TIterator begin, TIterator end, TUnaryPredicate predicate)
Definition algorithm.h:2210
etl::none_of
ETL_NODISCARD ETL_CONSTEXPR14 bool none_of(TIterator begin, TIterator end, TUnaryPredicate predicate)
Definition algorithm.h:2221
etl::integral_limits
Definition integral_limits.h:517
initializer_list.h
integral_limits.h
etl
Definition absolute.h:40
etl::swap
ETL_CONSTEXPR14 void swap(etl::typed_storage_ext< T > &lhs, etl::typed_storage_ext< T > &rhs) ETL_NOEXCEPT
Swap two etl::typed_storage_ext.
Definition alignment.h:856
etl::operator==
ETL_CONSTEXPR14 bool operator==(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1078
etl::data
ETL_CONSTEXPR TContainer::pointer data(TContainer &container)
Definition iterator.h:1470
etl::operator!=
ETL_CONSTEXPR14 bool operator!=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1090
platform.h
etl::value_type
type_traits.h