Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
typed_list.cc
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
6
7#include <cstring>
8
10
11namespace tonic {
12
13template <Dart_TypedData_Type kTypeName, typename ElemType>
15 : data_(nullptr), num_elements_(0), dart_handle_(nullptr) {}
16
17template <Dart_TypedData_Type kTypeName, typename ElemType>
19 : data_(nullptr), num_elements_(0), dart_handle_(list) {
20 if (Dart_IsNull(list))
21 return;
22
24 Dart_TypedDataAcquireData(list, &type, reinterpret_cast<void**>(&data_),
25 &num_elements_);
27 if (type != kTypeName)
28 Dart_ThrowException(ToDart("Non-genuine TypedData passed to engine."));
29}
30
31template <Dart_TypedData_Type kTypeName, typename ElemType>
34 : data_(other.data_),
35 num_elements_(other.num_elements_),
36 dart_handle_(other.dart_handle_) {
37 other.data_ = nullptr;
38 other.num_elements_ = 0;
39 other.dart_handle_ = nullptr;
40}
41
42template <Dart_TypedData_Type kTypeName, typename ElemType>
46
47template <Dart_TypedData_Type kTypeName, typename ElemType>
49 if (data_) {
50 Dart_TypedDataReleaseData(dart_handle_);
51 data_ = nullptr;
52 num_elements_ = 0;
53 dart_handle_ = nullptr;
54 }
55}
56
57template <Dart_TypedData_Type kTypeName, typename ElemType>
67
68template <Dart_TypedData_Type kTypeName, typename ElemType>
73 val.Release(); // Must release acquired typed data before calling Dart API.
75}
76
77template <Dart_TypedData_Type kTypeName, typename ElemType>
79 const ElemType* buffer,
80 unsigned int length) {
81 const intptr_t buffer_length = static_cast<intptr_t>(length);
82 Dart_Handle array = Dart_NewTypedData(kTypeName, buffer_length);
84 {
86 void* data = nullptr;
87 intptr_t data_length = 0;
88 Dart_TypedDataAcquireData(array, &type, &data, &data_length);
89 TONIC_CHECK(type == kTypeName);
90 TONIC_CHECK(data);
91 TONIC_CHECK(data_length == buffer_length);
92 std::memmove(data, buffer, data_length * sizeof(ElemType));
94 }
95 return array;
96}
97
98#define TONIC_TYPED_DATA_DEFINE(name, type) \
99 template class TypedList<Dart_TypedData_k##name, type>; \
100 template struct DartConverter<name##List>;
101
103
104#undef TONIC_TYPED_DATA_DEFINE
105
106} // namespace tonic
Dart_Handle dart_handle() const
Definition typed_list.h:42
struct _Dart_Handle * Dart_Handle
Definition dart_api.h:258
DART_EXPORT Dart_Handle Dart_TypedDataReleaseData(Dart_Handle object)
DART_EXPORT Dart_Handle Dart_GetNativeArgument(Dart_NativeArguments args, int index)
DART_EXPORT Dart_Handle Dart_TypedDataAcquireData(Dart_Handle object, Dart_TypedData_Type *type, void **data, intptr_t *len)
DART_EXPORT Dart_Handle Dart_ThrowException(Dart_Handle exception)
struct _Dart_NativeArguments * Dart_NativeArguments
Definition dart_api.h:3010
Dart_TypedData_Type
Definition dart_api.h:2603
DART_EXPORT Dart_Handle Dart_NewTypedData(Dart_TypedData_Type type, intptr_t length)
DART_EXPORT void Dart_SetReturnValue(Dart_NativeArguments args, Dart_Handle retval)
DART_EXPORT bool Dart_IsNull(Dart_Handle object)
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
static const uint8_t buffer[]
GAsyncResult * result
size_t length
Dart_Handle ToDart(const T &object)
bool CheckAndHandleError(Dart_Handle handle)
Definition dart_error.cc:33
#define TONIC_CHECK(condition)
Definition macros.h:23
#define TONIC_DCHECK
Definition macros.h:32
#define TONIC_TYPED_DATA_DEFINE(name, type)
Definition typed_list.cc:98
#define TONIC_TYPED_DATA_FOREACH(F)
Definition typed_list.h:77