Header only implementation of boolean in modern C++
Created
October 11, 2020 12:32
-
-
Save hnrck/0b623912bab67c1243087175fe2f6a3d to your computer and use it in GitHub Desktop.
Header only implementation of boolean in modern C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef MY_BOOLEAN_H | |
#define MY_BOOLEAN_H | |
#include <ostream> | |
/// @namespace my | |
/// @brief My implementation of part of the standard library | |
namespace my { | |
/// @class Boolean | |
/// @brief Homemade boolean representation | |
class boolean final { | |
public: | |
/// @enum Truth value | |
/// @brief Boolean truth value representation based on 8-bit unsigned int | |
enum class truth_value : uint8_t { | |
FALSE = 0, ///< @var False value | |
TRUE = 1, ///< @var True value | |
}; | |
private: | |
/// @var Boolean truth value, true by default | |
truth_value m_value{boolean::truth_value::TRUE}; | |
public: | |
/// @fn Boolean constructor | |
/// @brief Boolean default constructor | |
constexpr boolean() = default; | |
/// @fn Boolean constructor | |
/// @brief Boolean parametrized constructor | |
/// @param value Boolean init value | |
explicit constexpr boolean(boolean::truth_value value) : m_value{value} {} | |
/// @fn Boolean constructor | |
/// @brief Boolean cast constructor | |
/// @param value Boolean init value in int | |
explicit constexpr boolean([[maybe_unused]] uint8_t value) : m_value{ | |
value == 0 ? boolean{boolean::truth_value::FALSE} : boolean{boolean::truth_value::TRUE} | |
} {} | |
/// @fn Boolean destructor | |
/// @brief Boolean default destructor | |
~boolean() = default; | |
/// @fn Boolean copy constructor | |
/// @brief Boolean default copy constructor | |
boolean(const boolean &) = default; | |
/// @fn Boolean move constructor | |
/// @brief Boolean default move constructor | |
auto operator=(const boolean &) -> boolean & = default; | |
/// @fn Boolean copy assignment | |
/// @brief Boolean default copy assignment | |
boolean(boolean &&) = default; | |
/// @fn Boolean move assignment | |
/// @brief Boolean default move assignment | |
auto operator=(boolean &&) -> boolean & = default; | |
/// @fn Not operator | |
/// @return Not current value | |
[[nodiscard]] constexpr auto operator~() const -> boolean { | |
return (boolean(static_cast<boolean>((static_cast<uint8_t>(m_value) + 1) % 2))); | |
} | |
/// @fn And operator | |
/// @param other Other boolean | |
/// @return Current value and other value | |
[[nodiscard]] constexpr auto operator&&(const boolean &other) const -> boolean { | |
return (static_cast<boolean>(static_cast<uint8_t>(m_value) * static_cast<uint8_t>(other.m_value))); | |
} | |
/// @fn Or operator | |
/// @param other Other boolean | |
/// @return Current value or other value | |
[[nodiscard]] constexpr auto operator||(const boolean &other) const -> boolean { | |
return (boolean(static_cast<uint8_t>(m_value) + static_cast<uint8_t>(other.m_value))); | |
} | |
/// @fn Xor operator | |
/// @param other Other boolean | |
/// @return Current value xor other value | |
[[nodiscard]] constexpr auto operator^(const boolean &other) const -> boolean { | |
return (static_cast<boolean>(static_cast<uint8_t>(m_value) + static_cast<uint8_t>(other.m_value) % 2)); | |
} | |
/// @fn Truth value cast | |
/// @brief Cast current boolean to truth value | |
/// @return Truth value representation of current boolean value | |
[[nodiscard]] constexpr explicit operator boolean::truth_value() const { | |
return (m_value); | |
} | |
/// @fn To string | |
/// @return String representation of current boolean | |
[[nodiscard]] constexpr auto to_string() const -> const char * { | |
constexpr auto *const true_str = "my true"; | |
constexpr auto *const false_str = "my false"; | |
switch (m_value) { | |
case truth_value::TRUE:return (true_str); | |
case truth_value::FALSE:return (false_str); | |
} | |
} | |
/// @fn Ostream redirection | |
/// @param os Ostream to print in | |
/// @return String representation of current boolean printed in stream | |
friend auto operator<<(std::ostream &os, const boolean &boolean) -> std::ostream & { | |
return os << boolean.to_string(); | |
} | |
}; | |
} // namespace my | |
#endif // MY_BOOLEAN_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 3.17) | |
project(boolean) | |
set(CMAKE_CXX_STANDARD 20) | |
include_directories(.) | |
add_executable(boolean my_boolean.h main.cpp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <my_boolean.h> | |
auto main() -> int { | |
constexpr auto b1 = my::boolean{}; | |
constexpr auto b2 = my::boolean{my::boolean::truth_value::FALSE}; | |
constexpr auto not_b1 = ~b1; | |
constexpr auto b1_or_b2 = b1 or b2; | |
constexpr auto b1_and_b2 = b1 and b2; | |
constexpr auto b1_and_b2_or_b1 = (b1 and b2) or b1; | |
std::cout << "Default constructor: "; | |
std::cout << b1 << std::endl; | |
std::cout << "Constructor with my false value constructor: "; | |
std::cout << b2 << std::endl; | |
std::cout << "Not my true: "; | |
std::cout << not_b1 << std::endl; | |
std::cout << "My true or my false: "; | |
std::cout << b1_or_b2 << std::endl; | |
std::cout << "My true and my false: "; | |
std::cout << b1_and_b2 << std::endl; | |
std::cout << "My true and my false or my true: "; | |
std::cout << b1_and_b2_or_b1 << std::endl; | |
return (EXIT_SUCCESS); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment