diff --git a/list.cpp b/list.cpp index ff57ef27..1cbf1477 100644 --- a/list.cpp +++ b/list.cpp @@ -1,6 +1,20 @@ -/* Copyright 2015 Realm Inc - All Rights Reserved - * Proprietary and Confidential - */ +//////////////////////////////////////////////////////////////////////////// +// +// 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 @@ -8,22 +22,46 @@ using namespace realm; size_t List::size() { + verify_attached(); return m_link_view->size(); } Row List::get(std::size_t row_ndx) { + verify_attached(); verify_valid_row(row_ndx); return m_link_view->get(row_ndx); } void List::set(std::size_t row_ndx, std::size_t target_row_ndx) { + verify_attached(); + verify_in_tranaction(); verify_valid_row(row_ndx); m_link_view->set(row_ndx, target_row_ndx); } -void List::verify_valid_row(std::size_t row_ndx) { +void List::add(std::size_t target_row_ndx) { + verify_attached(); + verify_in_tranaction(); + m_link_view->add(target_row_ndx); +} + +void List::insert(std::size_t row_ndx, std::size_t target_row_ndx) { + verify_attached(); + verify_in_tranaction(); + verify_valid_row(row_ndx, true); + m_link_view->insert(row_ndx, target_row_ndx); +} + +void List::remove(std::size_t row_ndx) { + verify_attached(); + verify_in_tranaction(); + verify_valid_row(row_ndx); + m_link_view->remove(row_ndx); +} + +void List::verify_valid_row(std::size_t row_ndx, bool insertion) { size_t size = m_link_view->size(); - if (row_ndx >= size) { + if (row_ndx > size || (!insertion && 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) + "."); } } @@ -34,3 +72,9 @@ void List::verify_attached() { } m_link_view->sync_if_needed(); } + +void List::verify_in_tranaction() { + if (!m_realm->is_in_transaction()) { + throw std::runtime_error("Can only mutate a list within a transaction."); + } +} diff --git a/list.hpp b/list.hpp index 18ef44e7..54bbed41 100644 --- a/list.hpp +++ b/list.hpp @@ -1,6 +1,20 @@ -/* Copyright 2015 Realm Inc - All Rights Reserved - * Proprietary and Confidential - */ +//////////////////////////////////////////////////////////////////////////// +// +// 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 @@ -15,14 +29,27 @@ namespace realm { const ObjectSchema &object_schema; SharedRealm realm() { return m_realm; } - LinkViewRef link_view() { return m_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 add(size_t target_row_ndx); + void remove(size_t list_ndx); + void insert(size_t list_ndx, size_t target_row_ndx); + + template + void add(ContextType ctx, ValueType value); + + template + void insert(ContextType ctx, ValueType value, size_t list_ndx); + + template + void set(ContextType ctx, ValueType value, size_t list_ndx); + + void verify_valid_row(std::size_t row_ndx, bool insertion = false); void verify_attached(); + void verify_in_tranaction(); private: SharedRealm m_realm; @@ -31,4 +58,5 @@ namespace realm { } + #endif /* REALM_LIST_HPP */ diff --git a/object_accessor.hpp b/object_accessor.hpp index d1e78b80..5cc7a358 100644 --- a/object_accessor.hpp +++ b/object_accessor.hpp @@ -297,6 +297,27 @@ namespace realm { } return object; } + + // + // List implementation + // + template + void List::add(ContextType ctx, ValueType value) + { + 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)); + } + + 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)); + } } #endif /* defined(REALM_OBJECT_ACCESSOR_HPP) */ diff --git a/shared_realm.cpp b/shared_realm.cpp index d95b8bd2..9c0a4e1e 100644 --- a/shared_realm.cpp +++ b/shared_realm.cpp @@ -382,10 +382,6 @@ void Realm::close() m_notifier->remove_realm(this); } - if (m_group) { - m_shared_group->end_read(); - } - m_group = nullptr; m_shared_group = nullptr; m_history = nullptr; diff --git a/shared_realm.hpp b/shared_realm.hpp index 09685128..81b14bf7 100644 --- a/shared_realm.hpp +++ b/shared_realm.hpp @@ -197,12 +197,6 @@ namespace realm { public: UnitializedRealmException(std::string message) : std::runtime_error(message) {} }; - - class ClosedRealmException : public std::runtime_error - { - public: - ClosedRealmException(std::string message) : std::runtime_error(message) {} - }; } #endif /* defined(REALM_REALM_HPP) */