From dbac77f69b70b780f0521c26dd57a6515d37a8b0 Mon Sep 17 00:00:00 2001 From: Thomas Goyne Date: Fri, 4 Sep 2015 09:40:29 -0700 Subject: [PATCH] Make a bunch of things const --- object_schema.cpp | 7 +++---- object_schema.hpp | 5 ++++- object_store.cpp | 31 ++++++++++++++++++------------- object_store.hpp | 44 +++++++++++++++++++++++--------------------- shared_realm.cpp | 2 +- shared_realm.hpp | 6 +++--- 6 files changed, 52 insertions(+), 43 deletions(-) diff --git a/object_schema.cpp b/object_schema.cpp index cfb65f5d..33cd2497 100644 --- a/object_schema.cpp +++ b/object_schema.cpp @@ -28,9 +28,8 @@ using namespace realm; ObjectSchema::~ObjectSchema() = default; -ObjectSchema::ObjectSchema(Group *group, const std::string &name) : name(name) { - TableRef tableRef = ObjectStore::table_for_object_type(group, name); - Table *table = tableRef.get(); +ObjectSchema::ObjectSchema(const Group *group, const std::string &name) : name(name) { + ConstTableRef table = ObjectStore::table_for_object_type(group, name); size_t count = table->get_column_count(); properties.reserve(count); @@ -44,7 +43,7 @@ ObjectSchema::ObjectSchema(Group *group, const std::string &name) : name(name) { property.table_column = col; if (property.type == PropertyTypeObject || property.type == PropertyTypeArray) { // set link type for objects and arrays - realm::TableRef linkTable = table->get_link_target(col); + ConstTableRef linkTable = table->get_link_target(col); property.object_type = ObjectStore::object_type_for_table_name(linkTable->get_name().data()); } properties.push_back(std::move(property)); diff --git a/object_schema.hpp b/object_schema.hpp index 5b7bf343..aca4af31 100644 --- a/object_schema.hpp +++ b/object_schema.hpp @@ -35,7 +35,7 @@ namespace realm { // create object schema from existing table // if no table is provided it is looked up in the group - ObjectSchema(Group *group, const std::string &name); + ObjectSchema(const Group *group, const std::string &name); std::string name; std::vector properties; @@ -46,6 +46,9 @@ namespace realm { Property *primary_key_property() { return property_for_name(primary_key); } + const Property *primary_key_property() const { + return property_for_name(primary_key); + } }; } diff --git a/object_store.cpp b/object_store.cpp index 05682adf..9e31a1c7 100644 --- a/object_store.cpp +++ b/object_store.cpp @@ -45,7 +45,7 @@ const size_t c_object_table_prefix_length = c_object_table_prefix.length(); const uint64_t ObjectStore::NotVersioned = std::numeric_limits::max(); -bool ObjectStore::has_metadata_tables(Group *group) { +bool ObjectStore::has_metadata_tables(const Group *group) { return group->get_table(c_primaryKeyTableName) && group->get_table(c_metadataTableName); } @@ -71,8 +71,8 @@ bool ObjectStore::create_metadata_tables(Group *group) { return changed; } -uint64_t ObjectStore::get_schema_version(Group *group) { - TableRef table = group->get_table(c_metadataTableName); +uint64_t ObjectStore::get_schema_version(const Group *group) { + ConstTableRef table = group->get_table(c_metadataTableName); if (!table || table->get_column_count() == 0) { return ObjectStore::NotVersioned; } @@ -84,8 +84,8 @@ void ObjectStore::set_schema_version(Group *group, uint64_t version) { table->set_int(c_versionColumnIndex, c_zeroRowIndex, version); } -StringData ObjectStore::get_primary_key_for_object(Group *group, StringData object_type) { - TableRef table = group->get_table(c_primaryKeyTableName); +StringData ObjectStore::get_primary_key_for_object(const Group *group, StringData object_type) { + ConstTableRef table = group->get_table(c_primaryKeyTableName); if (!table) { return ""; } @@ -132,6 +132,10 @@ TableRef ObjectStore::table_for_object_type(Group *group, StringData object_type return group->get_table(table_name_for_object_type(object_type)); } +ConstTableRef ObjectStore::table_for_object_type(const Group *group, StringData object_type) { + return group->get_table(table_name_for_object_type(object_type)); +} + TableRef ObjectStore::table_for_object_type_create_if_needed(Group *group, const StringData &object_type, bool &created) { return group->get_or_add_table(table_name_for_object_type(object_type), &created); } @@ -147,7 +151,7 @@ static bool compare_by_name(ObjectSchema const& lft, ObjectSchema const& rgt) { return lft.name < rgt.name; } -void ObjectStore::verify_schema(Schema const& actual_schema, Schema &target_schema, bool allow_missing_tables) { +void ObjectStore::verify_schema(Schema const& actual_schema, Schema& target_schema, bool allow_missing_tables) { std::sort(begin(target_schema), end(target_schema), compare_by_name); std::vector errors; @@ -169,7 +173,9 @@ void ObjectStore::verify_schema(Schema const& actual_schema, Schema &target_sche } } -std::vector ObjectStore::verify_object_schema(ObjectSchema const& table_schema, ObjectSchema &target_schema, Schema &schema) { +std::vector ObjectStore::verify_object_schema(ObjectSchema const& table_schema, + ObjectSchema& target_schema, + Schema const& schema) { std::vector exceptions; ObjectSchema cmp; @@ -334,7 +340,7 @@ bool ObjectStore::create_tables(Group *group, Schema &target_schema, bool update return changed; } -bool ObjectStore::is_schema_at_version(Group *group, uint64_t version) { +bool ObjectStore::is_schema_at_version(const Group *group, uint64_t version) { uint64_t old_version = get_schema_version(group); if (old_version > version && old_version != NotVersioned) { throw InvalidSchemaVersionException(old_version, version); @@ -342,7 +348,7 @@ bool ObjectStore::is_schema_at_version(Group *group, uint64_t version) { return old_version == version; } -bool ObjectStore::needs_update(Schema const& old_schema, Schema& schema) { +bool ObjectStore::needs_update(Schema const& old_schema, Schema const& schema) { for (auto const& target_schema : schema) { auto matching_schema = std::lower_bound(begin(old_schema), end(old_schema), target_schema, compare_by_name); if (matching_schema == end(old_schema) || matching_schema->name != target_schema.name) { @@ -401,7 +407,7 @@ bool ObjectStore::update_realm_with_schema(Group *group, Schema const& old_schem return true; } -Schema ObjectStore::schema_from_group(Group *group) { +Schema ObjectStore::schema_from_group(const Group *group) { Schema schema; for (size_t i = 0; i < group->size(); i++) { std::string object_type = object_type_for_table_name(group->get_table_name(i)); @@ -443,14 +449,14 @@ bool ObjectStore::update_indexes(Group *group, Schema &schema) { return changed; } -void ObjectStore::validate_primary_column_uniqueness(Group *group, Schema &schema) { +void ObjectStore::validate_primary_column_uniqueness(const Group *group, Schema const& schema) { for (auto& object_schema : schema) { auto primary_prop = object_schema.primary_key_property(); if (!primary_prop) { continue; } - TableRef table = table_for_object_type(group, object_schema.name); + ConstTableRef table = table_for_object_type(group, object_schema.name); if (table->get_distinct_view(primary_prop->table_column).size() != table->size()) { throw DuplicatePrimaryKeyValueException(object_schema.name, *primary_prop); } @@ -567,4 +573,3 @@ DuplicatePrimaryKeysException::DuplicatePrimaryKeysException(std::string object_ { m_what = "Duplicate primary keys for object '" + object_type + "'."; } - diff --git a/object_store.hpp b/object_store.hpp index 34169ccd..89a7108e 100644 --- a/object_store.hpp +++ b/object_store.hpp @@ -37,19 +37,19 @@ namespace realm { static const uint64_t NotVersioned; // get the last set schema version - static uint64_t get_schema_version(Group *group); + static uint64_t get_schema_version(const Group *group); // checks if the schema in the group is at the given version - static bool is_schema_at_version(realm::Group *group, uint64_t version); + static bool is_schema_at_version(const Group *group, uint64_t version); // verify that schema from a group and a target schema are compatible // updates the column mapping on all ObjectSchema properties of the target schema // throws if the schema is invalid or does not match - static void verify_schema(Schema const& actual_schema, Schema &target_schema, bool allow_missing_tables = false); + static void verify_schema(Schema const& actual_schema, Schema& target_schema, bool allow_missing_tables = false); // determines if a realm with the given old schema needs non-migration // changes to make it compatible with the given target schema - static bool needs_update(Schema const& old_schema, Schema& schema); + static bool needs_update(Schema const& old_schema, Schema const& schema); // updates a Realm from old_schema to the given target schema, creating and updating tables as needed // returns if any changes were made @@ -62,9 +62,10 @@ namespace realm { // get a table for an object type static realm::TableRef table_for_object_type(Group *group, StringData object_type); + static realm::ConstTableRef table_for_object_type(const Group *group, StringData object_type); // get existing Schema from a group - static Schema schema_from_group(Group *group); + static Schema schema_from_group(const Group *group); // deletes the table for the given type static void delete_data_for_object(Group *group, const StringData &object_type); @@ -74,7 +75,7 @@ namespace realm { static void set_schema_version(Group *group, uint64_t version); // check if the realm already has all metadata tables - static bool has_metadata_tables(Group *group); + static bool has_metadata_tables(const Group *group); // create any metadata tables that don't already exist // must be in write transaction to set @@ -88,10 +89,12 @@ namespace realm { // verify a target schema against an expected schema, setting the table_column property on each schema object // updates the column mapping on the target_schema // returns array of validation errors - static std::vector verify_object_schema(ObjectSchema const& expected, ObjectSchema &target_schema, Schema &schema); + static std::vector verify_object_schema(ObjectSchema const& expected, + ObjectSchema &target_schema, + Schema const& schema); // get primary key property name for object type - static StringData get_primary_key_for_object(Group *group, StringData object_type); + static StringData get_primary_key_for_object(const Group *group, StringData object_type); // sets primary key property for object type // must be in write transaction to set @@ -105,7 +108,7 @@ namespace realm { static bool update_indexes(Group *group, Schema &schema); // validates that all primary key properties have unique values - static void validate_primary_column_uniqueness(Group *group, Schema &schema); + static void validate_primary_column_uniqueness(const Group *group, Schema const& schema); friend ObjectSchema; }; @@ -126,8 +129,8 @@ namespace realm { class InvalidSchemaVersionException : public MigrationException { public: InvalidSchemaVersionException(uint64_t old_version, uint64_t new_version); - uint64_t old_version() { return m_old_version; } - uint64_t new_version() { return m_new_version; } + uint64_t old_version() const { return m_old_version; } + uint64_t new_version() const { return m_new_version; } private: uint64_t m_old_version, m_new_version; }; @@ -135,8 +138,8 @@ namespace realm { class DuplicatePrimaryKeyValueException : public MigrationException { public: DuplicatePrimaryKeyValueException(std::string object_type, Property const& property); - std::string object_type() { return m_object_type; } - Property const& property() { return m_property; } + std::string object_type() const { return m_object_type; } + Property const& property() const { return m_property; } private: std::string m_object_type; Property m_property; @@ -156,7 +159,7 @@ namespace realm { ObjectSchemaValidationException(std::string object_type) : m_object_type(object_type) {} ObjectSchemaValidationException(std::string object_type, std::string message) : m_object_type(object_type) { m_what = message; } - std::string object_type() { return m_object_type; } + std::string object_type() const { return m_object_type; } protected: std::string m_object_type; }; @@ -165,7 +168,7 @@ namespace realm { public: ObjectSchemaPropertyException(std::string object_type, Property const& property) : ObjectSchemaValidationException(object_type), m_property(property) {} - Property const& property() { return m_property; } + Property const& property() const { return m_property; } private: Property m_property; }; @@ -203,8 +206,8 @@ namespace realm { class MismatchedPropertiesException : public ObjectSchemaValidationException { public: MismatchedPropertiesException(std::string object_type, Property const& old_property, Property const& new_property); - Property const& old_property() { return m_old_property; } - Property const& new_property() { return m_new_property; } + Property const& old_property() const { return m_old_property; } + Property const& new_property() const { return m_new_property; } private: Property m_old_property, m_new_property; }; @@ -212,8 +215,8 @@ namespace realm { class ChangedPrimaryKeyException : public ObjectSchemaValidationException { public: ChangedPrimaryKeyException(std::string object_type, std::string old_primary, std::string new_primary); - std::string old_primary() { return m_old_primary; } - std::string new_primary() { return m_new_primary; } + std::string old_primary() const { return m_old_primary; } + std::string new_primary() const { return m_new_primary; } private: std::string m_old_primary, m_new_primary; }; @@ -221,11 +224,10 @@ namespace realm { class InvalidPrimaryKeyException : public ObjectSchemaValidationException { public: InvalidPrimaryKeyException(std::string object_type, std::string primary_key); - std::string primary_key() { return m_primary_key; } + std::string primary_key() const { return m_primary_key; } private: std::string m_primary_key; }; } #endif /* defined(REALM_OBJECT_STORE_HPP) */ - diff --git a/shared_realm.cpp b/shared_realm.cpp index 8b1abd84..68972e4d 100644 --- a/shared_realm.cpp +++ b/shared_realm.cpp @@ -221,7 +221,7 @@ static void check_read_write(Realm *realm) } } -void Realm::verify_thread() +void Realm::verify_thread() const { if (m_thread_id != std::this_thread::get_id()) { throw IncorrectThreadException("Realm accessed from incorrect thread."); diff --git a/shared_realm.hpp b/shared_realm.hpp index 3836c5e9..e1c1a0c6 100644 --- a/shared_realm.hpp +++ b/shared_realm.hpp @@ -84,18 +84,18 @@ namespace realm { void begin_transaction(); void commit_transaction(); void cancel_transaction(); - bool is_in_transaction() { return m_in_transaction; } + bool is_in_transaction() const { return m_in_transaction; } bool refresh(); void set_auto_refresh(bool auto_refresh) { m_auto_refresh = auto_refresh; } - bool auto_refresh() { return m_auto_refresh; } + bool auto_refresh() const { return m_auto_refresh; } void notify(); void invalidate(); bool compact(); std::thread::id thread_id() const { return m_thread_id; } - void verify_thread(); + void verify_thread() const; ~Realm();