Skip to content

Commit

Permalink
:) #303 more Kage, what to do?
Browse files Browse the repository at this point in the history
  • Loading branch information
Mercury13 committed Aug 21, 2023
1 parent 25e63df commit 3a97adf
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 3 deletions.
1 change: 1 addition & 0 deletions GlyphWiki2/KageDemo/KageDemo.pro
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ SOURCES += \
HEADERS += \
../../Libs/SelfMade/Strings/u_Strings.h \
../KageLibs/kage.h \
../KageLibs/u_MaybeInt.h \
FmMain.h

FORMS += \
Expand Down
2 changes: 2 additions & 0 deletions GlyphWiki2/KageLibs/kage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 4 additions & 3 deletions GlyphWiki2/KageLibs/kage.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

#include <string>

// Libs
#include "u_Array.h"
#include "u_Vector.h"
#include "u_MaybeInt.h"

namespace kage {

Expand Down Expand Up @@ -34,9 +36,8 @@ namespace kage {
SafeVector<std::string_view> splitIntoLinesSv(std::string_view source);

struct Line {
Fix1d<float, 11> d;
Line() noexcept
{ std::fill(d.begin(), d.end(), std::numeric_limits<float>::quiet_NaN()); }
using T = MaybeInt<int>;
Fix1d<T, 11> d;
};

struct Glyph {
Expand Down
64 changes: 64 additions & 0 deletions GlyphWiki2/KageLibs/u_MaybeInt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#pragma once

// STL
#include <type_traits>
#include <limits>
#include <stdexcept>
#include <charconv>


template <class T>
struct MaybeInt
{
public:
static_assert(std::is_integral_v<T>, "MaybeInt should use integral types only");

using This = MaybeInt<T>;
static constexpr bool IS_SIGNED = std::is_signed_v<T>;
static constexpr bool IS_UNSIGNED = !IS_SIGNED;
static constexpr T MIN_VALUE = IS_SIGNED ? std::numeric_limits<T>::min() + 1 : 0;
static constexpr T MAX_VALUE = IS_SIGNED ? std::numeric_limits<T>::max() : std::numeric_limits<T>::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<T>::min() : std::numeric_limits<T>::max();
T x = NO_VALUE;
};


template <class T>
constexpr T MaybeInt<T>::operator * ()
{
if (!hasValue())
throw std::logic_error("[MaybeInt::op*] No value!");
return x;
}


template <class T>
auto MaybeInt<T>::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;
}

0 comments on commit 3a97adf

Please sign in to comment.