api to check if object has been deleted

This commit is contained in:
Ari Lazier 2016-04-19 14:41:33 -07:00
parent 7ceac728eb
commit 6072061463
2 changed files with 36 additions and 0 deletions

View File

@ -138,6 +138,7 @@ class Realm {
static void create(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &); static void create(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &);
static void delete_one(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &); static void delete_one(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &);
static void delete_all(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &); static void delete_all(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &);
static void is_object_valid(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &);
static void write(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &); static void write(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &);
static void add_listener(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &); static void add_listener(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &);
static void remove_listener(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &); static void remove_listener(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &);
@ -212,6 +213,7 @@ struct RealmClass : ClassDefinition<T, SharedRealm> {
{"create", wrap<Realm::create>}, {"create", wrap<Realm::create>},
{"delete", wrap<Realm::delete_one>}, {"delete", wrap<Realm::delete_one>},
{"deleteAll", wrap<Realm::delete_all>}, {"deleteAll", wrap<Realm::delete_all>},
{"isValid", wrap<Realm::is_object_valid>},
{"write", wrap<Realm::write>}, {"write", wrap<Realm::write>},
{"addListener", wrap<Realm::add_listener>}, {"addListener", wrap<Realm::add_listener>},
{"removeListener", wrap<Realm::remove_listener>}, {"removeListener", wrap<Realm::remove_listener>},
@ -454,6 +456,18 @@ void Realm<T>::delete_all(ContextType ctx, ObjectType this_object, size_t argc,
ObjectStore::table_for_object_type(realm->read_group(), objectSchema.name)->clear(); ObjectStore::table_for_object_type(realm->read_group(), objectSchema.name)->clear();
} }
} }
template<typename T>
void Realm<T>::is_object_valid(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
validate_argument_count(argc, 0);
ObjectType arg = Value::validated_to_object(ctx, arguments[0]);
if (!Object::template is_instance<RealmObjectClass<T>>(ctx, arg)) {
throw std::runtime_error("Argument to Realm.isValid must be a 'RealmObject'");
}
auto object = get_internal<T, RealmObjectClass<T>>(arg);
return_value.set(object->is_valid());
}
template<typename T> template<typename T>
void Realm<T>::write(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) { void Realm<T>::write(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {

View File

@ -47,6 +47,8 @@ namespace realm {
SharedRealm realm() { return m_realm; } SharedRealm realm() { return m_realm; }
const ObjectSchema &get_object_schema() { return *m_object_schema; } const ObjectSchema &get_object_schema() { return *m_object_schema; }
Row row() { return m_row; } Row row() { return m_row; }
bool is_valid() { return m_row.is_attached(); }
private: private:
SharedRealm m_realm; SharedRealm m_realm;
@ -57,6 +59,8 @@ namespace realm {
inline void set_property_value_impl(ContextType ctx, const Property &property, ValueType value, bool try_update); inline void set_property_value_impl(ContextType ctx, const Property &property, ValueType value, bool try_update);
template<typename ValueType, typename ContextType> template<typename ValueType, typename ContextType>
inline ValueType get_property_value_impl(ContextType ctx, const Property &property); inline ValueType get_property_value_impl(ContextType ctx, const Property &property);
inline void verify_attached();
}; };
// //
@ -109,6 +113,13 @@ namespace realm {
static Mixed to_mixed(ContextType ctx, ValueType &val) { throw std::runtime_error("'Any' type is unsupported"); } static Mixed to_mixed(ContextType ctx, ValueType &val) { throw std::runtime_error("'Any' type is unsupported"); }
}; };
class InvalidatedObjectException : public std::runtime_error
{
public:
InvalidatedObjectException(const std::string object_type, const std::string message) : std::runtime_error(message), object_type(object_type) {}
const std::string object_type;
};
class InvalidPropertyException : public std::runtime_error class InvalidPropertyException : public std::runtime_error
{ {
public: public:
@ -137,6 +148,8 @@ namespace realm {
template <typename ValueType, typename ContextType> template <typename ValueType, typename ContextType>
inline void Object::set_property_value(ContextType ctx, std::string prop_name, ValueType value, bool try_update) inline void Object::set_property_value(ContextType ctx, std::string prop_name, ValueType value, bool try_update)
{ {
verify_attached();
const Property *prop = m_object_schema->property_for_name(prop_name); const Property *prop = m_object_schema->property_for_name(prop_name);
if (!prop) { if (!prop) {
throw InvalidPropertyException(m_object_schema->name, prop_name, throw InvalidPropertyException(m_object_schema->name, prop_name,
@ -148,6 +161,8 @@ namespace realm {
template <typename ValueType, typename ContextType> template <typename ValueType, typename ContextType>
inline ValueType Object::get_property_value(ContextType ctx, std::string prop_name) inline ValueType Object::get_property_value(ContextType ctx, std::string prop_name)
{ {
verify_attached();
const Property *prop = m_object_schema->property_for_name(prop_name); const Property *prop = m_object_schema->property_for_name(prop_name);
if (!prop) { if (!prop) {
throw InvalidPropertyException(m_object_schema->name, prop_name, throw InvalidPropertyException(m_object_schema->name, prop_name,
@ -324,6 +339,13 @@ namespace realm {
} }
return object; return object;
} }
inline void Object::verify_attached() {
if (!m_row.is_attached()) {
throw InvalidatedObjectException("Accessing object of type " + m_object_schema->name + " which has been deleted",
m_object_schema->name);
}
}
// //
// List implementation // List implementation