Files
flutter/engine/src/flutter/impeller/base/comparable.h
Jonah Williams c8d04e64dc [Impeller] add support for specialization constants. (flutter/engine#47432)
Adds support for Specialization constants to Impeller for our usage in the engine. A motivating example has been added in the impeller markdown docs.

Fixes https://github.com/flutter/flutter/issues/136210
Fixes https://github.com/flutter/flutter/issues/119357
2023-11-02 17:32:08 +00:00

112 lines
2.4 KiB
C++

// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#pragma once
#include <cstddef>
#include <functional>
#include <map>
#include <memory>
#include <type_traits>
namespace impeller {
struct UniqueID {
size_t id;
UniqueID();
constexpr bool operator==(const UniqueID& other) const {
return id == other.id;
}
};
class ComparableBase {};
template <class Type>
class Comparable : ComparableBase {
public:
virtual std::size_t GetHash() const = 0;
virtual bool IsEqual(const Type& other) const = 0;
};
template <
class ComparableType,
class = std::enable_if_t<std::is_base_of_v<ComparableBase, ComparableType>>>
struct ComparableHash {
std::size_t operator()(const ComparableType& object) const {
return object.GetHash();
}
};
template <
class ComparableType,
class = std::enable_if_t<std::is_base_of_v<ComparableBase, ComparableType>>>
struct ComparableEqual {
bool operator()(const ComparableType& lhs, const ComparableType& rhs) const {
return lhs.IsEqual(rhs);
}
};
template <
class ComparableType,
class = std::enable_if_t<std::is_base_of_v<ComparableBase, ComparableType>>>
bool DeepComparePointer(const std::shared_ptr<ComparableType>& lhs,
const std::shared_ptr<ComparableType>& rhs) {
if (lhs == rhs) {
return true;
}
if (lhs && rhs) {
return lhs->IsEqual(*rhs);
}
return false;
}
template <
class Key,
class ComparableType,
class = std::enable_if_t<std::is_base_of_v<ComparableBase, ComparableType>>>
bool DeepCompareMap(const std::map<Key, std::shared_ptr<ComparableType>>& lhs,
const std::map<Key, std::shared_ptr<ComparableType>>& rhs) {
if (lhs.size() != rhs.size()) {
return false;
}
for (auto i = lhs.begin(), j = rhs.begin(); i != lhs.end(); i++, j++) {
if (i->first != j->first) {
return false;
}
if (!DeepComparePointer(i->second, j->second)) {
return false;
}
}
return true;
}
} // namespace impeller
namespace std {
template <>
struct hash<impeller::UniqueID> {
constexpr std::size_t operator()(const impeller::UniqueID& id) {
return id.id;
}
};
template <>
struct less<impeller::UniqueID> {
constexpr bool operator()(const impeller::UniqueID& lhs,
const impeller::UniqueID& rhs) const {
return lhs.id < rhs.id;
}
};
} // namespace std