susumu.yata
null+****@clear*****
Wed Oct 22 18:16:13 JST 2014
susumu.yata 2014-10-22 18:16:13 +0900 (Wed, 22 Oct 2014) New Revision: 550ce172188db8207acbd9bc40fd810fc9efc683 https://github.com/groonga/grnxx/commit/550ce172188db8207acbd9bc40fd810fc9efc683 Message: Add a candidate of new Bool and Int. Added files: include/grnxx/new_types/bool.hpp include/grnxx/new_types/int.hpp include/grnxx/new_types/na.hpp Added: include/grnxx/new_types/bool.hpp (+109 -0) 100644 =================================================================== --- /dev/null +++ include/grnxx/new_types/bool.hpp 2014-10-22 18:16:13 +0900 (141a92b) @@ -0,0 +1,109 @@ +#ifndef GRNXX_NEW_TYPES_BOOL_HPP +#define GRNXX_NEW_TYPES_BOOL_HPP + +#include <cstdint> + +#include "grnxx/new_types/na.hpp" + +namespace grnxx { + +class Bool { + public: + Bool() = default; + ~Bool() = default; + + constexpr Bool(const Bool &) = default; + Bool &operator=(const Bool &) & = default; + + explicit constexpr Bool(bool value) : value_(value) {} + explicit constexpr Bool(NA) : value_(na_value()) {} + + constexpr uint8_t value() const { + return value_; + } + + constexpr bool is_true() const { + return value_ == true_value(); + } + constexpr bool is_false() const { + return value_ == false_value(); + } + constexpr bool is_na() const { + return value_ == na_value(); + } + + explicit constexpr operator bool() const { + return is_true(); + } + + // -- Unary operators -- + + constexpr Bool operator!() const { + return is_na() ? na() : Bool(!value_); + } + constexpr Bool operator~() const { + return operator!(); + } + + // -- Binary operators -- + + // TODO: Difficult branch predictions (for is_false()) should be removed. + constexpr Bool operator&&(Bool rhs) const { + return (is_false() || rhs.is_false()) ? Bool(false) : + ((is_na() || rhs.is_na()) ? na() : Bool(true)); + } + constexpr Bool operator||(Bool rhs) const { + return (is_true() || rhs.is_true()) ? Bool(true) : + ((is_na() || rhs.is_na()) ? na() : Bool(false)); + } + + constexpr Bool operator&(Bool rhs) const { + return operator&&(rhs); + } + constexpr Bool operator|(Bool rhs) const { + return operator||(rhs); + } + constexpr Bool operator^(Bool rhs) const { + return (is_na() || rhs.is_na()) ? na() : Bool(value_ ^ rhs.value_); + } + + Bool &operator&=(Bool rhs) & { + return *this = operator&(rhs); + } + Bool operator|=(Bool rhs) & { + return *this = operator|(rhs); + } + Bool operator^=(Bool rhs) & { + return *this = operator^(rhs); + } + + // -- Comparison operators -- + + constexpr Bool operator==(Bool rhs) const { + return (is_na() || rhs.is_na()) ? na() : Bool(value_ == rhs.value_); + } + constexpr Bool operator!=(Bool rhs) const { + return (is_na() || rhs.is_na()) ? na() : Bool(value_ != rhs.value_); + } + + static constexpr Bool na() { + return Bool(NA()); + } + + static constexpr uint8_t true_value() { + return 1; + } + static constexpr uint8_t false_value() { + return 0; + } + static constexpr uint8_t na_value() { + return 2; + } + + private: + uint8_t value_; +}; + +} // namespace grnxx + +#endif // GRNXX_NEW_TYPES_BOOL_HPP Added: include/grnxx/new_types/int.hpp (+149 -0) 100644 =================================================================== --- /dev/null +++ include/grnxx/new_types/int.hpp 2014-10-22 18:16:13 +0900 (fb0c658) @@ -0,0 +1,149 @@ +#ifndef GRNXX_NEW_TYPES_INT_HPP +#define GRNXX_NEW_TYPES_INT_HPP + +#include <cstdint> +#include <limits> + +#include "grnxx/new_types/na.hpp" + +namespace grnxx { + +class Int { + public: + Int() = default; + ~Int() = default; + + constexpr Int(const Int &) = default; + Int &operator=(const Int &) = default; + + explicit constexpr Int(int64_t value) : value_(value) {} + explicit constexpr Int(NA) : value_(na_value()) {} + + constexpr int64_t value() const { + return value_; + } + + constexpr bool is_min() const { + return value_ == min_value(); + } + constexpr bool is_max() const { + return value_ == max_value(); + } + constexpr bool is_na() const { + return value_ == na_value(); + } + + // -- Unary operators -- + + constexpr Int operator+() const { + return *this; + } + constexpr Int operator-() const { + return is_na() ? na() : Int(-value_); + } + constexpr Int operator~() const { + return is_na() ? na() : Int(~value_); + } + + Int &operator++() & { + if (!is_na()) { + ++value_; + } + return *this; + } + Int operator++(int) & { + if (is_na()) { + return na(); + } + return Int(value_++); + } + Int &operator--() & { + if (!is_na()) { + --value_; + } + return *this; + } + Int operator--(int) & { + if (is_na()) { + return na(); + } + return Int(value_--); + } + + // -- Binary operators -- + + constexpr Int operator&(Int rhs) const { + return (is_na() || rhs.is_na()) ? na() : Int(value_ & rhs.value_); + } + constexpr Int operator|(Int rhs) const { + return (is_na() || rhs.is_na()) ? na() : Int(value_ | rhs.value_); + } + constexpr Int operator^(Int rhs) const { + return (is_na() || rhs.is_na()) ? na() : Int(value_ ^ rhs.value_); + } + + Int &operator&=(Int rhs) & { + if (!is_na()) { + value_ = rhs.is_na() ? na_value() : (value_ & rhs.value_); + } + return *this; + } + Int &operator|=(Int rhs) & { + if (!is_na()) { + value_ = rhs.is_na() ? na_value() : (value_ | rhs.value_); + } + return *this; + } + Int &operator^=(Int rhs) & { + if (!is_na()) { + value_ = rhs.is_na() ? na_value() : (value_ ^ rhs.value_); + } + return *this; + } + + // TODO: Bitwise shift operators. + + // TODO: Arithmetic operators. + + // -- Comparison operators -- + + constexpr Bool operator==(Int rhs) const { + return (is_na() || rhs.is_na()) ? Bool::na() : Bool(value_ == rhs.value_); + } + constexpr Bool operator!=(Int rhs) const { + return (is_na() || rhs.is_na()) ? Bool::na() : Bool(value_ != rhs.value_); + } + constexpr Bool operator<(Int rhs) const { + return (is_na() || rhs.is_na()) ? Bool::na() : Bool(value_ < rhs.value_); + } + constexpr Bool operator>(Int rhs) const { + return (is_na() || rhs.is_na()) ? Bool::na() : Bool(value_ > rhs.value_); + } + constexpr Bool operator<=(Int rhs) const { + return (is_na() || rhs.is_na()) ? Bool::na() : Bool(value_ <= rhs.value_); + } + constexpr Bool operator>=(Int rhs) const { + return (is_na() || rhs.is_na()) ? Bool::na() : Bool(value_ >= rhs.value_); + } + + static constexpr Int na() { + return Int(NA()); + } + + static constexpr int64_t min_value() { + return std::numeric_limits<int64_t>::min() + 1; + } + static constexpr int64_t max_value() { + return std::numeric_limits<int64_t>::max(); + } + static constexpr int64_t na_value() { + return std::numeric_limits<int64_t>::min(); + } + + private: + int64_t value_; +}; + +} // namespace grnxx + +#endif // GRNXX_NEW_TYPES_INT_HPP Added: include/grnxx/new_types/na.hpp (+10 -0) 100644 =================================================================== --- /dev/null +++ include/grnxx/new_types/na.hpp 2014-10-22 18:16:13 +0900 (bb324e8) @@ -0,0 +1,10 @@ +#ifndef GRNXX_NEW_TYPES_NA_HPP +#define GRNXX_NEW_TYPES_NA_HPP + +namespace grnxx { + +class NA {}; + +} // namespace grnxx + +#endif // GRNXX_NEW_TYPES_NA_HPP -------------- next part -------------- HTML����������������������������...Download