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_MAP_TRAITS_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_MAP_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 map.
14 //
15 // Usually you would like to do a partial specialization for a map template.
16 // Imagine you want to specialize it for CustomMap<>, you need to implement:
17 //
18 //   template <typename K, typename V>
19 //   struct MapTraits<CustomMap<K, V>> {
20 //     using Key = K;
21 //     using Value = V;
22 //
23 //     // These two methods are optional. Please see comments in struct_traits.h
24 //     // Note that unlike with StructTraits, IsNull() is called *twice* during
25 //     // serialization for MapTraits.
26 //     static bool IsNull(const CustomMap<K, V>& input);
27 //     static void SetToNull(CustomMap<K, V>* output);
28 //
29 //     static size_t GetSize(const CustomMap<K, V>& input);
30 //
31 //     static CustomConstIterator GetBegin(const CustomMap<K, V>& input);
32 //     static CustomIterator GetBegin(CustomMap<K, V>& input);
33 //
34 //     static void AdvanceIterator(CustomConstIterator& iterator);
35 //     static void AdvanceIterator(CustomIterator& iterator);
36 //
37 //     static const K& GetKey(CustomIterator& iterator);
38 //     static const K& GetKey(CustomConstIterator& iterator);
39 //
40 //     static V& GetValue(CustomIterator& iterator);
41 //     static const V& GetValue(CustomConstIterator& iterator);
42 //
43 //     // Returning false results in deserialization failure and causes the
44 //     // message pipe receiving it to be disconnected. |IK| and |IV| are
45 //     // separate input key/value template parameters that allows for the
46 //     // the key/value types to be forwarded.
47 //     template <typename IK, typename IV>
48 //     static bool Insert(CustomMap<K, V>& input,
49 //                        IK&& key,
50 //                        IV&& value);
51 //
52 //     static void SetToEmpty(CustomMap<K, V>* output);
53 //   };
54 //
55 template <typename T>
56 struct MapTraits {
57   static_assert(internal::AlwaysFalse<T>::value,
58                 "Cannot find the mojo::MapTraits specialization. Did you "
59                 "forget to include the corresponding header file?");
60 };
61 
62 }  // namespace mojo
63 
64 #endif  // MOJO_PUBLIC_CPP_BINDINGS_MAP_TRAITS_H_
65