1 ////////////////////////////////////////////////////////////////////////////
3 // Copyright 2017 Realm Inc.
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
17 ////////////////////////////////////////////////////////////////////////////
21 #include "impl/object_notifier.hpp"
22 #include "impl/realm_coordinator.hpp"
23 #include "object_schema.hpp"
24 #include "object_store.hpp"
26 using namespace realm;
28 InvalidatedObjectException::InvalidatedObjectException(const std::string& object_type)
29 : std::logic_error("Accessing object of type " + object_type + " which has been invalidated or deleted")
30 , object_type(object_type)
33 InvalidPropertyException::InvalidPropertyException(const std::string& object_type, const std::string& property_name)
34 : std::logic_error(util::format("Property '%1.%2' does not exist", object_type, property_name))
35 , object_type(object_type), property_name(property_name)
38 MissingPropertyValueException::MissingPropertyValueException(const std::string& object_type, const std::string& property_name)
39 : std::logic_error(util::format("Missing value for property '%1.%2'", object_type, property_name))
40 , object_type(object_type), property_name(property_name)
43 MissingPrimaryKeyException::MissingPrimaryKeyException(const std::string& object_type)
44 : std::logic_error(util::format("'%1' does not have a primary key defined", object_type))
45 , object_type(object_type)
48 ReadOnlyPropertyException::ReadOnlyPropertyException(const std::string& object_type, const std::string& property_name)
49 : std::logic_error(util::format("Cannot modify read-only property '%1.%2'", object_type, property_name))
50 , object_type(object_type), property_name(property_name) {}
52 Object::Object(SharedRealm r, ObjectSchema const& s, RowExpr const& o)
53 : m_realm(std::move(r)), m_object_schema(&s), m_row(o) { }
55 Object::Object(SharedRealm r, StringData object_type, size_t ndx)
56 : m_realm(std::move(r))
57 , m_object_schema(&*m_realm->schema().find(object_type))
58 , m_row(ObjectStore::table_for_object_type(m_realm->read_group(), object_type)->get(ndx))
61 Object::Object() = default;
62 Object::~Object() = default;
63 Object::Object(Object const&) = default;
64 Object::Object(Object&&) = default;
65 Object& Object::operator=(Object const&) = default;
66 Object& Object::operator=(Object&&) = default;
68 NotificationToken Object::add_notification_callback(CollectionChangeCallback callback) &
72 m_notifier = std::make_shared<_impl::ObjectNotifier>(m_row, m_realm);
73 _impl::RealmCoordinator::register_notifier(m_notifier);
75 return {m_notifier, m_notifier->add_callback(std::move(callback))};
78 void Object::verify_attached() const
80 m_realm->verify_thread();
81 if (!m_row.is_attached()) {
82 throw InvalidatedObjectException(m_object_schema->name);
86 Property const& Object::property_for_name(StringData prop_name) const
88 auto prop = m_object_schema->property_for_name(prop_name);
90 throw InvalidPropertyException(m_object_schema->name, prop_name);