realm-js/list.cpp

82 lines
2.3 KiB
C++
Raw Normal View History

////////////////////////////////////////////////////////////////////////////
//
// 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.
//
////////////////////////////////////////////////////////////////////////////
2015-10-13 21:44:31 +00:00
#include "list.hpp"
2015-12-03 17:03:31 +00:00
#include <stdexcept>
2015-10-13 21:44:31 +00:00
using namespace realm;
size_t List::size() {
verify_attached();
2015-10-26 20:32:29 +00:00
return m_link_view->size();
2015-10-13 21:44:31 +00:00
}
Row List::get(std::size_t row_ndx) {
verify_attached();
2015-10-13 21:44:31 +00:00
verify_valid_row(row_ndx);
2015-10-26 20:32:29 +00:00
return m_link_view->get(row_ndx);
2015-10-13 21:44:31 +00:00
}
void List::set(std::size_t row_ndx, std::size_t target_row_ndx) {
verify_attached();
verify_in_tranaction();
2015-10-13 21:44:31 +00:00
verify_valid_row(row_ndx);
2015-10-26 20:32:29 +00:00
m_link_view->set(row_ndx, target_row_ndx);
2015-10-13 21:44:31 +00:00
}
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) {
2015-10-26 20:32:29 +00:00
size_t size = m_link_view->size();
if (row_ndx > size || (!insertion && row_ndx == size)) {
2015-10-13 21:44:31 +00:00
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() {
2015-10-26 20:32:29 +00:00
if (!m_link_view->is_attached()) {
2015-10-13 21:44:31 +00:00
throw std::runtime_error("Tableview is not attached");
}
2015-10-26 20:32:29 +00:00
m_link_view->sync_if_needed();
2015-10-13 21:44:31 +00:00
}
void List::verify_in_tranaction() {
if (!m_realm->is_in_transaction()) {
throw std::runtime_error("Can only mutate a list within a transaction.");
}
}