From df901c636feaaaceaf2de433f0b9ab8d1023f067 Mon Sep 17 00:00:00 2001 From: Ari Lazier Date: Mon, 4 Jan 2016 16:04:01 -0800 Subject: [PATCH] store ObjectSchema as a pointer rather than a reference --- src/js_list.cpp | 8 ++++---- src/js_results.cpp | 6 +++--- src/object-store/list.hpp | 5 +++-- src/object-store/object_accessor.hpp | 6 +++--- src/object-store/results.cpp | 21 +++++---------------- src/object-store/results.hpp | 7 ++++--- src/rpc.cpp | 4 ++-- 7 files changed, 24 insertions(+), 33 deletions(-) diff --git a/src/js_list.cpp b/src/js_list.cpp index 987701fd..a60334b8 100644 --- a/src/js_list.cpp +++ b/src/js_list.cpp @@ -21,7 +21,7 @@ JSValueRef ListGetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef pro return JSValueMakeNumber(ctx, size); } - return RJSObjectCreate(ctx, Object(list->realm(), list->object_schema, list->get(RJSValidatedPositiveIndex(indexStr)))); + return RJSObjectCreate(ctx, Object(list->realm(), list->object_schema(), list->get(RJSValidatedPositiveIndex(indexStr)))); } catch (std::out_of_range &exp) { // getters for nonexistent properties in JS should always return undefined @@ -103,7 +103,7 @@ JSValueRef ListPop(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObjec return JSValueMakeUndefined(ctx); } size_t index = size - 1; - JSValueRef obj = RJSObjectCreate(ctx, Object(list->realm(), list->object_schema, list->get(index))); + JSValueRef obj = RJSObjectCreate(ctx, Object(list->realm(), list->object_schema(), list->get(index))); list->remove(index); return obj; } @@ -140,7 +140,7 @@ JSValueRef ListShift(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObj list->verify_in_tranaction(); return JSValueMakeUndefined(ctx); } - JSValueRef obj = RJSObjectCreate(ctx, Object(list->realm(), list->object_schema, list->get(0))); + JSValueRef obj = RJSObjectCreate(ctx, Object(list->realm(), list->object_schema(), list->get(0))); list->remove(0); return obj; } @@ -168,7 +168,7 @@ JSValueRef ListSplice(JSContextRef ctx, JSObjectRef function, JSObjectRef thisOb std::vector removedObjects(remove); for (size_t i = 0; i < remove; i++) { - removedObjects[i] = RJSObjectCreate(ctx, Object(list->realm(), list->object_schema, list->get(index))); + removedObjects[i] = RJSObjectCreate(ctx, Object(list->realm(), list->object_schema(), list->get(index))); list->remove(index); } for (size_t i = 2; i < argumentCount; i++) { diff --git a/src/js_results.cpp b/src/js_results.cpp index 789d18da..35132fe9 100644 --- a/src/js_results.cpp +++ b/src/js_results.cpp @@ -23,7 +23,7 @@ JSValueRef ResultsGetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef } return RJSObjectCreate(ctx, Object(results->get_realm(), - results->object_schema, + results->object_schema(), results->get(RJSValidatedPositiveIndex(indexStr)))); } catch (std::out_of_range &exp) { @@ -76,9 +76,9 @@ JSValueRef SortByProperty(JSContextRef ctx, JSObjectRef function, JSObjectRef th Results *results = RJSGetInternal(thisObject); RJSValidateArgumentRange(argumentCount, 1, 2); std::string propName = RJSValidatedStringForValue(ctx, arguments[0]); - const Property *prop = results->object_schema.property_for_name(propName); + const Property *prop = results->object_schema().property_for_name(propName); if (!prop) { - throw std::runtime_error("Property '" + propName + "' does not exist on object type '" + results->object_schema.name + "'"); + throw std::runtime_error("Property '" + propName + "' does not exist on object type '" + results->object_schema().name + "'"); } bool ascending = true; diff --git a/src/object-store/list.hpp b/src/object-store/list.hpp index ef84846a..e7c6465c 100644 --- a/src/object-store/list.hpp +++ b/src/object-store/list.hpp @@ -25,9 +25,9 @@ namespace realm { class List { public: - List(SharedRealm &r, const ObjectSchema &s, LinkViewRef l) : m_realm(r), object_schema(s), m_link_view(l) {} + List(SharedRealm &r, const ObjectSchema &s, LinkViewRef l) : m_realm(r), m_object_schema(&s), m_link_view(l) {} - const ObjectSchema &object_schema; + const ObjectSchema &object_schema() const { return *m_object_schema; } SharedRealm realm() { return m_realm; } size_t size(); @@ -53,6 +53,7 @@ namespace realm { private: SharedRealm m_realm; + const ObjectSchema *m_object_schema; LinkViewRef m_link_view; }; } diff --git a/src/object-store/object_accessor.hpp b/src/object-store/object_accessor.hpp index ae1025f0..eed939e1 100644 --- a/src/object-store/object_accessor.hpp +++ b/src/object-store/object_accessor.hpp @@ -308,19 +308,19 @@ namespace realm { template void List::add(ContextType ctx, ValueType value) { - add(NativeAccessor::to_object_index(ctx, m_realm, value, object_schema.name, false)); + add(NativeAccessor::to_object_index(ctx, m_realm, value, object_schema().name, false)); } template void List::insert(ContextType ctx, ValueType value, size_t list_ndx) { - insert(list_ndx, NativeAccessor::to_object_index(ctx, m_realm, value, object_schema.name, false)); + insert(list_ndx, NativeAccessor::to_object_index(ctx, m_realm, value, object_schema().name, false)); } template void List::set(ContextType ctx, ValueType value, size_t list_ndx) { - set(list_ndx, NativeAccessor::to_object_index(ctx, m_realm, value, object_schema.name, false)); + set(list_ndx, NativeAccessor::to_object_index(ctx, m_realm, value, object_schema().name, false)); } } diff --git a/src/object-store/results.cpp b/src/object-store/results.cpp index 32b00dda..e8d4bfd2 100644 --- a/src/object-store/results.cpp +++ b/src/object-store/results.cpp @@ -40,7 +40,7 @@ Results::Results(SharedRealm r, const ObjectSchema &o, Query q, SortOrder s) , m_table(m_query.get_table().get()) , m_sort(std::move(s)) , m_mode(Mode::Query) -, object_schema(o) +, m_object_schema(&o) { } @@ -48,21 +48,10 @@ Results::Results(SharedRealm r, const ObjectSchema &o, Table& table) : m_realm(std::move(r)) , m_table(&table) , m_mode(Mode::Table) -, object_schema(o) +, m_object_schema(&o) { } -Results& Results::operator=(Results const& r) -{ - m_realm = r.m_realm; - m_table = r.m_table; - m_sort = r.m_sort; - m_query = r.get_query(); - m_mode = Mode::Query; - const_cast(object_schema) = r.object_schema; - return *this; -} - void Results::validate_read() const { if (m_realm) @@ -171,7 +160,7 @@ size_t Results::index_of(Row const& row) throw DetatchedAccessorException{}; } if (m_table && row.get_table() != m_table) { - throw IncorrectTableException(object_schema.name, + throw IncorrectTableException(m_object_schema->name, ObjectStore::object_type_for_table_name(row.get_table()->get_name()), "Attempting to get the index of a Row of the wrong type" ); @@ -326,12 +315,12 @@ TableView Results::get_tableview() Results Results::sort(realm::SortOrder&& sort) const { - return Results(m_realm, object_schema, get_query(), std::move(sort)); + return Results(m_realm, object_schema(), get_query(), std::move(sort)); } Results Results::filter(Query&& q) const { - return Results(m_realm, object_schema, get_query().and_query(std::move(q)), get_sort()); + return Results(m_realm, object_schema(), get_query().and_query(std::move(q)), get_sort()); } Results::UnsupportedColumnTypeException::UnsupportedColumnTypeException(size_t column, const Table* table) diff --git a/src/object-store/results.hpp b/src/object-store/results.hpp index 9866cae8..42dbda8f 100644 --- a/src/object-store/results.hpp +++ b/src/object-store/results.hpp @@ -53,13 +53,13 @@ public: Results(Results const&) = default; Results(Results&&) = default; Results& operator=(Results&&) = default; - Results& operator=(Results const&); + Results& operator=(Results const&) = default; // Get the Realm SharedRealm get_realm() const { return m_realm; } // Object schema describing the vendored object type - const ObjectSchema &object_schema; + const ObjectSchema &object_schema() const { return *m_object_schema; } // Get a query which will match the same rows as is contained in this Results // Returned query will not be valid if the current mode is Empty @@ -72,7 +72,7 @@ public: TableView get_tableview(); // Get the object type which will be returned by get() - StringData get_object_type() const noexcept { return object_schema.name; } + StringData get_object_type() const noexcept { return object_schema().name; } // Get the size of this results // Can be either O(1) or O(N) depending on the state of things @@ -163,6 +163,7 @@ public: private: SharedRealm m_realm; + const ObjectSchema *m_object_schema; Query m_query; TableView m_table_view; Table* m_table = nullptr; diff --git a/src/rpc.cpp b/src/rpc.cpp index 962eb94d..2b04702b 100644 --- a/src/rpc.cpp +++ b/src/rpc.cpp @@ -229,7 +229,7 @@ json RPCServer::serialize_json_value(JSValueRef value) { {"type", RJSTypeGet(realm::PropertyTypeArray)}, {"id", store_object(js_object)}, {"size", list->size()}, - {"schema", serialize_object_schema(list->object_schema)} + {"schema", serialize_object_schema(list->object_schema())} }; } else if (JSValueIsObjectOfClass(m_context, value, RJSResultsClass())) { @@ -238,7 +238,7 @@ json RPCServer::serialize_json_value(JSValueRef value) { {"type", RealmObjectTypesResults}, {"id", store_object(js_object)}, {"size", results->size()}, - {"schema", serialize_object_schema(results->object_schema)} + {"schema", serialize_object_schema(results->object_schema())} }; } else if (RJSIsValueArray(m_context, value)) {