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 #include "mojo/public/cpp/bindings/tests/struct_with_traits_impl_traits.h"
6
7 namespace mojo {
8
9 // static
10 int32_t StructTraits<test::NestedStructWithTraitsDataView,
11 test::NestedStructWithTraitsImpl>::
value(const test::NestedStructWithTraitsImpl & input)12 value(const test::NestedStructWithTraitsImpl& input) {
13 return input.value;
14 }
15
16 // static
17 bool StructTraits<test::NestedStructWithTraitsDataView,
18 test::NestedStructWithTraitsImpl>::
Read(test::NestedStructWithTraits::DataView data,test::NestedStructWithTraitsImpl * output)19 Read(test::NestedStructWithTraits::DataView data,
20 test::NestedStructWithTraitsImpl* output) {
21 output->value = data.value();
22 return true;
23 }
24
25 test::EnumWithTraits
ToMojom(test::EnumWithTraitsImpl input)26 EnumTraits<test::EnumWithTraits, test::EnumWithTraitsImpl>::ToMojom(
27 test::EnumWithTraitsImpl input) {
28 switch (input) {
29 case test::EnumWithTraitsImpl::CUSTOM_VALUE_0:
30 return test::EnumWithTraits::VALUE_0;
31 case test::EnumWithTraitsImpl::CUSTOM_VALUE_1:
32 return test::EnumWithTraits::VALUE_1;
33 };
34
35 NOTREACHED();
36 return test::EnumWithTraits::VALUE_0;
37 }
38
FromMojom(test::EnumWithTraits input,test::EnumWithTraitsImpl * output)39 bool EnumTraits<test::EnumWithTraits, test::EnumWithTraitsImpl>::FromMojom(
40 test::EnumWithTraits input,
41 test::EnumWithTraitsImpl* output) {
42 switch (input) {
43 case test::EnumWithTraits::VALUE_0:
44 *output = test::EnumWithTraitsImpl::CUSTOM_VALUE_0;
45 return true;
46 case test::EnumWithTraits::VALUE_1:
47 *output = test::EnumWithTraitsImpl::CUSTOM_VALUE_1;
48 return true;
49 };
50
51 return false;
52 }
53
54 // static
55 bool StructTraits<test::StructWithTraitsDataView, test::StructWithTraitsImpl>::
Read(test::StructWithTraits::DataView data,test::StructWithTraitsImpl * out)56 Read(test::StructWithTraits::DataView data,
57 test::StructWithTraitsImpl* out) {
58 test::EnumWithTraitsImpl f_enum;
59 if (!data.ReadFEnum(&f_enum))
60 return false;
61 out->set_enum(f_enum);
62
63 out->set_bool(data.f_bool());
64 out->set_uint32(data.f_uint32());
65 out->set_uint64(data.f_uint64());
66
67 base::StringPiece f_string;
68 std::string f_string2;
69 if (!data.ReadFString(&f_string) || !data.ReadFString2(&f_string2) ||
70 f_string != f_string2) {
71 return false;
72 }
73 out->set_string(f_string2);
74
75 if (!data.ReadFStringArray(&out->get_mutable_string_array()))
76 return false;
77
78 // We can't deserialize as a std::set, so we have to manually copy from the
79 // data view.
80 ArrayDataView<StringDataView> string_set_data_view;
81 data.GetFStringSetDataView(&string_set_data_view);
82 for (size_t i = 0; i < string_set_data_view.size(); ++i) {
83 std::string value;
84 string_set_data_view.Read(i, &value);
85 out->get_mutable_string_set().insert(value);
86 }
87
88 if (!data.ReadFStruct(&out->get_mutable_struct()))
89 return false;
90
91 if (!data.ReadFStructArray(&out->get_mutable_struct_array()))
92 return false;
93
94 if (!data.ReadFStructMap(&out->get_mutable_struct_map()))
95 return false;
96
97 return true;
98 }
99
100 // static
101 bool StructTraits<test::MoveOnlyStructWithTraitsDataView,
102 test::MoveOnlyStructWithTraitsImpl>::
Read(test::MoveOnlyStructWithTraits::DataView data,test::MoveOnlyStructWithTraitsImpl * out)103 Read(test::MoveOnlyStructWithTraits::DataView data,
104 test::MoveOnlyStructWithTraitsImpl* out) {
105 out->get_mutable_handle() = data.TakeFHandle();
106 return true;
107 }
108
109 } // namespace mojo
110