This repository was archived by the owner on Jul 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Add Tuparg - semi-typesafe fast variadic argument iteration support #9
Draft
dekrain
wants to merge
4
commits into
HackyTeam:dev
Choose a base branch
from
dekrain:feature-tuparg
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| #include <Printf.hpp> | ||
|
|
||
| #include <random> | ||
|
|
||
| int main() { | ||
| std::random_device drng; | ||
| std::uniform_int_distribution dist(1, 999); | ||
| int num = dist(drng); | ||
|
|
||
| static const char* const days[] = { | ||
| "Monday", "Tuesday", "Wednesday", "Thursday", | ||
| "Friday", "Saturday", "Sunday", "Hiddenday", "Nullday" | ||
| }; | ||
|
|
||
| int di = num % 9; | ||
| const char* day = days[di]; | ||
|
|
||
| hsd::xprintf("Hello, User%d!\nYour random day is %s\n", num, day).unwrap(); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| #include "Printf.hpp" | ||
|
|
||
| #include <cstdio> | ||
|
|
||
| #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 { | ||
| while (*fmt) { | ||
| char ch = *fmt++; | ||
| if (ch != '%') | ||
| std::putchar(ch); | ||
| else { | ||
| char fch = *fmt++; | ||
| if (!fch) { | ||
| return runtime_error("\nError: Invalid format string"); | ||
| } | ||
| if (tbegin == tend) { | ||
| return runtime_error("\nError: Passed to few arguments"); | ||
| } | ||
| switch (fch) { | ||
| case 'd': { | ||
| 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*>().unwrap(); | ||
| std::fwrite(value, 1, cstring::length(value), stdout); | ||
| break; | ||
| } | ||
| case 'c': { | ||
| char value = tbegin.getnext<char>().unwrap(); | ||
| std::putchar(value); | ||
| break; | ||
| } | ||
| default: { | ||
| return runtime_error("\nError: Invalid format specifier"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| if (tbegin != tend) { | ||
| return runtime_error("\nError: Not all arguments parsed"); | ||
| } | ||
| return {}; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| #pragma once | ||
|
|
||
| #include "Tuparg.hpp" | ||
| #include "Result.hpp" | ||
|
|
||
| namespace hsd { | ||
| Result<void, runtime_error> xvprintf(const char* fmt, xtuple_iterator tbegin, xtuple_iterator tend) noexcept; | ||
|
|
||
| template <typename... Args> | ||
| Result<void, runtime_error> xprintf(const char* fmt, Args&&... args) noexcept { | ||
| auto targs = make_xtuple(forward<Args>(args)...); | ||
| const auto& layout = xtuple_layout<decltype(targs)>::get_layout(); | ||
| return xvprintf(fmt, layout.begin(targs), layout.end()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| #pragma once | ||
|
|
||
| #include "Types.hpp" | ||
| #include "Result.hpp" | ||
| #include <cassert> | ||
|
|
||
| namespace hsd { | ||
| struct xtuple_iterator { | ||
| const usize* layout; | ||
| void* addr; | ||
|
|
||
| template <typename X> | ||
| X& getnext_unsafe() { | ||
| assert(addr); | ||
dekrain marked this conversation as resolved.
Show resolved
Hide resolved
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens if
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
|
||
| X* p = reinterpret_cast<X*>(addr); | ||
| addr = reinterpret_cast<void*>( | ||
| reinterpret_cast<uptr>(addr) | ||
| + *layout | ||
| ); | ||
| if (* ++layout == 0) | ||
| addr = nullptr; | ||
| 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; | ||
| } | ||
|
|
||
| bool operator!=(const xtuple_iterator& o) const { | ||
| return addr != o.addr; | ||
| } | ||
| }; | ||
|
|
||
| template <typename Tfirst, typename... Trest> | ||
| struct xtuple { | ||
| Tfirst _first; | ||
| xtuple<Trest...> _rest; | ||
|
|
||
| xtuple(Tfirst f, Trest... r) | ||
| : _first(move(f)), _rest(move(r)...) | ||
| {} | ||
|
|
||
| consteval static usize get_offset() { | ||
| return offsetof(xtuple, _rest); | ||
dekrain marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| }; | ||
|
|
||
| template <typename Tfirst> | ||
| struct xtuple<Tfirst> { | ||
| Tfirst _first; | ||
|
|
||
| xtuple(Tfirst f) : _first(move(f)) {} | ||
|
|
||
| consteval static usize get_offset() { | ||
| return sizeof(xtuple); | ||
| } | ||
| }; | ||
|
|
||
| template <typename Tuple> | ||
| struct xtuple_layout; | ||
|
|
||
| template <typename Tfirst, typename... Trest> | ||
| struct xtuple_layout<xtuple<Tfirst, Trest...>> { | ||
| using tuple_type = xtuple<Tfirst, Trest...>; | ||
| constexpr static usize count = 1 + sizeof...(Trest); | ||
|
|
||
| usize data[count + 1]; | ||
|
|
||
| consteval xtuple_layout() { | ||
| assign_layout(data, 0); | ||
| data[count] = 0; | ||
| } | ||
|
|
||
| consteval static void assign_layout(usize* p, usize idx) { | ||
| p[idx] = tuple_type::get_offset(); | ||
| if constexpr (sizeof...(Trest)) | ||
| xtuple_layout<xtuple<Trest...>>::assign_layout(p, idx+1); | ||
| } | ||
|
|
||
| static inline xtuple_layout const& get_layout() { | ||
| static constinit const xtuple_layout _layout; | ||
| return _layout; | ||
| } | ||
|
|
||
| auto begin(tuple_type& tup) const { return xtuple_iterator{data, &tup._first}; } | ||
| auto end() const { return xtuple_iterator{data + count, nullptr}; } | ||
| }; | ||
|
|
||
| template <typename... Args> | ||
| auto make_xtuple(Args&&... args) { | ||
| return xtuple<remove_reference_t<Args>...>(forward<Args&&>(args)...); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should checkout
hsd::randomThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know, but
hsddoesn't haverandom_device