-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 在项目中新增 `SafeCallback` 模块,包含 `safecallback.hpp` 和 `safecallback_unittest.cc` 文件。 - `SafeCallback` 类通过 `std::weak_ptr` 管理对象生命周期,确保回调函数在对象销毁后不会被调用。 - 提供 `makeSafeCallback` 工厂函数,用于简化回调函数的创建。 - 编写单元测试 `safecallback_unittest.cc`,验证回调函数在对象存活和销毁后的行为。 - 更新 `CMakeLists.txt` 和 `README.md` 文件,将 `SafeCallback` 模块集成到项目中。
- Loading branch information
Showing
5 changed files
with
117 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 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,5 @@ | ||
add_executable(safecallback_unittest safecallback_unittest.cc safecallback.hpp) | ||
target_link_libraries( | ||
safecallback_unittest PRIVATE GTest::gtest GTest::gtest_main GTest::gmock | ||
GTest::gmock_main) | ||
add_test(NAME safecallback_unittest COMMAND safecallback_unittest) |
This file contains 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,45 @@ | ||
#pragma once | ||
|
||
#include <functional> | ||
#include <iostream> | ||
#include <memory> | ||
#include <utility> | ||
|
||
template<typename CLASS, typename... ARGS> | ||
class SafeCallback | ||
{ | ||
public: | ||
SafeCallback(const std::weak_ptr<CLASS> &object, | ||
const std::function<void(CLASS *, ARGS...)> &function) | ||
: m_object(object) | ||
, m_function(function) | ||
{} | ||
|
||
template<typename... CallARGS> | ||
void operator()(CallARGS &&...args) const | ||
{ | ||
if (auto ptr = m_object.lock()) { | ||
m_function(ptr.get(), std::forward<CallARGS>(args)...); | ||
} else { | ||
std::cout << "Callback object has been destroyed." << std::endl; | ||
} | ||
} | ||
|
||
private: | ||
std::weak_ptr<CLASS> m_object; | ||
std::function<void(CLASS *, ARGS...)> m_function; | ||
}; | ||
|
||
template<typename CLASS, typename... ARGS> | ||
SafeCallback<CLASS, ARGS...> makeSafeCallback(const std::shared_ptr<CLASS> &object, | ||
void (CLASS::*function)(ARGS...)) | ||
{ | ||
return SafeCallback<CLASS, ARGS...>(object, function); | ||
} | ||
|
||
template<typename CLASS, typename... ARGS> | ||
SafeCallback<CLASS, ARGS...> makeSafeCallback(const std::shared_ptr<CLASS> &object, | ||
void (CLASS::*function)(ARGS...) const) | ||
{ | ||
return SafeCallback<CLASS, ARGS...>(object, function); | ||
} |
This file contains 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,63 @@ | ||
#include "safecallback.hpp" | ||
|
||
#include <gtest/gtest.h> | ||
#include <iostream> | ||
#include <memory> | ||
#include <thread> | ||
|
||
class MyClass | ||
{ | ||
public: | ||
void notify(int value) | ||
{ | ||
std::cout << "Received value: " << value << std::endl; | ||
received_value = value; | ||
} | ||
|
||
int received_value = -1; | ||
}; | ||
|
||
int free_function_value = -1; | ||
|
||
void freeFunction(int value) | ||
{ | ||
std::cout << "Free function received value: " << value << std::endl; | ||
free_function_value = value; | ||
} | ||
|
||
class SafeCallbackTest : public ::testing::Test | ||
{ | ||
protected: | ||
void SetUp() override { free_function_value = -1; } | ||
}; | ||
|
||
TEST_F(SafeCallbackTest, MemberFunctionCallback) | ||
{ | ||
auto obj = std::make_shared<MyClass>(); | ||
SafeCallback callback = makeSafeCallback(obj, &MyClass::notify); | ||
|
||
int test_value = 42; | ||
callback(test_value); | ||
|
||
EXPECT_EQ(obj->received_value, test_value); | ||
} | ||
|
||
TEST_F(SafeCallbackTest, MemberFunctionCallbackAfterObjectDestroyed) | ||
{ | ||
auto obj = std::make_shared<MyClass>(); | ||
SafeCallback callback = makeSafeCallback(obj, &MyClass::notify); | ||
|
||
int initial_value = obj->received_value; | ||
obj.reset(); | ||
int test_value = 42; | ||
callback(test_value); | ||
|
||
EXPECT_EQ(initial_value, -1); | ||
EXPECT_EQ(free_function_value, -1); | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
::testing::InitGoogleTest(&argc, argv); | ||
return RUN_ALL_TESTS(); | ||
} |