Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1777)

Unified Diff: mojo/public/cpp/bindings/enum_traits.h

Issue 1635613002: [mojo-bindings] Support reuse of native enum classes (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix more missing public_deps Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: mojo/public/cpp/bindings/enum_traits.h
diff --git a/mojo/public/cpp/bindings/enum_traits.h b/mojo/public/cpp/bindings/enum_traits.h
new file mode 100644
index 0000000000000000000000000000000000000000..8864d28f95920ba14610a8cb956934ddcea831df
--- /dev/null
+++ b/mojo/public/cpp/bindings/enum_traits.h
@@ -0,0 +1,56 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef MOJO_PUBLIC_CPP_BINDINGS_ENUM_TRAITS_H_
+#define MOJO_PUBLIC_CPP_BINDINGS_ENUM_TRAITS_H_
+
+namespace mojo {
+
+// Specialize this over any enum class |T| in order to use the enum with
+// Mojo bindings in place of a mojom enum alias.
+//
+// For example, if you have an enum class:
+//
+// enum Feels : int32_t {
+// HAPPY,
+// SAD,
+// OK_I_GUESS
+// };
+//
+// you can leverage this in Mojo interfaces by declaring in mojom
+//
+// [Native=True]
+// enum MojoFeels;
+//
+// and generating those bindings with a typemap applied, mapping MojoFeels to
+// the native Feels type.
+//
+// NOTE: If this seems a bit convoluted, that's because it is. It's only meant
+// to avoid maintaining duplicate enum definitions in cases where you for some
+// reason cannot move an enum definition into mojom.
+//
+// Once you have this setup in place, you may (but are not required to) define
+// a mojo::EnumTraits<Feels> specialization with a single static method:
+//
+// namespace mojo {
+//
+// template <>
+// class EnumTraits<Feels> {
+// static bool IsKnownValue(int32_t value) { /* ... */ }
+// };
+//
+// } // namespace mojo
+//
+// This is used to check deserialized values on incoming messages. If it returns
+// |false| and the mojom enum declaration does not have the attribute
+// IsExtensible=True set, the message will be rejected without dispatch.
+
+template <typename T>
+struct EnumTraits {
+ static bool IsKnownValue(int32_t value) { return true; }
+};
+
+} // namespace mojo
+
+#endif // MOJO_PUBLIC_CPP_BINDINGS_ENUM_TRAITS_H_

Powered by Google App Engine
This is Rietveld 408576698