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_LIB_SERIALIZATION_FORWARD_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_FORWARD_H_ 7 8 #include "base/optional.h" 9 #include "mojo/public/cpp/bindings/array_traits.h" 10 #include "mojo/public/cpp/bindings/enum_traits.h" 11 #include "mojo/public/cpp/bindings/lib/template_util.h" 12 #include "mojo/public/cpp/bindings/map_traits.h" 13 #include "mojo/public/cpp/bindings/string_traits.h" 14 #include "mojo/public/cpp/bindings/struct_traits.h" 15 #include "mojo/public/cpp/bindings/union_traits.h" 16 17 // This file is included by serialization implementation files to avoid circular 18 // includes. 19 // Users of the serialization funtions should include serialization.h (and also 20 // wtf_serialization.h if necessary). 21 22 namespace mojo { 23 namespace internal { 24 25 template <typename MojomType, typename MaybeConstUserType> 26 struct Serializer; 27 28 template <typename T> 29 struct IsOptionalWrapper { 30 static const bool value = IsSpecializationOf< 31 base::Optional, 32 typename std::remove_const< 33 typename std::remove_reference<T>::type>::type>::value; 34 }; 35 36 template <typename MojomType, 37 typename InputUserType, 38 typename... Args, 39 typename std::enable_if< 40 !IsOptionalWrapper<InputUserType>::value>::type* = nullptr> 41 void Serialize(InputUserType&& input, Args&&... args) { 42 Serializer<MojomType, typename std::remove_reference<InputUserType>::type>:: 43 Serialize(std::forward<InputUserType>(input), 44 std::forward<Args>(args)...); 45 } 46 47 template <typename MojomType, 48 typename DataType, 49 typename InputUserType, 50 typename... Args, 51 typename std::enable_if< 52 !IsOptionalWrapper<InputUserType>::value>::type* = nullptr> 53 bool Deserialize(DataType&& input, InputUserType* output, Args&&... args) { 54 return Serializer<MojomType, InputUserType>::Deserialize( 55 std::forward<DataType>(input), output, std::forward<Args>(args)...); 56 } 57 58 template <typename MojomType, 59 typename InputUserType, 60 typename BufferWriterType, 61 typename... Args, 62 typename std::enable_if< 63 IsOptionalWrapper<InputUserType>::value>::type* = nullptr> 64 void Serialize(InputUserType&& input, 65 Buffer* buffer, 66 BufferWriterType* writer, 67 Args&&... args) { 68 if (!input) 69 return; 70 Serialize<MojomType>(*input, buffer, writer, std::forward<Args>(args)...); 71 } 72 73 template <typename MojomType, 74 typename DataType, 75 typename InputUserType, 76 typename... Args, 77 typename std::enable_if< 78 IsOptionalWrapper<InputUserType>::value>::type* = nullptr> 79 bool Deserialize(DataType&& input, InputUserType* output, Args&&... args) { 80 if (!input) { 81 *output = base::nullopt; 82 return true; 83 } 84 if (!*output) 85 output->emplace(); 86 return Deserialize<MojomType>(std::forward<DataType>(input), &output->value(), 87 std::forward<Args>(args)...); 88 } 89 90 } // namespace internal 91 } // namespace mojo 92 93 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_FORWARD_H_ 94