From 3570481b356b124cb8fac637e1675517f5696128 Mon Sep 17 00:00:00 2001 From: nwesem Date: Fri, 17 Feb 2023 11:40:20 +0100 Subject: [PATCH 1/2] update cpprealm installation and cmake examples Signed-off-by: nwesem --- .gitignore | 5 +++- README.md | 17 +++++++++++++- examples/cmake/CMakeLists.txt | 43 +++++++++++++++++++++++++++++++++-- examples/cmake/example.cpp | 39 +++++++++++++++++++++++++++++++ examples/cmake/helloworld.cpp | 8 +++---- src/CMakeLists.txt | 12 ++++++++-- tests/list_tests.cpp | 2 +- tests/mixed_tests.cpp | 2 +- 8 files changed, 116 insertions(+), 12 deletions(-) create mode 100644 examples/cmake/example.cpp diff --git a/.gitignore b/.gitignore index a8a8ecf5..e2a4dcfd 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,7 @@ Package.resolved # Qt demo apps examples/*.pro.user docs/ - +# other builds +*cmake-build* +#ide +*.idea diff --git a/README.md b/README.md index cebcfe94..c7eadab5 100644 --- a/README.md +++ b/README.md @@ -135,5 +135,20 @@ sudo cmake --build . --target install ``` You can then link to your library with `-lcpprealm`. - + +If you are using cmake you can link `cpprealm` using `find_library()` and `target_link_libraries()` see [examples/cmake/CMakeLists.txt](./examples/cmake/CMakeLists.txt) or the short example below + +```cmake +cmake_minimum_required(VERSION 3.15) +project (cmake-example) +find_library(REALMCPP_LIB NAMES cpprealm) +find_library(REALM_LIB NAMES realm) +add_executable(example example.cpp) +target_link_libraries(example + cpprealm + realm +) +``` +Note that you will probably need other libs to get cpprealm running (Threads, SSL, Curl, ...) + diff --git a/examples/cmake/CMakeLists.txt b/examples/cmake/CMakeLists.txt index 84bf76e6..a39a320b 100644 --- a/examples/cmake/CMakeLists.txt +++ b/examples/cmake/CMakeLists.txt @@ -4,6 +4,45 @@ project (hello) set(CMAKE_CXX_STANDARD 17) set(REALM_CPP_NO_TESTS 1) -add_subdirectory(../../ cmake-build-debug) + +if (USE_REALM_LOCAL) + add_subdirectory(../../ cmake-build-debug) +else() + message(STATUS "You did not define USE_REALM_LOCAL so we are assuming you have previously installed realm and + realmcpp by building it from source and doing a global install as described in the READMEs") +endif() + +set(THREADS_PREFER_PTHREAD_FLAG ON) +find_package(Threads REQUIRED) +find_package(OpenSSL REQUIRED) +find_package(CURL REQUIRED) + +if(NOT USE_REALM_LOCAL) + find_library(REALMCPP_LIB + NAMES cpprealm + ) + find_library(REALM_LIB + NAMES realm + ) + message(STATUS "REALMCPP_LIB = ${REALMCPP_LIB}") + message(STATUS "REALM_LIB = ${REALM_LIB}") +endif() + + add_executable(hello helloworld.cpp) -target_link_libraries(hello cpprealm) +target_link_libraries(hello + cpprealm + realm + Threads::Threads + OpenSSL::SSL + CURL::libcurl +) + +add_executable(example example.cpp) +target_link_libraries(example + cpprealm + realm + Threads::Threads + OpenSSL::SSL + CURL::libcurl +) diff --git a/examples/cmake/example.cpp b/examples/cmake/example.cpp new file mode 100644 index 00000000..3b495a6d --- /dev/null +++ b/examples/cmake/example.cpp @@ -0,0 +1,39 @@ +#include + +struct Dog : realm::object { + realm::persisted name; + realm::persisted age; + static constexpr auto schema = realm::schema("Dog", + realm::property<&Dog::name>("name"), + realm::property<&Dog::age>("age")); +}; +struct Person : realm::object { + realm::persisted _id; + realm::persisted name; + realm::persisted age; + // Create relationships by pointing an Object field to another Class + realm::persisted> dog; + static constexpr auto schema = realm::schema("Person", + realm::property<&Person::_id, true>("_id"), // primary key + realm::property<&Person::name>("name"), + realm::property<&Person::age>("age"), + realm::property<&Person::dog>("dog")); +}; + +void run_realm() { + // Use Realm objects like regular objects. + auto dog = Dog { .name = "Rex", .age = 1 }; + std::cout << "dog: " << dog << "\n"; + // Get the default Realm with compile time schema checking. + auto realm = realm::open(); + // Persist your data in a write transaction + realm.write([&realm, &dog] { + realm.add(dog); + }); +} + +int main() { + run_realm(); + return 0; +} + diff --git a/examples/cmake/helloworld.cpp b/examples/cmake/helloworld.cpp index 95a0c381..6b1cdf89 100644 --- a/examples/cmake/helloworld.cpp +++ b/examples/cmake/helloworld.cpp @@ -2,7 +2,7 @@ struct Foo: realm::object { - realm::persisted _id; + realm::persisted _id; realm::persisted bool_col; realm::persisted str_col; realm::persisted> date_col; @@ -28,11 +28,11 @@ void run_realm() { auto tsr = realm::async_open(flx_sync_config).get_future().get(); auto synced_realm = tsr.resolve(); - auto update_success = synced_realm.subscriptions().update([](realm::MutableSyncSubscriptionSet& subs) { + auto update_success = synced_realm.subscriptions().update([](realm::mutable_sync_subscription_set& subs) { subs.clear(); }).get_future().get(); - update_success = synced_realm.subscriptions().update([](realm::MutableSyncSubscriptionSet& subs) { + update_success = synced_realm.subscriptions().update([](realm::mutable_sync_subscription_set& subs) { subs.add("foo-strings", [](auto& obj) { return obj.str_col != "alex"; // sync all objects where name does not equal 'alex' }); @@ -53,7 +53,7 @@ void run_realm() { synced_realm.write([&synced_realm, &person]() { person.str_col = "sarah"; }); - user.sync_manager().path_for_realm() + // user.sync_manager().path_for_realm(); synced_realm.write([&synced_realm, &person]() { person.str_col = "bob"; diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ead80021..e56b4c29 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -93,8 +93,16 @@ set(HEADERS cpprealm/internal/bridge/object_id.hpp cpprealm/persisted_object_id.hpp) # REALM_INSTALL_HEADERS -add_library(cpprealm STATIC ${SOURCES} ${HEADERS}) - +add_library(cpprealm SHARED + ${SOURCES} +) +target_include_directories(cpprealm PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/cpprealm +) set_target_properties(cpprealm PROPERTIES PUBLIC_HEADER "${HEADERS}") +install(TARGETS cpprealm + ARCHIVE DESTINATION lib +) + install(DIRECTORY cpprealm/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/cpprealm) diff --git a/tests/list_tests.cpp b/tests/list_tests.cpp index 00afc1d6..c2ae36ae 100644 --- a/tests/list_tests.cpp +++ b/tests/list_tests.cpp @@ -442,7 +442,7 @@ TEST_CASE("list", "[list]") { auto realm = realm::open({path}); auto obj = AllTypesObject(); - obj.list_mixed_col.push_back(42); + obj.list_mixed_col.push_back(static_cast(42)); obj.list_mixed_col.push_back(true); obj.list_mixed_col.push_back("hello world"); obj.list_mixed_col.push_back(42.42); diff --git a/tests/mixed_tests.cpp b/tests/mixed_tests.cpp index ac58899b..b9236f82 100644 --- a/tests/mixed_tests.cpp +++ b/tests/mixed_tests.cpp @@ -7,7 +7,7 @@ TEST_CASE("mixed", "[mixed]") { realm_path path; SECTION("unmanaged_managed_mixed_get_set", "[mixed]") { auto obj = AllTypesObject(); - obj.mixed_col = 42; + obj.mixed_col = static_cast(42); CHECK(obj.mixed_col == static_cast(42)); auto realm = realm::open({path}); realm.write([&realm, &obj] { From 3d7323ed21f5f325fdf183f0ebed7ca228b2adff Mon Sep 17 00:00:00 2001 From: nwesem Date: Fri, 17 Feb 2023 11:49:05 +0100 Subject: [PATCH 2/2] update example CMakeLists Signed-off-by: nwesem --- examples/cmake/CMakeLists.txt | 53 ++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/examples/cmake/CMakeLists.txt b/examples/cmake/CMakeLists.txt index a39a320b..85edf32e 100644 --- a/examples/cmake/CMakeLists.txt +++ b/examples/cmake/CMakeLists.txt @@ -7,42 +7,43 @@ set(REALM_CPP_NO_TESTS 1) if (USE_REALM_LOCAL) add_subdirectory(../../ cmake-build-debug) + add_executable(hello helloworld.cpp) + target_link_libraries(hello cpprealm) else() message(STATUS "You did not define USE_REALM_LOCAL so we are assuming you have previously installed realm and realmcpp by building it from source and doing a global install as described in the READMEs") -endif() -set(THREADS_PREFER_PTHREAD_FLAG ON) -find_package(Threads REQUIRED) -find_package(OpenSSL REQUIRED) -find_package(CURL REQUIRED) + set(THREADS_PREFER_PTHREAD_FLAG ON) + find_package(Threads REQUIRED) + find_package(OpenSSL REQUIRED) + find_package(CURL REQUIRED) -if(NOT USE_REALM_LOCAL) find_library(REALMCPP_LIB NAMES cpprealm - ) + ) find_library(REALM_LIB NAMES realm - ) + ) message(STATUS "REALMCPP_LIB = ${REALMCPP_LIB}") message(STATUS "REALM_LIB = ${REALM_LIB}") -endif() -add_executable(hello helloworld.cpp) -target_link_libraries(hello - cpprealm - realm - Threads::Threads - OpenSSL::SSL - CURL::libcurl -) - -add_executable(example example.cpp) -target_link_libraries(example - cpprealm - realm - Threads::Threads - OpenSSL::SSL - CURL::libcurl -) + add_executable(hello helloworld.cpp) + target_link_libraries(hello + cpprealm + realm + Threads::Threads + OpenSSL::SSL + CURL::libcurl + ) + + add_executable(example example.cpp) + target_link_libraries(example + cpprealm + realm + Threads::Threads + OpenSSL::SSL + CURL::libcurl + ) +endif() +