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

Side by Side Diff: mojo/public/cpp/bindings/struct_traits.h

Issue 2253293002: Mojo C++ bindings: change the first template parameter of StructTraits and UnionTraits. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@91_extra
Patch Set: rebase Created 4 years, 3 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 unified diff | Download patch
OLDNEW
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. |DataViewType| is the corresponding data view type of the
12 // mojom struct. For example, if the mojom struct is example.Foo,
13 // |DataViewType| will be example::FooDataView, which can also be referred to by
14 // example::Foo::DataView (in chromium) and example::blink::Foo::DataView (in
15 // blink).
12 // 16 //
13 // Each specialization needs to implement a few things: 17 // Each specialization needs to implement a few things:
14 // 1. Static getters for each field in the Mojom type. These should be 18 // 1. Static getters for each field in the Mojom type. These should be
15 // of the form: 19 // of the form:
16 // 20 //
17 // static <return type> <field name>(const T& input); 21 // static <return type> <field name>(const T& input);
18 // 22 //
19 // and should return a serializable form of the named field as extracted 23 // and should return a serializable form of the named field as extracted
20 // from |input|. 24 // from |input|.
21 // 25 //
22 // Serializable form of a field: 26 // Serializable form of a field:
23 // Value or reference of the same type used in |MojomType|, or the 27 // Value or reference of the same type used in the generated stuct
24 // following alternatives: 28 // wrapper type, or the following alternatives:
25 // - string: 29 // - string:
26 // Value or reference of any type that has a StringTraits defined. 30 // Value or reference of any type that has a StringTraits defined.
27 // Supported by default: base::StringPiece, std::string. 31 // Supported by default: base::StringPiece, std::string, mojo::String,
32 // WTF::String (in blink).
28 // 33 //
29 // - array: 34 // - array:
30 // Value or reference of any type that has an ArrayTraits defined. 35 // Value or reference of any type that has an ArrayTraits defined.
31 // Supported by default: std::vector, WTF::Vector (in blink), CArray. 36 // Supported by default: std::vector, CArray, mojo::Array, WTF::Vector
37 // (in blink), mojo::WTFArray (in blink).
32 // 38 //
33 // - map: 39 // - map:
34 // Value or reference of any type that has a MapTraits defined. 40 // Value or reference of any type that has a MapTraits defined.
35 // Supported by default: std::map. 41 // Supported by default: std::map, std::unordered_map, mojo::Map,
42 // WTF::HashMap (in blink), mojo::WTFMap (in blink).
36 // 43 //
37 // - struct: 44 // - struct:
38 // Value or reference of any type that has a StructTraits defined. 45 // Value or reference of any type that has a StructTraits defined.
39 // 46 //
40 // - enum: 47 // - enum:
41 // Value of any type that has an EnumTraits defined. 48 // Value of any type that has an EnumTraits defined.
42 // 49 //
50 // For any nullable string/struct/array/map/union field you could also
51 // return value or reference of base::Optional<T>/WTF::Optional<T>, if T
52 // has the right *Traits defined.
53 //
43 // During serialization, getters for string/struct/array/map/union fields 54 // During serialization, getters for string/struct/array/map/union fields
44 // are called twice (one for size calculation and one for actual 55 // are called twice (one for size calculation and one for actual
45 // serialization). If you want to return a value (as opposed to a 56 // serialization). If you want to return a value (as opposed to a
46 // reference) from these getters, you have to be sure that constructing and 57 // reference) from these getters, you have to be sure that constructing and
47 // copying the returned object is really cheap. 58 // copying the returned object is really cheap.
48 // 59 //
49 // Getters for fields of other types are called once. 60 // Getters for fields of other types are called once.
50 // 61 //
51 // 2. A static Read() method to set the contents of a |T| instance from a 62 // 2. A static Read() method to set the contents of a |T| instance from a
52 // |MojomType|DataView (e.g., if |MojomType| is test::Example, the data 63 // DataViewType.
53 // view will be test::ExampleDataView).
54 // 64 //
55 // static bool Read(|MojomType|DataView data, T* output); 65 // static bool Read(DataViewType data, T* output);
56 // 66 //
57 // The generated |MojomType|DataView type provides a convenient, 67 // The generated DataViewType provides a convenient, inexpensive view of a
58 // inexpensive view of a serialized struct's field data. The caller 68 // serialized struct's field data. The caller guarantees that
59 // guarantees that |!data.is_null()|. 69 // |!data.is_null()|.
60 // 70 //
61 // Returning false indicates invalid incoming data and causes the message 71 // Returning false indicates invalid incoming data and causes the message
62 // pipe receiving it to be disconnected. Therefore, you can do custom 72 // pipe receiving it to be disconnected. Therefore, you can do custom
63 // validation for |T| in this method. 73 // validation for |T| in this method.
64 // 74 //
65 // 3. [Optional] A static IsNull() method indicating whether a given |T| 75 // 3. [Optional] A static IsNull() method indicating whether a given |T|
66 // instance is null: 76 // instance is null:
67 // 77 //
68 // static bool IsNull(const T& input); 78 // static bool IsNull(const T& input);
69 // 79 //
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 // to getters. After serialization is done, it calls TearDownContext() so 115 // to getters. After serialization is done, it calls TearDownContext() so
106 // that you can do any necessary cleanup. 116 // that you can do any necessary cleanup.
107 // 117 //
108 // In the description above, methods having an |input| parameter define it as 118 // In the description above, methods having an |input| parameter define it as
109 // const reference of T. Actually, it can be a non-const reference of T too. 119 // const reference of T. Actually, it can be a non-const reference of T too.
110 // E.g., if T contains Mojo handles or interfaces whose ownership needs to be 120 // E.g., if T contains Mojo handles or interfaces whose ownership needs to be
111 // transferred. Correspondingly, it requies you to always give non-const T 121 // transferred. Correspondingly, it requies you to always give non-const T
112 // reference/value to the Mojo bindings for serialization: 122 // reference/value to the Mojo bindings for serialization:
113 // - if T is used in the "type_mappings" section of a typemap config file, 123 // - if T is used in the "type_mappings" section of a typemap config file,
114 // you need to declare it as pass-by-value: 124 // you need to declare it as pass-by-value:
115 // type_mappings = [ "MojomType=T(pass_by_value)" ] 125 // type_mappings = [ "MojomType=T(move_only)" ]
116 // - if another type U's StructTraits has a getter for T, it needs to return 126 // or
117 // non-const reference/value. 127 // type_mappings = [ "MojomType=T(copyable_pass_by_value)" ]
128 //
129 // - if another type U's StructTraits/UnionTraits has a getter for T, it
130 // needs to return non-const reference/value.
118 // 131 //
119 // EXAMPLE: 132 // EXAMPLE:
120 // 133 //
121 // Mojom definition: 134 // Mojom definition:
122 // struct Bar {}; 135 // struct Bar {};
123 // struct Foo { 136 // struct Foo {
124 // int32 f_integer; 137 // int32 f_integer;
125 // string f_string; 138 // string f_string;
126 // array<string> f_string_array; 139 // array<string> f_string_array;
127 // Bar f_bar; 140 // Bar f_bar;
128 // }; 141 // };
129 // 142 //
130 // StructTraits for Foo: 143 // StructTraits for Foo:
131 // template <> 144 // template <>
132 // struct StructTraits<Foo, CustomFoo> { 145 // struct StructTraits<FooDataView, CustomFoo> {
133 // // Optional methods dealing with null: 146 // // Optional methods dealing with null:
134 // static bool IsNull(const CustomFoo& input); 147 // static bool IsNull(const CustomFoo& input);
135 // static void SetToNull(CustomFoo* output); 148 // static void SetToNull(CustomFoo* output);
136 // 149 //
137 // // Field getters: 150 // // Field getters:
138 // static int32_t f_integer(const CustomFoo& input); 151 // static int32_t f_integer(const CustomFoo& input);
139 // static const std::string& f_string(const CustomFoo& input); 152 // static const std::string& f_string(const CustomFoo& input);
140 // static const std::vector<std::string>& f_string_array( 153 // static const std::vector<std::string>& f_string_array(
141 // const CustomFoo& input); 154 // const CustomFoo& input);
142 // // Assuming there is a StructTraits<Bar, CustomBar> defined. 155 // // Assuming there is a StructTraits<Bar, CustomBar> defined.
143 // static const CustomBar& f_bar(const CustomFoo& input); 156 // static const CustomBar& f_bar(const CustomFoo& input);
144 // 157 //
145 // static bool Read(FooDataView data, CustomFoo* output); 158 // static bool Read(FooDataView data, CustomFoo* output);
146 // }; 159 // };
147 // 160 //
148 template <typename MojomType, typename T> 161 template <typename DataViewType, typename T>
149 struct StructTraits; 162 struct StructTraits;
150 163
151 } // namespace mojo 164 } // namespace mojo
152 165
153 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRUCT_TRAITS_H_ 166 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRUCT_TRAITS_H_
OLDNEW
« no previous file with comments | « mojo/common/common_custom_types_struct_traits.cc ('k') | mojo/public/cpp/bindings/tests/rect_blink_traits.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698