File indexing completed on 2025-01-18 10:15:44
0001 #ifndef DEPTH_GUARD_H_00000000000000000000000000000000000000000000000000000000
0002 #define DEPTH_GUARD_H_00000000000000000000000000000000000000000000000000000000
0003
0004 #if defined(_MSC_VER) || \
0005 (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \
0006 (__GNUC__ >= 4))
0007 #pragma once
0008 #endif
0009
0010 #include "exceptions.h"
0011
0012 namespace YAML {
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022 class DeepRecursion : public ParserException {
0023 public:
0024 virtual ~DeepRecursion() = default;
0025
0026 DeepRecursion(int depth, const Mark& mark_, const std::string& msg_);
0027
0028
0029 int depth() const {
0030 return m_depth;
0031 }
0032
0033 private:
0034 int m_depth = 0;
0035 };
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048 template <int max_depth = 2000>
0049 class DepthGuard final {
0050 public:
0051 DepthGuard(int & depth_, const Mark& mark_, const std::string& msg_) : m_depth(depth_) {
0052 ++m_depth;
0053 if ( max_depth <= m_depth ) {
0054 throw DeepRecursion{m_depth, mark_, msg_};
0055 }
0056 }
0057
0058 DepthGuard(const DepthGuard & copy_ctor) = delete;
0059 DepthGuard(DepthGuard && move_ctor) = delete;
0060 DepthGuard & operator=(const DepthGuard & copy_assign) = delete;
0061 DepthGuard & operator=(DepthGuard && move_assign) = delete;
0062
0063 ~DepthGuard() {
0064 --m_depth;
0065 }
0066
0067 int current_depth() const {
0068 return m_depth;
0069 }
0070
0071 private:
0072 int & m_depth;
0073 };
0074
0075 }
0076
0077 #endif