Program Listing for File flags.hpp

Return to documentation for file (include/util/flags.hpp)

/*
 * util - a collection of utility classes and functions for C++
 * <https://github.com/mostsignificant/util>
 *
 * MIT License
 *
 * Copyright (c) 2020-2021 Christian Göhring
 *
 * 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 THAT_THIS_UTIL_FLAGS_HEADER_HEADER_FILE_IS_ALREADY_INCLUDED
#define THAT_THIS_UTIL_FLAGS_HEADER_HEADER_FILE_IS_ALREADY_INCLUDED

#include <cstdint>
#include <initializer_list>

namespace util {

template <class Enum, class EnumUnderlyingType = int32_t>
class flags {
public:
    explicit flags(Enum value);
    flags(std::initializer_list<Enum> values);

    flags() = default;
    ~flags() = default;
    flags(flags&& other) noexcept = default;
    flags(const flags& other) = default;
    auto operator=(flags&& other) noexcept -> flags& = default;
    auto operator=(const flags& other) -> flags& = default;

    explicit operator EnumUnderlyingType() const;
    explicit operator bool() const noexcept;
    auto operator!() const noexcept -> bool;

    auto flip(Enum value) noexcept -> flags&;
    void reset() noexcept;
    void reset(Enum value) noexcept;
    auto set(Enum value, bool on = true) noexcept -> flags&;
    auto test(Enum value) const noexcept -> bool;

private:
    EnumUnderlyingType value = static_cast<EnumUnderlyingType>(0);
};

}  // namespace util

template <class Enum, class EnumUnderlyingType>
util::flags<Enum, EnumUnderlyingType>::flags(Enum value) : value(value) {}

template <class Enum, class EnumUnderlyingType>
util::flags<Enum, EnumUnderlyingType>::flags(std::initializer_list<Enum> values) {
    for (auto&& value : values) {
        this->value |= value;
    }
}

template <class Enum, class EnumUnderlyingType>
util::flags<Enum, EnumUnderlyingType>::operator EnumUnderlyingType() const {
    return value;
}

template <class Enum, class EnumUnderlyingType>
util::flags<Enum, EnumUnderlyingType>::operator bool() const noexcept {
    return value != 0;
}

template <class Enum, class EnumUnderlyingType>
auto util::flags<Enum, EnumUnderlyingType>::operator!() const noexcept -> bool {
    return !static_cast<bool>(*this);
}

template <class Enum, class EnumUnderlyingType>
auto util::flags<Enum, EnumUnderlyingType>::flip(Enum value) noexcept
    -> util::flags<Enum, EnumUnderlyingType>& {
    set(value, !test(value));
}

template <class Enum, class EnumUnderlyingType>
void util::flags<Enum, EnumUnderlyingType>::reset() noexcept {
    value = static_cast<EnumUnderlyingType>(0);
}

template <class Enum, class EnumUnderlyingType>
void util::flags<Enum, EnumUnderlyingType>::reset(Enum value) noexcept {
    set(value, false);
}

template <class Enum, class EnumUnderlyingType>
auto util::flags<Enum, EnumUnderlyingType>::set(Enum value, bool on) noexcept
    -> util::flags<Enum, EnumUnderlyingType>& {
    if (on) {
        this->value |= value;
    } else {
        this->value &= ~value;
    }
}

template <class Enum, class EnumUnderlyingType>
auto util::flags<Enum, EnumUnderlyingType>::test(Enum value) const noexcept -> bool {
    return this->value & value;
}

#endif  // THAT_THIS_UTIL_FLAGS_HEADER_HEADER_FILE_IS_ALREADY_INCLUDED