From 3a97adfe84b52adbda036d1de3d71a2f12dc98bb Mon Sep 17 00:00:00 2001 From: Mercury13 Date: Mon, 21 Aug 2023 14:08:04 +0300 Subject: [PATCH] :) #303 more Kage, what to do? --- GlyphWiki2/KageDemo/KageDemo.pro | 1 + GlyphWiki2/KageLibs/kage.cpp | 2 + GlyphWiki2/KageLibs/kage.h | 7 ++-- GlyphWiki2/KageLibs/u_MaybeInt.h | 64 ++++++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 GlyphWiki2/KageLibs/u_MaybeInt.h diff --git a/GlyphWiki2/KageDemo/KageDemo.pro b/GlyphWiki2/KageDemo/KageDemo.pro index 9ed4508aa..6f3205e9e 100644 --- a/GlyphWiki2/KageDemo/KageDemo.pro +++ b/GlyphWiki2/KageDemo/KageDemo.pro @@ -21,6 +21,7 @@ SOURCES += \ HEADERS += \ ../../Libs/SelfMade/Strings/u_Strings.h \ ../KageLibs/kage.h \ + ../KageLibs/u_MaybeInt.h \ FmMain.h FORMS += \ diff --git a/GlyphWiki2/KageLibs/kage.cpp b/GlyphWiki2/KageLibs/kage.cpp index fe6de98dc..9819454ea 100644 --- a/GlyphWiki2/KageLibs/kage.cpp +++ b/GlyphWiki2/KageLibs/kage.cpp @@ -81,7 +81,9 @@ kage::Glyph kage::toGlyph(std::string_view source, const SourceEngine& engine) Glyph r; auto lines = splitIntoLinesSv(source); for (auto line : lines) { + auto columns = str::splitSv(line, ':', false); auto& q = r.lines.emplace_back(); + /// @todo [urgent] what to do with lines? } return r; diff --git a/GlyphWiki2/KageLibs/kage.h b/GlyphWiki2/KageLibs/kage.h index 43c150194..f8031cb7e 100644 --- a/GlyphWiki2/KageLibs/kage.h +++ b/GlyphWiki2/KageLibs/kage.h @@ -2,8 +2,10 @@ #include +// Libs #include "u_Array.h" #include "u_Vector.h" +#include "u_MaybeInt.h" namespace kage { @@ -34,9 +36,8 @@ namespace kage { SafeVector splitIntoLinesSv(std::string_view source); struct Line { - Fix1d d; - Line() noexcept - { std::fill(d.begin(), d.end(), std::numeric_limits::quiet_NaN()); } + using T = MaybeInt; + Fix1d d; }; struct Glyph { diff --git a/GlyphWiki2/KageLibs/u_MaybeInt.h b/GlyphWiki2/KageLibs/u_MaybeInt.h new file mode 100644 index 000000000..3d3504881 --- /dev/null +++ b/GlyphWiki2/KageLibs/u_MaybeInt.h @@ -0,0 +1,64 @@ +#pragma once + +// STL +#include +#include +#include +#include + + +template +struct MaybeInt +{ +public: + static_assert(std::is_integral_v, "MaybeInt should use integral types only"); + + using This = MaybeInt; + static constexpr bool IS_SIGNED = std::is_signed_v; + static constexpr bool IS_UNSIGNED = !IS_SIGNED; + static constexpr T MIN_VALUE = IS_SIGNED ? std::numeric_limits::min() + 1 : 0; + static constexpr T MAX_VALUE = IS_SIGNED ? std::numeric_limits::max() : std::numeric_limits::max() - 1; + + constexpr MaybeInt() noexcept = default; + constexpr MaybeInt(T y) noexcept : x(y) {} + + constexpr MaybeInt& operator = (const This& y) noexcept = default; + constexpr MaybeInt& operator = (T y) noexcept { x = y; return *this; } + + constexpr T rawValue() const { return x; } + constexpr T operator * (); + + constexpr bool hasValue() const { return (x != NO_VALUE); } + constexpr T valueOr(T y) const { return hasValue() ? x : y; } + explicit operator bool() const { return hasValue(); } + + bool operator == (const This& y) const noexcept = default; + static This fromStr(std::string_view y); +private: + static constexpr T NO_VALUE = IS_SIGNED ? std::numeric_limits::min() : std::numeric_limits::max(); + T x = NO_VALUE; +}; + + +template +constexpr T MaybeInt::operator * () +{ + if (!hasValue()) + throw std::logic_error("[MaybeInt::op*] No value!"); + return x; +} + + +template +auto MaybeInt::fromStr(std::string_view y) -> This +{ + This r; + auto q = std::from_chars( + std::to_address(y.begin()), + std::to_address(y.end()), + r.x); + if (q.ec != std::errc{}) { + q.x = NO_VALUE; + } + return r; +}