Bond
 
Loading...
Searching...
No Matches
recursionguard.h
1// Copyright (c) Microsoft. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
4#pragma once
5
6#include <bond/core/config.h>
7#include <bond/core/exception.h>
8#include <stdint.h>
9
10namespace bond
11{
12namespace detail
13{
14
15template<class T>
16struct RecursionGuardStaticsHolder
17{
19 static uint32_t maxDepth;
20
22 thread_local static uint32_t currentDepth;
23};
24
25template<class T>
26uint32_t RecursionGuardStaticsHolder<T>::maxDepth = 64;
27
28template<class T>
29thread_local uint32_t RecursionGuardStaticsHolder<T>::currentDepth;
30
32class RecursionGuard : RecursionGuardStaticsHolder<void>
33{
34public:
35 RecursionGuard()
36 {
37 uint32_t depth = currentDepth;
38 if (depth >= maxDepth)
39 {
40 bond::ExceededMaxRecursionDepthException();
41 }
42
43 currentDepth = depth + 1;
44 }
45
46 ~RecursionGuard()
47 {
48 currentDepth--;
49 }
50
52 static void SetMaxDepth(uint32_t value)
53 {
54 maxDepth = value;
55 }
56};
57
58} // namespace detail
59} // namespace bond
static void SetMaxDepth(uint32_t value)
Sets the maximum recursion depth permitted.
Definition recursionguard.h:52
namespace bond
Definition apply.h:17