Skip to content
This repository was archived by the owner on Jul 2, 2022. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions cpp/Printf.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "Printf.hpp"

#include <cstdio>
#include <cstring>

#include "String.hpp"
#include "Io.hpp"

using namespace hsd;
Result<void, runtime_error> hsd::xvprintf(const char* fmt, xtuple_iterator tbegin, xtuple_iterator tend) noexcept {
Expand All @@ -19,17 +21,18 @@ Result<void, runtime_error> hsd::xvprintf(const char* fmt, xtuple_iterator tbegi
}
switch (fch) {
case 'd': {
int value = tbegin.getnext<int>();
std::printf("%d", value);
int value = tbegin.getnext<int>().unwrap();
auto s = string::to_string(value);
std::fwrite(s.data(), 1, s.size(), stdout);
break;
}
case 's': {
auto value = tbegin.getnext<const char*>();
std::fwrite(value, 1, std::strlen(value), stdout);
auto value = tbegin.getnext<const char*>().unwrap();
std::fwrite(value, 1, cstring::length(value), stdout);
break;
}
case 'c': {
char value = tbegin.getnext<char>();
char value = tbegin.getnext<char>().unwrap();
std::putchar(value);
break;
}
Expand Down
14 changes: 9 additions & 5 deletions cpp/Result.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ namespace hsd
}
};

// Tags for result construction
struct ok_tag_t {};
struct err_tag_t {};

template < typename Ok, typename Err >
class Result
{
Expand All @@ -94,17 +98,17 @@ namespace hsd
HSD_CONSTEXPR Result& operator=(const Result&) = delete;
HSD_CONSTEXPR Result& operator=(Result&&) = delete;

HSD_CONSTEXPR Result(const Ok& value)
HSD_CONSTEXPR Result(const Ok& value, ok_tag_t = {})
: _ok_data{value}, _initialized{true}
{}

HSD_CONSTEXPR Result(Ok&& value)
HSD_CONSTEXPR Result(Ok&& value, ok_tag_t = {})
: _ok_data{move(value)}, _initialized{true}
{}

template <typename T>
requires (std::is_convertible_v<T, Err>)
HSD_CONSTEXPR Result(T&& value)
HSD_CONSTEXPR Result(T&& value, err_tag_t = {})
: _err_data{forward<T>(value)}, _initialized{false}
{}

Expand Down Expand Up @@ -395,13 +399,13 @@ namespace hsd
constexpr void unwrap_or_default() = delete;
constexpr void unwrap_or() = delete;

HSD_CONSTEXPR Result()
HSD_CONSTEXPR Result(ok_tag_t = {})
: _initialized{true}
{}

template <typename T>
requires (std::is_convertible_v<T, Err>)
HSD_CONSTEXPR Result(T&& value)
HSD_CONSTEXPR Result(T&& value, err_tag_t = {})
: _err_data{forward<Err>(value)}, _initialized{false}
{}

Expand Down
11 changes: 9 additions & 2 deletions cpp/Tuparg.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "Types.hpp"
#include "Utility.hpp"
#include "Result.hpp"
#include <cassert>

namespace hsd {
Expand All @@ -10,7 +10,7 @@ namespace hsd {
void* addr;

template <typename X>
X& getnext() {
X& getnext_unsafe() {
assert(addr);
Comment thread
dekrain marked this conversation as resolved.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if addr is nullptr that you really need to use an assert?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's more of a safety measure. The program normally shouldn't allow this function to be called with null pointer, but if it gets called (by some other way than getnext), then it's a debugging check, and it should be erased when compiling for release


X* p = reinterpret_cast<X*>(addr);
Expand All @@ -23,6 +23,13 @@ namespace hsd {
return *p;
}

template <typename X>
Result<reference<X>, runtime_error> getnext() {
if (!addr)
return runtime_error("Extracting value past end of the container");
return Result<reference<X>, runtime_error>(getnext_unsafe<X>(), ok_tag_t{});
}

bool operator==(const xtuple_iterator& o) const {
return addr == o.addr;
}
Expand Down