Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
alabuzhev committed Sep 1, 2023
1 parent 6061025 commit 25e4eba
Show file tree
Hide file tree
Showing 18 changed files with 170 additions and 130 deletions.
5 changes: 5 additions & 0 deletions far/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
--------------------------------------------------------------------------------
drkns 2023-09-01 21:06:39+01:00 - build 6182

1. Refactoring.

--------------------------------------------------------------------------------
yjh 2023-08-25 12:09:02+03:00 - build 6181

Expand Down
2 changes: 1 addition & 1 deletion far/color_picker_rgb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ static bool pick_color_rgb_tui(COLORREF& Color, [[maybe_unused]] std::array<COLO
DlgHeight = static_cast<int>(ColorDlg[cd_border].Y2) + 2;

Dlg->SetPosition({ -1, -1, DlgWidth, DlgHeight });
Dlg->SetHelp(L"ColorPicker"sv);
Dlg->SetHelp(L"ColorPickerRGB"sv);
Dlg->Process();

if (Dlg->GetExitCode() != cd_button_ok)
Expand Down
4 changes: 2 additions & 2 deletions far/colormix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -670,11 +670,11 @@ static bool ExtractColor(string_view const Str, COLORREF& Target, FARCOLORFLAGS&

static bool ExtractStyle(string_view const Str, FARCOLORFLAGS& TargetFlags)
{
const auto Flags = ColorStringToFlags(Str);
const auto Flags = ColorStringToFlags(Str) & FCF_STYLEMASK;
if (!Flags)
return false;

TargetFlags |= Flags & FCF_STYLEMASK;
TargetFlags |= Flags;
return true;
}

Expand Down
22 changes: 6 additions & 16 deletions far/common.tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1754,23 +1754,13 @@ TEST_CASE("utility.base")

TEST_CASE("utility.grow_exp_noshrink")
{
static const struct
{
size_t Current, Desired, Expected;
}
Tests[]
{
{ 0, 1, 1 },
{ 1, 1, 1 },
{ 1, 0, 1 },
{ 0, 2, 2 },
{ 1, 2, 2 },
{ 2, 2, 2 },
};

for (const auto& i : Tests)
for (const auto& i: irange(size_t{32}))
{
REQUIRE(grow_exp_noshrink(i.Current, i.Desired) == i.Expected);
const auto ExpectedIncrease = std::max(size_t{1}, i / 2);
REQUIRE(grow_exp_noshrink(i, 0) == i);
REQUIRE(grow_exp_noshrink(i, {}) == i + ExpectedIncrease);
REQUIRE(grow_exp_noshrink(i, i + 1) == i + ExpectedIncrease);
REQUIRE(grow_exp_noshrink(i, i * 2) == i * 2);
}
}

Expand Down
4 changes: 4 additions & 0 deletions far/common/placement.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include <memory>

#ifdef _DEBUG
#include <cstring>
#endif

//----------------------------------------------------------------------------

namespace placement
Expand Down
2 changes: 1 addition & 1 deletion far/common/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ inline size_t grow_exp_noshrink(size_t const Current, std::optional<size_t> cons

// For vector reserve typically allocates exactly the requested amount instead of exponential growth.
// This can be really bad if called in a loop.
const auto LowerBound = Current + (Current + 2) / 2;
const auto LowerBound = Current + std::max(size_t{1}, Current / 2);
return Desired? std::max(LowerBound, *Desired) : LowerBound;
}

Expand Down
4 changes: 2 additions & 2 deletions far/exception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ namespace detail
LOGTRACE(L"{}"sv, *this);
}

std::string far_std_exception::convert_message() const
std::string far_std_exception::convert_message(string_view const Message)
{
return encoding::utf8::get_bytes(full_message());
return encoding::utf8::get_bytes(Message);
}

break_into_debugger::break_into_debugger()
Expand Down
4 changes: 2 additions & 2 deletions far/exception.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ namespace detail
template<typename... args>
explicit far_std_exception(args&&... Args):
far_base_exception(FWD(Args)...),
std::runtime_error(convert_message())
std::runtime_error(convert_message(full_message()))
{}

private:
[[nodiscard]] std::string convert_message() const;
[[nodiscard]] static std::string convert_message(string_view Message);
};

class break_into_debugger
Expand Down
7 changes: 0 additions & 7 deletions far/exception_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -440,11 +440,6 @@ static string system_timestamp()
return timestamp(os::chrono::nt_clock::now());
}

static string kernel_version()
{
return os::version::get_file_version(L"ntoskrnl.exe"sv);
}

static void read_registers(string& To, CONTEXT const& Context, string_view const Eol)
{
const auto r = [&](string_view const Name, auto const Value)
Expand Down Expand Up @@ -1403,7 +1398,6 @@ static string collect_information(
const auto SystemTime = system_timestamp();
const auto Uptime = get_uptime();
const auto OsVersion = os::version::os_version();
const auto KernelVersion = kernel_version();
const auto Locale = get_locale();
const auto ConsoleHost = get_console_host();
const auto Parent = get_parent_process();
Expand Down Expand Up @@ -1438,7 +1432,6 @@ static string collect_information(
{ L"Time: "sv, SystemTime, },
{ L"Uptime: "sv, Uptime, },
{ L"OS: "sv, OsVersion, },
{ L"Kernel: "sv, KernelVersion, },
{ L"Locale: "sv, Locale, },
{ L"Host: "sv, ConsoleHost, },
{ L"Parent: "sv, Parent, },
Expand Down
3 changes: 3 additions & 0 deletions far/exception_handler_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,10 @@ namespace tests
#endif
}

WARNING_PUSH()
WARNING_DISABLE_CLANG("-Wmissing-noreturn")
static void cpp_assertion_failure()
WARNING_POP()
{
assert(true == false);
}
Expand Down
22 changes: 11 additions & 11 deletions far/far.natvis
Original file line number Diff line number Diff line change
Expand Up @@ -232,17 +232,17 @@
</Synthetic>
<Synthetic Name="Style">
<DisplayString>
{int(is_inherit())}|
{int(is_bold())}|
{int(is_italic())}|
{int(is_underline())}|
{int(is_underline2())}|
{int(is_overline())}|
{int(is_strikeout())}|
{int(is_faint())}|
{int(is_blink())}|
{int(is_inverse())}|
{int(is_invisible())}
{int(is_inherit()),d}|
{int(is_bold()),d}|
{int(is_italic()),d}|
{int(is_underline()),d}|
{int(is_underline2()),d}|
{int(is_overline()),d}|
{int(is_strikeout()),d}|
{int(is_faint()),d}|
{int(is_blink()),d}|
{int(is_inverse()),d}|
{int(is_invisible()),d}
</DisplayString>
<Expand>
<Item Name="inherit">is_inherit()</Item>
Expand Down
2 changes: 1 addition & 1 deletion far/makefile_gcc_common
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,6 @@ CLANG_FLAGS = \
-Weverything \

CFLAGS += $(CLANG_FLAGS)
CFLAGS += -D __FLOAT_H # 8.1 incompatibility

LNKFLAGS += \
$(CLANG_FLAGS) \
Expand All @@ -233,6 +232,7 @@ endif
ifdef USE_LLD
LNKFLAGS += \
-fuse-ld=lld \
-Xlinker --allow-multiple-definition \

endif

Expand Down
92 changes: 88 additions & 4 deletions far/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,60 @@ namespace os
{
namespace detail
{
HANDLE handle_implementation::normalise(HANDLE const Handle)
static bool ApiDynamicStringReceiverImpl(
string& Destination,
function_ref<size_t(span<wchar_t> WritableBuffer)> const Callable,
function_ref<bool(size_t ReturnedSize, size_t AllocatedSize)> const Condition
)
{
return Handle == INVALID_HANDLE_VALUE? nullptr : Handle;
return ApiDynamicReceiver(
buffer<wchar_t>(),
Callable,
Condition,
[&](span<wchar_t const> const Buffer)
{
Destination.assign(Buffer.data(), Buffer.size());
}
);
}

bool ApiDynamicStringReceiver(
string& Destination,
function_ref<size_t(span<wchar_t> WritableBuffer)> const Callable
)
{
return ApiDynamicStringReceiverImpl(
Destination,
Callable,
[](size_t const ReturnedSize, size_t const AllocatedSize)
{
// Why such a condition?
// Usually API functions return string length (without \0) on success and
// required buffer size (i. e. string length + \0) on failure.
// Some of them, however, always return buffer size.
// It's Callable's responsibility to handle and fix that.
return ReturnedSize >= AllocatedSize;
}
);
}

bool handle_implementation::wait(HANDLE const Handle, std::optional<std::chrono::milliseconds> const Timeout)
bool ApiDynamicErrorBasedStringReceiver(
DWORD const ExpectedErrorCode,
string& Destination,
function_ref<size_t(span<wchar_t> WritableBuffer)> const Callable)
{
return ApiDynamicStringReceiverImpl(
Destination,
Callable,
[&](size_t const ReturnedSize, size_t /*const AllocatedSize*/)
{
return !ReturnedSize && GetLastError() == ExpectedErrorCode;
}
);
}

[[nodiscard]]
static bool single_wait(HANDLE const Handle, std::optional<std::chrono::milliseconds> const Timeout = {})
{
switch (const auto Result = WaitForSingleObject(Handle, Timeout? *Timeout / 1ms : INFINITE))
{
Expand All @@ -87,7 +135,8 @@ namespace os
}
}

std::optional<size_t> handle_implementation::wait(span<HANDLE const> const Handles, bool const WaitAll, std::optional<std::chrono::milliseconds> Timeout)
[[nodiscard]]
static std::optional<size_t> multi_wait(span<HANDLE const> const Handles, bool const WaitAll, std::optional<std::chrono::milliseconds> Timeout = {})
{
assert(!Handles.empty());
assert(Handles.size() <= MAXIMUM_WAIT_OBJECTS);
Expand All @@ -109,6 +158,41 @@ namespace os
}
}

void handle_implementation::wait(HANDLE const Handle)
{
(void)single_wait(Handle);
}

bool handle_implementation::is_signaled(HANDLE const Handle, std::chrono::milliseconds const Timeout)
{
return single_wait(Handle, Timeout);
}

size_t handle_implementation::wait_any(span<HANDLE const> const Handles)
{
return *multi_wait(Handles, false);
}

std::optional<size_t> handle_implementation::wait_any(span<HANDLE const> const Handles, std::optional<std::chrono::milliseconds> const Timeout)
{
return multi_wait(Handles, false, Timeout);
}

bool handle_implementation::wait_all(span<HANDLE const> const Handles, std::optional<std::chrono::milliseconds> const Timeout)
{
return multi_wait(Handles, true, Timeout).has_value();
}

void handle_implementation::wait_all(span<HANDLE const> const Handles)
{
(void)multi_wait(Handles, true);
}

HANDLE handle_implementation::normalise(HANDLE const Handle)
{
return Handle == INVALID_HANDLE_VALUE? nullptr : Handle;
}

void handle_closer::operator()(HANDLE Handle) const noexcept
{
if (!CloseHandle(Handle))
Expand Down
10 changes: 5 additions & 5 deletions far/platform.debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,14 +236,14 @@ namespace os::debug
return Data;
}

static auto address(DWORD64 const Offset)
{
return ADDRESS64{ Offset, 0, AddrModeFlat };
};

template<typename T, typename data>
static void stack_walk(data const& Data, function_ref<bool(T&)> const& Walker, function_ref<void(uintptr_t, DWORD)> const& Handler)
{
const auto address = [](DWORD64 const Offset)
{
return ADDRESS64{ Offset, 0, AddrModeFlat };
};

T StackFrame{};
StackFrame.AddrPC = address(Data.PC);
StackFrame.AddrFrame = address(Data.Frame);
Expand Down
2 changes: 1 addition & 1 deletion far/platform.fs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1614,7 +1614,7 @@ namespace os::fs
security::descriptor Result(default_buffer_size);

if (!os::detail::ApiDynamicReceiver(Result,
[&](security::descriptor const& Buffer)
[&](span<SECURITY_DESCRIPTOR> const Buffer)
{
DWORD LengthNeeded = 0;
if (!::GetFileSecurity(Object, RequestedInformation, Buffer.data(), static_cast<DWORD>(Buffer.size()), &LengthNeeded))
Expand Down
Loading

0 comments on commit 25e4eba

Please sign in to comment.