2016-02-18 19:59:34 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
2015-08-13 16:12:48 +00:00
|
|
|
|
2015-12-01 22:52:38 +00:00
|
|
|
#pragma once
|
|
|
|
|
2016-04-12 21:42:05 +00:00
|
|
|
#include "js_collection.hpp"
|
2016-03-28 20:21:36 +00:00
|
|
|
#include "js_object.hpp"
|
2016-04-12 21:42:05 +00:00
|
|
|
#include "js_results.hpp"
|
2016-03-30 21:11:57 +00:00
|
|
|
#include "js_types.hpp"
|
2015-12-01 22:52:38 +00:00
|
|
|
#include "js_util.hpp"
|
2016-03-28 20:21:36 +00:00
|
|
|
|
2015-12-01 22:52:38 +00:00
|
|
|
#include "shared_realm.hpp"
|
|
|
|
#include "list.hpp"
|
2016-03-28 20:21:36 +00:00
|
|
|
#include "parser.hpp"
|
|
|
|
#include "query_builder.hpp"
|
|
|
|
|
2016-03-29 21:12:27 +00:00
|
|
|
namespace realm {
|
|
|
|
namespace js {
|
2016-03-28 20:21:36 +00:00
|
|
|
|
2016-03-29 21:12:27 +00:00
|
|
|
template<typename T>
|
2016-04-18 05:49:07 +00:00
|
|
|
class List {
|
2016-04-14 17:54:43 +00:00
|
|
|
using TContext = typename T::Context;
|
|
|
|
using TObject = typename T::Object;
|
|
|
|
using TValue = typename T::Value;
|
2016-03-30 21:11:57 +00:00
|
|
|
using Object = Object<T>;
|
|
|
|
using Value = Value<T>;
|
|
|
|
using ReturnValue = ReturnValue<T>;
|
|
|
|
|
2016-04-18 05:49:07 +00:00
|
|
|
public:
|
2016-04-14 18:19:01 +00:00
|
|
|
static TObject create_instance(TContext, realm::List &);
|
2016-03-30 21:11:57 +00:00
|
|
|
|
2016-04-18 05:49:07 +00:00
|
|
|
// properties
|
|
|
|
static void get_length(TContext, TObject, ReturnValue &);
|
|
|
|
static void get_index(TContext, TObject, uint32_t, ReturnValue &);
|
|
|
|
static bool set_index(TContext, TObject, uint32_t, TValue);
|
|
|
|
|
|
|
|
// methods
|
|
|
|
static void push(TContext, TObject, size_t, const TValue[], ReturnValue &);
|
|
|
|
static void pop(TContext, TObject, size_t, const TValue[], ReturnValue &);
|
|
|
|
static void unshift(TContext, TObject, size_t, const TValue[], ReturnValue &);
|
|
|
|
static void shift(TContext, TObject, size_t, const TValue[], ReturnValue &);
|
|
|
|
static void splice(TContext, TObject, size_t, const TValue[], ReturnValue &);
|
|
|
|
static void snapshot(TContext, TObject, size_t, const TValue[], ReturnValue &);
|
|
|
|
static void filtered(TContext, TObject, size_t, const TValue[], ReturnValue &);
|
|
|
|
static void sorted(TContext, TObject, size_t, const TValue[], ReturnValue &);
|
2016-03-29 21:12:27 +00:00
|
|
|
};
|
2016-03-30 21:11:57 +00:00
|
|
|
|
2016-03-30 21:18:44 +00:00
|
|
|
template<typename T>
|
2016-04-16 23:46:16 +00:00
|
|
|
struct ListClass : ClassDefinition<T, realm::List, CollectionClass<T>> {
|
2016-03-30 21:11:57 +00:00
|
|
|
using List = List<T>;
|
|
|
|
|
|
|
|
std::string const name = "List";
|
|
|
|
|
|
|
|
MethodMap<T> const methods = {
|
2016-04-18 05:49:07 +00:00
|
|
|
{"push", wrap<List::push>},
|
|
|
|
{"pop", wrap<List::pop>},
|
|
|
|
{"unshift", wrap<List::unshift>},
|
|
|
|
{"shift", wrap<List::shift>},
|
|
|
|
{"splice", wrap<List::splice>},
|
|
|
|
{"snapshot", wrap<List::snapshot>},
|
|
|
|
{"filtered", wrap<List::filtered>},
|
|
|
|
{"sorted", wrap<List::sorted>},
|
2016-03-30 21:11:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
PropertyMap<T> const properties = {
|
2016-04-18 05:49:07 +00:00
|
|
|
{"length", {wrap<List::get_length>, nullptr}},
|
2016-03-30 21:11:57 +00:00
|
|
|
};
|
|
|
|
|
2016-04-18 05:49:07 +00:00
|
|
|
IndexPropertyType<T> const index_accessor = {wrap<List::get_index>, wrap<List::set_index>};
|
2016-03-30 21:11:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
2016-04-14 18:19:01 +00:00
|
|
|
typename T::Object List<T>::create_instance(TContext ctx, realm::List &list) {
|
2016-04-15 20:47:01 +00:00
|
|
|
return create_object<T, ListClass<T>>(ctx, new realm::List(list));
|
2016-03-30 21:18:44 +00:00
|
|
|
}
|
2016-03-30 21:11:57 +00:00
|
|
|
|
2016-03-30 21:18:44 +00:00
|
|
|
template<typename T>
|
2016-04-18 05:49:07 +00:00
|
|
|
void List<T>::get_length(TContext ctx, TObject object, ReturnValue &return_value) {
|
2016-04-15 20:47:01 +00:00
|
|
|
auto list = get_internal<T, ListClass<T>>(object);
|
2016-03-30 21:11:57 +00:00
|
|
|
return_value.set((uint32_t)list->size());
|
2016-03-30 21:18:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2016-04-18 05:49:07 +00:00
|
|
|
void List<T>::get_index(TContext ctx, TObject object, uint32_t index, ReturnValue &return_value) {
|
2016-04-15 20:47:01 +00:00
|
|
|
auto list = get_internal<T, ListClass<T>>(object);
|
2016-03-30 21:11:57 +00:00
|
|
|
auto realm_object = realm::Object(list->get_realm(), list->get_object_schema(), list->get(index));
|
|
|
|
|
2016-04-14 18:19:01 +00:00
|
|
|
return_value.set(RealmObject<T>::create_instance(ctx, realm_object));
|
2016-03-30 21:11:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2016-04-18 05:49:07 +00:00
|
|
|
bool List<T>::set_index(TContext ctx, TObject object, uint32_t index, TValue value) {
|
2016-04-15 20:47:01 +00:00
|
|
|
auto list = get_internal<T, ListClass<T>>(object);
|
2016-03-30 21:18:44 +00:00
|
|
|
list->set(ctx, value, index);
|
2016-03-30 21:11:57 +00:00
|
|
|
return true;
|
2016-03-30 21:18:44 +00:00
|
|
|
}
|
2016-03-29 21:12:27 +00:00
|
|
|
|
|
|
|
template<typename T>
|
2016-04-18 05:49:07 +00:00
|
|
|
void List<T>::push(TContext ctx, TObject this_object, size_t argc, const TValue arguments[], ReturnValue &return_value) {
|
2016-03-30 21:11:57 +00:00
|
|
|
validate_argument_count_at_least(argc, 1);
|
|
|
|
|
2016-04-15 20:47:01 +00:00
|
|
|
auto list = get_internal<T, ListClass<T>>(this_object);
|
2016-03-30 21:11:57 +00:00
|
|
|
for (size_t i = 0; i < argc; i++) {
|
2016-03-30 18:55:13 +00:00
|
|
|
list->add(ctx, arguments[i]);
|
2016-03-28 20:21:36 +00:00
|
|
|
}
|
2016-03-30 21:11:57 +00:00
|
|
|
|
|
|
|
return_value.set((uint32_t)list->size());
|
2016-03-28 20:21:36 +00:00
|
|
|
}
|
|
|
|
|
2016-03-29 21:12:27 +00:00
|
|
|
template<typename T>
|
2016-04-18 05:49:07 +00:00
|
|
|
void List<T>::pop(TContext ctx, TObject this_object, size_t argc, const TValue arguments[], ReturnValue &return_value) {
|
2016-03-30 21:11:57 +00:00
|
|
|
validate_argument_count(argc, 0);
|
|
|
|
|
2016-04-15 20:47:01 +00:00
|
|
|
auto list = get_internal<T, ListClass<T>>(this_object);
|
2016-03-30 18:55:13 +00:00
|
|
|
size_t size = list->size();
|
|
|
|
if (size == 0) {
|
|
|
|
list->verify_in_transaction();
|
2016-03-30 21:11:57 +00:00
|
|
|
return_value.set_undefined();
|
2016-03-28 20:21:36 +00:00
|
|
|
}
|
2016-03-30 18:55:13 +00:00
|
|
|
else {
|
|
|
|
size_t index = size - 1;
|
2016-03-30 21:11:57 +00:00
|
|
|
auto realm_object = realm::Object(list->get_realm(), list->get_object_schema(), list->get(index));
|
|
|
|
|
2016-04-14 18:19:01 +00:00
|
|
|
return_value.set(RealmObject<T>::create_instance(ctx, realm_object));
|
2016-03-30 18:55:13 +00:00
|
|
|
list->remove(index);
|
2016-03-28 20:21:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-29 21:12:27 +00:00
|
|
|
template<typename T>
|
2016-04-18 05:49:07 +00:00
|
|
|
void List<T>::unshift(TContext ctx, TObject this_object, size_t argc, const TValue arguments[], ReturnValue &return_value) {
|
2016-03-30 21:11:57 +00:00
|
|
|
validate_argument_count_at_least(argc, 1);
|
|
|
|
|
2016-04-15 20:47:01 +00:00
|
|
|
auto list = get_internal<T, ListClass<T>>(this_object);
|
2016-03-30 21:11:57 +00:00
|
|
|
for (size_t i = 0; i < argc; i++) {
|
2016-03-30 18:55:13 +00:00
|
|
|
list->insert(ctx, arguments[i], i);
|
2016-03-28 20:21:36 +00:00
|
|
|
}
|
2016-03-30 21:11:57 +00:00
|
|
|
|
|
|
|
return_value.set((uint32_t)list->size());
|
2016-03-28 20:21:36 +00:00
|
|
|
}
|
|
|
|
|
2016-03-29 21:12:27 +00:00
|
|
|
template<typename T>
|
2016-04-18 05:49:07 +00:00
|
|
|
void List<T>::shift(TContext ctx, TObject this_object, size_t argc, const TValue arguments[], ReturnValue &return_value) {
|
2016-03-30 21:11:57 +00:00
|
|
|
validate_argument_count(argc, 0);
|
|
|
|
|
2016-04-15 20:47:01 +00:00
|
|
|
auto list = get_internal<T, ListClass<T>>(this_object);
|
2016-03-30 18:55:13 +00:00
|
|
|
if (list->size() == 0) {
|
|
|
|
list->verify_in_transaction();
|
2016-03-30 21:11:57 +00:00
|
|
|
return_value.set_undefined();
|
2016-03-28 20:21:36 +00:00
|
|
|
}
|
2016-03-30 18:55:13 +00:00
|
|
|
else {
|
2016-03-30 21:11:57 +00:00
|
|
|
auto realm_object = realm::Object(list->get_realm(), list->get_object_schema(), list->get(0));
|
|
|
|
|
2016-04-14 18:19:01 +00:00
|
|
|
return_value.set(RealmObject<T>::create_instance(ctx, realm_object));
|
2016-03-30 18:55:13 +00:00
|
|
|
list->remove(0);
|
2016-03-28 20:21:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-29 21:12:27 +00:00
|
|
|
template<typename T>
|
2016-04-18 05:49:07 +00:00
|
|
|
void List<T>::splice(TContext ctx, TObject this_object, size_t argc, const TValue arguments[], ReturnValue &return_value) {
|
2016-03-30 21:11:57 +00:00
|
|
|
validate_argument_count_at_least(argc, 1);
|
|
|
|
|
2016-04-15 20:47:01 +00:00
|
|
|
auto list = get_internal<T, ListClass<T>>(this_object);
|
2016-03-30 18:55:13 +00:00
|
|
|
size_t size = list->size();
|
2016-04-12 21:42:05 +00:00
|
|
|
long index = std::min<long>(Value::to_number(ctx, arguments[0]), size);
|
2016-03-30 18:55:13 +00:00
|
|
|
if (index < 0) {
|
|
|
|
index = std::max<long>(size + index, 0);
|
|
|
|
}
|
2016-04-18 05:49:07 +00:00
|
|
|
|
|
|
|
size_t remove;
|
2016-03-30 21:11:57 +00:00
|
|
|
if (argc < 2) {
|
2016-03-30 18:55:13 +00:00
|
|
|
remove = size - index;
|
|
|
|
}
|
|
|
|
else {
|
2016-04-12 21:42:05 +00:00
|
|
|
remove = std::max<long>(Value::to_number(ctx, arguments[1]), 0);
|
2016-03-30 18:55:13 +00:00
|
|
|
remove = std::min<long>(remove, size - index);
|
|
|
|
}
|
|
|
|
|
2016-04-14 17:54:43 +00:00
|
|
|
std::vector<TValue> removed_objects;
|
2016-03-30 21:11:57 +00:00
|
|
|
removed_objects.reserve(remove);
|
|
|
|
|
2016-03-30 18:55:13 +00:00
|
|
|
for (size_t i = 0; i < remove; i++) {
|
2016-03-30 21:11:57 +00:00
|
|
|
auto realm_object = realm::Object(list->get_realm(), list->get_object_schema(), list->get(index));
|
|
|
|
|
2016-04-14 18:19:01 +00:00
|
|
|
removed_objects.push_back(RealmObject<T>::create_instance(ctx, realm_object));
|
2016-03-30 18:55:13 +00:00
|
|
|
list->remove(index);
|
2016-03-28 20:21:36 +00:00
|
|
|
}
|
2016-03-30 21:11:57 +00:00
|
|
|
for (size_t i = 2; i < argc; i++) {
|
2016-03-30 18:55:13 +00:00
|
|
|
list->insert(ctx, arguments[i], index + i - 2);
|
2016-03-28 20:21:36 +00:00
|
|
|
}
|
|
|
|
|
2016-03-30 21:11:57 +00:00
|
|
|
return_value.set(Object::create_array(ctx, removed_objects));
|
|
|
|
}
|
2016-03-28 20:21:36 +00:00
|
|
|
|
2016-03-29 21:12:27 +00:00
|
|
|
template<typename T>
|
2016-04-18 05:49:07 +00:00
|
|
|
void List<T>::snapshot(TContext ctx, TObject this_object, size_t argc, const TValue arguments[], ReturnValue &return_value) {
|
2016-03-30 21:11:57 +00:00
|
|
|
validate_argument_count(argc, 0);
|
|
|
|
|
2016-04-15 20:47:01 +00:00
|
|
|
auto list = get_internal<T, ListClass<T>>(this_object);
|
2016-04-14 18:19:01 +00:00
|
|
|
return_value.set(Results<T>::create_instance(ctx, *list, false));
|
2016-03-28 20:21:36 +00:00
|
|
|
}
|
|
|
|
|
2016-03-29 21:12:27 +00:00
|
|
|
template<typename T>
|
2016-04-18 05:49:07 +00:00
|
|
|
void List<T>::filtered(TContext ctx, TObject this_object, size_t argc, const TValue arguments[], ReturnValue &return_value) {
|
2016-03-30 21:11:57 +00:00
|
|
|
validate_argument_count_at_least(argc, 1);
|
|
|
|
|
2016-04-15 20:47:01 +00:00
|
|
|
auto list = get_internal<T, ListClass<T>>(this_object);
|
2016-04-12 21:42:05 +00:00
|
|
|
return_value.set(Results<T>::create_filtered(ctx, *list, argc, arguments));
|
2016-03-28 20:21:36 +00:00
|
|
|
}
|
2015-08-13 16:12:48 +00:00
|
|
|
|
2016-03-29 21:12:27 +00:00
|
|
|
template<typename T>
|
2016-04-18 05:49:07 +00:00
|
|
|
void List<T>::sorted(TContext ctx, TObject this_object, size_t argc, const TValue arguments[], ReturnValue &return_value) {
|
2016-03-30 21:11:57 +00:00
|
|
|
validate_argument_count(argc, 1, 2);
|
|
|
|
|
2016-04-15 20:47:01 +00:00
|
|
|
auto list = get_internal<T, ListClass<T>>(this_object);
|
2016-04-12 21:42:05 +00:00
|
|
|
return_value.set(Results<T>::create_sorted(ctx, *list, argc, arguments));
|
2016-03-29 21:12:27 +00:00
|
|
|
}
|
|
|
|
|
2016-03-30 21:11:57 +00:00
|
|
|
} // js
|
|
|
|
} // realm
|