| Flutter Impeller |

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 #pragma once
6
7 #include <cstddef>
8 #include <functional>
9 #include <map>
10 #include <memory>
11 #include <string>
12 #include <type_traits>
13
14 #include "flutter/fml/hash_combine.h"
15 #include "flutter/fml/macros.h"
16
17 namespace impeller {
18
19 struct UniqueID {
20 size_t id;
21
22 UniqueID();
23
24 constexpr bool operator==(const UniqueID& other) const {
25 return id == other.id;
26 }
27 };
28
29 class ComparableBase {};
30
31 template <class Type>
32 class Comparable : ComparableBase {
33 public:
34 virtual std::size_t GetHash() const = 0;
35
36 virtual bool IsEqual(const Type& other) const = 0;
37 };
38
39 template <
40 class ComparableType,
41 class = std::enable_if_t<std::is_base_of_v<ComparableBase, ComparableType>>>
42 struct ComparableHash {
43 std::size_t operator()(const ComparableType& object) const {
44 return object.GetHash();
45 }
46 };
47
48 template <
49 class ComparableType,
50 class = std::enable_if_t<std::is_base_of_v<ComparableBase, ComparableType>>>
51 struct ComparableEqual {
52 bool operator()(const ComparableType& lhs, const ComparableType& rhs) const {
53 return lhs.IsEqual(rhs);
54 }
55 };
56
57 template <
58 class ComparableType,
59 class = std::enable_if_t<std::is_base_of_v<ComparableBase, ComparableType>>>
60 bool DeepComparePointer(const std::shared_ptr<ComparableType>& lhs,
61 const std::shared_ptr<ComparableType>& rhs) {
62 if (lhs == rhs) {
63 return true;
64 }
65
66 if (lhs && rhs) {
67 return lhs->IsEqual(*rhs);
68 }
69
70 return false;
71 }
72
73 template <
74 class Key,
75 class ComparableType,
76 class = std::enable_if_t<std::is_base_of_v<ComparableBase, ComparableType>>>
77 bool DeepCompareMap(const std::map<Key, std::shared_ptr<ComparableType>>& lhs,
78 const std::map<Key, std::shared_ptr<ComparableType>>& rhs) {
79 if (lhs.size() != rhs.size()) {
80 return false;
81 }
82
83 for (auto i = lhs.begin(), j = rhs.begin(); i != lhs.end(); i++, j++) {
84 if (i->first != j->first) {
85 return false;
86 }
87
88 if (!DeepComparePointer(i->second, j->second)) {
89 return false;
90 }
91 }
92
93 return true;
94 }
95
96 } // namespace impeller
97
98 namespace std {
99
100 template <>
101 struct hash<impeller::UniqueID> {
102 constexpr std::size_t operator()(const impeller::UniqueID& id) {
103 return id.id;
104 }
105 };
106
107 template <>
108 struct less<impeller::UniqueID> {
109 constexpr bool operator()(const impeller::UniqueID& lhs,
110 const impeller::UniqueID& rhs) const {
111 return lhs.id < rhs.id;
112 }
113 };
114
115 } // namespace std
impeller::DeepComparePointer
bool DeepComparePointer(const std::shared_ptr< ComparableType > &lhs, const std::shared_ptr< ComparableType > &rhs)
Definition: comparable.h:60
std::hash< impeller::UniqueID >::operator()
constexpr std::size_t operator()(const impeller::UniqueID &id)
Definition: comparable.h:102
impeller::ComparableEqual::operator()
bool operator()(const ComparableType &lhs, const ComparableType &rhs) const
Definition: comparable.h:52
impeller::ComparableHash
Definition: comparable.h:42
impeller::UniqueID::UniqueID
UniqueID()
Definition: comparable.cc:12
impeller::Comparable::GetHash
virtual std::size_t GetHash() const =0
impeller::UniqueID::id
size_t id
Definition: comparable.h:20
impeller::ComparableHash::operator()
std::size_t operator()(const ComparableType &object) const
Definition: comparable.h:43
std::less< impeller::UniqueID >::operator()
constexpr bool operator()(const impeller::UniqueID &lhs, const impeller::UniqueID &rhs) const
Definition: comparable.h:109
impeller::UniqueID::operator==
constexpr bool operator==(const UniqueID &other) const
Definition: comparable.h:24
impeller::ComparableEqual
Definition: comparable.h:51
impeller::Comparable
Definition: comparable.h:32
impeller::Comparable::IsEqual
virtual bool IsEqual(const Type &other) const =0
std
Definition: comparable.h:98
impeller::DeepCompareMap
bool DeepCompareMap(const std::map< Key, std::shared_ptr< ComparableType >> &lhs, const std::map< Key, std::shared_ptr< ComparableType >> &rhs)
Definition: comparable.h:77
impeller::UniqueID
Definition: comparable.h:19
impeller::ComparableBase
Definition: comparable.h:29
impeller
Definition: aiks_context.cc:10