OLD | NEW |
| (Empty) |
1 // Copyright 2014 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_BINDINGS_TYPE_CONVERTER_H_ | |
6 #define MOJO_PUBLIC_BINDINGS_TYPE_CONVERTER_H_ | |
7 | |
8 namespace mojo { | |
9 | |
10 // Specialize to perform type conversion for Mojom-defined structs and arrays. | |
11 // Here, T is the Mojom-defined struct or array, and U is some other non-Mojom | |
12 // struct or array type. | |
13 // | |
14 // EXAMPLE: | |
15 // | |
16 // Suppose you have the following Mojom-defined struct: | |
17 // | |
18 // module geometry { | |
19 // struct Point { | |
20 // int32 x; | |
21 // int32 y; | |
22 // }; | |
23 // } | |
24 // | |
25 // Now, imagine you wanted to write a TypeConverter specialization for | |
26 // gfx::Point. It might look like this: | |
27 // | |
28 // namespace mojo { | |
29 // template <> | |
30 // class TypeConverter<geometry::Point, gfx::Point> { | |
31 // public: | |
32 // static geometry::Point ConvertFrom(const gfx::Point& input, | |
33 // Buffer* buf) { | |
34 // geometry::Point::Builder builder(buf); | |
35 // builder.set_x(input.x()); | |
36 // builder.set_y(input.y()); | |
37 // return builder.Finish(); | |
38 // } | |
39 // static gfx::Point ConvertTo(const geometry::Point& input) { | |
40 // return gfx::Point(input.x(), input.y()); | |
41 // } | |
42 // }; | |
43 // } | |
44 // | |
45 // With the above TypeConverter defined, it is possible to write code like this: | |
46 // | |
47 // void SomeFunction(const gfx::Point& pt); | |
48 // | |
49 // void AcceptPoint(const geometry::Point& input) { | |
50 // // With an explicit cast using the .To<> method. | |
51 // gfx::Point pt = input.To<gfx::Point>(); | |
52 // | |
53 // // With an implicit copy conversion: | |
54 // SomeFunction(input); | |
55 // | |
56 // mojo::AllocationScope scope; | |
57 // // With an implicit copy conversion: | |
58 // geometry::Point output = pt; | |
59 // } | |
60 // | |
61 template <typename T, typename U> class TypeConverter { | |
62 // static T ConvertFrom(const U& input, Buffer* buf); | |
63 // static U ConvertTo(const T& input); | |
64 }; | |
65 | |
66 } // namespace mojo | |
67 | |
68 #endif // MOJO_PUBLIC_BINDINGS_TYPE_CONVERTER_H_ | |
OLD | NEW |