OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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_TYPE_CONVERTER_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_TYPE_CONVERTER_H_ |
6 #define MOJO_PUBLIC_CPP_BINDINGS_TYPE_CONVERTER_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_TYPE_CONVERTER_H_ |
7 | 7 |
| 8 #include <stdint.h> |
| 9 |
8 namespace mojo { | 10 namespace mojo { |
9 | 11 |
10 // Specialize the following class: | 12 // Specialize the following class: |
11 // template <typename T, typename U> struct TypeConverter; | 13 // template <typename T, typename U> struct TypeConverter; |
12 // to perform type conversion for Mojom-defined structs and arrays. Here, T is | 14 // to perform type conversion for Mojom-defined structs and arrays. Here, T is |
13 // the target type; U is the input type. | 15 // the target type; U is the input type. |
14 // | 16 // |
15 // Specializations should implement the following interfaces: | 17 // Specializations should implement the following interfaces: |
16 // namespace mojo { | 18 // namespace mojo { |
17 // template <> | 19 // template <> |
18 // struct TypeConverter<X, Y> { | 20 // struct TypeConverter<X, Y> { |
19 // static X Convert(const Y& input); | 21 // static X Convert(const Y& input); |
20 // }; | 22 // }; |
21 // template <> | 23 // template <> |
22 // struct TypeConverter<Y, X> { | 24 // struct TypeConverter<Y, X> { |
23 // static Y Convert(const X& input); | 25 // static Y Convert(const X& input); |
24 // }; | 26 // }; |
25 // } | 27 // } |
26 // | 28 // |
27 // EXAMPLE: | 29 // EXAMPLE: |
28 // | 30 // |
29 // Suppose you have the following Mojom-defined struct: | 31 // Suppose you have the following Mojom-defined struct: |
30 // | 32 // |
31 // module geometry { | 33 // module geometry { |
32 // struct Point { | 34 // struct Point { |
33 // int32 x; | 35 // int32_t x; |
34 // int32 y; | 36 // int32_t y; |
35 // }; | 37 // }; |
36 // } | 38 // } |
37 // | 39 // |
38 // Now, imagine you wanted to write a TypeConverter specialization for | 40 // Now, imagine you wanted to write a TypeConverter specialization for |
39 // gfx::Point. It might look like this: | 41 // gfx::Point. It might look like this: |
40 // | 42 // |
41 // namespace mojo { | 43 // namespace mojo { |
42 // template <> | 44 // template <> |
43 // struct TypeConverter<geometry::PointPtr, gfx::Point> { | 45 // struct TypeConverter<geometry::PointPtr, gfx::Point> { |
44 // static geometry::PointPtr Convert(const gfx::Point& input) { | 46 // static geometry::PointPtr Convert(const gfx::Point& input) { |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 // the input type, so you can write: | 85 // the input type, so you can write: |
84 // OutputType out = ConvertTo<OutputType>(input); | 86 // OutputType out = ConvertTo<OutputType>(input); |
85 template <typename T, typename U> | 87 template <typename T, typename U> |
86 inline T ConvertTo(const U& obj) { | 88 inline T ConvertTo(const U& obj) { |
87 return TypeConverter<T, U>::Convert(obj); | 89 return TypeConverter<T, U>::Convert(obj); |
88 }; | 90 }; |
89 | 91 |
90 } // namespace mojo | 92 } // namespace mojo |
91 | 93 |
92 #endif // MOJO_PUBLIC_CPP_BINDINGS_TYPE_CONVERTER_H_ | 94 #endif // MOJO_PUBLIC_CPP_BINDINGS_TYPE_CONVERTER_H_ |
OLD | NEW |