File tree Expand file tree Collapse file tree 6 files changed +102
-0
lines changed Expand file tree Collapse file tree 6 files changed +102
-0
lines changed Original file line number Diff line number Diff line change 1+ #include " CppUnitTest.h"
2+
3+ #include < YY/Base/Utils/AutoCleanup.h>
4+
5+ using namespace Microsoft ::VisualStudio::CppUnitTestFramework;
6+
7+ namespace UnitTest
8+ {
9+ TEST_CLASS (AutoCleanup)
10+ {
11+ public:
12+ TEST_METHOD (普通)
13+ {
14+ int _iValue = 0 ;
15+
16+ {
17+ auto _oCleanup = YY::MakeAutoCleanup (
18+ [&]()
19+ {
20+ _iValue++;
21+ });
22+
23+ Assert::AreEqual (0 , _iValue);
24+ }
25+ Assert::AreEqual (1 , _iValue);
26+ }
27+ };
28+ }
Original file line number Diff line number Diff line change 174174 </ItemGroup >
175175 <ItemGroup >
176176 <ClCompile Include =" AsyncFileUnitTest.cpp" />
177+ <ClCompile Include =" AutoCleanupUnitTest.cpp" />
177178 <ClCompile Include =" BindUnitTest.cpp" />
178179 <ClCompile Include =" BitMapUnitTest.cpp" />
179180 <ClCompile Include =" DynamicArrayUnitTest.cpp" />
Original file line number Diff line number Diff line change 4242 <ClCompile Include =" SpanUnitTest.cpp" >
4343 <Filter >单元测试</Filter >
4444 </ClCompile >
45+ <ClCompile Include =" AutoCleanupUnitTest.cpp" >
46+ <Filter >单元测试</Filter >
47+ </ClCompile >
4548 </ItemGroup >
4649 <ItemGroup >
4750 <ClInclude Include =" ToStringHelper.h" >
Original file line number Diff line number Diff line change 1+ #pragma once
2+ #include < YY/Base/YY.h>
3+
4+ /*
5+ AutoCleanup 使用RAII思想,在对象生命周期结束时,自动调用指定的清理函数。
6+ 但是不建议频繁的使用它,优先建议使用智能指针等更合适的资源管理方式。
7+
8+ ```cpp
9+ {
10+ int _iValue = 0;
11+ {
12+ auto _oCleanup = YY::MakeAutoCleanup(
13+ [&]()
14+ {
15+ _iValue++;
16+ });
17+ // 在这里 _iValue 仍然是 0
18+ _iValue == 0;
19+ }
20+ // 离开作用域后,清理函数被调用,_iValue 变为 1
21+ _iValue == 1;
22+ }
23+ ```
24+ */
25+
26+ #pragma pack(push, __YY_PACKING)
27+
28+ namespace YY
29+ {
30+ namespace Base
31+ {
32+ namespace Utils
33+ {
34+ template <typename Lambda>
35+ class AutoCleanupImpl
36+ {
37+ private:
38+ Lambda pfnCleanupCallBack;
39+
40+ public:
41+ AutoCleanupImpl (Lambda&& _pfnCallBack) noexcept
42+ : pfnCleanupCallBack(std::forward<Lambda>(_pfnCallBack))
43+ {
44+ }
45+
46+ AutoCleanupImpl (const AutoCleanupImpl&) = delete ;
47+ AutoCleanupImpl& operator =(const AutoCleanupImpl&) = delete ;
48+
49+ ~AutoCleanupImpl ()
50+ {
51+ pfnCleanupCallBack ();
52+ }
53+ };
54+
55+ template <typename Lambda>
56+ _Check_return_ AutoCleanupImpl<Lambda> __YYAPI MakeAutoCleanup (Lambda&& _pfnCallBack)
57+ {
58+ return AutoCleanupImpl<Lambda>(std::forward<Lambda>(_pfnCallBack));
59+ }
60+ }
61+ }
62+
63+ using namespace YY ::Base::Utils;
64+ }
65+
66+ #pragma pack(pop)
Original file line number Diff line number Diff line change 5858 </ClCompile >
5959 </ItemGroup >
6060 <ItemGroup >
61+ <ClInclude Include =" $(MSBuildThisFileDirectory)..\include\YY\Base\Utils\AutoCleanup.h" />
6162 <ClInclude Include =" $(MSBuildThisFileDirectory)\..\include\YY\Base\Containers\Array.h" />
6263 <ClInclude Include =" $(MSBuildThisFileDirectory)\..\include\YY\Base\Containers\Span.h" />
6364 <ClInclude Include =" $(MSBuildThisFileDirectory)\..\include\YY\Base\Containers\BitMap.h" />
Original file line number Diff line number Diff line change 293293 <ClInclude Include =" $(MSBuildThisFileDirectory)\..\include\YY\Base\Time\DataTime.h" >
294294 <Filter >头文件\YY\Base\Time</Filter >
295295 </ClInclude >
296+ <ClInclude Include =" $(MSBuildThisFileDirectory)..\include\YY\Base\Utils\AutoCleanup.h" >
297+ <Filter >头文件\YY\Base\Utils</Filter >
298+ </ClInclude >
296299 </ItemGroup >
297300 <ItemGroup >
298301 <Natvis Include =" $(MSBuildThisFileDirectory)\YY.Base.natvis" >
You can’t perform that action at this time.
0 commit comments