Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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
11namespace 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.
18template <Dart_TypedData_Type kTypeName, typename ElemType>
19class 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
52template <Dart_TypedData_Type kTypeName, typename ElemType>
53struct DartConverter<TypedList<kTypeName, ElemType>> {
56 static constexpr const char* kFfiRepresentation = "Handle";
57 static constexpr const char* kDartRepresentation = "Object";
58 static constexpr bool kAllowedInLeafCall = false;
59
60 static void SetReturnValue(Dart_NativeArguments args, NativeType val);
61 static NativeType FromArguments(Dart_NativeArguments args,
62 int index,
63 Dart_Handle& exception);
64 static Dart_Handle ToDart(const ElemType* buffer, unsigned int length);
65
66 static NativeType FromFfi(FfiType val) { return NativeType(val); }
67 static FfiType ToFfi(NativeType val) {
68 Dart_Handle handle = val.dart_handle();
69 val.Release();
70 return handle;
71 }
72 static const char* GetFfiRepresentation() { return kFfiRepresentation; }
73 static const char* GetDartRepresentation() { return kDartRepresentation; }
74 static bool AllowedInLeafCall() { return kAllowedInLeafCall; }
75};
76
77#define TONIC_TYPED_DATA_FOREACH(F) \
78 F(Int8, int8_t) \
79 F(Uint8, uint8_t) \
80 F(Int16, int16_t) \
81 F(Uint16, uint16_t) \
82 F(Int32, int32_t) \
83 F(Uint32, uint32_t) \
84 F(Int64, int64_t) \
85 F(Uint64, uint64_t) \
86 F(Float32, float) \
87 F(Float64, double)
88
89#define TONIC_TYPED_DATA_DECLARE(name, type) \
90 using name##List = TypedList<Dart_TypedData_k##name, type>; \
91 extern template class TypedList<Dart_TypedData_k##name, type>; \
92 extern template struct DartConverter<name##List>;
93
95
96#undef TONIC_TYPED_DATA_DECLARE
97
98} // namespace tonic
99
100#endif // LIB_TONIC_TYPED_DATA_TYPED_LIST_H_
const ElemType * data() const
Definition typed_list.h:40
Dart_Handle dart_handle() const
Definition typed_list.h:42
intptr_t num_elements() const
Definition typed_list.h:41
ElemType & at(intptr_t i)
Definition typed_list.h:26
const ElemType & operator[](intptr_t i) const
Definition typed_list.h:38
ElemType & operator[](intptr_t i)
Definition typed_list.h:37
const ElemType & at(intptr_t i) const
Definition typed_list.h:31
struct _Dart_Handle * Dart_Handle
Definition dart_api.h:258
struct _Dart_NativeArguments * Dart_NativeArguments
Definition dart_api.h:3010
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
static const uint8_t buffer[]
size_t length
Dart_Handle ToDart(const T &object)
#define TONIC_CHECK(condition)
Definition macros.h:23
#define TONIC_TYPED_DATA_DECLARE(name, type)
Definition typed_list.h:89
#define TONIC_TYPED_DATA_FOREACH(F)
Definition typed_list.h:77