Home | History | Annotate | Download | only in bindings
      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_ARRAY_TRAITS_H_
      6 #define MOJO_PUBLIC_CPP_BINDINGS_ARRAY_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 array.
     14 //
     15 // Usually you would like to do a partial specialization for a container (e.g.
     16 // vector) template. Imagine you want to specialize it for Container<>, you need
     17 // to implement:
     18 //
     19 //   template <typename T>
     20 //   struct ArrayTraits<Container<T>> {
     21 //     using Element = T;
     22 //     // These two statements are optional. Use them if you'd like to serialize
     23 //     // a container that supports iterators but does not support O(1) random
     24 //     // access and so GetAt(...) would be expensive.
     25 //     // using Iterator = T::iterator;
     26 //     // using ConstIterator = T::const_iterator;
     27 //
     28 //     // These two methods are optional. Please see comments in struct_traits.h
     29 //     // Note that unlike with StructTraits, IsNull() is called *twice* during
     30 //     // serialization for ArrayTraits.
     31 //     static bool IsNull(const Container<T>& input);
     32 //     static void SetToNull(Container<T>* output);
     33 //
     34 //     static size_t GetSize(const Container<T>& input);
     35 //
     36 //     // These two methods are optional. They are used to access the
     37 //     // underlying storage of the array to speed up copy of POD types.
     38 //     static T* GetData(Container<T>& input);
     39 //     static const T* GetData(const Container<T>& input);
     40 //
     41 //     // The following six methods are optional if the GetAt(...) methods are
     42 //     // implemented. These methods specify how to read the elements of
     43 //     // Container in some sequential order specified by the iterator.
     44 //     //
     45 //     // Acquires an iterator positioned at the first element in the container.
     46 //     static ConstIterator GetBegin(const Container<T>& input);
     47 //     static Iterator GetBegin(Container<T>& input);
     48 //
     49 //     // Advances |iterator| to the next position within the container.
     50 //     static void AdvanceIterator(ConstIterator& iterator);
     51 //     static void AdvanceIterator(Iterator& iterator);
     52 //
     53 //     // Returns a reference to the value at the current position of
     54 //     // |iterator|. Optionally, the ConstIterator version of GetValue can
     55 //     // return by value instead of by reference if it makes sense for the
     56 //     // type.
     57 //     static const T& GetValue(ConstIterator& iterator);
     58 //     static T& GetValue(Iterator& iterator);
     59 //
     60 //     // These two methods are optional if the iterator methods are
     61 //     // implemented.
     62 //     static T& GetAt(Container<T>& input, size_t index);
     63 //     static const T& GetAt(const Container<T>& input, size_t index);
     64 //
     65 //     // Returning false results in deserialization failure and causes the
     66 //     // message pipe receiving it to be disconnected.
     67 //     static bool Resize(Container<T>& input, size_t size);
     68 //   };
     69 //
     70 template <typename T>
     71 struct ArrayTraits {
     72   static_assert(internal::AlwaysFalse<T>::value,
     73                 "Cannot find the mojo::ArrayTraits specialization. Did you "
     74                 "forget to include the corresponding header file?");
     75 };
     76 
     77 }  // namespace mojo
     78 
     79 #endif  // MOJO_PUBLIC_CPP_BINDINGS_ARRAY_TRAITS_H_
     80