Esempio n. 1
0
void List<T>::shift(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
    validate_argument_count(argc, 0);

    auto list = get_internal<T, ListClass<T>>(this_object);
    if (list->size() == 0) {
        list->verify_in_transaction();
        return_value.set_undefined();
    }
    else {
        auto realm_object = realm::Object(list->get_realm(), list->get_object_schema(), list->get(0));

        return_value.set(RealmObject<T>::create_instance(ctx, realm_object));
        list->remove(0);
    }
}
Esempio n. 2
0
void ListClass<T>::pop(ContextType ctx, FunctionType, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
    validate_argument_count(argc, 0);

    auto list = get_internal<T, ListClass<T>>(this_object);
    size_t size = list->size();
    if (size == 0) {
        list->verify_in_transaction();
        return_value.set_undefined();
    }
    else {
        size_t index = size - 1;
        auto realm_object = realm::Object(list->get_realm(), list->get_object_schema(), list->get(index));

        return_value.set(RealmObjectClass<T>::create_instance(ctx, std::move(realm_object)));
        list->remove(index);
    }
}
Esempio n. 3
0
void ListClass<T>::remove_listener(ContextType ctx, FunctionType, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
    validate_argument_count(argc, 1);
    
    auto list = get_internal<T, ListClass<T>>(this_object);
    auto callback = Value::validated_to_function(ctx, arguments[0]);
    auto protected_function = Protected<FunctionType>(ctx, callback);

    auto iter = list->m_notification_tokens.begin();
    typename Protected<FunctionType>::Comparator compare;
    while (iter != list->m_notification_tokens.end()) {
        if(compare(iter->first, protected_function)) {
            iter = list->m_notification_tokens.erase(iter);
        }
        else {
            iter++;
        }
    }
}
Esempio n. 4
0
void ListClass<T>::add_listener(ContextType ctx, FunctionType, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
    validate_argument_count(argc, 1);
    
    auto list = get_internal<T, ListClass<T>>(this_object);
    auto callback = Value::validated_to_function(ctx, arguments[0]);
    Protected<FunctionType> protected_callback(ctx, callback);
    Protected<ObjectType> protected_this(ctx, this_object);
    Protected<typename T::GlobalContext> protected_ctx(Context<T>::get_global_context(ctx));

    auto token = list->add_notification_callback([=](CollectionChangeSet change_set, std::exception_ptr exception) {
        HANDLESCOPE

        ValueType arguments[2];
        arguments[0] = static_cast<ObjectType>(protected_this);
        arguments[1] = CollectionClass<T>::create_collection_change_set(protected_ctx, change_set);
        Function<T>::callback(protected_ctx, protected_callback, protected_this, 2, arguments);
    });
    list->m_notification_tokens.emplace_back(protected_callback, std::move(token));
}
Esempio n. 5
0
void List<T>::sorted(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
    validate_argument_count(argc, 1, 2);

    auto list = get_internal<T, ListClass<T>>(this_object);
    return_value.set(Results<T>::create_sorted(ctx, *list, argc, arguments));
}
Esempio n. 6
0
void List<T>::snapshot(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
    validate_argument_count(argc, 0);

    auto list = get_internal<T, ListClass<T>>(this_object);
    return_value.set(Results<T>::create_instance(ctx, *list, false));
}
Esempio n. 7
0
void ListClass<T>::remove_all_listeners(ContextType ctx, FunctionType, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
    validate_argument_count(argc, 0);

    auto list = get_internal<T, ListClass<T>>(this_object);
    list->m_notification_tokens.clear();
}