-
-
Notifications
You must be signed in to change notification settings - Fork 519
fix: take handler for sol_lua_check
as function reference
#6393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b313633
fix: take handler for `sol_lua_check` as function reference
Nerixyz a9d5275
changelog
Nerixyz 0a5f6ca
fix: includes
Nerixyz 2ccb1ee
uintptr type
Nerixyz b3926e7
fix: ub and eq
Nerixyz 7bdf86a
Merge branch 'master' into fix/sol-fn-cb
pajlada 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
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
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,82 @@ | ||
#pragma once | ||
|
||
#include <concepts> | ||
#include <cstdint> | ||
#include <type_traits> | ||
#include <utility> | ||
|
||
namespace chatterino { | ||
|
||
/// A non-owning, type-erased reference to a callable. Callables can be lambdas, | ||
/// functions, or std::functions. | ||
/// | ||
/// It is intended to be used for functions taking a callback that is called | ||
/// immediately (such as filter/map functions). Since this doesn't own the | ||
/// callable, it's not safe to store. | ||
/// | ||
/// This is based on llvm::function_ref (updated for C++ 20). | ||
template <typename Fn> | ||
class FunctionRef; | ||
|
||
template <typename Ret, typename... Params> | ||
class FunctionRef<Ret(Params...)> | ||
{ | ||
public: | ||
FunctionRef() = default; | ||
FunctionRef(std::nullptr_t) | ||
{ | ||
} | ||
|
||
template <typename Callable> | ||
FunctionRef(Callable &&callable) // NOLINT | ||
requires | ||
// This is not the copy-constructor. | ||
(!std::same_as<std::remove_cvref_t<Callable>, FunctionRef>) && | ||
// Functor must be callable and return a suitable type. | ||
(std::is_void_v<Ret> || | ||
std::convertible_to< | ||
decltype(std::declval<Callable>()(std::declval<Params>()...)), | ||
Ret>) | ||
: callback(callTrampoline<std::remove_reference_t<Callable>>) | ||
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) | ||
, callable(reinterpret_cast<uintptr_t>(&callable)) | ||
Nerixyz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
} | ||
|
||
Ret operator()(Params... params) const | ||
{ | ||
return this->callback(this->callable, std::forward<Params>(params)...); | ||
Nerixyz marked this conversation as resolved.
Show resolved
Hide resolved
Nerixyz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
explicit operator bool() const | ||
{ | ||
return this->callback != nullptr; | ||
} | ||
|
||
bool operator==(const FunctionRef &other) const | ||
{ | ||
return this->callback == other.callback && | ||
this->callable == other.callable; | ||
} | ||
|
||
bool operator!=(const FunctionRef &other) const = default; | ||
|
||
private: | ||
// same signature as callTrampoline | ||
using Callback = Ret(uintptr_t, Params...); | ||
Nerixyz marked this conversation as resolved.
Show resolved
Hide resolved
Nerixyz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/// Pointer to the call trampoline that, given the callable, calls the target function. | ||
Callback *callback = nullptr; | ||
/// Pointer to the actual function | ||
uintptr_t callable = 0; | ||
Nerixyz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
template <typename Callable> | ||
static Ret callTrampoline(uintptr_t callable, Params... params) | ||
Nerixyz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) | ||
return (*reinterpret_cast<Callable *>(callable))( | ||
std::forward<Params>(params)...); | ||
Nerixyz marked this conversation as resolved.
Show resolved
Hide resolved
Nerixyz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
}; | ||
|
||
} // namespace chatterino |
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,171 @@ | ||
#include "util/FunctionRef.hpp" | ||
|
||
#include "Test.hpp" | ||
|
||
#include <memory> | ||
|
||
using namespace chatterino; | ||
|
||
namespace { | ||
|
||
size_t myFunc(const std::string &s) | ||
{ | ||
return s.size(); | ||
} | ||
|
||
std::string myFunc2(int i) | ||
{ | ||
return std::to_string(i); | ||
} | ||
|
||
std::string refCaller(FunctionRef<std::string(int)> fn, int i) | ||
Nerixyz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
return fn(i); | ||
} | ||
|
||
} // namespace | ||
|
||
TEST(FunctionRef, lambda) | ||
{ | ||
static int i = 0; | ||
auto empty = []() { | ||
i++; | ||
}; | ||
|
||
FunctionRef<void()>{empty}(); | ||
Nerixyz marked this conversation as resolved.
Show resolved
Hide resolved
Nerixyz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ASSERT_EQ(i, 1); | ||
|
||
auto unique = std::make_unique<int>(0); | ||
int *up = unique.get(); | ||
auto noncopyable = [p{std::move(unique)}]() { | ||
*p += 1; | ||
}; | ||
FunctionRef<void()>{noncopyable}(); | ||
Nerixyz marked this conversation as resolved.
Show resolved
Hide resolved
Nerixyz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ASSERT_EQ(*up, 1); | ||
{ | ||
FunctionRef<void()> f{std::move(noncopyable)}; | ||
Nerixyz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
f(); | ||
ASSERT_EQ(*up, 2); | ||
} | ||
|
||
auto identity = [](auto &&it) { | ||
return std::forward<decltype(it)>(it); | ||
}; | ||
|
||
ASSERT_EQ(FunctionRef<int(int)>{identity}(1), 1); | ||
Nerixyz marked this conversation as resolved.
Show resolved
Hide resolved
Nerixyz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ASSERT_EQ( | ||
FunctionRef<std::string(std::string)>{identity}(std::string{"foo"}), | ||
Nerixyz marked this conversation as resolved.
Show resolved
Hide resolved
Nerixyz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"foo"); | ||
|
||
ASSERT_EQ(refCaller( | ||
[](int i) { | ||
return std::to_string(i); | ||
}, | ||
42), | ||
"42"); | ||
|
||
ASSERT_EQ(refCaller( | ||
[p{std::make_unique<int>(1)}](int i) { | ||
return std::to_string(i + *p); | ||
}, | ||
42), | ||
"43"); | ||
} | ||
|
||
TEST(FunctionRef, pointer) | ||
{ | ||
ASSERT_EQ(FunctionRef<int(std::string)>{myFunc}("foo"), 3); | ||
ASSERT_EQ(FunctionRef<std::string(int)>{myFunc2}(2), "2"); | ||
ASSERT_EQ(refCaller(myFunc2, 42), "42"); | ||
} | ||
|
||
TEST(FunctionRef, stdFunction) | ||
{ | ||
int i = 0; | ||
FunctionRef<void()>{std::function{[&] { | ||
i++; | ||
}}}(); | ||
ASSERT_EQ(i, 1); | ||
ASSERT_EQ(FunctionRef<size_t(std::string)>{std::function{myFunc}}("foo"), | ||
3); | ||
} | ||
|
||
TEST(FunctionRef, operatorEq) | ||
{ | ||
int i = 0; | ||
FunctionRef<void()> f1([] {}); | ||
FunctionRef<void()> f2([&] { | ||
i++; | ||
}); | ||
FunctionRef<void()> f3([=]() mutable { | ||
i++; | ||
}); | ||
ASSERT_EQ(f1, f1); | ||
ASSERT_NE(f1, f2); | ||
ASSERT_NE(f1, f3); | ||
ASSERT_EQ(f2, f2); | ||
ASSERT_NE(f2, f1); | ||
ASSERT_NE(f2, f3); | ||
ASSERT_EQ(f3, f3); | ||
ASSERT_NE(f3, f1); | ||
ASSERT_NE(f3, f2); | ||
|
||
FunctionRef<size_t(std::string)> f4(myFunc); | ||
FunctionRef<size_t(std::string)> f5(myFunc); | ||
FunctionRef<size_t(std::string)> f6([](std::string /*s*/) { // NOLINT | ||
return 0; | ||
}); | ||
|
||
ASSERT_EQ(f4, f4); | ||
ASSERT_EQ(f4, f5); | ||
ASSERT_EQ(f5, f4); | ||
ASSERT_NE(f4, f6); | ||
ASSERT_NE(f6, f4); | ||
ASSERT_EQ(f6, f6); | ||
} | ||
|
||
TEST(FunctionRef, operatorBool) | ||
{ | ||
FunctionRef<void()> f0; | ||
FunctionRef<void()> f1([] {}); | ||
FunctionRef<size_t(std::string)> f2(myFunc); | ||
|
||
ASSERT_FALSE(f0); | ||
ASSERT_TRUE(f1); | ||
ASSERT_TRUE(f2); | ||
} | ||
|
||
TEST(FunctionRef, copyCtor) | ||
{ | ||
FunctionRef<void()> f0; | ||
auto f0Copy = f0; | ||
ASSERT_EQ(f0, f0Copy); | ||
ASSERT_FALSE(f0); | ||
ASSERT_FALSE(f0Copy); | ||
|
||
int i = 0; | ||
auto cb = [&] { | ||
i++; | ||
}; | ||
FunctionRef<void()> f1(cb); | ||
auto f1Copy = f1; | ||
ASSERT_EQ(f1, f1Copy); | ||
f1(); | ||
ASSERT_EQ(i, 1); | ||
f1Copy(); | ||
ASSERT_EQ(i, 2); | ||
|
||
FunctionRef<size_t(std::string)> f2(myFunc); | ||
auto f2Copy = f2; | ||
ASSERT_EQ(f2, f2Copy); | ||
ASSERT_EQ(f2("foobar"), f2Copy("foobar")); | ||
|
||
static_assert(std::is_trivially_copyable_v<decltype(f0)>); | ||
static_assert(std::is_trivially_copyable_v<decltype(f2)>); | ||
|
||
static_assert(std::is_trivially_move_assignable_v<decltype(f0)>); | ||
static_assert(std::is_trivially_move_assignable_v<decltype(f2)>); | ||
|
||
static_assert(std::is_trivially_move_constructible_v<decltype(f0)>); | ||
static_assert(std::is_trivially_move_constructible_v<decltype(f2)>); | ||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.