diff --git a/include/cpprealm/db.hpp b/include/cpprealm/db.hpp index 697765d1..0e74ef9b 100644 --- a/include/cpprealm/db.hpp +++ b/include/cpprealm/db.hpp @@ -59,17 +59,13 @@ namespace realm { template struct thread_safe_reference; -} - -namespace realm { struct db { - static inline std::vector schemas; internal::bridge::realm m_realm; explicit db(realm::db_config config) { if (!config.get_schema()) - config.set_schema(db::schemas); + config.set_schema(schemagen::registered_schemas()); m_realm = internal::bridge::realm(config); } @@ -203,7 +199,7 @@ namespace realm { inline db open(const db_config& config) { auto config_copy = config; if constexpr (sizeof...(Ts) == 0) { - config_copy.set_schema(db::schemas); + config_copy.set_schema(schemagen::registered_schemas()); } else { std::vector schema; (schema.push_back(managed::schema.to_core_schema()), ...); diff --git a/include/cpprealm/macros.hpp b/include/cpprealm/macros.hpp index dfb8b603..1aaa5247 100644 --- a/include/cpprealm/macros.hpp +++ b/include/cpprealm/macros.hpp @@ -591,10 +591,7 @@ rbool managed>::operator op(const std::optional& rhs) }; \ struct meta_schema_##cls { \ meta_schema_##cls() { \ - auto s = managed::schema.to_core_schema(); \ - auto it = std::find(std::begin(realm::db::schemas), std::end(realm::db::schemas), s); \ - if (it == std::end(realm::db::schemas)) \ - realm::db::schemas.push_back(s); \ + realm::schemagen::registered_schemas(managed::schema.to_core_schema()); \ } \ }; \ static inline meta_schema_##cls _meta_schema_##cls{}; diff --git a/include/cpprealm/schema.hpp b/include/cpprealm/schema.hpp index 7494d600..1555b121 100644 --- a/include/cpprealm/schema.hpp +++ b/include/cpprealm/schema.hpp @@ -63,9 +63,17 @@ namespace realm { }; } - // MARK: schema namespace schemagen { + /** + * @brief Internal use only, for use with automatic schema discovery. + * + * @param schema Optionally append a new object schema to the + * registered schemas if it does not already exist. + * @return A vector of object schemas + */ + std::vector registered_schemas(const std::optional& schema = std::nullopt); + template struct property { using Result = typename internal::persisted_type_extractor::member_type>::Result; diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2d4c5532..261855f2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -24,6 +24,7 @@ set(SOURCES cpprealm/managed_uuid.cpp cpprealm/types.cpp cpprealm/flex_sync.cpp + cpprealm/schema.cpp cpprealm/internal/bridge/async_open_task.cpp cpprealm/internal/bridge/binary.cpp cpprealm/internal/bridge/col_key.cpp diff --git a/src/cpprealm/schema.cpp b/src/cpprealm/schema.cpp new file mode 100644 index 00000000..5abfb069 --- /dev/null +++ b/src/cpprealm/schema.cpp @@ -0,0 +1,21 @@ +#include +#include + +namespace realm { +namespace schemagen { + + std::mutex schema_mtx; + + std::vector registered_schemas(const std::optional& schema) { + static std::vector schemas; + std::lock_guard lock(schema_mtx); + if (schema) { + auto it = std::find(std::begin(schemas), std::end(schemas), *schema); + if (it == std::end(schemas)) + schemas.push_back(*schema); + } + return schemas; + } + +} // namespace schemagen +} // namespace realm