Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions Code/max/Algorithms/Defer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright 2026, The max Contributors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MAX_ALGORITHMS_DEFER_HPP
#define MAX_ALGORITHMS_DEFER_HPP

#include <functional>

#include <max/Compiling/Configuration.hpp>
#include <max/Compiling/CurrentVersionNamespace.hpp>
#include <max/Compiling/AliasingOptimizations.hpp>

namespace max
{
MAX_CURRENT_VERSION_NAMESPACE_BEGIN(v0)
{
namespace Algorithms
{

template<typename Handler>
class Defer {
public:

Defer(Handler handler);
~Defer();

private:

Handler handler_;

};

#define MAX_DEFER(Callback) auto defer = max::Algorithms::Defer{[&](){Callback}};

} // namespace Algorithms
} // MAX_CURRENT_VERSION_NAMESPACE_BEGIN( v0 )
MAX_CURRENT_VERSION_NAMESPACE_END(v0)
} // namespace max

#include <max/Algorithms/Defer.inl>

#endif // #ifndef MAX_ALGORITHMS_DEFER_HPP
29 changes: 29 additions & 0 deletions Code/max/Algorithms/Defer.inl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2026, The max Contributors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <utility>

#include <max/Compiling/Configuration.hpp>
#include <max/Compiling/Assume.hpp>

namespace max
{
namespace v0
{
namespace Algorithms
{

template<typename Handler>
Defer<Handler>::Defer(Handler handler)
: handler_(std::move(handler))
{}

template<typename Handler>
Defer<Handler>::~Defer() {
handler_();
}

} // namespace Algorithms
} // namespace v0
} // namespace max
39 changes: 39 additions & 0 deletions Code/max/Algorithms/DeferTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2026, The max Contributors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "DeferTest.hpp"
#include <max/Algorithms/Defer.hpp>
#include <max/Testing/TestSuite.hpp>
#include <max/Testing/CoutResultPolicy.hpp>
#include <utility>

namespace maxAutomatedTests
{
namespace Algorithms
{

void RunDeferTestSuite()
{
max::Testing::CoutResultPolicy ResultPolicy;
auto DeferTestSuite = max::Testing::TestSuite< max::Testing::CoutResultPolicy >{ "max::Algorithms::Defer test suite", std::move(ResultPolicy) };

DeferTestSuite.AddTest(max::Testing::Test< max::Testing::CoutResultPolicy >{ "inside range", [](max::Testing::Test< max::Testing::CoutResultPolicy >& CurrentTest, max::Testing::CoutResultPolicy const& ResultPolicy) {
bool was_defer_called = false;

// Create a scope so the defer is called
{
MAX_DEFER(
was_defer_called = true;
);
}

CurrentTest.MAX_TESTING_ASSERT(was_defer_called == true);
}
});

DeferTestSuite.RunTests();
}

} // namespace Algorithms
} // namespace maxAutomatedTests
18 changes: 18 additions & 0 deletions Code/max/Algorithms/DeferTest.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2026, The max Contributors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MAXAUTOMATEDTESTS_ALGORITHMS_DEFERTEST_HPP
#define MAXAUTOMATEDTESTS_ALGORITHMS_DEFERTEST_HPP

namespace maxAutomatedTests
{
namespace Algorithms
{

void RunDeferTestSuite();

} // namespace Algorithms
} // namespace maxAutomatedTests

#endif // #ifndef MAXAUTOMATEDTESTS_ALGORITHMS_DEFERTEST_HPP
4 changes: 3 additions & 1 deletion Code/max/Testing/AutomatedTestsEntryPoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <max/Algorithms/CountZeroesTest.hpp>
#include <max/Algorithms/DeferTest.hpp>
#include <max/Algorithms/IsBetweenTest.hpp>
#include <max/Algorithms/SwapEndianTest.hpp>
#include <max/Algorithms/CountZeroesTest.hpp>
#include <max/Containers/PointTest.hpp>
#include <max/Containers/RangeTest.hpp>
#include <max/Containers/RectangleTest.hpp>
Expand All @@ -14,6 +15,7 @@

int main()
{
maxAutomatedTests::Algorithms::RunDeferTestSuite();
maxAutomatedTests::Algorithms::RunIsBetweenTestSuite();
maxAutomatedTests::Algorithms::RunSwapEndianTestSuite();
maxAutomatedTests::Algorithms::RunCountZeroesTestSuite();
Expand Down
14 changes: 14 additions & 0 deletions Projects/VisualStudio/max/max.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<None Include="..\..\..\AUTHORS" />
<None Include="..\..\..\Code\max\Algorithms\Branchless.inl" />
<None Include="..\..\..\Code\max\Algorithms\CountZeroes.inl" />
<None Include="..\..\..\Code\max\Algorithms\Defer.inl" />
<None Include="..\..\..\Code\max\Algorithms\IsBetween.inl" />
<None Include="..\..\..\Code\max\Algorithms\IsBetween.md" />
<None Include="..\..\..\Code\max\Algorithms\Math.inl" />
Expand Down Expand Up @@ -71,6 +72,13 @@
<ItemGroup>
<ClInclude Include="..\..\..\Code\max\Algorithms\Branchless.hpp" />
<ClInclude Include="..\..\..\Code\max\Algorithms\CountZeroes.hpp" />
<ClInclude Include="..\..\..\Code\max\Algorithms\Defer.hpp" />
<ClInclude Include="..\..\..\Code\max\Algorithms\DeferTest.hpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClInclude>
<ClInclude Include="..\..\..\Code\max\Algorithms\IsBetween.hpp" />
<ClInclude Include="..\..\..\Code\max\Algorithms\IsBetweenTest.hpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
Expand Down Expand Up @@ -284,6 +292,12 @@
<ClInclude Include="..\..\..\Code\max\Testing\TestSuite.hpp" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\..\Code\max\Algorithms\DeferTest.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\..\Code\max\Algorithms\IsBetweenTest.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
Expand Down
12 changes: 12 additions & 0 deletions Projects/VisualStudio/max/max.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@
<None Include="..\..\..\Code\max\Containers\SlotMap.inl">
<Filter>Code\max\Containers</Filter>
</None>
<None Include="..\..\..\Code\max\Algorithms\Defer.inl">
<Filter>Code\max\Algorithms</Filter>
</None>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\..\Code\max\Algorithms\IsBetween.hpp">
Expand Down Expand Up @@ -483,6 +486,12 @@
<ClInclude Include="..\..\..\Code\max\Containers\SlotMapTest.hpp">
<Filter>Code\max\Containers</Filter>
</ClInclude>
<ClInclude Include="..\..\..\Code\max\Algorithms\Defer.hpp">
<Filter>Code\max\Algorithms</Filter>
</ClInclude>
<ClInclude Include="..\..\..\Code\max\Algorithms\DeferTest.hpp">
<Filter>Code\max\Algorithms</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\..\Code\max\Algorithms\IsBetweenTest.cpp">
Expand Down Expand Up @@ -611,6 +620,9 @@
<ClCompile Include="..\..\..\Code\max\Containers\SlotMapTest.cpp">
<Filter>Code\max\Containers</Filter>
</ClCompile>
<ClCompile Include="..\..\..\Code\max\Algorithms\DeferTest.cpp">
<Filter>Code\max\Algorithms</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<MASM Include="..\..\..\Code\max\Hardware\CPU\IsCPUIDAvailablePolicies\max_Hardware_CPU_X64AssemblyIsCPUIDAvailablePolicies_IsCPUIDAvailable.asm">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\..\Code\max\Algorithms\CountZeroesTest.cpp" />
<ClCompile Include="..\..\..\Code\max\Algorithms\DeferTest.cpp" />
<ClCompile Include="..\..\..\Code\max\Algorithms\IsBetweenTest.cpp" />
<ClCompile Include="..\..\..\Code\max\Algorithms\SwapEndianTest.cpp" />
<ClCompile Include="..\..\..\Code\max\Containers\PointTest.cpp" />
Expand All @@ -39,6 +40,7 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\..\Code\max\Algorithms\CountZeroesTest.hpp" />
<ClInclude Include="..\..\..\Code\max\Algorithms\DeferTest.hpp" />
<ClInclude Include="..\..\..\Code\max\Algorithms\IsBetweenTest.hpp" />
<ClInclude Include="..\..\..\Code\max\Algorithms\SwapEndianTest.hpp" />
<ClInclude Include="..\..\..\Code\max\Containers\PointTest.hpp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@
<ClCompile Include="..\..\..\Code\max\Containers\SlotMapTest.cpp">
<Filter>Containers</Filter>
</ClCompile>
<ClCompile Include="..\..\..\Code\max\Algorithms\DeferTest.cpp">
<Filter>Algorithms</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\..\Code\max\Algorithms\IsBetweenTest.hpp">
Expand Down Expand Up @@ -116,5 +119,8 @@
<ClInclude Include="..\..\..\Code\max\Containers\SlotMapTest.hpp">
<Filter>Containers</Filter>
</ClInclude>
<ClInclude Include="..\..\..\Code\max\Algorithms\DeferTest.hpp">
<Filter>Algorithms</Filter>
</ClInclude>
</ItemGroup>
</Project>
Loading