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 namespace { 9 10 struct Context { 11 int32_t value; 12 }; 13 14 } // namespace 15 16 // static 17 void* StructTraits<test::NestedStructWithTraits, 18 test::NestedStructWithTraitsImpl>:: 19 SetUpContext(const test::NestedStructWithTraitsImpl& input) { 20 Context* context = new Context; 21 context->value = input.value; 22 return context; 23 } 24 25 // static 26 void StructTraits<test::NestedStructWithTraits, 27 test::NestedStructWithTraitsImpl>:: 28 TearDownContext(const test::NestedStructWithTraitsImpl& input, 29 void* context) { 30 Context* context_obj = static_cast<Context*>(context); 31 CHECK_EQ(context_obj->value, input.value); 32 delete context_obj; 33 } 34 35 // static 36 int32_t StructTraits<test::NestedStructWithTraits, 37 test::NestedStructWithTraitsImpl>:: 38 value(const test::NestedStructWithTraitsImpl& input, void* context) { 39 Context* context_obj = static_cast<Context*>(context); 40 CHECK_EQ(context_obj->value, input.value); 41 return input.value; 42 } 43 44 // static 45 bool StructTraits<test::NestedStructWithTraits, 46 test::NestedStructWithTraitsImpl>:: 47 Read(test::NestedStructWithTraits::DataView data, 48 test::NestedStructWithTraitsImpl* output) { 49 output->value = data.value(); 50 return true; 51 } 52 53 test::EnumWithTraits 54 EnumTraits<test::EnumWithTraits, test::EnumWithTraitsImpl>::ToMojom( 55 test::EnumWithTraitsImpl input) { 56 switch (input) { 57 case test::EnumWithTraitsImpl::CUSTOM_VALUE_0: 58 return test::EnumWithTraits::VALUE_0; 59 case test::EnumWithTraitsImpl::CUSTOM_VALUE_1: 60 return test::EnumWithTraits::VALUE_1; 61 }; 62 63 NOTREACHED(); 64 return test::EnumWithTraits::VALUE_0; 65 } 66 67 bool EnumTraits<test::EnumWithTraits, test::EnumWithTraitsImpl>::FromMojom( 68 test::EnumWithTraits input, 69 test::EnumWithTraitsImpl* output) { 70 switch (input) { 71 case test::EnumWithTraits::VALUE_0: 72 *output = test::EnumWithTraitsImpl::CUSTOM_VALUE_0; 73 return true; 74 case test::EnumWithTraits::VALUE_1: 75 *output = test::EnumWithTraitsImpl::CUSTOM_VALUE_1; 76 return true; 77 }; 78 79 return false; 80 } 81 82 // static 83 bool StructTraits<test::StructWithTraits, test::StructWithTraitsImpl>::Read( 84 test::StructWithTraits::DataView data, 85 test::StructWithTraitsImpl* out) { 86 test::EnumWithTraitsImpl f_enum; 87 if (!data.ReadFEnum(&f_enum)) 88 return false; 89 out->set_enum(f_enum); 90 91 out->set_bool(data.f_bool()); 92 out->set_uint32(data.f_uint32()); 93 out->set_uint64(data.f_uint64()); 94 95 base::StringPiece f_string; 96 std::string f_string2; 97 if (!data.ReadFString(&f_string) || !data.ReadFString2(&f_string2) || 98 f_string != f_string2) { 99 return false; 100 } 101 out->set_string(f_string2); 102 103 if (!data.ReadFStringArray(&out->get_mutable_string_array())) 104 return false; 105 106 if (!data.ReadFStruct(&out->get_mutable_struct())) 107 return false; 108 109 if (!data.ReadFStructArray(&out->get_mutable_struct_array())) 110 return false; 111 112 if (!data.ReadFStructMap(&out->get_mutable_struct_map())) 113 return false; 114 115 return true; 116 } 117 118 // static 119 bool StructTraits<test::PassByValueStructWithTraits, 120 test::PassByValueStructWithTraitsImpl>:: 121 Read(test::PassByValueStructWithTraits::DataView data, 122 test::PassByValueStructWithTraitsImpl* out) { 123 out->get_mutable_handle() = data.TakeFHandle(); 124 return true; 125 } 126 127 } // namespace mojo 128