[FarGroup/FarManager] master: Refactoring (a0e924a8e)

1 view
Skip to first unread message

farg...@farmanager.com

unread,
Oct 4, 2022, 5:15:48 PM10/4/22
to farco...@googlegroups.com
Repository : https://github.com/FarGroup/FarManager
On branch : master
Link : https://github.com/FarGroup/FarManager/commit/a0e924a8e13dad7cfbe5fb21e318ad7520ed0d04

>---------------------------------------------------------------

commit a0e924a8e13dad7cfbe5fb21e318ad7520ed0d04
Author: Alex Alabuzhev <alab...@gmail.com>
Date: Tue Oct 4 22:03:54 2022 +0100

Refactoring


>---------------------------------------------------------------

a0e924a8e13dad7cfbe5fb21e318ad7520ed0d04
far/RegExp.cpp | 2 +-
far/common.tests.cpp | 56 ++++++++++++++++++++++++++++++++++++++++++
far/common/function_ref.hpp | 28 ++++++++++++++++++---
far/common/function_traits.hpp | 1 +
far/interf.cpp | 8 ++----
5 files changed, 85 insertions(+), 10 deletions(-)

diff --git a/far/RegExp.cpp b/far/RegExp.cpp
index b6ad9be59..8881e9682 100644
--- a/far/RegExp.cpp
+++ b/far/RegExp.cpp
@@ -3983,7 +3983,7 @@ TEST_CASE("regex.named_groups")

for (const auto& [k, v]: i.NamedMatch)
{
- const auto It = NamedMatch.Matches.find(k);
+ const auto It = NamedMatch.Matches.find(string_comparer::generic_key{ k });
REQUIRE(It != NamedMatch.Matches.cend());
REQUIRE(It->second == v);
}
diff --git a/far/common.tests.cpp b/far/common.tests.cpp
index 941086caa..a28899dc4 100644
--- a/far/common.tests.cpp
+++ b/far/common.tests.cpp
@@ -506,6 +506,62 @@ TEST_CASE("from_string")

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

+#include "common/function_ref.hpp"
+
+TEST_CASE("function_ref")
+{
+ {
+ struct s
+ {
+ static int square(int const i) { return i * i; }
+ };
+
+ function_ref const Func = &s::square;
+ REQUIRE(Func(2) == 4);
+ }
+
+ {
+ struct square
+ {
+ int operator()(int const i) const { return i * i; }
+ };
+
+ square const Square;
+ function_ref const Func = Square;
+ REQUIRE(Func(2) == 4);
+ }
+
+ {
+ const auto square = [](int const i) { return i * i; };
+
+ function_ref const Func = square;
+ REQUIRE(Func(2) == 4);
+ }
+
+ {
+ const auto square = [&](int const i) { return i * i; };
+
+ function_ref const Func = square;
+ REQUIRE(Func(2) == 4);
+ }
+
+ {
+ auto square = [&](int const i) mutable { return i * i; };
+
+ function_ref const Func = square;
+ REQUIRE(Func(2) == 4);
+ }
+
+ {
+ const auto square = [](int const i) mutable { return i * i; };
+
+ function_ref const Func = square;
+ REQUIRE(Func(2) == 4);
+ }
+}
+
+//----------------------------------------------------------------------------
+
#include "common/function_traits.hpp"

TEST_CASE("function_traits")
diff --git a/far/common/function_ref.hpp b/far/common/function_ref.hpp
index a7ce51e2d..daca92b05 100644
--- a/far/common/function_ref.hpp
+++ b/far/common/function_ref.hpp
@@ -33,6 +33,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "preprocessor.hpp"
+#include "function_traits.hpp"

#include <functional>

@@ -48,11 +49,11 @@ public:
WARNING_PUSH()
WARNING_DISABLE_MSC(4180) // qualifier applied to function type has no meaning; ignored
template<typename callable_type, REQUIRES(!std::is_same_v<std::decay_t<callable_type>, function_ref>)>
- function_ref(const callable_type& Callable) noexcept:
- m_Ptr(const_cast<void*>(reinterpret_cast<const void*>(&Callable))),
+ function_ref(callable_type&& Callable) noexcept:
+ m_Ptr(to_ptr(FWD(Callable))),
m_ErasedFn([](void* Ptr, args... Args) -> return_type
{
- return std::invoke(*reinterpret_cast<const callable_type*>(Ptr), FWD(Args)...);
+ return std::invoke(from_ptr<callable_type>(Ptr), FWD(Args)...);
})
{
}
@@ -76,10 +77,31 @@ WARNING_POP()
}

private:
+ template<typename callable_type>
+ static auto to_ptr(callable_type&& Callable)
+ {
+ if constexpr (std::is_pointer_v<callable_type>)
+ return const_cast<void*>(reinterpret_cast<const void*>(Callable));
+ else
+ return const_cast<void*>(reinterpret_cast<const void*>(&Callable));
+ }
+
+ template<typename callable_type>
+ static auto& from_ptr(void* Ptr)
+ {
+ if constexpr (std::is_pointer_v<callable_type>)
+ return *reinterpret_cast<callable_type>(Ptr);
+ else
+ return *reinterpret_cast<std::add_pointer_t<callable_type>>(Ptr);
+ }
+
using signature_type = return_type(void*, args...);

void* m_Ptr;
signature_type* m_ErasedFn;
};

+template<typename object>
+function_ref(object) -> function_ref<typename function_traits<object>::signature_type>;
+
#endif // FUNCTION_REF_HPP_0B2E3AF4_AB0A_4C89_9FC1_1A92AC2699A4
diff --git a/far/common/function_traits.hpp b/far/common/function_traits.hpp
index d20fc1812..17115cad1 100644
--- a/far/common/function_traits.hpp
+++ b/far/common/function_traits.hpp
@@ -42,6 +42,7 @@ namespace detail
struct function_traits_impl
{
using result_type = result;
+ using signature_type = result(args...);

static constexpr auto arity = sizeof...(args);

diff --git a/far/interf.cpp b/far/interf.cpp
index 11133fcc8..fbf73bf82 100644
--- a/far/interf.cpp
+++ b/far/interf.cpp
@@ -1206,9 +1206,7 @@ size_t string_pos_to_visual_pos(string_view Str, size_t const StringPos, size_t
if (SavedState && StringPos > SavedState->StringIndex)
State = *SavedState;

- // Lambda capture is essential here, without it the lambda will happily convert itself
- // to a temporary function pointer and function_ref will refer to that temporary.
- const auto nop_signal = [&](size_t, size_t){};
+ const auto nop_signal = [](size_t, size_t){};
const auto signal = State.signal? State.signal : nop_signal;

const auto End = std::min(Str.size(), StringPos);
@@ -1263,9 +1261,7 @@ size_t visual_pos_to_string_pos(string_view Str, size_t const VisualPos, size_t
if (SavedState && VisualPos > SavedState->VisualIndex)
State = *SavedState;

- // Lambda capture is essential here, without it the lambda will happily convert itself
- // to a temporary function pointer and function_ref will refer to that temporary.
- const auto nop_signal = [&](size_t, size_t){};
+ const auto nop_signal = [](size_t, size_t){};
const auto signal = State.signal? State.signal : nop_signal;

const auto End = Str.size();


Reply all
Reply to author
Forward
0 new messages