Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try fix static initialisation on Windows #206

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
21 changes: 21 additions & 0 deletions src/cpprealm/schema.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <cpprealm/schema.hpp>
#include <mutex>

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
Loading