summaryrefslogtreecommitdiff
path: root/audio/Acx/Samples/Inc/cpp_utils.h
diff options
context:
space:
mode:
authorAdonais Romero González <[email protected]>2024-05-06 16:21:31 -0700
committerGitHub <[email protected]>2024-05-06 16:21:31 -0700
commita74a241c664c4e1d7c0838287b34076c19d9858a (patch)
tree6ff7562612967b122acf8acf8a69c4dcfd5905db /audio/Acx/Samples/Inc/cpp_utils.h
parentdef8e8e34ed2b7b1deb2fc9112ac4255f1a0f2ba (diff)
parent15477ce52bbb6b42ca591ecdfb484cac089f89ab (diff)
Merge develop changes prior to upcoming WDK release (May 2024)
Diffstat (limited to 'audio/Acx/Samples/Inc/cpp_utils.h')
-rw-r--r--audio/Acx/Samples/Inc/cpp_utils.h71
1 files changed, 71 insertions, 0 deletions
diff --git a/audio/Acx/Samples/Inc/cpp_utils.h b/audio/Acx/Samples/Inc/cpp_utils.h
new file mode 100644
index 00000000..f4ed6220
--- /dev/null
+++ b/audio/Acx/Samples/Inc/cpp_utils.h
@@ -0,0 +1,71 @@
+/*++
+
+Copyright (c) Microsoft Corporation. All rights reserved.
+
+ THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
+ KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
+ PURPOSE.
+
+Module Name:
+
+ cpp_utils.h
+
+Abstract:
+
+ Contains CPP utilities
+
+Environment:
+
+ Kernel mode
+
+--*/
+
+#pragma once
+
+// Function scope_exit instantiates scope_exit object
+// Constructor accepts lamda as parameter.
+// Assign tasks in lamdba to be executed on scope exit.
+template <typename F>
+auto scope_exit(F f)
+{
+ class scope_exit
+ {
+ public:
+ scope_exit(F f) :
+ _f{ f }
+ {
+ }
+
+ ~scope_exit()
+ {
+ if (_call)
+ {
+ _f();
+ }
+ }
+
+ // Ensures the scope_exit lambda will not be called
+ void release()
+ {
+ _call = false;
+ }
+
+ // Executes the scope_exit lambda immediately if not yet run; ensures it will not run again
+ void reset()
+ {
+ if (_call)
+ {
+ _f();
+ _call = false;
+ }
+ }
+
+ private:
+ F _f;
+ bool _call = true;
+ };
+
+ return scope_exit{ f };
+};
+