• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 The PDFium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef CORE_FXCRT_SCOPED_SET_INSERTION_H_
6 #define CORE_FXCRT_SCOPED_SET_INSERTION_H_
7 
8 #include <set>
9 #include <utility>
10 
11 #include "core/fxcrt/fx_memory.h"
12 #include "third_party/base/check.h"
13 
14 namespace fxcrt {
15 
16 // Track the addition of an object to a set, removing it automatically when
17 // the ScopedSetInsertion goes out of scope.
18 template <typename T>
19 class ScopedSetInsertion {
20  public:
21   FX_STACK_ALLOCATED();
22 
ScopedSetInsertion(std::set<T> * org_set,const T & elem)23   ScopedSetInsertion(std::set<T>* org_set, const T& elem)
24       : set_(org_set), insert_results_(set_->insert(elem)) {
25     CHECK(insert_results_.second);
26   }
27   ScopedSetInsertion(const ScopedSetInsertion&) = delete;
28   ScopedSetInsertion& operator=(const ScopedSetInsertion&) = delete;
~ScopedSetInsertion()29   ~ScopedSetInsertion() { set_->erase(insert_results_.first); }
30 
31  private:
32   std::set<T>* const set_;
33   const std::pair<typename std::set<T>::iterator, bool> insert_results_;
34 };
35 
36 }  // namespace fxcrt
37 
38 using fxcrt::ScopedSetInsertion;
39 
40 #endif  // CORE_FXCRT_SCOPED_SET_INSERTION_H_
41