OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_STRUCT_TRAITS_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_STRUCT_TRAITS_H_ |
6 #define MOJO_PUBLIC_CPP_BINDINGS_STRUCT_TRAITS_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_STRUCT_TRAITS_H_ |
7 | 7 |
8 namespace mojo { | 8 namespace mojo { |
9 | 9 |
10 // This must be specialized for any type |T| to be serialized/deserialized as | 10 // This must be specialized for any type |T| to be serialized/deserialized as |
11 // a mojom struct of type |MojomType|. | 11 // a mojom struct of type |MojomType|. |
12 // | 12 // |
13 // Each specialization must implement a few things: | 13 // Each specialization needs to implement a few things: |
14 // | |
15 // 1. Static getters for each field in the Mojom type. These should be | 14 // 1. Static getters for each field in the Mojom type. These should be |
16 // of the form: | 15 // of the form: |
17 // | 16 // |
18 // static <return type> <field name>(const T&) | 17 // static <return type> <field name>(const T& input); |
19 // | 18 // |
20 // and should return a serializable form of the named field as extracted | 19 // and should return a serializable form of the named field as extracted |
21 // from the referenced |T| instance. | 20 // from |input|. |
22 // | 21 // |
23 // 2. A static Read method to initialize a new |T| from a MojomType::Reader: | 22 // Serializable form of a field: |
| 23 // Value or reference of the same type used in |MojomType|, or the |
| 24 // following alternatives: |
| 25 // - string: |
| 26 // Value or reference of any type that has a StringTraits defined. |
| 27 // Supported by default: base::StringPiece, std::string. |
24 // | 28 // |
25 // static bool Read(MojomType::Reader r, T* out); | 29 // - array: |
| 30 // Value or reference of any type that has an ArrayTraits defined. |
| 31 // Supported by default: std::vector, WTF::Vector (in blink). |
26 // | 32 // |
27 // The generated MojomType::Reader type provides a convenient, inexpensive | 33 // - struct: |
28 // view of a serialized struct's field data. | 34 // Value or reference of any type that has a StructTraits defined. |
| 35 // |
| 36 // 2. A static Read() method to set the contents of a |T| instance from a |
| 37 // |MojomType|DataView (e.g., if |MojomType| is test::Example, the data |
| 38 // view will be test::ExampleDataView). |
| 39 // |
| 40 // static bool Read(|MojomType|DataView data, T* output); |
| 41 // |
| 42 // The generated |MojomType|DataView type provides a convenient, |
| 43 // inexpensive view of a serialized struct's field data. |
| 44 // |
| 45 // Returning false indicates invalid incoming data and causes the message |
| 46 // pipe receiving it to be disconnected. Therefore, you can do custom |
| 47 // validation for |T| in this method. |
| 48 // |
| 49 // 3. [Optional] A static IsNull() method indicating whether a given |T| |
| 50 // instance is null: |
| 51 // |
| 52 // static bool IsNull(const T& input); |
| 53 // |
| 54 // If this method returns true, it is guaranteed that none of the getters |
| 55 // (described in section 1) will be called for the same |input|. So you |
| 56 // don't have to check whether |input| is null in those getters. |
| 57 // |
| 58 // If it is not defined, |T| instances are always considered non-null. |
| 59 // |
| 60 // [Optional] A static SetToNull() method to set the contents of a given |
| 61 // |T| instance to null. |
| 62 // |
| 63 // static void SetToNull(T* output); |
| 64 // |
| 65 // When a null serialized struct is received, the deserialization code |
| 66 // calls this method instead of Read(). |
| 67 // |
| 68 // NOTE: It is to set |*output|'s contents to a null state, not to set the |
| 69 // |output| pointer itself to null. "Null state" means whatever state you |
| 70 // think it makes sense to map a null serialized struct to. |
| 71 // |
| 72 // If it is not defined, null is not allowed to be converted to |T|. In |
| 73 // that case, an incoming null value is considered invalid and causes the |
| 74 // message pipe to be disconnected. |
| 75 // |
| 76 // EXAMPLE: |
| 77 // |
| 78 // Mojom definition: |
| 79 // struct Bar {}; |
| 80 // struct Foo { |
| 81 // int32 f_integer; |
| 82 // string f_string; |
| 83 // array<string> f_string_array; |
| 84 // Bar f_bar; |
| 85 // }; |
| 86 // |
| 87 // StructTraits for Foo: |
| 88 // template <> |
| 89 // struct StructTraits<Foo, CustomFoo> { |
| 90 // // Optional methods dealing with null: |
| 91 // static bool IsNull(const CustomFoo& input); |
| 92 // static void SetToNull(CustomFoo* output); |
| 93 // |
| 94 // // Field getters: |
| 95 // static int32_t f_integer(const CustomFoo& input); |
| 96 // static const std::string& f_string(const CustomFoo& input); |
| 97 // static const std::vector<std::string>& f_string_array( |
| 98 // const CustomFoo& input); |
| 99 // // Assuming there is a StructTraits<Bar, CustomBar> defined. |
| 100 // static const CustomBar& f_bar(const CustomFoo& input); |
| 101 // |
| 102 // static bool Read(FooDataView data, CustomFoo* output); |
| 103 // }; |
29 // | 104 // |
30 template <typename MojomType, typename T> | 105 template <typename MojomType, typename T> |
31 struct StructTraits; | 106 struct StructTraits; |
32 | 107 |
33 } // namespace mojo | 108 } // namespace mojo |
34 | 109 |
35 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRUCT_TRAITS_H_ | 110 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRUCT_TRAITS_H_ |
OLD | NEW |