Skip to content

Commit 3c07f27

Browse files
author
Ivan Dlugos
committed
generated C-API binding - queries & static analysis issues
1 parent bbb5246 commit 3c07f27

File tree

5 files changed

+117
-141
lines changed

5 files changed

+117
-141
lines changed

lib/src/bindings/objectbox-c.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// ignore_for_file: non_constant_identifier_names
12
// AUTO GENERATED FILE, DO NOT EDIT.
23
//
34
// Generated by `package:ffigen`.

lib/src/bindings/structs.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class OBX_bytes_wrapper {
8181
}
8282

8383
class OBX_string_array_wrapper {
84-
Pointer<OBX_string_array> _cPtr;
84+
final Pointer<OBX_string_array> _cPtr;
8585

8686
OBX_string_array_wrapper(this._cPtr);
8787

lib/src/query/property.dart

Lines changed: 81 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,29 @@ abstract class PropertyQuery<T> {
4242
}
4343
}
4444

45-
Pointer<TStruct>
46-
_curryWithDefault<TStruct extends Struct, N extends NativeType>(
47-
obx_query_prop_find_native_t<Pointer<TStruct>, N> findFn,
48-
Pointer<N> cDefault,
49-
String errorMessage) {
45+
List<R> _find<R, StructT extends NativeType, ValT extends NativeType>(
46+
Pointer<StructT> Function(Pointer<OBX_query_prop>, Pointer<ValT>) findFn,
47+
Pointer<ValT> cDefault,
48+
List<R> Function(Pointer<StructT>) listReadFn,
49+
void Function(Pointer<StructT>) listFreeFn) {
50+
Pointer<StructT> cItems;
5051
try {
51-
return checkObxPtr(findFn(_cProp, cDefault), errorMessage);
52+
cItems = checkObxPtr(findFn(_cProp, cDefault), 'Property query failed');
5253
} finally {
5354
if (cDefault.address != 0) {
5455
free(cDefault);
5556
}
5657
}
58+
59+
try {
60+
return listReadFn(cItems);
61+
} finally {
62+
listFreeFn(cItems);
63+
}
5764
}
65+
66+
Pointer<ValT> _cDefault<ValT extends NativeType>(dynamic valueIfNull) =>
67+
valueIfNull == null ? nullptr : allocate<ValT>();
5868
}
5969

6070
/// shared implementation, hence mixin
@@ -74,7 +84,9 @@ class IntegerPropertyQuery extends PropertyQuery<int> with _CommonNumeric {
7484
IntegerPropertyQuery(Pointer<OBX_query> query, int propertyId, int obxType)
7585
: super(query, propertyId, obxType);
7686

77-
int _op(obx_query_prop_op_t<int, Int64> fn) {
87+
int _op(
88+
int Function(Pointer<OBX_query_prop>, Pointer<Int64>, Pointer<Int64>)
89+
fn) {
7890
final ptr = allocate<Int64>();
7991
try {
8092
checkObx(fn(_cProp, ptr, nullptr));
@@ -96,64 +108,47 @@ class IntegerPropertyQuery extends PropertyQuery<int> with _CommonNumeric {
96108
return _op(bindings.obx_query_prop_sum_int);
97109
}
98110

99-
List<int> _unpack8(Pointer<OBX_int8_array> ptr) {
100-
try {
101-
return ptr.ref.items();
102-
} finally {
103-
bindings.obx_int8_array_free(ptr);
104-
}
105-
}
106-
107-
List<int> _unpack16(Pointer<OBX_int16_array> ptr) {
108-
try {
109-
return ptr.ref.items();
110-
} finally {
111-
bindings.obx_int16_array_free(ptr);
112-
}
113-
}
114-
115-
List<int> _unpack32(Pointer<OBX_int32_array> ptr) {
116-
try {
117-
return ptr.ref.items();
118-
} finally {
119-
bindings.obx_int32_array_free(ptr);
120-
}
121-
}
122-
123-
List<int> _unpack64(Pointer<OBX_int64_array> ptr) {
124-
try {
125-
return ptr.ref.items();
126-
} finally {
127-
bindings.obx_int64_array_free(ptr);
128-
}
129-
}
130-
131111
@override
132112
List<int> find({int replaceNullWith}) {
133-
final ptr = replaceNullWith != null
134-
? (allocate<Int64>()..value = replaceNullWith)
135-
: Pointer<Int64>.fromAddress(0);
136113
switch (_type) {
137114
case OBXPropertyType.Bool:
138115
case OBXPropertyType.Byte:
139116
case OBXPropertyType.Char: // Int8
140-
return _unpack8(_curryWithDefault<OBX_int8_array, Int8>(
141-
bindings.obx_query_prop_find_int8s, ptr.cast<Int8>(), 'find int8'));
117+
final cDefault = _cDefault<Int8>(replaceNullWith);
118+
if (replaceNullWith != null) cDefault.value = replaceNullWith;
119+
return _find(
120+
bindings.obx_query_prop_find_int8s,
121+
cDefault,
122+
(Pointer<OBX_int8_array> cItems) =>
123+
cItems.ref.items.asTypedList(cItems.ref.count),
124+
bindings.obx_int8_array_free);
142125
case OBXPropertyType.Short: // Int16
143-
return _unpack16(_curryWithDefault<OBX_int16_array, Int16>(
126+
final cDefault = _cDefault<Int16>(replaceNullWith);
127+
if (replaceNullWith != null) cDefault.value = replaceNullWith;
128+
return _find(
144129
bindings.obx_query_prop_find_int16s,
145-
ptr.cast<Int16>(),
146-
'find int16'));
130+
cDefault,
131+
(Pointer<OBX_int16_array> cItems) =>
132+
cItems.ref.items.asTypedList(cItems.ref.count),
133+
bindings.obx_int16_array_free);
147134
case OBXPropertyType.Int: // Int32
148-
return _unpack32(_curryWithDefault<OBX_int32_array, Int32>(
135+
final cDefault = _cDefault<Int32>(replaceNullWith);
136+
if (replaceNullWith != null) cDefault.value = replaceNullWith;
137+
return _find(
149138
bindings.obx_query_prop_find_int32s,
150-
ptr.cast<Int32>(),
151-
'find int32'));
139+
cDefault,
140+
(Pointer<OBX_int32_array> cItems) =>
141+
cItems.ref.items.asTypedList(cItems.ref.count),
142+
bindings.obx_int32_array_free);
152143
case OBXPropertyType.Long: // Int64
153-
return _unpack64(_curryWithDefault<OBX_int64_array, Int64>(
144+
final cDefault = _cDefault<Int64>(replaceNullWith);
145+
if (replaceNullWith != null) cDefault.value = replaceNullWith;
146+
return _find(
154147
bindings.obx_query_prop_find_int64s,
155-
ptr.cast<Int64>(),
156-
'find int64'));
148+
cDefault,
149+
(Pointer<OBX_int64_array> cItems) =>
150+
cItems.ref.items.asTypedList(cItems.ref.count),
151+
bindings.obx_int64_array_free);
157152
default:
158153
throw Exception(
159154
'Property query: unsupported type (OBXPropertyType: ${_type})');
@@ -165,7 +160,9 @@ class DoublePropertyQuery extends PropertyQuery<double> with _CommonNumeric {
165160
DoublePropertyQuery(Pointer<OBX_query> query, int propertyId, int obxType)
166161
: super(query, propertyId, obxType);
167162

168-
double _op(obx_query_prop_op_t<int, Double> fn) {
163+
double _op(
164+
int Function(Pointer<OBX_query_prop>, Pointer<Double>, Pointer<Int64>)
165+
fn) {
169166
final ptr = allocate<Double>();
170167
try {
171168
checkObx(fn(_cProp, ptr, nullptr));
@@ -187,37 +184,27 @@ class DoublePropertyQuery extends PropertyQuery<double> with _CommonNumeric {
187184
return _op(bindings.obx_query_prop_sum);
188185
}
189186

190-
List<double> _unpack32(Pointer<OBX_float_array> ptr) {
191-
try {
192-
return ptr.ref.items();
193-
} finally {
194-
bindings.obx_float_array_free(ptr);
195-
}
196-
}
197-
198-
List<double> _unpack64(Pointer<OBX_double_array> ptr) {
199-
try {
200-
return ptr.ref.items();
201-
} finally {
202-
bindings.obx_double_array_free(ptr);
203-
}
204-
}
205-
206187
@override
207188
List<double> find({double replaceNullWith}) {
208189
switch (_type) {
209190
case OBXPropertyType.Float:
210-
final valueIfNull = replaceNullWith != null
211-
? (allocate<Float>()..value = replaceNullWith)
212-
: Pointer<Float>.fromAddress(0);
213-
return _unpack32(_curryWithDefault<OBX_float_array, Float>(
214-
bindings.obx_query_prop_find_floats, valueIfNull, 'find float32'));
191+
final cDefault = _cDefault<Float>(replaceNullWith);
192+
if (replaceNullWith != null) cDefault.value = replaceNullWith;
193+
return _find(
194+
bindings.obx_query_prop_find_floats,
195+
cDefault,
196+
(Pointer<OBX_float_array> cItems) =>
197+
cItems.ref.items.asTypedList(cItems.ref.count),
198+
bindings.obx_float_array_free);
215199
case OBXPropertyType.Double:
216-
final valueIfNull = replaceNullWith != null
217-
? (allocate<Double>()..value = replaceNullWith)
218-
: Pointer<Double>.fromAddress(0);
219-
return _unpack64(_curryWithDefault<OBX_double_array, Double>(
220-
bindings.obx_query_prop_find_doubles, valueIfNull, 'find float64'));
200+
final cDefault = _cDefault<Double>(replaceNullWith);
201+
if (replaceNullWith != null) cDefault.value = replaceNullWith;
202+
return _find(
203+
bindings.obx_query_prop_find_doubles,
204+
cDefault,
205+
(Pointer<OBX_double_array> cItems) =>
206+
cItems.ref.items.asTypedList(cItems.ref.count),
207+
bindings.obx_double_array_free);
221208
default:
222209
throw Exception(
223210
'Property query: unsupported type (OBXPropertyType: ${_type})');
@@ -251,12 +238,19 @@ class StringPropertyQuery extends PropertyQuery<String> {
251238

252239
@override
253240
List<String> find({String replaceNullWith}) {
254-
final ptr = replaceNullWith != null
255-
? Utf8.toUtf8(replaceNullWith).cast<Int8>()
256-
: Pointer<Int8>.fromAddress(0);
257-
final stringArray = OBX_string_array_wrapper(
258-
_curryWithDefault<OBX_string_array, Int8>(
259-
bindings.obx_query_prop_find_strings, ptr, 'find utf8'));
260-
return stringArray.items();
241+
final cDefault = replaceNullWith == null
242+
? nullptr
243+
: Utf8.toUtf8(replaceNullWith).cast<Int8>();
244+
245+
try {
246+
return _find(
247+
bindings.obx_query_prop_find_strings,
248+
cDefault,
249+
(Pointer<OBX_string_array> cItems) =>
250+
OBX_string_array_wrapper(cItems).items(),
251+
bindings.obx_string_array_free);
252+
} finally {
253+
if (cDefault != nullptr) free(cDefault);
254+
}
261255
}
262256
}

0 commit comments

Comments
 (0)