Mercurial > code
changeset 468:becd06089e8f
Get rid of Flags, update README
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 04 Nov 2015 21:21:30 +0100 |
parents | 41766abe942e |
children | bcfb05fa961c |
files | C++/modules/Flags/Flags.h C++/tests/Flags/main.cpp README.md |
diffstat | 3 files changed, 29 insertions(+), 639 deletions(-) [+] |
line wrap: on
line diff
--- a/C++/modules/Flags/Flags.h Wed Nov 04 20:53:36 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,341 +0,0 @@ -/* - * Flags.h -- safe wrapper for enum flags - * - * Copyright (c) 2013-2015 David Demelier <markand@malikania.fr> - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#ifndef _FLAGS_H_ -#define _FLAGS_H_ - -/** - * @file Flags.h - * @brief Provide template class Flags and operators for enum classes - */ - -#include <type_traits> - -/** - * @class Flags - * @brief Wrapper that store enum flags - * - * This class is used to store an enum and make bitwise operations on it. User do not need - * to supply these operators because they are implemented in the class. - * - * This class is very cheap and store the real underlying type from the enum and all functions - * are inlined. - */ -template <typename Enum, typename Type = std::underlying_type_t<Enum>> -class Flags final { -private: - Type m_value { 0 }; - -public: - /** - * Construct flags to 0. - */ - constexpr Flags() noexcept = default; - - /** - * Construct flags with the enum value. - * - * @param m the mask - */ - constexpr Flags(Enum m) noexcept - : m_value(static_cast<Type>(m)) - { - } - - /** - * Construct flags with the enum value. - * - * @param m the mask - */ - constexpr Flags(Type m) noexcept - : m_value(m) - { - } - - /** - * Move constructor. - * - * @param other the other - */ - inline Flags(Flags &&other) noexcept = default; - - /** - * Copy constructor. - * - * @param other the other - */ - inline Flags(const Flags &other) noexcept = default; - - /** - * Move operator. - * - * @param other the other - */ - inline Flags &operator=(Flags &&other) noexcept = default; - - /** - * Copy operator. - * - * @param other the other - */ - inline Flags &operator=(const Flags &other) noexcept = default; - - /** - * &= operator. - * - * @param m the mask - * @return *this - */ - inline Flags &operator&=(Enum m) noexcept - { - m_value &= static_cast<Type>(m); - - return *this; - } - - /** - * &= operator. - * - * @param m the mask - * @return *this - */ - inline Flags &operator&=(Type m) noexcept - { - m_value &= m; - - return *this; - } - - /** - * |= operator. - * - * @param m the mask - * @return *this - */ - inline Flags &operator|=(Enum m) noexcept - { - m_value |= static_cast<Type>(m); - - return *this; - } - - /** - * |= operator. - * - * @param m the mask - * @return *this - */ - inline Flags &operator|=(Type m) noexcept - { - m_value |= m; - - return *this; - } - - /** - * ^= operator. - * - * @param m the mask - * @return *this - */ - inline Flags &operator^=(Enum m) noexcept - { - m_value ^= static_cast<Type>(m); - - return *this; - } - - /** - * ^= operator. - * - * @param m the mask - * @return *this - */ - inline Flags &operator^=(Type m) noexcept - { - m_value ^= m; - - return *this; - } - - /** - * & operator. - * - * @param m the mask - * @return & combination - */ - constexpr Flags operator&(Enum m) const noexcept - { - return m_value & static_cast<Type>(m); - } - - /** - * & operator. - * - * @param m the mask - * @return & combination - */ - constexpr Flags operator&(Type m) const noexcept - { - return m_value & m; - } - - /** - * | operator. - * - * @param m the mask - * @return | combination - */ - constexpr Flags operator|(Enum m) const noexcept - { - return m_value | static_cast<Type>(m); - } - - /** - * | operator. - * - * @param m the mask - * @return | combination - */ - constexpr Flags operator|(Type m) const noexcept - { - return m_value | m; - } - - /** - * ^ operator. - * - * @param m the mask - * @return ^ combination - */ - constexpr Flags operator^(Enum m) const noexcept - { - return m_value & static_cast<Type>(m); - } - - /** - * ^ operator. - * - * @param m the mask - * @return ^ combination - */ - constexpr Flags operator^(Type m) const noexcept - { - return m_value & m; - } - - /** - * ~ unary operator. - * - * @return ~ - */ - constexpr Flags operator~() const noexcept - { - return ~m_value; - } - - /** - * Convert to bool. Simply check if value is not 0. - * - * @return true if value != 0 - */ - constexpr operator bool() const noexcept - { - return m_value != 0; - } - - /** - * Operator !. Simply check if value is 0. - * - * @return true if value == 0 - */ - constexpr bool operator!() const noexcept - { - return m_value == 0; - } - - /** - * Test comparison with other mask. - * - * @return true if this value == m - */ - constexpr bool operator==(Enum m) const noexcept - { - return m_value == static_cast<Type>(m); - } - - /** - * Test comparison with other mask. - * - * @return true if this value == m - */ - constexpr bool operator==(Type m) const noexcept - { - return m_value == m; - } -}; - -/** - * Apply & operator on the enum. - * - * @param x1 the first value - * @param x2 the second value - */ -template <typename Value, typename Type = std::enable_if_t<std::is_enum<Value>::value, std::underlying_type_t<Value>>> -constexpr Value operator&(Value x1, Value x2) noexcept -{ - return static_cast<Value>(static_cast<Type>(x1) & static_cast<Type>(x2)); -} - -/** - * Apply | operator on the enum. - * - * @param x1 the first value - * @param x2 the second value - */ -template <typename Value, typename Type = std::enable_if_t<std::is_enum<Value>::value, std::underlying_type_t<Value>>> -constexpr Value operator|(Value x1, Value x2) noexcept -{ - return static_cast<Value>(static_cast<Type>(x1) | static_cast<Type>(x2)); -} - -/** - * Apply ^ operator on the enum. - * - * @param x1 the first value - * @param x2 the second value - */ -template <typename Value, typename Type = std::enable_if_t<std::is_enum<Value>::value, std::underlying_type_t<Value>>> -constexpr Value operator^(Value x1, Value x2) noexcept -{ - return static_cast<Value>(static_cast<Type>(x1) ^ static_cast<Type>(x2)); -} - -/** - * Apply ~ operator on the enum. - * - * @param x1 the first value - * @param x2 the second value - */ -template <typename Value, typename Type = std::enable_if_t<std::is_enum<Value>::value, std::underlying_type_t<Value>>> -constexpr Value operator~(Value x) noexcept -{ - return static_cast<Value>(~static_cast<Type>(x)); -} - -#endif // !_FLAGS_H_ \ No newline at end of file
--- a/C++/tests/Flags/main.cpp Wed Nov 04 20:53:36 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,298 +0,0 @@ -/* - * main.cpp -- main test file for Flags - * - * Copyright (c) 2013-2015 David Demelier <markand@malikania.fr> - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include <cstdint> - -#include <gtest/gtest.h> - -#include <Flags.h> - -enum Standard : uint8_t { - Fullscreen = (1 << 0), - Audio = (1 << 1) -}; - -enum class Strong : uint8_t { - NoLog = (1 << 0), - NoCheck = (1 << 1) -}; - -/* -------------------------------------------------------- - * Global operators on standard enum - * -------------------------------------------------------- */ - -TEST(OperatorsStandard, opand) -{ - Standard s(Fullscreen | Audio); - - ASSERT_EQ(Fullscreen, (s & Fullscreen)); - ASSERT_EQ(Audio, (s & Audio)); -} - -TEST(OperatorsStandard, opor) -{ - Standard s(Fullscreen); - - ASSERT_EQ(3, (s | Audio)); -} - -TEST(OperatorsStandard, opxor) -{ - Standard s(Fullscreen); - - ASSERT_EQ(3, (s ^ Audio)); -} - -TEST(OperatorsStandard, opnot) -{ - Standard s(Fullscreen); - - ASSERT_EQ(254, ~s); -} - -/* -------------------------------------------------------- - * Global operators on strong enum - * -------------------------------------------------------- */ - -TEST(OperatorsStrong, opand) -{ - Strong s(Strong::NoLog | Strong::NoCheck); - - ASSERT_EQ(Strong::NoLog, (s & Strong::NoLog)); - ASSERT_EQ(Strong::NoCheck, (s & Strong::NoCheck)); -} - -TEST(OperatorsStrong, opor) -{ - Strong s(Strong::NoLog); - - ASSERT_EQ(3, static_cast<int>((s | Strong::NoCheck))); -} - -TEST(OperatorsStrong, opxor) -{ - Strong s(Strong::NoLog); - - ASSERT_EQ(3, static_cast<int>((s ^ Strong::NoCheck))); -} - -TEST(OperatorsStrong, opnot) -{ - Strong s(Strong::NoLog); - - ASSERT_EQ(254, static_cast<int>(~s)); -} - -/* -------------------------------------------------------- - * Flags with standard enums - * -------------------------------------------------------- */ - -TEST(Standard, construct) -{ - Flags<Standard> s; - - ASSERT_FALSE(s); - ASSERT_TRUE(!s); - - Flags <Standard> s2(Fullscreen | Audio); - - ASSERT_TRUE(s2); - ASSERT_FALSE(!s2); - ASSERT_TRUE(s2 & Fullscreen); - ASSERT_TRUE(s2 & Audio); - ASSERT_TRUE((s2 & Fullscreen) == Fullscreen); - ASSERT_TRUE((s2 & Audio) == Audio); - ASSERT_TRUE(s2 == (Audio | Fullscreen)); -} - -TEST(Standard, addByOne) -{ - Flags<Standard> s; - - ASSERT_FALSE(s); - ASSERT_TRUE(!s); - ASSERT_TRUE(!(s & Fullscreen)); - ASSERT_TRUE(!(s & Audio)); - - s |= Fullscreen; - ASSERT_TRUE(s); - ASSERT_FALSE(!s); - ASSERT_TRUE(s & Fullscreen); - ASSERT_TRUE((s & Fullscreen) == Fullscreen); - ASSERT_FALSE(s & Audio); - ASSERT_FALSE((s & Audio) == Audio); - ASSERT_TRUE(s == Fullscreen); - - s |= Audio; - ASSERT_TRUE(s); - ASSERT_FALSE(!s); - ASSERT_TRUE(s & Fullscreen); - ASSERT_TRUE((s & Fullscreen) == Fullscreen); - ASSERT_TRUE(s & Audio); - ASSERT_TRUE((s & Audio) == Audio); - ASSERT_TRUE(s == (Fullscreen | Audio)); -} - -TEST(Standard, add) -{ - Flags<Standard> s; - - s |= Fullscreen | Audio; - ASSERT_TRUE(s & (Fullscreen | Audio)); - ASSERT_TRUE((s & (Fullscreen | Audio)) == (Fullscreen | Audio)); -} - -TEST(Standard, removeByOne) -{ - Flags<Standard> s(Fullscreen | Audio); - - s &= ~(Fullscreen); - ASSERT_TRUE(s); - ASSERT_FALSE(!s); - ASSERT_FALSE(s & Fullscreen); - ASSERT_FALSE((s & Fullscreen) == Fullscreen); - ASSERT_TRUE(s & Audio); - ASSERT_TRUE((s & Audio) == Audio); - - s &= ~(Audio); - ASSERT_FALSE(s); - ASSERT_TRUE(!s); -} - -TEST(Standard, remove) -{ - Flags<Standard> s(Fullscreen | Audio); - - s &= ~(Fullscreen | Audio); - ASSERT_FALSE(s); - ASSERT_TRUE(!s); -} - -TEST(Standard, xorAdd) -{ - Flags<Standard> s(Fullscreen | Audio); - - s ^= Audio; - ASSERT_TRUE(s & Fullscreen); - ASSERT_TRUE((s & Fullscreen) == Fullscreen); - ASSERT_FALSE(s & Audio); - ASSERT_FALSE((s & Audio) == Audio); -} - -/* -------------------------------------------------------- - * Flags with strong enums - * -------------------------------------------------------- */ - -TEST(Strong, construct) -{ - Flags<Strong> s; - - ASSERT_FALSE(s); - ASSERT_TRUE(!s); - - Flags <Strong> s2(Strong::NoLog | Strong::NoCheck); - - ASSERT_TRUE(s2); - ASSERT_FALSE(!s2); - ASSERT_TRUE(s2 & Strong::NoLog); - ASSERT_TRUE(s2 & Strong::NoCheck); - ASSERT_TRUE((s2 & Strong::NoLog) == Strong::NoLog); - ASSERT_TRUE((s2 & Strong::NoCheck) == Strong::NoCheck); - ASSERT_TRUE(s2 == (Strong::NoLog | Strong::NoCheck)); -} - -TEST(Strong, addByOne) -{ - Flags<Strong> s; - - ASSERT_FALSE(s); - ASSERT_TRUE(!s); - ASSERT_TRUE(!(s & Strong::NoLog)); - ASSERT_TRUE(!(s & Strong::NoCheck)); - - s |= Strong::NoLog; - ASSERT_TRUE(s); - ASSERT_FALSE(!s); - ASSERT_TRUE(s & Strong::NoLog); - ASSERT_TRUE((s & Strong::NoLog) == Strong::NoLog); - ASSERT_FALSE(s & Strong::NoCheck); - ASSERT_FALSE((s & Strong::NoCheck) == Strong::NoCheck); - ASSERT_TRUE(s == Strong::NoLog); - - s |= Strong::NoCheck; - ASSERT_TRUE(s); - ASSERT_FALSE(!s); - ASSERT_TRUE(s & Strong::NoLog); - ASSERT_TRUE((s & Strong::NoLog) == Strong::NoLog); - ASSERT_TRUE(s & Strong::NoCheck); - ASSERT_TRUE((s & Strong::NoCheck) == Strong::NoCheck); - ASSERT_TRUE(s == (Strong::NoLog | Strong::NoCheck)); -} - -TEST(Strong, add) -{ - Flags<Strong> s; - - s |= Strong::NoLog | Strong::NoCheck; - ASSERT_TRUE(s & (Strong::NoLog | Strong::NoCheck)); - ASSERT_TRUE((s & (Strong::NoLog | Strong::NoCheck)) == (Strong::NoLog | Strong::NoCheck)); -} - -TEST(Strong, removeByOne) -{ - Flags<Strong> s(Strong::NoLog | Strong::NoCheck); - - s &= ~(Strong::NoLog); - ASSERT_TRUE(s); - ASSERT_FALSE(!s); - ASSERT_FALSE(s & Strong::NoLog); - ASSERT_FALSE((s & Strong::NoLog) == Strong::NoLog); - ASSERT_TRUE(s & Strong::NoCheck); - ASSERT_TRUE((s & Strong::NoCheck) == Strong::NoCheck); - - s &= ~(Strong::NoCheck); - ASSERT_FALSE(s); - ASSERT_TRUE(!s); -} - -TEST(Strong, remove) -{ - Flags<Strong> s(Strong::NoLog | Strong::NoCheck); - - s &= ~(Strong::NoLog | Strong::NoCheck); - ASSERT_FALSE(s); - ASSERT_TRUE(!s); -} - -TEST(Strong, xorAdd) -{ - Flags<Strong> s(Strong::NoLog | Strong::NoCheck); - - s ^= Strong::NoCheck; - ASSERT_TRUE(s & Strong::NoLog); - ASSERT_TRUE((s & Strong::NoLog) == Strong::NoLog); - ASSERT_FALSE(s & Strong::NoCheck); - ASSERT_FALSE((s & Strong::NoCheck) == Strong::NoCheck); -} - -int main(int argc, char **argv) -{ - testing::InitGoogleTest(&argc, argv); - - return RUN_ALL_TESTS(); -} \ No newline at end of file
--- a/README.md Wed Nov 04 20:53:36 2015 +0100 +++ b/README.md Wed Nov 04 21:21:30 2015 +0100 @@ -1,3 +1,32 @@ # Common code This repository contains common code reusable in C++. + +## Status + +The following status are defined: + +- **Frozen**, the module will not changed and only have bugfixes, +- **Stable**, the module is stable but may still change a little bit, +- **Testing**, the module is being tested and may change, +- **Experimental**, the module is new and completely experimental or unstable, +- **New**, the module is completely new. + +| Module | Status | Tests quality | +|--------------|--------------|---------------| +| Base64 | Frozen | \*\*\* | +| Converter | Experimental | no tests | +| Date | Experimental | no tests | +| Directory | Stable | \*\*\* | +| Dynlib | Frozen | \*\*\* | +| Format | New | no tests | +| Hash | Frozen | \*\*\* | +| Ini | Stable | \*\*\* | +| Js | New | \*\* | +| Json | Stable | \*\*\* | +| OptionParser | Testing | \*\*\* | +| Socket | Experimental | \* | +| Treenode | Frozen | \*\*\* | +| Unicode | Frozen | \*\*\* | +| Xdg | Frozen | \* | +| Zip | Testing | \*\* |