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

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

Issue 2007813004: Mojo C++ bindings: support attaching context object for StructTraits. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 6 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
(...skipping 15 matching lines...) Expand all
26 // Value or reference of any type that has a StringTraits defined. 26 // Value or reference of any type that has a StringTraits defined.
27 // Supported by default: base::StringPiece, std::string. 27 // Supported by default: base::StringPiece, std::string.
28 // 28 //
29 // - array: 29 // - array:
30 // Value or reference of any type that has an ArrayTraits defined. 30 // Value or reference of any type that has an ArrayTraits defined.
31 // Supported by default: std::vector, WTF::Vector (in blink). 31 // Supported by default: std::vector, WTF::Vector (in blink).
32 // 32 //
33 // - struct: 33 // - struct:
34 // Value or reference of any type that has a StructTraits defined. 34 // Value or reference of any type that has a StructTraits defined.
35 // 35 //
36 // During serialization, getters for string/struct/array/map/union fields
37 // are called twice (one for size calculation and one for actual
38 // serialization); while the others are called once.
39 //
36 // 2. A static Read() method to set the contents of a |T| instance from a 40 // 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 41 // |MojomType|DataView (e.g., if |MojomType| is test::Example, the data
38 // view will be test::ExampleDataView). 42 // view will be test::ExampleDataView).
39 // 43 //
40 // static bool Read(|MojomType|DataView data, T* output); 44 // static bool Read(|MojomType|DataView data, T* output);
41 // 45 //
42 // The generated |MojomType|DataView type provides a convenient, 46 // The generated |MojomType|DataView type provides a convenient,
43 // inexpensive view of a serialized struct's field data. 47 // inexpensive view of a serialized struct's field data.
44 // 48 //
45 // Returning false indicates invalid incoming data and causes the message 49 // Returning false indicates invalid incoming data and causes the message
(...skipping 20 matching lines...) Expand all
66 // calls this method instead of Read(). 70 // calls this method instead of Read().
67 // 71 //
68 // NOTE: It is to set |*output|'s contents to a null state, not to set the 72 // 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 73 // |output| pointer itself to null. "Null state" means whatever state you
70 // think it makes sense to map a null serialized struct to. 74 // think it makes sense to map a null serialized struct to.
71 // 75 //
72 // If it is not defined, null is not allowed to be converted to |T|. In 76 // 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 77 // that case, an incoming null value is considered invalid and causes the
74 // message pipe to be disconnected. 78 // message pipe to be disconnected.
75 // 79 //
80 // 4. [Optional] As mentioned above, getters for string/struct/array/map/union
81 // fields are called multiple times (twice to be exact). If you need to do
82 // some expensive calculation/conversion, you probably want to cache the
83 // result across multiple calls. You can introduce an arbitrary context
84 // object by adding two optional methods:
85 // static void* SetUpContext(const T& input);
86 // static void TearDownContext(const T& input, void* context);
87 //
88 // And then you append a second parameter, void* context, to getters:
89 // static <return type> <field name>(const T& input, void* context);
90 //
91 // If a T instance is not null, the serialization code will call
92 // SetUpContext() at the beginning, and pass the resulting context pointer
93 // to getters. After serialization is done, it calls TearDownContext() so
94 // that you can do any necessary cleanup.
95 //
96 // In the description above, methods having an |input| parameter define it as
97 // const reference of T. Actually, it can be a non-const reference of T too.
98 // E.g., if T contains Mojo handles or interfaces whose ownership needs to be
99 // transferred. Correspondingly, it requies you to always give non-const T
100 // reference/value to the Mojo bindings for serialization:
101 // - if T is used in the "type_mappings" section of a typemap config file,
102 // you need to declare it as pass-by-value:
103 // type_mappings = [ "MojomType=T(pass_by_value)" ]
104 // - if another type U's StructTraits has a getter for T, it needs to return
105 // non-const reference/value.
106 //
76 // EXAMPLE: 107 // EXAMPLE:
77 // 108 //
78 // Mojom definition: 109 // Mojom definition:
79 // struct Bar {}; 110 // struct Bar {};
80 // struct Foo { 111 // struct Foo {
81 // int32 f_integer; 112 // int32 f_integer;
82 // string f_string; 113 // string f_string;
83 // array<string> f_string_array; 114 // array<string> f_string_array;
84 // Bar f_bar; 115 // Bar f_bar;
85 // }; 116 // };
(...skipping 15 matching lines...) Expand all
101 // 132 //
102 // static bool Read(FooDataView data, CustomFoo* output); 133 // static bool Read(FooDataView data, CustomFoo* output);
103 // }; 134 // };
104 // 135 //
105 template <typename MojomType, typename T> 136 template <typename MojomType, typename T>
106 struct StructTraits; 137 struct StructTraits;
107 138
108 } // namespace mojo 139 } // namespace mojo
109 140
110 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRUCT_TRAITS_H_ 141 #endif // MOJO_PUBLIC_CPP_BINDINGS_STRUCT_TRAITS_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/lib/serialization_util.h ('k') | mojo/public/cpp/bindings/tests/struct_with_traits_impl_traits.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698