• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_CARRAY_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_ARRAY_TRAITS_CARRAY_H_
7 
8 #include <cstddef>
9 
10 #include "base/containers/span.h"
11 #include "mojo/public/cpp/bindings/array_traits.h"
12 
13 namespace mojo {
14 
15 template <typename T>
16 struct ArrayTraits<base::span<T>> {
17   using Element = T;
18 
19   // There is no concept of a null span, as it is indistinguishable from the
20   // empty span.
21   static bool IsNull(const base::span<T>& input) { return false; }
22 
23   static size_t GetSize(const base::span<T>& input) { return input.size(); }
24 
25   static T* GetData(base::span<T>& input) { return input.data(); }
26 
27   static const T* GetData(const base::span<T>& input) { return input.data(); }
28 
29   static T& GetAt(base::span<T>& input, size_t index) {
30     return input.data()[index];
31   }
32 
33   static const T& GetAt(const base::span<T>& input, size_t index) {
34     return input.data()[index];
35   }
36 
37   static bool Resize(base::span<T>& input, size_t size) {
38     if (size > input.size())
39       return false;
40     input = input.subspan(0, size);
41     return true;
42   }
43 };
44 
45 }  // namespace mojo
46 
47 #endif  // MOJO_PUBLIC_CPP_BINDINGS_ARRAY_TRAITS_CARRAY_H_
48