/
object_cs.cpp
440 lines (359 loc) · 17 KB
/
object_cs.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2016 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////
#include <realm.hpp>
#include "error_handling.hpp"
#include "marshalling.hpp"
#include "realm_export_decls.hpp"
#include "object_accessor.hpp"
#include "timestamp_helpers.hpp"
#include "object_cs.hpp"
#include "object-store/src/thread_safe_reference.hpp"
#include "notifications_cs.hpp"
using namespace realm;
using namespace realm::binding;
extern "C" {
REALM_EXPORT bool object_get_is_valid(const Object& object, NativeException::Marshallable& ex)
{
return handle_errors(ex, [&]() {
return object.is_valid();
});
}
REALM_EXPORT void object_destroy(Object* object)
{
delete object;
}
REALM_EXPORT size_t object_get_row_index(const Object& object, NativeException::Marshallable& ex)
{
return handle_errors(ex, [&]() {
if (!object.is_valid())
throw RowDetachedException();
return object.row().get_index();
});
}
REALM_EXPORT Object* object_get_link(const Object& object, size_t property_ndx, NativeException::Marshallable& ex)
{
return handle_errors(ex, [&]() -> Object* {
verify_can_get(object);
const size_t column_ndx = get_column_index(object, property_ndx);
const size_t link_row_ndx = object.row().get_link(column_ndx);
if (link_row_ndx == realm::npos)
return nullptr;
auto target_table_ptr = object.row().get_table()->get_link_target(column_ndx);
const std::string target_name(ObjectStore::object_type_for_table_name(target_table_ptr->get_name()));
auto& target_schema = *object.realm()->schema().find(target_name);
return new Object(object.realm(), target_schema, Row((*target_table_ptr)[link_row_ndx]));
});
}
REALM_EXPORT List* object_get_list(const Object& object, size_t property_ndx, NativeException::Marshallable& ex)
{
return handle_errors(ex, [&]() -> List* {
verify_can_get(object);
const size_t column_ndx = get_column_index(object, property_ndx);
return new List(object.realm(), *object.row().get_table(), column_ndx, object.row().get_index());
});
}
REALM_EXPORT size_t object_get_bool(const Object& object, size_t property_ndx, NativeException::Marshallable& ex)
{
return handle_errors(ex, [&]() {
verify_can_get(object);
const size_t column_ndx = get_column_index(object, property_ndx);
return bool_to_size_t(object.row().get_bool(column_ndx));
});
}
// Return value is a boolean indicating whether result has a value (i.e. is not null). If true (1), ret_value will contain the actual value.
REALM_EXPORT size_t object_get_nullable_bool(const Object& object, size_t property_ndx, size_t& ret_value, NativeException::Marshallable& ex)
{
return handle_errors(ex, [&]() {
verify_can_get(object);
const size_t column_ndx = get_column_index(object, property_ndx);
if (object.row().is_null(column_ndx))
return 0;
ret_value = bool_to_size_t(object.row().get_bool(column_ndx));
return 1;
});
}
REALM_EXPORT int64_t object_get_int64(const Object& object, size_t property_ndx, NativeException::Marshallable& ex)
{
return handle_errors(ex, [&]() {
verify_can_get(object);
const size_t column_ndx = get_column_index(object, property_ndx);
return object.row().get_int(column_ndx);
});
}
REALM_EXPORT size_t object_get_nullable_int64(const Object& object, size_t property_ndx, int64_t& ret_value, NativeException::Marshallable& ex)
{
return handle_errors(ex, [&]() {
verify_can_get(object);
const size_t column_ndx = get_column_index(object, property_ndx);
if (object.row().is_null(column_ndx))
return 0;
ret_value = object.row().get_int(column_ndx);
return 1;
});
}
REALM_EXPORT float object_get_float(const Object& object, size_t property_ndx, NativeException::Marshallable& ex)
{
return handle_errors(ex, [&]() {
verify_can_get(object);
const size_t column_ndx = get_column_index(object, property_ndx);
return object.row().get_float(column_ndx);
});
}
REALM_EXPORT size_t object_get_nullable_float(const Object& object, size_t property_ndx, float& ret_value, NativeException::Marshallable& ex)
{
return handle_errors(ex, [&]() {
verify_can_get(object);
const size_t column_ndx = get_column_index(object, property_ndx);
if (object.row().is_null(column_ndx))
return 0;
ret_value = object.row().get_float(column_ndx);
return 1;
});
}
REALM_EXPORT double object_get_double(const Object& object, size_t property_ndx, NativeException::Marshallable& ex)
{
return handle_errors(ex, [&]() {
verify_can_get(object);
const size_t column_ndx = get_column_index(object, property_ndx);
return object.row().get_double(column_ndx);
});
}
REALM_EXPORT size_t object_get_nullable_double(const Object& object, size_t property_ndx, double& ret_value, NativeException::Marshallable& ex)
{
return handle_errors(ex, [&]() {
verify_can_get(object);
const size_t column_ndx = get_column_index(object, property_ndx);
if (object.row().is_null(column_ndx))
return 0;
ret_value = object.row().get_double(column_ndx);
return 1;
});
}
REALM_EXPORT size_t object_get_string(const Object& object, size_t property_ndx, uint16_t * datatochsarp, size_t bufsize, bool* is_null, NativeException::Marshallable& ex)
{
return handle_errors(ex, [&]() -> size_t {
verify_can_get(object);
const size_t column_ndx = get_column_index(object, property_ndx);
const StringData fielddata(object.row().get_string(column_ndx));
if ((*is_null = fielddata.is_null()))
return 0;
return stringdata_to_csharpstringbuffer(fielddata, datatochsarp, bufsize);
});
}
REALM_EXPORT size_t object_get_binary(const Object& object, size_t property_ndx, char* return_buffer, size_t buffer_size, bool* is_null, NativeException::Marshallable& ex)
{
return handle_errors(ex, [&]() {
verify_can_get(object);
const size_t column_ndx = get_column_index(object, property_ndx);
const BinaryData fielddata = object.row().get_binary(column_ndx);
if ((*is_null = fielddata.is_null()))
return (size_t)0;
const size_t data_size = fielddata.size();
if (data_size <= buffer_size)
std::copy(fielddata.data(), fielddata.data() + data_size, return_buffer);
return data_size;
});
}
REALM_EXPORT int64_t object_get_timestamp_ticks(const Object& object, size_t property_ndx, NativeException::Marshallable& ex)
{
return handle_errors(ex, [&]() {
verify_can_get(object);
const size_t column_ndx = get_column_index(object, property_ndx);
return to_ticks(object.row().get_timestamp(column_ndx));
});
}
REALM_EXPORT size_t object_get_nullable_timestamp_ticks(const Object& object, size_t property_ndx, int64_t& ret_value, NativeException::Marshallable& ex)
{
return handle_errors(ex, [&]() {
verify_can_get(object);
const size_t column_ndx = get_column_index(object, property_ndx);
if (object.row().is_null(column_ndx))
return 0;
ret_value = to_ticks(object.row().get_timestamp(column_ndx));
return 1;
});
}
REALM_EXPORT Results* object_get_backlinks(Object& object, size_t property_ndx, NativeException::Marshallable& ex)
{
return handle_errors(ex, [&] {
verify_can_get(object);
const Property& prop = object.get_object_schema().computed_properties[property_ndx];
REALM_ASSERT_DEBUG(prop.type == PropertyType::LinkingObjects);
const ObjectSchema& relationship = *object.realm()->schema().find(prop.object_type);
const TableRef table = ObjectStore::table_for_object_type(object.realm()->read_group(), relationship.name);
const Property& link = *relationship.property_for_name(prop.link_origin_property_name);
TableView backlink_view = object.row().get_table()->get_backlink_view(object.row().get_index(), table.get(), link.table_column);
return new Results(object.realm(), backlink_view);
});
}
REALM_EXPORT Results* object_get_backlinks_for_type(Object& object, uint16_t* type_buf, size_t type_len, uint16_t* property_buf, size_t property_len, NativeException::Marshallable& ex)
{
return handle_errors(ex, [&] {
verify_can_get(object);
Utf16StringAccessor type(type_buf, type_len);
Utf16StringAccessor property(property_buf, property_len);
// type and property are validated by C#
auto target_object_schema = object.realm()->schema().find(type);
auto link_property = target_object_schema->property_for_name(property);
if (link_property->object_type != object.get_object_schema().name) {
throw std::logic_error(util::format("'%1.%2' is not a relationship to '%3'", type.to_string(), property.to_string(), object.get_object_schema().name));
}
auto table = ObjectStore::table_for_object_type(object.realm()->read_group(), target_object_schema->name);
auto tv = object.row().get_table()->get_backlink_view(object.row().get_index(), table.get(), link_property->table_column);
return new Results(object.realm(), std::move(tv));
});
}
REALM_EXPORT void object_set_link(Object& object, size_t property_ndx, const Object& target_object, NativeException::Marshallable& ex)
{
return handle_errors(ex, [&]() {
verify_can_set(object);
const size_t column_ndx = get_column_index(object, property_ndx);
object.row().set_link(column_ndx, target_object.row().get_index());
});
}
REALM_EXPORT void object_clear_link(Object& object, size_t property_ndx, NativeException::Marshallable& ex)
{
return handle_errors(ex, [&]() {
verify_can_set(object);
const size_t column_ndx = get_column_index(object, property_ndx);
object.row().nullify_link(column_ndx);
});
}
REALM_EXPORT void object_set_null(Object& object, size_t property_ndx, NativeException::Marshallable& ex)
{
return handle_errors(ex, [&]() {
verify_can_set(object);
const size_t column_ndx = get_column_index(object, property_ndx);
if (!object.row().get_table()->is_nullable(column_ndx))
throw std::invalid_argument("Column is not nullable");
object.row().set_null(column_ndx);
});
}
REALM_EXPORT void object_set_bool(Object& object, size_t property_ndx, size_t value, NativeException::Marshallable& ex)
{
return handle_errors(ex, [&]() {
verify_can_set(object);
const size_t column_ndx = get_column_index(object, property_ndx);
object.row().set_bool(column_ndx, size_t_to_bool(value));
});
}
REALM_EXPORT void object_set_int64(Object& object, size_t property_ndx, int64_t value, NativeException::Marshallable& ex)
{
return handle_errors(ex, [&]() {
verify_can_set(object);
const size_t column_ndx = get_column_index(object, property_ndx);
object.row().set_int(column_ndx, value);
});
}
REALM_EXPORT void object_set_float(Object& object, size_t property_ndx, float value, NativeException::Marshallable& ex)
{
return handle_errors(ex, [&]() {
verify_can_set(object);
const size_t column_ndx = get_column_index(object, property_ndx);
object.row().set_float(column_ndx, value);
});
}
REALM_EXPORT void object_set_double(Object& object, size_t property_ndx, double value, NativeException::Marshallable& ex)
{
return handle_errors(ex, [&]() {
verify_can_set(object);
const size_t column_ndx = get_column_index(object, property_ndx);
object.row().set_double(column_ndx, value);
});
}
REALM_EXPORT void object_set_string(Object& object, size_t property_ndx, uint16_t* value, size_t value_len, NativeException::Marshallable& ex)
{
return handle_errors(ex, [&]() {
verify_can_set(object);
const size_t column_ndx = get_column_index(object, property_ndx);
Utf16StringAccessor str(value, value_len);
object.row().set_string(column_ndx, str);
});
}
REALM_EXPORT void object_set_binary(Object& object, size_t property_ndx, char* value, size_t value_len, NativeException::Marshallable& ex)
{
return handle_errors(ex, [&]() {
verify_can_set(object);
const size_t column_ndx = get_column_index(object, property_ndx);
object.row().set_binary(column_ndx, BinaryData(value, value_len));
});
}
REALM_EXPORT void object_set_timestamp_ticks(Object& object, size_t property_ndx, int64_t value, NativeException::Marshallable& ex)
{
return handle_errors(ex, [&]() {
verify_can_set(object);
const size_t column_ndx = get_column_index(object, property_ndx);
object.row().set_timestamp(column_ndx, from_ticks(value));
});
}
REALM_EXPORT void object_remove_row(Object& object, SharedRealm& realm, NativeException::Marshallable& ex)
{
handle_errors(ex, [&]() {
if (object.realm() != realm) {
throw ObjectManagedByAnotherRealmException("Can only delete an object from the Realm it belongs to.");
}
verify_can_set(object);
auto const row_index = object.row().get_index();
object.row().get_table()->move_last_over(row_index);
});
}
REALM_EXPORT bool object_equals_object(const Object& object, const Object& other_object, NativeException::Marshallable& ex)
{
return handle_errors(ex, [&]() {
return object.is_valid() &&
other_object.is_valid() &&
object.row().get_index() == other_object.row().get_index();
});
}
REALM_EXPORT ThreadSafeReference<Object>* object_get_thread_safe_reference(const Object& object, NativeException::Marshallable& ex)
{
return handle_errors(ex, [&]() {
return new ThreadSafeReference<Object>{ object.realm()->obtain_thread_safe_reference(object) };
});
}
REALM_EXPORT void* object_destroy_notificationtoken(ManagedNotificationTokenContext* token_ptr, NativeException::Marshallable& ex)
{
return handle_errors(ex, [&]() {
void* managed_object = token_ptr->managed_object;
delete token_ptr;
return managed_object;
});
}
REALM_EXPORT ManagedNotificationTokenContext* object_add_notification_callback(Object* object, void* managed_object, ManagedNotificationCallback callback, NativeException::Marshallable& ex)
{
return handle_errors(ex, [=]() {
return subscribe_for_notifications(managed_object, callback, [object](CollectionChangeCallback callback) {
return object->add_notification_callback(callback);
}, new ObjectSchema(object->get_object_schema()));
});
}
REALM_EXPORT void object_add_int64(Object& object, size_t property_ndx, int64_t value, NativeException::Marshallable& ex)
{
return handle_errors(ex, [&]() {
verify_can_set(object);
const size_t column_ndx = get_column_index(object, property_ndx);
object.row().add_int(column_ndx, value);
});
}
REALM_EXPORT size_t object_get_backlink_count(Object& object, NativeException::Marshallable& ex)
{
return handle_errors(ex, [&]() {
return object.row().get_backlink_count();
});
}
} // extern "C"