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 ////////////////////////////////////////////////////////////////////////////
19 #ifndef REALM_OS_OBJECT_HPP
20 #define REALM_OS_OBJECT_HPP
22 #include "impl/collection_notifier.hpp"
24 #include <realm/row.hpp>
29 using RowExpr = BasicRowExpr<Table>;
38 Object(std::shared_ptr<Realm> r, ObjectSchema const& s, RowExpr const& o);
39 Object(std::shared_ptr<Realm> r, StringData object_type, size_t ndx);
41 Object(Object const&);
43 Object& operator=(Object const&);
44 Object& operator=(Object&&);
48 std::shared_ptr<Realm> const& realm() const { return m_realm; }
49 ObjectSchema const& get_object_schema() const { return *m_object_schema; }
50 RowExpr row() const { return m_row; }
52 bool is_valid() const { return m_row.is_attached(); }
54 NotificationToken add_notification_callback(CollectionChangeCallback callback) &;
56 // The following functions require an accessor context which converts from
57 // the binding's native data types to the core data types. See CppContext
58 // for a reference implementation of such a context.
60 // The actual definitions of these tempated functions is in object_accessor.hpp
62 // property getter/setter
63 template<typename ValueType, typename ContextType>
64 void set_property_value(ContextType& ctx, StringData prop_name,
65 ValueType value, bool try_update);
67 template<typename ValueType, typename ContextType>
68 ValueType get_property_value(ContextType& ctx, StringData prop_name);
70 // create an Object from a native representation
71 template<typename ValueType, typename ContextType>
72 static Object create(ContextType& ctx, std::shared_ptr<Realm> const& realm,
73 const ObjectSchema &object_schema, ValueType value,
74 bool try_update = false, Row* = nullptr);
76 template<typename ValueType, typename ContextType>
77 static Object create(ContextType& ctx, std::shared_ptr<Realm> const& realm,
78 StringData object_type, ValueType value,
79 bool try_update = false, Row* = nullptr);
81 template<typename ValueType, typename ContextType>
82 static Object get_for_primary_key(ContextType& ctx,
83 std::shared_ptr<Realm> const& realm,
84 const ObjectSchema &object_schema,
85 ValueType primary_value);
87 template<typename ValueType, typename ContextType>
88 static Object get_for_primary_key(ContextType& ctx,
89 std::shared_ptr<Realm> const& realm,
90 StringData object_type,
91 ValueType primary_value);
94 std::shared_ptr<Realm> m_realm;
95 const ObjectSchema *m_object_schema;
97 _impl::CollectionNotifier::Handle<_impl::ObjectNotifier> m_notifier;
100 template<typename ValueType, typename ContextType>
101 void set_property_value_impl(ContextType& ctx, const Property &property,
102 ValueType value, bool try_update, bool is_default=false);
103 template<typename ValueType, typename ContextType>
104 ValueType get_property_value_impl(ContextType& ctx, const Property &property);
106 template<typename ValueType, typename ContextType>
107 static size_t get_for_primary_key_impl(ContextType& ctx, Table const& table,
108 const Property &primary_prop, ValueType primary_value);
110 void verify_attached() const;
111 Property const& property_for_name(StringData prop_name) const;
114 struct InvalidatedObjectException : public std::logic_error {
115 InvalidatedObjectException(const std::string& object_type);
116 const std::string object_type;
119 struct InvalidPropertyException : public std::logic_error {
120 InvalidPropertyException(const std::string& object_type, const std::string& property_name);
121 const std::string object_type;
122 const std::string property_name;
125 struct MissingPropertyValueException : public std::logic_error {
126 MissingPropertyValueException(const std::string& object_type, const std::string& property_name);
127 const std::string object_type;
128 const std::string property_name;
131 struct MissingPrimaryKeyException : public std::logic_error {
132 MissingPrimaryKeyException(const std::string& object_type);
133 const std::string object_type;
136 struct ReadOnlyPropertyException : public std::logic_error {
137 ReadOnlyPropertyException(const std::string& object_type, const std::string& property_name);
138 const std::string object_type;
139 const std::string property_name;
143 #endif // REALM_OS_OBJECT_HPP