realm-js/src/js_object_accessor.hpp

148 lines
6.2 KiB
C++
Raw Normal View History

////////////////////////////////////////////////////////////////////////////
//
// 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.
//
////////////////////////////////////////////////////////////////////////////
#pragma once
#include "js_list.hpp"
#include "js_realm_object.hpp"
#include "js_schema.hpp"
namespace realm {
namespace js {
template<typename T>
2016-04-14 18:06:17 +00:00
struct NativeAccessor {
2016-04-18 19:15:00 +00:00
using ContextType = typename T::Context;
using ObjectType = typename T::Object;
using ValueType = typename T::Value;
using Object = js::Object<T>;
using Value = js::Value<T>;
2016-04-18 19:15:00 +00:00
static bool dict_has_value_for_key(ContextType ctx, ValueType dict, const std::string &prop_name) {
ObjectType object = Value::validated_to_object(ctx, dict);
return Object::has_property(ctx, object, prop_name);
}
2016-04-18 19:15:00 +00:00
static ValueType dict_value_for_key(ContextType ctx, ValueType dict, const std::string &prop_name) {
ObjectType object = Value::validated_to_object(ctx, dict);
return Object::get_property(ctx, object, prop_name);
}
2016-04-18 19:15:00 +00:00
static bool has_default_value_for_property(ContextType ctx, realm::Realm *realm, const ObjectSchema &object_schema, const std::string &prop_name) {
auto defaults = get_delegate<T>(realm)->m_defaults[object_schema.name];
return defaults.count(prop_name) != 0;
}
2016-04-18 19:15:00 +00:00
static ValueType default_value_for_property(ContextType ctx, realm::Realm *realm, const ObjectSchema &object_schema, const std::string &prop_name) {
auto defaults = get_delegate<T>(realm)->m_defaults[object_schema.name];
return defaults.at(prop_name);
}
// These must be implemented for each JS engine.
2016-04-18 19:15:00 +00:00
static std::string to_binary(ContextType, ValueType &);
static ValueType from_binary(ContextType, BinaryData);
2016-04-18 19:15:00 +00:00
static bool to_bool(ContextType ctx, ValueType &value) {
return Value::validated_to_boolean(ctx, value, "Property");
}
2016-04-18 19:15:00 +00:00
static ValueType from_bool(ContextType ctx, bool boolean) {
return Value::from_boolean(ctx, boolean);
}
2016-04-18 19:15:00 +00:00
static long long to_long(ContextType ctx, ValueType &value) {
return Value::validated_to_number(ctx, value, "Property");
}
2016-04-18 19:15:00 +00:00
static ValueType from_long(ContextType ctx, long long number) {
return Value::from_number(ctx, number);
}
2016-04-18 19:15:00 +00:00
static float to_float(ContextType ctx, ValueType &value) {
return Value::validated_to_number(ctx, value, "Property");
}
2016-04-18 19:15:00 +00:00
static ValueType from_float(ContextType ctx, float number) {
return Value::from_number(ctx, number);
}
2016-04-18 19:15:00 +00:00
static double to_double(ContextType ctx, ValueType &value) {
return Value::validated_to_number(ctx, value, "Property");
}
2016-04-18 19:15:00 +00:00
static ValueType from_double(ContextType ctx, double number) {
return Value::from_number(ctx, number);
}
2016-04-18 19:15:00 +00:00
static std::string to_string(ContextType ctx, ValueType &value) {
return Value::validated_to_string(ctx, value, "Property");
}
2016-04-18 19:15:00 +00:00
static ValueType from_string(ContextType ctx, StringData string) {
2016-04-12 21:42:05 +00:00
return Value::from_string(ctx, string.data());
}
static Timestamp to_timestamp(ContextType ctx, ValueType &value) {
2016-04-18 19:15:00 +00:00
ObjectType date = Value::validated_to_date(ctx, value, "Property");
double milliseconds = Value::to_number(ctx, date);
u_int64_t seconds = milliseconds / 1000;
u_int32_t nanoseconds = ((u_int64_t)milliseconds % 1000) * 1000000;
return Timestamp(seconds, nanoseconds);
}
static ValueType from_timestamp(ContextType ctx, Timestamp ts) {
return Object::create_date(ctx, ts.get_seconds() * 1000 + ts.get_nanoseconds() / 1000000);
}
2016-04-18 19:15:00 +00:00
static bool is_null(ContextType ctx, ValueType &value) {
return Value::is_null(ctx, value) || Value::is_undefined(ctx, value);
}
2016-04-18 19:15:00 +00:00
static ValueType null_value(ContextType ctx) {
return Value::from_null(ctx);
}
2016-04-18 19:15:00 +00:00
static size_t to_object_index(ContextType ctx, SharedRealm realm, ValueType &value, const std::string &type, bool try_update) {
ObjectType object = Value::validated_to_object(ctx, value);
if (Object::template is_instance<RealmObjectClass<T>>(ctx, object)) {
return get_internal<T, RealmObjectClass<T>>(object)->row().get_index();
}
auto object_schema = realm->config().schema->find(type);
if (Value::is_array(ctx, object)) {
object = Schema<T>::dict_for_property_array(ctx, *object_schema, object);
}
2016-04-18 19:15:00 +00:00
auto child = realm::Object::create<ValueType>(ctx, realm, *object_schema, static_cast<ValueType>(object), try_update);
return child.row().get_index();
}
2016-04-18 19:15:00 +00:00
static size_t to_existing_object_index(ContextType ctx, ValueType &value) {
ObjectType object = Value::validated_to_object(ctx, value);
if (Object::template is_instance<RealmObjectClass<T>>(ctx, object)) {
return get_internal<T, RealmObjectClass<T>>(object)->row().get_index();
}
throw std::runtime_error("object is not a Realm Object");
}
2016-04-18 19:15:00 +00:00
static ValueType from_object(ContextType ctx, realm::Object realm_object) {
2016-04-14 18:19:01 +00:00
return RealmObject<T>::create_instance(ctx, realm_object);
}
2016-04-18 19:15:00 +00:00
static size_t list_size(ContextType ctx, ValueType &value) {
return Object::validated_get_length(ctx, Value::validated_to_object(ctx, value));
}
2016-04-18 19:15:00 +00:00
static ValueType list_value_at_index(ContextType ctx, ValueType &value, size_t index) {
return Object::validated_get_object(ctx, Value::validated_to_object(ctx, value), (uint32_t)index);
}
2016-04-18 19:15:00 +00:00
static ValueType from_list(ContextType ctx, realm::List list) {
return ListClass<T>::create_instance(ctx, list);
}
2016-04-18 19:15:00 +00:00
static Mixed to_mixed(ContextType ctx, ValueType &val) {
throw std::runtime_error("'Any' type is unsupported");
}
};
} // js
} // realm