Back to home page

EIC code displayed by LXR

 
 

    


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))  // GCC supports "pragma once" correctly since 3.4
0007 #pragma once
0008 #endif
0009 
0010 #include "exceptions.h"
0011 
0012 namespace YAML {
0013 
0014 /**
0015  * @brief The DeepRecursion class
0016  *  An exception class which is thrown by DepthGuard. Ideally it should be
0017  * a member of DepthGuard. However, DepthGuard is a templated class which means
0018  * that any catch points would then need to know the template parameters. It is
0019  * simpler for clients to not have to know at the catch point what was the
0020  * maximum depth.
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   // Returns the recursion depth when the exception was thrown
0029   int depth() const {
0030     return m_depth;
0031   }
0032 
0033 private:
0034   int m_depth = 0;
0035 };
0036 
0037 /**
0038  * @brief The DepthGuard class
0039  *  DepthGuard takes a reference to an integer. It increments the integer upon
0040  * construction of DepthGuard and decrements the integer upon destruction.
0041  *
0042  * If the integer would be incremented past max_depth, then an exception is
0043  * thrown. This is ideally geared toward guarding against deep recursion.
0044  *
0045  * @param max_depth
0046  *  compile-time configurable maximum depth.
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 } // namespace YAML
0076 
0077 #endif // DEPTH_GUARD_H_00000000000000000000000000000000000000000000000000000000