From 77359f14e39d89b2763ebf96b5e343789f09ce18 Mon Sep 17 00:00:00 2001 From: Ari Lazier Date: Tue, 13 Oct 2015 14:44:31 -0700 Subject: [PATCH] move List class to its own file --- RealmJS.xcodeproj/project.pbxproj | 8 +++++ src/RJSArray.cpp | 28 ----------------- src/RJSArray.hpp | 18 +---------- src/object-store/list.cpp | 50 +++++++++++++++++++++++++++++++ src/object-store/list.hpp | 42 ++++++++++++++++++++++++++ 5 files changed, 101 insertions(+), 45 deletions(-) create mode 100644 src/object-store/list.cpp create mode 100644 src/object-store/list.hpp diff --git a/RealmJS.xcodeproj/project.pbxproj b/RealmJS.xcodeproj/project.pbxproj index 3366743b..6b4c1b3d 100644 --- a/RealmJS.xcodeproj/project.pbxproj +++ b/RealmJS.xcodeproj/project.pbxproj @@ -54,6 +54,8 @@ 0270BCD11B7D067300010E03 /* RealmReactModule.m in Sources */ = {isa = PBXBuildFile; fileRef = 0270BCD01B7D067300010E03 /* RealmReactModule.m */; }; 02B29A311B7CF86D008A7E6B /* RealmJS.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 02B58CB11AE99CEC009B348C /* RealmJS.framework */; }; 02B58CCE1AE99D4D009B348C /* JavaScriptCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 02B58CCD1AE99D4D009B348C /* JavaScriptCore.framework */; }; + 02C0864E1BCDB27000942F9C /* list.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 02C0864C1BCDB27000942F9C /* list.cpp */; settings = {ASSET_TAGS = (); }; }; + 02C0864F1BCDB27000942F9C /* list.hpp in Headers */ = {isa = PBXBuildFile; fileRef = 02C0864D1BCDB27000942F9C /* list.hpp */; settings = {ASSET_TAGS = (); }; }; 02D456DA1B7E59A500EE1299 /* ArrayTests.js in Resources */ = {isa = PBXBuildFile; fileRef = 02D456D91B7E59A500EE1299 /* ArrayTests.js */; }; 02D8D1F71B601984006DB49D /* JavaScriptCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 02B58CCD1AE99D4D009B348C /* JavaScriptCore.framework */; }; /* End PBXBuildFile section */ @@ -183,6 +185,8 @@ 02B58CB11AE99CEC009B348C /* RealmJS.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = RealmJS.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 02B58CBC1AE99CEC009B348C /* RealmJSTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = RealmJSTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 02B58CCD1AE99D4D009B348C /* JavaScriptCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = JavaScriptCore.framework; path = System/Library/Frameworks/JavaScriptCore.framework; sourceTree = SDKROOT; }; + 02C0864C1BCDB27000942F9C /* list.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = list.cpp; path = "src/object-store/list.cpp"; sourceTree = ""; }; + 02C0864D1BCDB27000942F9C /* list.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = list.hpp; path = "src/object-store/list.hpp"; sourceTree = ""; }; 02D456D91B7E59A500EE1299 /* ArrayTests.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; name = ArrayTests.js; path = tests/ArrayTests.js; sourceTree = SOURCE_ROOT; }; /* End PBXFileReference section */ @@ -234,6 +238,8 @@ 02601F021BA0F0C4007C91FF /* external_commit_helper.hpp */, 02601F051BA0F0CD007C91FF /* index_set.cpp */, 02601F061BA0F0CD007C91FF /* index_set.hpp */, + 02C0864C1BCDB27000942F9C /* list.cpp */, + 02C0864D1BCDB27000942F9C /* list.hpp */, 02601F0B1BA0F3A7007C91FF /* schema.cpp */, 02601F0C1BA0F3A7007C91FF /* schema.hpp */, 02601F0F1BA10228007C91FF /* transact_log_handler.cpp */, @@ -353,6 +359,7 @@ 02601F041BA0F0C4007C91FF /* external_commit_helper.hpp in Headers */, 02601F091BA0F0CD007C91FF /* index_set.hpp in Headers */, 0270BC6D1B7CFC1C00010E03 /* property.hpp in Headers */, + 02C0864F1BCDB27000942F9C /* list.hpp in Headers */, 0270BC6F1B7CFC1C00010E03 /* results.hpp in Headers */, ); runOnlyForDeploymentPostprocessing = 0; @@ -563,6 +570,7 @@ 02601F111BA10228007C91FF /* transact_log_handler.cpp in Sources */, 0270BC591B7CFC0D00010E03 /* RJSUtil.mm in Sources */, 0270BC551B7CFC0D00010E03 /* RJSResults.mm in Sources */, + 02C0864E1BCDB27000942F9C /* list.cpp in Sources */, 02601F031BA0F0C4007C91FF /* external_commit_helper.cpp in Sources */, 0270BC6B1B7CFC1C00010E03 /* object_store.cpp in Sources */, 0270BC701B7CFC1C00010E03 /* shared_realm.cpp in Sources */, diff --git a/src/RJSArray.cpp b/src/RJSArray.cpp index b96f70ba..94b5dec4 100644 --- a/src/RJSArray.cpp +++ b/src/RJSArray.cpp @@ -24,34 +24,6 @@ using RJSAccessor = realm::NativeAccessor; using namespace realm; -size_t List::size() { - return link_view->size(); -} - -Row List::get(std::size_t row_ndx) { - verify_valid_row(row_ndx); - return link_view->get(row_ndx); -} - -void List::set(std::size_t row_ndx, std::size_t target_row_ndx) { - verify_valid_row(row_ndx); - link_view->set(row_ndx, target_row_ndx); -} - -void List::verify_valid_row(std::size_t row_ndx) { - size_t size = link_view->size(); - if (row_ndx >= size) { - throw std::out_of_range(std::string("Index ") + std::to_string(row_ndx) + " is outside of range 0..." + std::to_string(size) + "."); - } -} - -void List::verify_attached() { - if (!link_view->is_attached()) { - throw std::runtime_error("Tableview is not attached"); - } - link_view->sync_if_needed(); -} - static inline List * RJSVerifiedArray(JSObjectRef object) { List *list = RJSGetInternal(object); list->verify_attached(); diff --git a/src/RJSArray.hpp b/src/RJSArray.hpp index bafbb3b5..0cb70e36 100644 --- a/src/RJSArray.hpp +++ b/src/RJSArray.hpp @@ -18,23 +18,7 @@ #import "RJSUtil.hpp" #import "shared_realm.hpp" -#import - -namespace realm { - struct List { - List(SharedRealm &r, ObjectSchema &s, LinkViewRef l) : realm(r), object_schema(s), link_view(l) {} - // FIXME - all should be const - SharedRealm realm; - ObjectSchema &object_schema; - LinkViewRef link_view; - - size_t size(); - Row get(std::size_t row_ndx); - void set(std::size_t row_ndx, std::size_t target_row_ndx); - void verify_valid_row(std::size_t row_ndx); - void verify_attached(); - }; -} +#import "list.hpp" extern const JSStaticFunction RJSArrayFuncs[]; JSClassRef RJSArrayClass(); diff --git a/src/object-store/list.cpp b/src/object-store/list.cpp new file mode 100644 index 00000000..d4b2bb57 --- /dev/null +++ b/src/object-store/list.cpp @@ -0,0 +1,50 @@ +//////////////////////////////////////////////////////////////////////////// +// +// Copyright 2015 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 "list.hpp" +#import + +using namespace realm; + +size_t List::size() { + return link_view->size(); +} + +Row List::get(std::size_t row_ndx) { + verify_valid_row(row_ndx); + return link_view->get(row_ndx); +} + +void List::set(std::size_t row_ndx, std::size_t target_row_ndx) { + verify_valid_row(row_ndx); + link_view->set(row_ndx, target_row_ndx); +} + +void List::verify_valid_row(std::size_t row_ndx) { + size_t size = link_view->size(); + if (row_ndx >= size) { + throw std::out_of_range(std::string("Index ") + std::to_string(row_ndx) + " is outside of range 0..." + std::to_string(size) + "."); + } +} + +void List::verify_attached() { + if (!link_view->is_attached()) { + throw std::runtime_error("Tableview is not attached"); + } + link_view->sync_if_needed(); +} diff --git a/src/object-store/list.hpp b/src/object-store/list.hpp new file mode 100644 index 00000000..a1fa1dde --- /dev/null +++ b/src/object-store/list.hpp @@ -0,0 +1,42 @@ +//////////////////////////////////////////////////////////////////////////// +// +// Copyright 2015 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. +// +//////////////////////////////////////////////////////////////////////////// + +#ifndef REALM_LIST_HPP +#define REALM_LIST_HPP + +#import "shared_realm.hpp" +#import + +namespace realm { + struct List { + List(SharedRealm &r, ObjectSchema &s, LinkViewRef l) : realm(r), object_schema(s), link_view(l) {} + // FIXME - all should be const + SharedRealm realm; + ObjectSchema &object_schema; + LinkViewRef link_view; + + size_t size(); + Row get(std::size_t row_ndx); + void set(std::size_t row_ndx, std::size_t target_row_ndx); + void verify_valid_row(std::size_t row_ndx); + void verify_attached(); + }; +} + + +#endif /* REALM_LIST_HPP */