Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(204)

Side by Side Diff: mojo/public/cpp/bindings/tests/struct_with_traits_impl_traits.h

Issue 2228733002: Mojo C++ bindings: add test cases for union custom typemapping. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@86_union_traits
Patch Set: Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 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_TESTS_STRUCT_WITH_TRAITS_IMPL_TRAITS_H_ 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_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_TESTS_STRUCT_WITH_TRAITS_IMPL_TRAITS_H_
7 7
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <string> 10 #include <string>
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 130
131 static int f_int32(const std::unique_ptr<int>& data) { return *data; } 131 static int f_int32(const std::unique_ptr<int>& data) { return *data; }
132 132
133 static bool Read(test::StructWithTraitsForUniquePtr::DataView data, 133 static bool Read(test::StructWithTraitsForUniquePtr::DataView data,
134 std::unique_ptr<int>* out) { 134 std::unique_ptr<int>* out) {
135 out->reset(new int(data.f_int32())); 135 out->reset(new int(data.f_int32()));
136 return true; 136 return true;
137 } 137 }
138 }; 138 };
139 139
140 template <>
141 struct UnionTraits<test::UnionWithTraits,
142 std::unique_ptr<test::UnionWithTraitsBase>> {
143 static bool IsNull(const std::unique_ptr<test::UnionWithTraitsBase>& data) {
144 return !data;
145 }
146 static void SetToNull(std::unique_ptr<test::UnionWithTraitsBase>* data) {
147 data->reset();
148 }
149
150 static test::UnionWithTraits::DataView::Tag GetTag(
151 const std::unique_ptr<test::UnionWithTraitsBase>& data) {
152 if (data->type() == test::UnionWithTraitsBase::Type::INT32)
153 return test::UnionWithTraits::DataView::Tag::F_INT32;
154
155 return test::UnionWithTraits::DataView::Tag::F_STRUCT;
156 }
157
158 static int32_t f_int32(
159 const std::unique_ptr<test::UnionWithTraitsBase>& data) {
160 return static_cast<test::UnionWithTraitsInt32*>(data.get())->value();
161 }
162
163 static const test::NestedStructWithTraitsImpl& f_struct(
164 const std::unique_ptr<test::UnionWithTraitsBase>& data) {
165 return static_cast<test::UnionWithTraitsStruct*>(data.get())->get_struct();
166 }
167
168 static bool Read(test::UnionWithTraits::DataView data,
169 std::unique_ptr<test::UnionWithTraitsBase>* out) {
170 switch (data.tag()) {
171 case test::UnionWithTraits::DataView::Tag::F_INT32: {
172 out->reset(new test::UnionWithTraitsInt32(data.f_int32()));
173 return true;
174 }
175 case test::UnionWithTraits::DataView::Tag::F_STRUCT: {
176 auto* struct_object = new test::UnionWithTraitsStruct();
177 out->reset(struct_object);
178 return data.ReadFStruct(&struct_object->get_mutable_struct());
179 }
180 }
181
182 NOTREACHED();
183 return false;
184 }
185 };
186
140 } // namespace mojo 187 } // namespace mojo
141 188
142 #endif // MOJO_PUBLIC_CPP_BINDINGS_TESTS_STRUCT_WITH_TRAITS_IMPL_TRAITS_H_ 189 #endif // MOJO_PUBLIC_CPP_BINDINGS_TESTS_STRUCT_WITH_TRAITS_IMPL_TRAITS_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698