Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(445)

Side by Side Diff: mojo/public/cpp/bindings/array_traits_carray.h

Issue 2379993003: Mojo C++ bindings: make String16 and gfx::Size available in Blink (Closed)
Patch Set: Using ConstCArray to avoid copying Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_ARRAY_TRAITS_CARRAY_H_ 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_ARRAY_TRAITS_CARRAY_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_ARRAY_TRAITS_CARRAY_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_ARRAY_TRAITS_CARRAY_H_
7 7
8 #include "mojo/public/cpp/bindings/array_traits.h" 8 #include "mojo/public/cpp/bindings/array_traits.h"
9 9
10 namespace mojo { 10 namespace mojo {
11 11
12 template <typename T> 12 template <typename T>
13 struct CArray { 13 struct CArray {
14 CArray() : size(0), max_size(0), data(nullptr) {} 14 CArray() : size(0), max_size(0), data(nullptr) {}
15 CArray(size_t size, size_t max_size, T* data) 15 CArray(size_t size, size_t max_size, T* data)
16 : size(size), max_size(max_size), data(data) {} 16 : size(size), max_size(max_size), data(data) {}
17 size_t size; 17 size_t size;
18 const size_t max_size; 18 const size_t max_size;
19 T* data; 19 T* data;
20 }; 20 };
21 21
22 template <typename T> 22 template <typename T>
23 struct ConstCArray {
24 ConstCArray() : size(0), data(nullptr) {}
25 ConstCArray(size_t size, const T* data)
26 : size(size), data(data) {}
27 size_t size;
28 const T* data;
29 };
30
31 template <typename T>
23 struct ArrayTraits<CArray<T>> { 32 struct ArrayTraits<CArray<T>> {
24 using Element = T; 33 using Element = T;
25 34
26 static bool IsNull(const CArray<T>& input) { return !input.data; } 35 static bool IsNull(const CArray<T>& input) { return !input.data; }
27 36
28 static void SetToNull(CArray<T>* output) { output->data = nullptr; } 37 static void SetToNull(CArray<T>* output) { output->data = nullptr; }
29 38
30 static size_t GetSize(const CArray<T>& input) { return input.size; } 39 static size_t GetSize(const CArray<T>& input) { return input.size; }
31 40
32 static T* GetData(CArray<T>& input) { return input.data; } 41 static T* GetData(CArray<T>& input) { return input.data; }
33 42
34 static const T* GetData(const CArray<T>& input) { return input.data; } 43 static const T* GetData(const CArray<T>& input) { return input.data; }
35 44
36 static T& GetAt(CArray<T>& input, size_t index) { return input.data[index]; } 45 static T& GetAt(CArray<T>& input, size_t index) { return input.data[index]; }
37 46
38 static const T& GetAt(const CArray<T>& input, size_t index) { 47 static const T& GetAt(const CArray<T>& input, size_t index) {
39 return input.data[index]; 48 return input.data[index];
40 } 49 }
41 50
42 static bool Resize(CArray<T>& input, size_t size) { 51 static bool Resize(CArray<T>& input, size_t size) {
43 if (size > input.max_size) 52 if (size > input.max_size)
44 return false; 53 return false;
45 54
46 input.size = size; 55 input.size = size;
47 return true; 56 return true;
48 } 57 }
49 }; 58 };
50 59
60 template <typename T>
61 struct ArrayTraits<ConstCArray<T>> {
62 using Element = T;
63
64 static bool IsNull(const ConstCArray<T>& input) { return !input.data; }
65
66 static size_t GetSize(const ConstCArray<T>& input) { return input.size; }
67
68 static const T* GetData(const ConstCArray<T>& input) { return input.data; }
69
70 static const T& GetAt(const ConstCArray<T>& input, size_t index) {
71 return input.data[index];
72 }
73 };
74
51 } // namespace mojo 75 } // namespace mojo
52 76
53 #endif // MOJO_PUBLIC_CPP_BINDINGS_ARRAY_TRAITS_CARRAY_H_ 77 #endif // MOJO_PUBLIC_CPP_BINDINGS_ARRAY_TRAITS_CARRAY_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698