File: unique_ptr_adapters.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (98 lines) | stat: -rw-r--r-- 3,040 bytes parent folder | download | duplicates (6)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BASE_CONTAINERS_UNIQUE_PTR_ADAPTERS_H_
#define BASE_CONTAINERS_UNIQUE_PTR_ADAPTERS_H_

#include <memory>

#include "base/memory/raw_ptr.h"

namespace base {

// This transparent comparator allows to lookup by raw pointer in
// a container of unique pointers. This functionality is based on C++14
// extensions to std::set/std::map interface, and can also be used
// with base::flat_set/base::flat_map.
//
// Example usage:
//   Foo* foo = ...
//   std::set<std::unique_ptr<Foo>, base::UniquePtrComparator> set;
//   set.insert(std::unique_ptr<Foo>(foo));
//   ...
//   auto it = set.find(foo);
//   EXPECT_EQ(foo, it->get());
//
// You can find more information about transparent comparisons here:
// http://en.cppreference.com/w/cpp/utility/functional/less_void
struct UniquePtrComparator {
  using is_transparent = int;

  template <typename T, class Deleter>
  bool operator()(const std::unique_ptr<T, Deleter>& lhs,
                  const std::unique_ptr<T, Deleter>& rhs) const {
    return lhs < rhs;
  }

  template <typename T, class Deleter>
  bool operator()(const T* lhs, const std::unique_ptr<T, Deleter>& rhs) const {
    return lhs < rhs.get();
  }

  template <typename T, class Deleter, base::RawPtrTraits Traits>
  bool operator()(const raw_ptr<T, Traits>& lhs,
                  const std::unique_ptr<T, Deleter>& rhs) const {
    return lhs < rhs.get();
  }

  template <typename T, class Deleter>
  bool operator()(const std::unique_ptr<T, Deleter>& lhs, const T* rhs) const {
    return lhs.get() < rhs;
  }

  template <typename T, class Deleter, base::RawPtrTraits Traits>
  bool operator()(const std::unique_ptr<T, Deleter>& lhs,
                  const raw_ptr<T, Traits>& rhs) const {
    return lhs.get() < rhs;
  }
};

// UniquePtrMatcher is useful for finding an element in a container of
// unique_ptrs when you have the raw pointer.
//
// Example usage:
//   std::vector<std::unique_ptr<Foo>> vector;
//   Foo* element = ...
//   auto iter = std::ranges::find_if(vector, MatchesUniquePtr(element));
//
// Example of erasing from container:
//   EraseIf(v, MatchesUniquePtr(element));
//
template <class T, class Deleter = std::default_delete<T>>
struct UniquePtrMatcher {
  explicit UniquePtrMatcher(T* t) : t_(t) {}

  bool operator()(const std::unique_ptr<T, Deleter>& o) {
    return o.get() == t_;
  }

 private:
  const raw_ptr<T, DanglingUntriaged> t_;
};

template <class T, class Deleter = std::default_delete<T>>
UniquePtrMatcher<T, Deleter> MatchesUniquePtr(T* t) {
  return UniquePtrMatcher<T, Deleter>(t);
}

template <class T,
          class Deleter = std::default_delete<T>,
          base::RawPtrTraits Traits = base::RawPtrTraits::kEmpty>
UniquePtrMatcher<T, Deleter> MatchesUniquePtr(const raw_ptr<T, Traits>& t) {
  return UniquePtrMatcher<T, Deleter>(t.get());
}

}  // namespace base

#endif  // BASE_CONTAINERS_UNIQUE_PTR_ADAPTERS_H_