@@ -2,80 +2,62 @@ import 'dart:ffi';
22import 'dart:typed_data' show Uint8List;
33import 'package:ffi/ffi.dart' show allocate, free, Utf8;
44import '../common.dart' ;
5+ import 'bindings.dart' ;
56
67// Disable some linter rules for this file
78// ignore_for_file: camel_case_types
89
9- // Note: IntPtr seems to be the the correct representation for size_t: "Represents a native pointer-sized integer in C."
10-
11- /// Represents the following C struct:
12- /// struct OBX_id_array {
13- /// obx_id* ids;
14- /// size_t count;
15- /// };
16- class OBX_id_array extends Struct {
17- Pointer <Uint64 > _itemsPtr;
18-
19- @IntPtr () // size_t
20- int length;
21-
22- /// Get a copy of the list
23- List <int > items () => _itemsPtr.asTypedList (length).toList ();
24-
25- /// Execute the given function, managing the resources consistently
26- static R executeWith <R >(
27- List <int > items, R Function (Pointer <OBX_id_array >) fn) {
28- // allocate a temporary structure
29- final ptr = allocate <OBX_id_array >();
30-
31- // fill it with data
32- final array = ptr.ref;
33- array.length = items.length;
34- array._itemsPtr = allocate <Uint64 >(count: array.length);
35- for (var i = 0 ; i < items.length; ++ i) {
36- array._itemsPtr[i] = items[i];
37- }
10+ /// Execute the given function, managing the resources consistently
11+ R executeWithIdArray <R >(List <int > items, R Function (Pointer <OBX_id_array >) fn) {
12+ // allocate a temporary structure
13+ final ptr = allocate <OBX_id_array >();
14+
15+ // fill it with data
16+ final array = ptr.ref;
17+ array.count = items.length;
18+ array.ids = allocate <Uint64 >(count: items.length);
19+ for (var i = 0 ; i < items.length; ++ i) {
20+ array.ids[i] = items[i];
21+ }
3822
39- // call the function with the structure and free afterwards
40- try {
41- return fn (ptr);
42- } finally {
43- free (array._itemsPtr);
44- free (ptr);
45- }
23+ // call the function with the structure and free afterwards
24+ try {
25+ return fn (ptr);
26+ } finally {
27+ free (array.ids);
28+ free (ptr);
4629 }
4730}
4831
49- /// Represents the following C struct:
50- /// struct OBX_bytes {
51- /// const void* data;
52- /// size_t size;
53- /// };
54- class OBX_bytes extends Struct {
55- Pointer <Uint8 > _dataPtr;
32+ class OBX_bytes_wrapper {
33+ Pointer <OBX_bytes > _cBytes;
34+
35+ OBX_bytes_wrapper (this ._cBytes);
5636
57- @IntPtr () // size_t
58- int length;
37+ int get size => _cBytes == nullptr ? 0 : _cBytes.ref.size;
38+
39+ Uint8List get data => safeDataAccess (_cBytes);
5940
6041 /// Get access to the data (no-copy)
61- Uint8List get data => isEmpty
62- ? throw ObjectBoxException (
63- dartMsg: "can't access data of empty OBX_bytes" )
64- : _dataPtr.asTypedList (length);
42+ static Uint8List safeDataAccess (Pointer <OBX_bytes > cBytes) =>
43+ cBytes.address == 0 || cBytes.ref.size == 0
44+ ? throw ObjectBoxException (
45+ dartMsg: "can't access data of empty OBX_bytes" )
46+ : cBytes.ref.data.cast <Uint8 >().asTypedList (cBytes.ref.size);
6547
66- bool get isEmpty => length == 0 || _dataPtr.address == 0 ;
48+ bool get isEmpty => size == 0 ;
6749
68- Pointer <Uint8 > get ptr => _dataPtr ;
50+ Pointer <Void > get ptr => _cBytes.ref.data ;
6951
7052 /// Returns a pointer to OBX_bytes with copy of the passed data.
7153 /// Warning: this creates two unmanaged pointers which must be freed manually: OBX_bytes.freeManaged(result).
72- static Pointer < OBX_bytes > managedCopyOf (Uint8List data) {
73- final ptr = allocate <OBX_bytes >();
74- final bytes = ptr .ref;
54+ OBX_bytes_wrapper . managedCopyOf (Uint8List data) {
55+ _cBytes = allocate <OBX_bytes >();
56+ final bytes = _cBytes .ref;
7557
76- const align =
77- true ; // ObjectBox requires data to be aligned to the length of 4
78- bytes.length = align ? ((data.length + 3.0 ) ~ / 4.0 ) * 4 : data.length;
58+ // ObjectBox requires data to be aligned to the length of 4
59+ const align = true ;
60+ bytes.size = align ? ((data.length + 3.0 ) ~ / 4.0 ) * 4 : data.length;
7961
8062 // NOTE: currently there's no way to get access to the underlying memory of Uint8List to avoid a copy.
8163 // See https://github.com/dart-lang/ffi/issues/27
@@ -85,107 +67,29 @@ class OBX_bytes extends Struct {
8567 // }
8668
8769 // create a copy of the data
88- bytes._dataPtr = allocate <Uint8 >(count: bytes.length );
70+ bytes.data = allocate <Uint8 >(count: bytes.size). cast < Void >( );
8971 for (var i = 0 ; i < data.length; ++ i) {
90- bytes._dataPtr [i] = data[i];
72+ bytes.data. cast < Uint8 >() [i] = data[i];
9173 }
92-
93- return ptr;
9474 }
9575
9676 /// Free a dart-created OBX_bytes pointer.
97- static void freeManaged (Pointer <OBX_bytes > ptr) {
98- free (ptr.ref._dataPtr);
99- free (ptr);
100- }
101- }
102-
103- /// Represents the following C struct:
104- /// struct OBX_bytes_array {
105- /// OBX_bytes* bytes;
106- /// size_t count;
107- /// };
108- class OBX_bytes_array extends Struct {
109- Pointer <OBX_bytes > _items;
110-
111- @IntPtr () // size_t
112- int length;
113-
114- /// Get a list of the underlying OBX_bytes (a shallow copy).
115- List <OBX_bytes > items () {
116- final result = < OBX_bytes > [];
117- for (var i = 0 ; i < length; i++ ) {
118- result.add (_items.elementAt (i).ref);
119- }
120- return result;
77+ void freeManaged () {
78+ free (_cBytes.ref.data);
79+ free (_cBytes);
12180 }
12281}
12382
124- class OBX_int8_array extends Struct {
125- Pointer <Int8 > _itemsPtr;
126-
127- @IntPtr () // size_t
128- int count;
129-
130- List <int > items () => _itemsPtr.asTypedList (count).toList ();
131- }
132-
133- class OBX_int16_array extends Struct {
134- Pointer <Int16 > _itemsPtr;
135-
136- @IntPtr () // size_t
137- int count;
138-
139- List <int > items () => _itemsPtr.asTypedList (count).toList ();
140- }
141-
142- class OBX_int32_array extends Struct {
143- Pointer <Int32 > _itemsPtr;
144-
145- @IntPtr () // size_t
146- int count;
147-
148- List <int > items () => _itemsPtr.asTypedList (count).toList ();
149- }
150-
151- class OBX_int64_array extends Struct {
152- Pointer <Int64 > _itemsPtr;
153-
154- @IntPtr () // size_t
155- int count;
156-
157- List <int > items () => _itemsPtr.asTypedList (count).toList ();
158- }
159-
160- class OBX_string_array extends Struct {
161- Pointer <Pointer <Uint8 >> _itemsPtr;
83+ class OBX_string_array_wrapper {
84+ Pointer <OBX_string_array > _cPtr;
16285
163- @IntPtr () // size_t
164- int count;
86+ OBX_string_array_wrapper (this ._cPtr);
16587
16688 List <String > items () {
16789 final list = < String > [];
168- for (var i = 0 ; i < count; i++ ) {
169- list.add (Utf8 .fromUtf8 (_itemsPtr .elementAt (i).value.cast <Utf8 >()));
90+ for (var i = 0 ; i < _cPtr.ref. count; i++ ) {
91+ list.add (Utf8 .fromUtf8 (_cPtr.ref.items .elementAt (i).value.cast <Utf8 >()));
17092 }
17193 return list;
17294 }
17395}
174-
175- class OBX_float_array extends Struct {
176- Pointer <Float > _itemsPtr;
177-
178- @IntPtr () // size_t
179- int count;
180-
181- List <double > items () => _itemsPtr.asTypedList (count).toList ();
182- }
183-
184- class OBX_double_array extends Struct {
185- Pointer <Double > _itemsPtr;
186-
187- @IntPtr () // size_t
188- int count;
189-
190- List <double > items () => _itemsPtr.asTypedList (count).toList ();
191- }
0 commit comments