Skip to content

Commit ffce39b

Browse files
committed
Add defer
1 parent 67c3a2a commit ffce39b

9 files changed

Lines changed: 166 additions & 1 deletion

File tree

Code/max/Algorithms/Defer.hpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2026, The max Contributors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef MAX_ALGORITHMS_DEFER_HPP
6+
#define MAX_ALGORITHMS_DEFER_HPP
7+
8+
#include <functional>
9+
10+
#include <max/Compiling/Configuration.hpp>
11+
#include <max/Compiling/CurrentVersionNamespace.hpp>
12+
#include <max/Compiling/AliasingOptimizations.hpp>
13+
14+
namespace max
15+
{
16+
MAX_CURRENT_VERSION_NAMESPACE_BEGIN(v0)
17+
{
18+
namespace Algorithms
19+
{
20+
21+
template<typename Handler>
22+
class Defer {
23+
public:
24+
25+
Defer(Handler handler);
26+
~Defer();
27+
28+
private:
29+
30+
Handler handler_;
31+
32+
};
33+
34+
#define MAX_DEFER(Callback) auto defer = max::Algorithms::Defer{[&](){Callback}};
35+
36+
} // namespace Algorithms
37+
} // MAX_CURRENT_VERSION_NAMESPACE_BEGIN( v0 )
38+
MAX_CURRENT_VERSION_NAMESPACE_END(v0)
39+
} // namespace max
40+
41+
#include <max/Algorithms/Defer.inl>
42+
43+
#endif // #ifndef MAX_ALGORITHMS_DEFER_HPP

Code/max/Algorithms/Defer.inl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2026, The max Contributors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include <utility>
6+
7+
#include <max/Compiling/Configuration.hpp>
8+
#include <max/Compiling/Assume.hpp>
9+
10+
namespace max
11+
{
12+
namespace v0
13+
{
14+
namespace Algorithms
15+
{
16+
17+
template<typename Handler>
18+
Defer<Handler>::Defer(Handler handler)
19+
: handler_(std::move(handler))
20+
{}
21+
22+
template<typename Handler>
23+
Defer<Handler>::~Defer() {
24+
handler_();
25+
}
26+
27+
} // namespace Algorithms
28+
} // namespace v0
29+
} // namespace max

Code/max/Algorithms/DeferTest.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2026, The max Contributors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "DeferTest.hpp"
6+
#include <max/Algorithms/Defer.hpp>
7+
#include <max/Testing/TestSuite.hpp>
8+
#include <max/Testing/CoutResultPolicy.hpp>
9+
#include <utility>
10+
11+
namespace maxAutomatedTests
12+
{
13+
namespace Algorithms
14+
{
15+
16+
void RunDeferTestSuite()
17+
{
18+
max::Testing::CoutResultPolicy ResultPolicy;
19+
auto DeferTestSuite = max::Testing::TestSuite< max::Testing::CoutResultPolicy >{ "max::Algorithms::Defer test suite", std::move(ResultPolicy) };
20+
21+
DeferTestSuite.AddTest(max::Testing::Test< max::Testing::CoutResultPolicy >{ "inside range", [](max::Testing::Test< max::Testing::CoutResultPolicy >& CurrentTest, max::Testing::CoutResultPolicy const& ResultPolicy) {
22+
bool was_defer_called = false;
23+
24+
// Create a scope so the defer is called
25+
{
26+
MAX_DEFER(
27+
was_defer_called = true;
28+
);
29+
}
30+
31+
CurrentTest.MAX_TESTING_ASSERT(was_defer_called == true);
32+
}
33+
});
34+
35+
DeferTestSuite.RunTests();
36+
}
37+
38+
} // namespace Algorithms
39+
} // namespace maxAutomatedTests

Code/max/Algorithms/DeferTest.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2026, The max Contributors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef MAXAUTOMATEDTESTS_ALGORITHMS_DEFERTEST_HPP
6+
#define MAXAUTOMATEDTESTS_ALGORITHMS_DEFERTEST_HPP
7+
8+
namespace maxAutomatedTests
9+
{
10+
namespace Algorithms
11+
{
12+
13+
void RunDeferTestSuite();
14+
15+
} // namespace Algorithms
16+
} // namespace maxAutomatedTests
17+
18+
#endif // #ifndef MAXAUTOMATEDTESTS_ALGORITHMS_DEFERTEST_HPP

Code/max/Testing/AutomatedTestsEntryPoint.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
#include <max/Algorithms/CountZeroesTest.hpp>
6+
#include <max/Algorithms/DeferTest.hpp>
57
#include <max/Algorithms/IsBetweenTest.hpp>
68
#include <max/Algorithms/SwapEndianTest.hpp>
7-
#include <max/Algorithms/CountZeroesTest.hpp>
89
#include <max/Containers/PointTest.hpp>
910
#include <max/Containers/RangeTest.hpp>
1011
#include <max/Containers/RectangleTest.hpp>
@@ -14,6 +15,7 @@
1415

1516
int main()
1617
{
18+
maxAutomatedTests::Algorithms::RunDeferTestSuite();
1719
maxAutomatedTests::Algorithms::RunIsBetweenTestSuite();
1820
maxAutomatedTests::Algorithms::RunSwapEndianTestSuite();
1921
maxAutomatedTests::Algorithms::RunCountZeroesTestSuite();

Projects/VisualStudio/max/max.vcxproj

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<None Include="..\..\..\AUTHORS" />
2626
<None Include="..\..\..\Code\max\Algorithms\Branchless.inl" />
2727
<None Include="..\..\..\Code\max\Algorithms\CountZeroes.inl" />
28+
<None Include="..\..\..\Code\max\Algorithms\Defer.inl" />
2829
<None Include="..\..\..\Code\max\Algorithms\IsBetween.inl" />
2930
<None Include="..\..\..\Code\max\Algorithms\IsBetween.md" />
3031
<None Include="..\..\..\Code\max\Algorithms\Math.inl" />
@@ -71,6 +72,13 @@
7172
<ItemGroup>
7273
<ClInclude Include="..\..\..\Code\max\Algorithms\Branchless.hpp" />
7374
<ClInclude Include="..\..\..\Code\max\Algorithms\CountZeroes.hpp" />
75+
<ClInclude Include="..\..\..\Code\max\Algorithms\Defer.hpp" />
76+
<ClInclude Include="..\..\..\Code\max\Algorithms\DeferTest.hpp">
77+
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
78+
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
79+
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
80+
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
81+
</ClInclude>
7482
<ClInclude Include="..\..\..\Code\max\Algorithms\IsBetween.hpp" />
7583
<ClInclude Include="..\..\..\Code\max\Algorithms\IsBetweenTest.hpp">
7684
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
@@ -284,6 +292,12 @@
284292
<ClInclude Include="..\..\..\Code\max\Testing\TestSuite.hpp" />
285293
</ItemGroup>
286294
<ItemGroup>
295+
<ClCompile Include="..\..\..\Code\max\Algorithms\DeferTest.cpp">
296+
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
297+
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
298+
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
299+
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
300+
</ClCompile>
287301
<ClCompile Include="..\..\..\Code\max\Algorithms\IsBetweenTest.cpp">
288302
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
289303
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>

Projects/VisualStudio/max/max.vcxproj.filters

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@
207207
<None Include="..\..\..\Code\max\Containers\SlotMap.inl">
208208
<Filter>Code\max\Containers</Filter>
209209
</None>
210+
<None Include="..\..\..\Code\max\Algorithms\Defer.inl">
211+
<Filter>Code\max\Algorithms</Filter>
212+
</None>
210213
</ItemGroup>
211214
<ItemGroup>
212215
<ClInclude Include="..\..\..\Code\max\Algorithms\IsBetween.hpp">
@@ -483,6 +486,12 @@
483486
<ClInclude Include="..\..\..\Code\max\Containers\SlotMapTest.hpp">
484487
<Filter>Code\max\Containers</Filter>
485488
</ClInclude>
489+
<ClInclude Include="..\..\..\Code\max\Algorithms\Defer.hpp">
490+
<Filter>Code\max\Algorithms</Filter>
491+
</ClInclude>
492+
<ClInclude Include="..\..\..\Code\max\Algorithms\DeferTest.hpp">
493+
<Filter>Code\max\Algorithms</Filter>
494+
</ClInclude>
486495
</ItemGroup>
487496
<ItemGroup>
488497
<ClCompile Include="..\..\..\Code\max\Algorithms\IsBetweenTest.cpp">
@@ -611,6 +620,9 @@
611620
<ClCompile Include="..\..\..\Code\max\Containers\SlotMapTest.cpp">
612621
<Filter>Code\max\Containers</Filter>
613622
</ClCompile>
623+
<ClCompile Include="..\..\..\Code\max\Algorithms\DeferTest.cpp">
624+
<Filter>Code\max\Algorithms</Filter>
625+
</ClCompile>
614626
</ItemGroup>
615627
<ItemGroup>
616628
<MASM Include="..\..\..\Code\max\Hardware\CPU\IsCPUIDAvailablePolicies\max_Hardware_CPU_X64AssemblyIsCPUIDAvailablePolicies_IsCPUIDAvailable.asm">

Projects/VisualStudio/maxAutomatedTests/maxAutomatedTests.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
</ItemGroup>
2121
<ItemGroup>
2222
<ClCompile Include="..\..\..\Code\max\Algorithms\CountZeroesTest.cpp" />
23+
<ClCompile Include="..\..\..\Code\max\Algorithms\DeferTest.cpp" />
2324
<ClCompile Include="..\..\..\Code\max\Algorithms\IsBetweenTest.cpp" />
2425
<ClCompile Include="..\..\..\Code\max\Algorithms\SwapEndianTest.cpp" />
2526
<ClCompile Include="..\..\..\Code\max\Containers\PointTest.cpp" />
@@ -39,6 +40,7 @@
3940
</ItemGroup>
4041
<ItemGroup>
4142
<ClInclude Include="..\..\..\Code\max\Algorithms\CountZeroesTest.hpp" />
43+
<ClInclude Include="..\..\..\Code\max\Algorithms\DeferTest.hpp" />
4244
<ClInclude Include="..\..\..\Code\max\Algorithms\IsBetweenTest.hpp" />
4345
<ClInclude Include="..\..\..\Code\max\Algorithms\SwapEndianTest.hpp" />
4446
<ClInclude Include="..\..\..\Code\max\Containers\PointTest.hpp" />

Projects/VisualStudio/maxAutomatedTests/maxAutomatedTests.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@
6666
<ClCompile Include="..\..\..\Code\max\Containers\SlotMapTest.cpp">
6767
<Filter>Containers</Filter>
6868
</ClCompile>
69+
<ClCompile Include="..\..\..\Code\max\Algorithms\DeferTest.cpp">
70+
<Filter>Algorithms</Filter>
71+
</ClCompile>
6972
</ItemGroup>
7073
<ItemGroup>
7174
<ClInclude Include="..\..\..\Code\max\Algorithms\IsBetweenTest.hpp">
@@ -116,5 +119,8 @@
116119
<ClInclude Include="..\..\..\Code\max\Containers\SlotMapTest.hpp">
117120
<Filter>Containers</Filter>
118121
</ClInclude>
122+
<ClInclude Include="..\..\..\Code\max\Algorithms\DeferTest.hpp">
123+
<Filter>Algorithms</Filter>
124+
</ClInclude>
119125
</ItemGroup>
120126
</Project>

0 commit comments

Comments
 (0)