aboutsummaryrefslogtreecommitdiff
path: root/MoonParser/pegtl/analysis/insert_guard.hpp
blob: 9a4cc35f4fa2436fd782833d3516bb5c5e3d4848 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright (c) 2014-2017 Dr. Colin Hirsch and Daniel Frey
// Please see LICENSE for license or visit https://github.com/taocpp/PEGTL/

#ifndef TAOCPP_PEGTL_INCLUDE_ANALYSIS_INSERT_GUARD_HPP
#define TAOCPP_PEGTL_INCLUDE_ANALYSIS_INSERT_GUARD_HPP

#include <utility>

#include "../config.hpp"

namespace tao
{
   namespace TAOCPP_PEGTL_NAMESPACE
   {
      namespace analysis
      {
         template< typename C >
         class insert_guard
         {
         public:
            insert_guard( insert_guard&& other ) noexcept
               : m_i( other.m_i ),
                 m_c( other.m_c )
            {
               other.m_c = nullptr;
            }

            insert_guard( C& container, const typename C::value_type& value )
               : m_i( container.insert( value ) ),
                 m_c( &container )
            {
            }

            ~insert_guard()
            {
               if( m_c && m_i.second ) {
                  m_c->erase( m_i.first );
               }
            }

            insert_guard( const insert_guard& ) = delete;
            void operator=( const insert_guard& ) = delete;

            explicit operator bool() const noexcept
            {
               return m_i.second;
            }

         private:
            const std::pair< typename C::iterator, bool > m_i;
            C* m_c;
         };

         template< typename C >
         insert_guard< C > make_insert_guard( C& container, const typename C::value_type& value )
         {
            return insert_guard< C >( container, value );
         }

      }  // namespace analysis

   }  // namespace TAOCPP_PEGTL_NAMESPACE

}  // namespace tao

#endif