Flutter Engine
typed_list.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter 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 LIB_TONIC_TYPED_DATA_TYPED_LIST_H_
6 #define LIB_TONIC_TYPED_DATA_TYPED_LIST_H_
7 
8 #include "third_party/dart/runtime/include/dart_api.h"
10 
11 namespace tonic {
12 
13 // A simple wrapper around Dart TypedData objects. It uses
14 // Dart_TypedDataAcquireData to obtain a raw pointer to the data, which is
15 // released when this object is destroyed.
16 //
17 // This is designed to be used with DartConverter only.
18 template <Dart_TypedData_Type kTypeName, typename ElemType>
19 class TypedList {
20  public:
21  explicit TypedList(Dart_Handle list);
23  TypedList();
24  ~TypedList();
25 
26  ElemType& at(intptr_t i) {
27  TONIC_CHECK(0 <= i);
28  TONIC_CHECK(i < num_elements_);
29  return data_[i];
30  }
31  const ElemType& at(intptr_t i) const {
32  TONIC_CHECK(0 <= i);
33  TONIC_CHECK(i < num_elements_);
34  return data_[i];
35  }
36 
37  ElemType& operator[](intptr_t i) { return at(i); }
38  const ElemType& operator[](intptr_t i) const { return at(i); }
39 
40  const ElemType* data() const { return data_; }
41  intptr_t num_elements() const { return num_elements_; }
42  Dart_Handle dart_handle() const { return dart_handle_; }
43 
44  void Release();
45 
46  private:
47  ElemType* data_;
48  intptr_t num_elements_;
49  Dart_Handle dart_handle_;
50 };
51 
52 template <Dart_TypedData_Type kTypeName, typename ElemType>
53 struct DartConverter<TypedList<kTypeName, ElemType>> {
54  static void SetReturnValue(Dart_NativeArguments args,
56  static TypedList<kTypeName, ElemType> FromArguments(Dart_NativeArguments args,
57  int index,
58  Dart_Handle& exception);
59  static Dart_Handle ToDart(const ElemType* buffer, unsigned int length);
60 };
61 
62 #define TONIC_TYPED_DATA_FOREACH(F) \
63  F(Int8, int8_t) \
64  F(Uint8, uint8_t) \
65  F(Int16, int16_t) \
66  F(Uint16, uint16_t) \
67  F(Int32, int32_t) \
68  F(Uint32, uint32_t) \
69  F(Int64, int64_t) \
70  F(Uint64, uint64_t) \
71  F(Float32, float) \
72  F(Float64, double)
73 
74 #define TONIC_TYPED_DATA_DECLARE(name, type) \
75  using name##List = TypedList<Dart_TypedData_k##name, type>; \
76  extern template class TypedList<Dart_TypedData_k##name, type>; \
77  extern template struct DartConverter<name##List>;
78 
80 
81 #undef TONIC_TYPED_DATA_DECLARE
82 
83 } // namespace tonic
84 
85 #endif // LIB_TONIC_TYPED_DATA_TYPED_LIST_H_
Dart_Handle dart_handle() const
Definition: typed_list.h:42
G_BEGIN_DECLS FlValue * args
const ElemType & operator[](intptr_t i) const
Definition: typed_list.h:38
intptr_t num_elements() const
Definition: typed_list.h:41
ElemType & at(intptr_t i)
Definition: typed_list.h:26
const ElemType & at(intptr_t i) const
Definition: typed_list.h:31
const ElemType * data() const
Definition: typed_list.h:40
size_t length
#define TONIC_TYPED_DATA_FOREACH(F)
Definition: typed_list.h:62
#define TONIC_CHECK(condition)
Definition: macros.h:23
Dart_Handle ToDart(const T &object)
#define TONIC_TYPED_DATA_DECLARE(name, type)
Definition: typed_list.h:74
ElemType & operator[](intptr_t i)
Definition: typed_list.h:37