Skip to content

Commit

Permalink
Try fix static initialisation on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
leemaguire committed Jun 26, 2024
1 parent 9170fe1 commit 966300f
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 11 deletions.
8 changes: 2 additions & 6 deletions include/cpprealm/db.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,13 @@ namespace realm {

template <typename T>
struct thread_safe_reference;
}

namespace realm {

struct db {
static inline std::vector<internal::bridge::object_schema> 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);
}

Expand Down Expand Up @@ -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<internal::bridge::object_schema> schema;
(schema.push_back(managed<Ts>::schema.to_core_schema()), ...);
Expand Down
5 changes: 1 addition & 4 deletions include/cpprealm/macros.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -591,10 +591,7 @@ rbool managed<std::optional<type>>::operator op(const std::optional<type>& rhs)
}; \
struct meta_schema_##cls { \
meta_schema_##cls() { \
auto s = managed<cls>::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<cls>::schema.to_core_schema()); \
} \
}; \
static inline meta_schema_##cls _meta_schema_##cls{};
Expand Down
10 changes: 9 additions & 1 deletion include/cpprealm/schema.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<internal::bridge::object_schema> registered_schemas(const std::optional<internal::bridge::object_schema>& schema = std::nullopt);

template <auto Ptr, bool IsPrimaryKey = false>
struct property {
using Result = typename internal::persisted_type_extractor<typename internal::ptr_type_extractor<Ptr>::member_type>::Result;
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions src/cpprealm/schema.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <cpprealm/schema.hpp>

namespace realm {
namespace schemagen {

std::mutex schema_mtx;

std::vector<internal::bridge::object_schema> registered_schemas(const std::optional<internal::bridge::object_schema>& schema) {
static std::vector<internal::bridge::object_schema> schemas;
std::lock_guard<std::mutex> 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

0 comments on commit 966300f

Please sign in to comment.