Skip to content

Commit

Permalink
directvt#393 WIP: Use std::any for gui_command
Browse files Browse the repository at this point in the history
  • Loading branch information
o-sdn-o committed Jan 10, 2025
1 parent 57ac771 commit de27192
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 13 deletions.
1 change: 0 additions & 1 deletion src/netxs/desktopio/baseui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include "events.hpp"
#include "xml.hpp"

#include <typeindex>
#include <future>

namespace netxs
Expand Down
4 changes: 2 additions & 2 deletions src/netxs/desktopio/console.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,7 @@ namespace netxs::ui
gear_ptr->set_handled();
}
gui_cmd.cmd_id = syscmd::tunecellheight;
gui_cmd.args = luafx.get_args_or(1, fp32{ 1.f });
gui_cmd.args.emplace_back(luafx.get_args_or(1, fp32{ 1.f }));
boss.bell::signal(tier::preview, e2::command::gui, gui_cmd);
luafx.set_return();
}},
Expand All @@ -876,7 +876,7 @@ namespace netxs::ui
gear_ptr->set_handled();
}
gui_cmd.cmd_id = syscmd::rollfontlist;
gui_cmd.args = luafx.get_args_or(1, fp32{ 1.f });
gui_cmd.args.emplace_back(luafx.get_args_or(1, si32{ 1 }));
boss.bell::signal(tier::preview, e2::command::gui, gui_cmd);
luafx.set_return();
}},
Expand Down
60 changes: 58 additions & 2 deletions src/netxs/desktopio/directvt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,38 @@ namespace netxs::directvt
{
// Noop.
}
else if constexpr (std::is_same_v<D, std::any>)
{
// Four letters type encoding.
#define type_id_list \
X(char) \
X(text) \
X(byte) \
X(ui16) \
X(ui32) \
X(ui64) \
X(si16) \
X(si32) \
X(si64) \
X(twod) \
X(rect) \
X(fp32) \
X(fp64) \
X(fp2d) \
X(time) \
X(span) \
X(argb) \
X(dent)
#define X(item_t) if (data.type() == typeid(item_t)) { \
fuse_ext(block, make_ui32(#item_t)); \
fuse_ext(block, *std::any_cast<item_t>(&data)); \
} else
type_id_list
{
log(prompt::dtvt, "Unsupported data type");
}
#undef X
}
else if constexpr (requires{ std::begin(std::declval<D>()); })
{
auto length = (sz_t)data.size();
Expand Down Expand Up @@ -275,6 +307,29 @@ namespace netxs::directvt
}
return crop;
}
else if constexpr (std::is_same_v<D, std::any>)
{
auto crop = std::any{};
if (data.size() <= sizeof(si32))
{
log(prompt::dtvt, "Corrupted frame data");
if constexpr (!PeekOnly) data.remove_prefix(data.size());
return crop;
}
auto type_id = netxs::aligned<si32>(data.data()); // Get four letters type index.
auto bits = data.substr(sizeof(si32));
switch (type_id)
{
#define X(item_t) case make_ui32(#item_t): \
crop = _take_item<item_t>(bits); \
break;
type_id_list
#undef X
#undef type_id_list
}
if constexpr (!PeekOnly) data = bits;
return crop;
}
else if constexpr (requires{ std::begin(std::declval<D>()); })
{
using data_type = decltype(*std::begin(std::declval<D>()));
Expand Down Expand Up @@ -847,7 +902,8 @@ namespace netxs::directvt

auto& operator << (std::ostream& s, wchr const& o) { return s << utf::to_hex_0x(o); }
auto& operator << (std::ostream& s, time const& o) { return s << utf::to_hex_0x(o.time_since_epoch().count()); }
auto& operator << (std::ostream& s, regs const& rs) { s << '{'; for (auto r : rs) s << r; return s << '}'; }
auto& operator << (std::ostream& s, regs const& rs) { s << '{'; for (auto& r : rs) s << r; return s << '}'; }
auto& operator << (std::ostream& s, many const& my) { s << '{'; for (auto& r : my) s << r.type().name(); return s << '}'; }

STRUCT_macro(frame_element, (blob, data))
STRUCT_macro(jgc_element, (ui64, token) (text, cluster))
Expand Down Expand Up @@ -915,7 +971,7 @@ namespace netxs::directvt
STRUCT_macro(restored, (id_t, gear_id))
STRUCT_macro(req_input_fields, (id_t, gear_id) (si32, acpStart) (si32, acpEnd))
STRUCT_macro(ack_input_fields, (id_t, gear_id) (regs, field_list))
STRUCT_macro(gui_command, (id_t, gear_id) (si32, cmd_id) (fp32, args))
STRUCT_macro(gui_command, (id_t, gear_id) (si32, cmd_id) (many, args))

#undef STRUCT_macro
#undef STRUCT_macro_lite
Expand Down
21 changes: 13 additions & 8 deletions src/netxs/desktopio/gui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2941,8 +2941,9 @@ namespace netxs::gui
{
whlacc = {};
}
void IncreaseCellHeight(fp32 dir)
void IncreaseCellHeight(many const& args)
{
auto dir = args.size() ? any_get_or(args.front(), 0.f) : 0.f;
change_cell_size(faux, dir);
sync_cellsz();
update_gui();
Expand All @@ -2965,11 +2966,12 @@ namespace netxs::gui
{
set_aa_mode(!gcache.aamode);
}
void RollFontList(fp32 dir)
void RollFontList(many const& args)
{
if (fcache.families.empty()) return;
auto dir = args.size() ? any_get_or<si32>(args.front()) : 0;
auto& families = fcache.families;
if (dir > 0)
if (dir >= 0)
{
families.push_back(std::move(families.front()));
families.pop_front();
Expand Down Expand Up @@ -3141,14 +3143,17 @@ namespace netxs::gui
update_gui();
});
}
void sys_command(si32 menucmd, fp32 dir = {})
void sys_command(si32 menucmd, many args = {})
{
if (menucmd == syscmd::update && !reload) return;
if (menucmd == syscmd::tunecellheight)
{
if (isbusy.exchange(true) || dir == 0.f) return;
if (isbusy.exchange(true) || args.empty() || any_get_or(args.front(), 0.f) == 0.f)
{
return;
}
}
bell::enqueue(This(), [&, menucmd, dir](auto& /*boss*/)
bell::enqueue(This(), [&, menucmd, args](auto& /*boss*/)
{
//log("sys_command: menucmd=", utf::to_hex_0x(menucmd));
switch (menucmd)
Expand All @@ -3163,11 +3168,11 @@ namespace netxs::gui
case syscmd::update: update_gui(); break;
//
case syscmd::resetwheelaccum: ResetWheelAccumulator(); break;
case syscmd::tunecellheight: IncreaseCellHeight(dir); break;
case syscmd::tunecellheight: IncreaseCellHeight(args); break;
case syscmd::resetcellheight: ResetCellHeight(); break;
case syscmd::togglefsmode: ToggleFullscreenMode(); break;
case syscmd::toggleaamode: ToggleAntialiasingMode(); break;
case syscmd::rollfontlist: RollFontList(dir); break;
case syscmd::rollfontlist: RollFontList(args); break;
}
});
}
Expand Down
16 changes: 16 additions & 0 deletions src/netxs/desktopio/intmath.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@
#pragma once

#include <array>
#include <vector>
#include <optional>
#include <algorithm>
#include <limits>
#include <cmath>
#include <cfenv>
#include <cassert>
#include <any>
#include <bit>
#include <atomic>
#include <cstring> // std::memcpy
#include <utility> // std::cmp_equal
#include <numeric> // std::accumulate
#include <typeindex>

#ifndef faux
#define faux (false)
Expand All @@ -36,6 +39,7 @@ namespace netxs
using sz_t = uint32_t;
using arch = size_t;
using flag = std::atomic<bool>;
using many = std::vector<std::any>;

constexpr size_t operator "" _sz (unsigned long long i) { return static_cast<size_t>(i); }
static constexpr auto bytemin = std::numeric_limits<byte>::min();
Expand Down Expand Up @@ -125,6 +129,18 @@ namespace netxs
using to_signed_t = std::conditional_t<(si64)std::numeric_limits<std::remove_reference_t<T>>::max() <= si16max, si16,
std::conditional_t<(si64)std::numeric_limits<std::remove_reference_t<T>>::max() <= si32max, si32, si64>>;

template<class T>
auto& any_get_or(std::any const& value, T& fallback)
{
return value.type() == typeid(T) ? *std::any_cast<T*>(value)
: fallback;
}
template<class T>
auto any_get_or(std::any const& value, T const& fallback = {})
{
return value.type() == typeid(T) ? std::any_cast<T>(value)
: fallback;
}
// intmath: Set a single p-bit to v.
template<sz_t P, class T>
void set_bit(T&& n, bool v)
Expand Down
4 changes: 4 additions & 0 deletions src/netxs/desktopio/utf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ namespace netxs
static constexpr auto base64code = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static constexpr auto whitespace = ' '; // '.';
static constexpr auto emptyspace = "\0"sv; //"\xC0\x80"sv; // In Modified UTF-8, the null character (U+0000) uses the two-byte overlong encoding 11000000 10000000 (hexadecimal C0 80), instead of 00000000 (hexadecimal 00).
static consteval auto make_ui32(view four_bytes){ return ((ui32)four_bytes[0] << 24)
| ((ui32)four_bytes[1] << 16)
| ((ui32)four_bytes[2] << 8)
| ((ui32)four_bytes[3] << 0); }
}

namespace netxs::utf
Expand Down

0 comments on commit de27192

Please sign in to comment.