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_UNION_TRAITS_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_UNION_TRAITS_H_ 7 8 #include "mojo/public/cpp/bindings/lib/template_util.h" 9 10 namespace mojo { 11 12 // This must be specialized for any type |T| to be serialized/deserialized as 13 // a mojom union. |DataViewType| is the corresponding data view type of the 14 // mojom union. For example, if the mojom union is example.Foo, |DataViewType| 15 // will be example::FooDataView, which can also be referred to by 16 // example::Foo::DataView (in chromium) and example::blink::Foo::DataView (in 17 // blink). 18 // 19 // Similar to StructTraits, each specialization of UnionTraits implements the 20 // following methods: 21 // 1. Getters for each field in the Mojom type. 22 // 2. Read() method. 23 // 3. [Optional] IsNull() and SetToNull(). 24 // 4. [Optional] SetUpContext() and TearDownContext(). 25 // Please see the documentation of StructTraits for details of these methods. 26 // 27 // Unlike StructTraits, there is one more method to implement: 28 // 5. A static GetTag() method indicating which field is the current active 29 // field for serialization: 30 // 31 // static DataViewType::Tag GetTag(const T& input); 32 // 33 // During serialization, only the field getter corresponding to this tag 34 // will be called. 35 // 36 template <typename DataViewType, typename T> 37 struct UnionTraits { 38 static_assert(internal::AlwaysFalse<T>::value, 39 "Cannot find the mojo::UnionTraits specialization. Did you " 40 "forget to include the corresponding header file?"); 41 }; 42 43 } // namespace mojo 44 45 #endif // MOJO_PUBLIC_CPP_BINDINGS_UNION_TRAITS_H_ 46