From ffce39bbfe6915a39e21b2d1388801fb1eb14878 Mon Sep 17 00:00:00 2001 From: Chris Blume Date: Thu, 12 Feb 2026 01:40:02 -0500 Subject: [PATCH] Add defer --- Code/max/Algorithms/Defer.hpp | 43 +++++++++++++++++++ Code/max/Algorithms/Defer.inl | 29 +++++++++++++ Code/max/Algorithms/DeferTest.cpp | 39 +++++++++++++++++ Code/max/Algorithms/DeferTest.hpp | 18 ++++++++ Code/max/Testing/AutomatedTestsEntryPoint.cpp | 4 +- Projects/VisualStudio/max/max.vcxproj | 14 ++++++ Projects/VisualStudio/max/max.vcxproj.filters | 12 ++++++ .../maxAutomatedTests.vcxproj | 2 + .../maxAutomatedTests.vcxproj.filters | 6 +++ 9 files changed, 166 insertions(+), 1 deletion(-) create mode 100644 Code/max/Algorithms/Defer.hpp create mode 100644 Code/max/Algorithms/Defer.inl create mode 100644 Code/max/Algorithms/DeferTest.cpp create mode 100644 Code/max/Algorithms/DeferTest.hpp diff --git a/Code/max/Algorithms/Defer.hpp b/Code/max/Algorithms/Defer.hpp new file mode 100644 index 0000000..856f8b7 --- /dev/null +++ b/Code/max/Algorithms/Defer.hpp @@ -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 + +#include +#include +#include + +namespace max +{ + MAX_CURRENT_VERSION_NAMESPACE_BEGIN(v0) + { + namespace Algorithms + { + + template + 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 + +#endif // #ifndef MAX_ALGORITHMS_DEFER_HPP diff --git a/Code/max/Algorithms/Defer.inl b/Code/max/Algorithms/Defer.inl new file mode 100644 index 0000000..82c1db2 --- /dev/null +++ b/Code/max/Algorithms/Defer.inl @@ -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 + +#include +#include + +namespace max +{ + namespace v0 + { + namespace Algorithms + { + + template + Defer::Defer(Handler handler) + : handler_(std::move(handler)) + {} + + template + Defer::~Defer() { + handler_(); + } + + } // namespace Algorithms + } // namespace v0 +} // namespace max diff --git a/Code/max/Algorithms/DeferTest.cpp b/Code/max/Algorithms/DeferTest.cpp new file mode 100644 index 0000000..832cb08 --- /dev/null +++ b/Code/max/Algorithms/DeferTest.cpp @@ -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 +#include +#include +#include + +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 diff --git a/Code/max/Algorithms/DeferTest.hpp b/Code/max/Algorithms/DeferTest.hpp new file mode 100644 index 0000000..8742216 --- /dev/null +++ b/Code/max/Algorithms/DeferTest.hpp @@ -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 diff --git a/Code/max/Testing/AutomatedTestsEntryPoint.cpp b/Code/max/Testing/AutomatedTestsEntryPoint.cpp index f8f8091..70719e8 100644 --- a/Code/max/Testing/AutomatedTestsEntryPoint.cpp +++ b/Code/max/Testing/AutomatedTestsEntryPoint.cpp @@ -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 +#include #include #include -#include #include #include #include @@ -14,6 +15,7 @@ int main() { + maxAutomatedTests::Algorithms::RunDeferTestSuite(); maxAutomatedTests::Algorithms::RunIsBetweenTestSuite(); maxAutomatedTests::Algorithms::RunSwapEndianTestSuite(); maxAutomatedTests::Algorithms::RunCountZeroesTestSuite(); diff --git a/Projects/VisualStudio/max/max.vcxproj b/Projects/VisualStudio/max/max.vcxproj index 7a9decf..54ee0a8 100644 --- a/Projects/VisualStudio/max/max.vcxproj +++ b/Projects/VisualStudio/max/max.vcxproj @@ -25,6 +25,7 @@ + @@ -71,6 +72,13 @@ + + + true + true + true + true + true @@ -284,6 +292,12 @@ + + true + true + true + true + true true diff --git a/Projects/VisualStudio/max/max.vcxproj.filters b/Projects/VisualStudio/max/max.vcxproj.filters index 4d421cf..4f8f626 100644 --- a/Projects/VisualStudio/max/max.vcxproj.filters +++ b/Projects/VisualStudio/max/max.vcxproj.filters @@ -207,6 +207,9 @@ Code\max\Containers + + Code\max\Algorithms + @@ -483,6 +486,12 @@ Code\max\Containers + + Code\max\Algorithms + + + Code\max\Algorithms + @@ -611,6 +620,9 @@ Code\max\Containers + + Code\max\Algorithms + diff --git a/Projects/VisualStudio/maxAutomatedTests/maxAutomatedTests.vcxproj b/Projects/VisualStudio/maxAutomatedTests/maxAutomatedTests.vcxproj index 1d2f622..826695a 100644 --- a/Projects/VisualStudio/maxAutomatedTests/maxAutomatedTests.vcxproj +++ b/Projects/VisualStudio/maxAutomatedTests/maxAutomatedTests.vcxproj @@ -20,6 +20,7 @@ + @@ -39,6 +40,7 @@ + diff --git a/Projects/VisualStudio/maxAutomatedTests/maxAutomatedTests.vcxproj.filters b/Projects/VisualStudio/maxAutomatedTests/maxAutomatedTests.vcxproj.filters index 61ea15d..43cbc4c 100644 --- a/Projects/VisualStudio/maxAutomatedTests/maxAutomatedTests.vcxproj.filters +++ b/Projects/VisualStudio/maxAutomatedTests/maxAutomatedTests.vcxproj.filters @@ -66,6 +66,9 @@ Containers + + Algorithms + @@ -116,5 +119,8 @@ Containers + + Algorithms + \ No newline at end of file