OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium 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 MOJO_PUBLIC_CPP_BINDINGS_ENUM_TRAITS_H_ |
| 6 #define MOJO_PUBLIC_CPP_BINDINGS_ENUM_TRAITS_H_ |
| 7 |
| 8 namespace mojo { |
| 9 |
| 10 // Specialize this over any enum class |T| in order to use the enum with |
| 11 // Mojo bindings in place of a mojom enum alias. |
| 12 // |
| 13 // For example, if you have an enum class: |
| 14 // |
| 15 // enum Feels : int32_t { |
| 16 // HAPPY, |
| 17 // SAD, |
| 18 // OK_I_GUESS |
| 19 // }; |
| 20 // |
| 21 // you can leverage this in Mojo interfaces by declaring in mojom |
| 22 // |
| 23 // [Native=True] |
| 24 // enum MojoFeels; |
| 25 // |
| 26 // and generating those bindings with a typemap applied, mapping MojoFeels to |
| 27 // the native Feels type. |
| 28 // |
| 29 // NOTE: If this seems a bit convoluted, that's because it is. It's only meant |
| 30 // to avoid maintaining duplicate enum definitions in cases where you for some |
| 31 // reason cannot move an enum definition into mojom. |
| 32 // |
| 33 // Once you have this setup in place, you may (but are not required to) define |
| 34 // a mojo::EnumTraits<Feels> specialization with a single static method: |
| 35 // |
| 36 // namespace mojo { |
| 37 // |
| 38 // template <> |
| 39 // class EnumTraits<Feels> { |
| 40 // static bool IsKnownValue(int32_t value) { /* ... */ } |
| 41 // }; |
| 42 // |
| 43 // } // namespace mojo |
| 44 // |
| 45 // This is used to check deserialized values on incoming messages. If it returns |
| 46 // |false| and the mojom enum declaration does not have the attribute |
| 47 // IsExtensible=True set, the message will be rejected without dispatch. |
| 48 |
| 49 template <typename T> |
| 50 struct EnumTraits { |
| 51 static bool IsKnownValue(int32_t value) { return true; } |
| 52 }; |
| 53 |
| 54 } // namespace mojo |
| 55 |
| 56 #endif // MOJO_PUBLIC_CPP_BINDINGS_ENUM_TRAITS_H_ |
OLD | NEW |