1 // Copyright 2016 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_CPP_BINDINGS_TESTS_STRUCT_WITH_TRAITS_IMPL_TRAITS_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_TESTS_STRUCT_WITH_TRAITS_IMPL_TRAITS_H_
7 
8 #include <stdint.h>
9 
10 #include <string>
11 #include <vector>
12 
13 #include "base/strings/string_piece.h"
14 #include "mojo/public/cpp/bindings/struct_traits.h"
15 #include "mojo/public/cpp/bindings/tests/struct_with_traits_impl.h"
16 #include "mojo/public/interfaces/bindings/tests/struct_with_traits.mojom.h"
17 
18 namespace mojo {
19 
20 template <>
21 struct StructTraits<test::NestedStructWithTraitsDataView,
22                     test::NestedStructWithTraitsImpl> {
23   static int32_t value(const test::NestedStructWithTraitsImpl& input);
24 
25   static bool Read(test::NestedStructWithTraitsDataView data,
26                    test::NestedStructWithTraitsImpl* output);
27 };
28 
29 template <>
30 struct EnumTraits<test::EnumWithTraits, test::EnumWithTraitsImpl> {
31   static test::EnumWithTraits ToMojom(test::EnumWithTraitsImpl input);
32   static bool FromMojom(test::EnumWithTraits input,
33                         test::EnumWithTraitsImpl* output);
34 };
35 
36 template <>
37 struct StructTraits<test::StructWithTraitsDataView,
38                     test::StructWithTraitsImpl> {
39   // Deserialization to test::StructTraitsImpl.
40   static bool Read(test::StructWithTraitsDataView data,
41                    test::StructWithTraitsImpl* out);
42 
43   // Fields in test::StructWithTraits.
44   // See src/mojo/public/interfaces/bindings/tests/struct_with_traits.mojom.
45   static test::EnumWithTraitsImpl f_enum(
46       const test::StructWithTraitsImpl& value) {
47     return value.get_enum();
48   }
49 
50   static bool f_bool(const test::StructWithTraitsImpl& value) {
51     return value.get_bool();
52   }
53 
54   static uint32_t f_uint32(const test::StructWithTraitsImpl& value) {
55     return value.get_uint32();
56   }
57 
58   static uint64_t f_uint64(const test::StructWithTraitsImpl& value) {
59     return value.get_uint64();
60   }
61 
62   static base::StringPiece f_string(const test::StructWithTraitsImpl& value) {
63     return value.get_string_as_string_piece();
64   }
65 
66   static const std::string& f_string2(const test::StructWithTraitsImpl& value) {
67     return value.get_string();
68   }
69 
70   static const std::vector<std::string>& f_string_array(
71       const test::StructWithTraitsImpl& value) {
72     return value.get_string_array();
73   }
74 
75   static const std::set<std::string>& f_string_set(
76       const test::StructWithTraitsImpl& value) {
77     return value.get_string_set();
78   }
79 
80   static const test::NestedStructWithTraitsImpl& f_struct(
81       const test::StructWithTraitsImpl& value) {
82     return value.get_struct();
83   }
84 
85   static const std::vector<test::NestedStructWithTraitsImpl>& f_struct_array(
86       const test::StructWithTraitsImpl& value) {
87     return value.get_struct_array();
88   }
89 
90   static const std::map<std::string, test::NestedStructWithTraitsImpl>&
91   f_struct_map(const test::StructWithTraitsImpl& value) {
92     return value.get_struct_map();
93   }
94 };
95 
96 template <>
97 struct StructTraits<test::StructWithUnreachableTraitsDataView,
98                     test::StructWithUnreachableTraitsImpl> {
99  public:
100   static bool ignore_me(const test::StructWithUnreachableTraitsImpl& input) {
101     NOTREACHED();
102     return false;
103   }
104 
105   static bool Read(test::StructWithUnreachableTraitsDataView data,
106                    test::StructWithUnreachableTraitsImpl* out) {
107     NOTREACHED();
108     return false;
109   }
110 };
111 
112 template <>
113 struct StructTraits<test::TrivialStructWithTraitsDataView,
114                     test::TrivialStructWithTraitsImpl> {
115   // Deserialization to test::TrivialStructTraitsImpl.
116   static bool Read(test::TrivialStructWithTraitsDataView data,
117                    test::TrivialStructWithTraitsImpl* out) {
118     out->value = data.value();
119     return true;
120   }
121 
122   // Fields in test::TrivialStructWithTraits.
123   // See src/mojo/public/interfaces/bindings/tests/struct_with_traits.mojom.
124   static int32_t value(test::TrivialStructWithTraitsImpl& input) {
125     return input.value;
126   }
127 };
128 
129 template <>
130 struct StructTraits<test::MoveOnlyStructWithTraitsDataView,
131                     test::MoveOnlyStructWithTraitsImpl> {
132   // Deserialization to test::MoveOnlyStructTraitsImpl.
133   static bool Read(test::MoveOnlyStructWithTraitsDataView data,
134                    test::MoveOnlyStructWithTraitsImpl* out);
135 
136   // Fields in test::MoveOnlyStructWithTraits.
137   // See src/mojo/public/interfaces/bindings/tests/struct_with_traits.mojom.
138   static ScopedHandle f_handle(test::MoveOnlyStructWithTraitsImpl& value) {
139     return std::move(value.get_mutable_handle());
140   }
141 };
142 
143 template <>
144 struct StructTraits<test::StructWithTraitsForUniquePtrDataView,
145                     std::unique_ptr<int>> {
146   static bool IsNull(const std::unique_ptr<int>& data) { return !data; }
147   static void SetToNull(std::unique_ptr<int>* data) { data->reset(); }
148 
149   static int f_int32(const std::unique_ptr<int>& data) { return *data; }
150 
151   static bool Read(test::StructWithTraitsForUniquePtrDataView data,
152                    std::unique_ptr<int>* out) {
153     out->reset(new int(data.f_int32()));
154     return true;
155   }
156 };
157 
158 template <>
159 struct UnionTraits<test::UnionWithTraitsDataView,
160                    std::unique_ptr<test::UnionWithTraitsBase>> {
161   static bool IsNull(const std::unique_ptr<test::UnionWithTraitsBase>& data) {
162     return !data;
163   }
164   static void SetToNull(std::unique_ptr<test::UnionWithTraitsBase>* data) {
165     data->reset();
166   }
167 
168   static test::UnionWithTraitsDataView::Tag GetTag(
169       const std::unique_ptr<test::UnionWithTraitsBase>& data) {
170     if (data->type() == test::UnionWithTraitsBase::Type::INT32)
171       return test::UnionWithTraitsDataView::Tag::F_INT32;
172 
173     return test::UnionWithTraitsDataView::Tag::F_STRUCT;
174   }
175 
176   static int32_t f_int32(
177       const std::unique_ptr<test::UnionWithTraitsBase>& data) {
178     return static_cast<test::UnionWithTraitsInt32*>(data.get())->value();
179   }
180 
181   static const test::NestedStructWithTraitsImpl& f_struct(
182       const std::unique_ptr<test::UnionWithTraitsBase>& data) {
183     return static_cast<test::UnionWithTraitsStruct*>(data.get())->get_struct();
184   }
185 
186   static bool Read(test::UnionWithTraitsDataView data,
187                    std::unique_ptr<test::UnionWithTraitsBase>* out) {
188     switch (data.tag()) {
189       case test::UnionWithTraitsDataView::Tag::F_INT32: {
190         out->reset(new test::UnionWithTraitsInt32(data.f_int32()));
191         return true;
192       }
193       case test::UnionWithTraitsDataView::Tag::F_STRUCT: {
194         auto* struct_object = new test::UnionWithTraitsStruct();
195         out->reset(struct_object);
196         return data.ReadFStruct(&struct_object->get_mutable_struct());
197       }
198     }
199 
200     NOTREACHED();
201     return false;
202   }
203 };
204 
205 template <>
206 struct StructTraits<test::StructForceSerializeDataView,
207                     test::StructForceSerializeImpl> {
208   static int32_t value(const test::StructForceSerializeImpl& impl) {
209     impl.set_was_serialized();
210     return impl.value();
211   }
212 
213   static bool Read(test::StructForceSerializeDataView data,
214                    test::StructForceSerializeImpl* out) {
215     out->set_value(data.value());
216     out->set_was_deserialized();
217     return true;
218   }
219 };
220 
221 template <>
222 struct StructTraits<test::StructNestedForceSerializeDataView,
223                     test::StructNestedForceSerializeImpl> {
224   static const test::StructForceSerializeImpl& force(
225       const test::StructNestedForceSerializeImpl& impl) {
226     impl.set_was_serialized();
227     return impl.force();
228   }
229 
230   static bool Read(test::StructNestedForceSerializeDataView data,
231                    test::StructNestedForceSerializeImpl* out) {
232     if (!data.ReadForce(&out->force()))
233       return false;
234     out->set_was_deserialized();
235     return true;
236   }
237 };
238 
239 }  // namespace mojo
240 
241 #endif  // MOJO_PUBLIC_CPP_BINDINGS_TESTS_STRUCT_WITH_TRAITS_IMPL_TRAITS_H_
242