added iOS source code
[wl-app.git] / iOS / Pods / Realm / Realm / ObjectStore / src / object.cpp
1 ////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright 2017 Realm Inc.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17 ////////////////////////////////////////////////////////////////////////////
18
19 #include "object.hpp"
20
21 #include "impl/object_notifier.hpp"
22 #include "impl/realm_coordinator.hpp"
23 #include "object_schema.hpp"
24 #include "object_store.hpp"
25
26 using namespace realm;
27
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)
31 {}
32
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)
36 {}
37
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)
41 {}
42
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)
46 {}
47
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) {}
51
52 Object::Object(SharedRealm r, ObjectSchema const& s, RowExpr const& o)
53 : m_realm(std::move(r)), m_object_schema(&s), m_row(o) { }
54
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))
59 { }
60
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;
67
68 NotificationToken Object::add_notification_callback(CollectionChangeCallback callback) &
69 {
70     verify_attached();
71     if (!m_notifier) {
72         m_notifier = std::make_shared<_impl::ObjectNotifier>(m_row, m_realm);
73         _impl::RealmCoordinator::register_notifier(m_notifier);
74     }
75     return {m_notifier, m_notifier->add_callback(std::move(callback))};
76 }
77
78 void Object::verify_attached() const
79 {
80     m_realm->verify_thread();
81     if (!m_row.is_attached()) {
82         throw InvalidatedObjectException(m_object_schema->name);
83     }
84 }
85
86 Property const& Object::property_for_name(StringData prop_name) const
87 {
88     auto prop = m_object_schema->property_for_name(prop_name);
89     if (!prop) {
90         throw InvalidPropertyException(m_object_schema->name, prop_name);
91     }
92     return *prop;
93 }