Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
comparable.h
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
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 FLUTTER_IMPELLER_BASE_COMPARABLE_H_
6#define FLUTTER_IMPELLER_BASE_COMPARABLE_H_
7
8#include <cstddef>
9#include <functional>
10#include <map>
11#include <memory>
12#include <type_traits>
13
14namespace impeller {
15
16struct UniqueID {
17 size_t id;
18
19 UniqueID();
20
21 constexpr auto operator<=>(const UniqueID&) const = default;
22};
23
25
26template <class Type>
28 public:
29 virtual std::size_t GetHash() const = 0;
30
31 virtual bool IsEqual(const Type& other) const = 0;
32};
33
34template <
35 class ComparableType,
36 class = std::enable_if_t<std::is_base_of_v<ComparableBase, ComparableType>>>
38 std::size_t operator()(const ComparableType& object) const {
39 return object.GetHash();
40 }
41};
42
43template <
44 class ComparableType,
45 class = std::enable_if_t<std::is_base_of_v<ComparableBase, ComparableType>>>
47 bool operator()(const ComparableType& lhs, const ComparableType& rhs) const {
48 return lhs.IsEqual(rhs);
49 }
50};
51
52template <
53 class ComparableType,
54 class = std::enable_if_t<std::is_base_of_v<ComparableBase, ComparableType>>>
55bool DeepComparePointer(const std::shared_ptr<ComparableType>& lhs,
56 const std::shared_ptr<ComparableType>& rhs) {
57 if (lhs == rhs) {
58 return true;
59 }
60
61 if (lhs && rhs) {
62 return lhs->IsEqual(*rhs);
63 }
64
65 return false;
66}
67
68template <
69 class Key,
70 class ComparableType,
71 class = std::enable_if_t<std::is_base_of_v<ComparableBase, ComparableType>>>
72bool DeepCompareMap(const std::map<Key, std::shared_ptr<ComparableType>>& lhs,
73 const std::map<Key, std::shared_ptr<ComparableType>>& rhs) {
74 if (lhs.size() != rhs.size()) {
75 return false;
76 }
77
78 for (auto i = lhs.begin(), j = rhs.begin(); i != lhs.end(); i++, j++) {
79 if (i->first != j->first) {
80 return false;
81 }
82
83 if (!DeepComparePointer(i->second, j->second)) {
84 return false;
85 }
86 }
87
88 return true;
89}
90
91} // namespace impeller
92
93namespace std {
94
95template <>
96struct hash<impeller::UniqueID> {
97 constexpr std::size_t operator()(const impeller::UniqueID& id) {
98 return id.id;
99 }
100};
101
102} // namespace std
103
104#endif // FLUTTER_IMPELLER_BASE_COMPARABLE_H_
virtual bool IsEqual(const Type &other) const =0
virtual std::size_t GetHash() const =0
bool DeepComparePointer(const std::shared_ptr< ComparableType > &lhs, const std::shared_ptr< ComparableType > &rhs)
Definition comparable.h:55
bool DeepCompareMap(const std::map< Key, std::shared_ptr< ComparableType > > &lhs, const std::map< Key, std::shared_ptr< ComparableType > > &rhs)
Definition comparable.h:72
Definition ref_ptr.h:261
bool operator()(const ComparableType &lhs, const ComparableType &rhs) const
Definition comparable.h:47
std::size_t operator()(const ComparableType &object) const
Definition comparable.h:38
constexpr auto operator<=>(const UniqueID &) const =default
constexpr std::size_t operator()(const impeller::UniqueID &id)
Definition comparable.h:97