basedOpinionated utility library |
git clone git://git.dimitrijedobrota.com/based.git |
Log | Files | Refs | README | LICENSE | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING |
commit | febff86821e4e3d9e26be13f75ccaa0be051ba3e |
parent | 223b31d044289270bed7b4498caa151fb8004430 |
author | Dimitrije Dobrota < mail@dimitrijedobrota.com > |
date | Tue, 3 Jun 2025 18:33:54 +0200 |
Character wrapper for convinience and safety
A | include/based/char/character.hpp | | | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
M | include/based/types/strong.hpp | | | ++++++++++++ -- |
2 files changed, 73 insertions(+), 2 deletions(-)
diff --git a/ include/based/char/character.hpp b/ include/based/char/character.hpp
@@ -0,0 +1,61 @@
#pragma once
#include "based/types/types.hpp"
namespace based
{
class character : public strong_type<unsigned char, character>
{
static constexpr auto cast(char pos)
{
return static_cast<unsigned char>(pos);
}
static constexpr auto cast(unsigned char pos)
{
return static_cast<char>(pos);
}
public:
using strong_type::strong_type;
using strong_type::operator=;
explicit constexpr character(char chr)
: strong_type(cast(chr))
{
}
explicit constexpr character(u8 ord)
: strong_type(ord.value)
{
}
[[nodiscard]] char chr() const { return cast(value); }
[[nodiscard]] u8 ord() const { return u8::basic_cast(value); }
friend constexpr bool operator==(character lhs, char rhs)
{
return lhs.value == cast(rhs);
}
friend constexpr bool operator==(char lhs, character rhs)
{
return cast(lhs) == rhs.value;
}
friend constexpr auto operator<=>(character lhs, char rhs)
{
return lhs.value <=> cast(rhs);
}
friend constexpr auto operator<=>(char lhs, character rhs)
{
return cast(lhs) <=> rhs.value;
}
};
auto compare(character, character) -> bool;
auto order(character, character) -> bool;
} // namespace based
diff --git a/ include/based/types/strong.hpp b/ include/based/types/strong.hpp
@@ -1,6 +1,9 @@
#pragma once
#include <compare>
#include "based/concepts/is/same.hpp"
#include "based/trait/is/class.hpp"
namespace based
{
@@ -24,12 +27,12 @@
struct strong_type
constexpr ~strong_type() = default;
constexpr explicit strong_type()
explicit constexpr strong_type()
: value(0)
{
}
constexpr explicit strong_type(basic_type val)
explicit constexpr strong_type(basic_type val)
: value(val)
{
}
@@ -47,6 +50,13 @@
struct strong_type
{
return Tag {static_cast<basic_type>(value)};
}
template<class T>
requires is_class_v<T>
static constexpr Tag basic_cast(T value)
{
return static_cast<Tag>(value);
}
};
// NOLINTEND(*crtp*)