Skip to content

Commit f356a80

Browse files
authored
Define a concept for UniqueObjectTraits. (flutter#174905)
Earlier, it was just a comment. If you didn't do what the comment said, you'd get a mysterious compiler error when the template was instantiated at the point where the trait method was invoked. This would be extremely far away from where the template was instantiated and typically in `fml/unique_object.h`. Now, the exact reason and where a fix would go is printed in the compiler error. For instance, if I delete the Free method in `UniqueDirTraits`, I get (among other output): ``` no member named 'Free' in 'fml::internal::os_unix::UniqueDirTraits' ```
1 parent e25651b commit f356a80

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

engine/src/flutter/fml/unique_object.h

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,28 @@
55
#ifndef FLUTTER_FML_UNIQUE_OBJECT_H_
66
#define FLUTTER_FML_UNIQUE_OBJECT_H_
77

8+
#include <concepts>
89
#include <utility>
910

1011
#include "flutter/fml/logging.h"
1112
#include "flutter/fml/macros.h"
1213

1314
namespace fml {
1415

15-
// struct UniqueFooTraits {
16-
// // This function should be fast and inline.
17-
// static int InvalidValue() { return 0; }
18-
//
19-
// // This function should be fast and inline.
20-
// static bool IsValid(const T& value) { return value != InvalidValue(); }
21-
//
22-
// // This free function will not be called if f == InvalidValue()!
23-
// static void Free(int f) { ::FreeFoo(f); }
24-
// };
16+
template <typename T, typename Traits>
17+
concept UniqueObjectTraits = requires {
18+
// |InvalidValue| should be fast and inline.
19+
{ Traits::InvalidValue() } -> std::same_as<T>;
20+
21+
// |IsValid| function should be fast and inline.
22+
{ Traits::IsValid(std::declval<T>()) } -> std::same_as<bool>;
23+
24+
// |Free| function will not be called if value == InvalidValue()!
25+
{ Traits::Free(std::declval<T>()) };
26+
};
2527

2628
template <typename T, typename Traits>
29+
requires UniqueObjectTraits<T, Traits>
2730
class UniqueObject {
2831
private:
2932
// This must be first since it's used inline below.

engine/src/flutter/shell/platform/embedder/tests/embedder_config_builder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ struct UniqueEngineTraits {
1818

1919
static bool IsValid(const FlutterEngine& value) { return value != nullptr; }
2020

21-
static void Free(FlutterEngine& engine) {
21+
static void Free(FlutterEngine engine) {
2222
auto result = FlutterEngineShutdown(engine);
2323
FML_CHECK(result == kSuccess);
2424
}

0 commit comments

Comments
 (0)