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::NestedStructWithTraits, 22 test::NestedStructWithTraitsImpl> { 23 static void* SetUpContext(const test::NestedStructWithTraitsImpl& input); 24 static void TearDownContext(const test::NestedStructWithTraitsImpl& input, 25 void* context); 26 27 static int32_t value(const test::NestedStructWithTraitsImpl& input, 28 void* context); 29 30 static bool Read(test::NestedStructWithTraits::DataView data, 31 test::NestedStructWithTraitsImpl* output); 32 }; 33 34 template <> 35 struct EnumTraits<test::EnumWithTraits, test::EnumWithTraitsImpl> { 36 static test::EnumWithTraits ToMojom(test::EnumWithTraitsImpl input); 37 static bool FromMojom(test::EnumWithTraits input, 38 test::EnumWithTraitsImpl* output); 39 }; 40 41 template <> 42 struct StructTraits<test::StructWithTraits, test::StructWithTraitsImpl> { 43 // Deserialization to test::StructTraitsImpl. 44 static bool Read(test::StructWithTraits::DataView data, 45 test::StructWithTraitsImpl* out); 46 47 // Fields in test::StructWithTraits. 48 // See src/mojo/public/interfaces/bindings/tests/struct_with_traits.mojom. 49 static test::EnumWithTraitsImpl f_enum( 50 const test::StructWithTraitsImpl& value) { 51 return value.get_enum(); 52 } 53 54 static bool f_bool(const test::StructWithTraitsImpl& value) { 55 return value.get_bool(); 56 } 57 58 static uint32_t f_uint32(const test::StructWithTraitsImpl& value) { 59 return value.get_uint32(); 60 } 61 62 static uint64_t f_uint64(const test::StructWithTraitsImpl& value) { 63 return value.get_uint64(); 64 } 65 66 static base::StringPiece f_string(const test::StructWithTraitsImpl& value) { 67 return value.get_string_as_string_piece(); 68 } 69 70 static const std::string& f_string2(const test::StructWithTraitsImpl& value) { 71 return value.get_string(); 72 } 73 74 static const std::vector<std::string>& f_string_array( 75 const test::StructWithTraitsImpl& value) { 76 return value.get_string_array(); 77 } 78 79 static const test::NestedStructWithTraitsImpl& f_struct( 80 const test::StructWithTraitsImpl& value) { 81 return value.get_struct(); 82 } 83 84 static const std::vector<test::NestedStructWithTraitsImpl>& f_struct_array( 85 const test::StructWithTraitsImpl& value) { 86 return value.get_struct_array(); 87 } 88 89 static const std::map<std::string, test::NestedStructWithTraitsImpl>& 90 f_struct_map(const test::StructWithTraitsImpl& value) { 91 return value.get_struct_map(); 92 } 93 }; 94 95 template <> 96 struct StructTraits<test::PassByValueStructWithTraits, 97 test::PassByValueStructWithTraitsImpl> { 98 // Deserialization to test::PassByValueStructTraitsImpl. 99 static bool Read(test::PassByValueStructWithTraits::DataView data, 100 test::PassByValueStructWithTraitsImpl* out); 101 102 // Fields in test::PassByValueStructWithTraits. 103 // See src/mojo/public/interfaces/bindings/tests/struct_with_traits.mojom. 104 static ScopedHandle f_handle(test::PassByValueStructWithTraitsImpl& value) { 105 return std::move(value.get_mutable_handle()); 106 } 107 }; 108 109 template <> 110 struct StructTraits<test::StructWithTraitsForUniquePtrTest, 111 std::unique_ptr<int>> { 112 static int f_int32(const std::unique_ptr<int>& data) { return *data; } 113 114 static bool Read(test::StructWithTraitsForUniquePtrTest::DataView data, 115 std::unique_ptr<int>* out) { 116 out->reset(new int(data.f_int32())); 117 return true; 118 } 119 }; 120 121 } // namespace mojo 122 123 #endif // MOJO_PUBLIC_CPP_BINDINGS_TESTS_STRUCT_WITH_TRAITS_IMPL_TRAITS_H_ 124