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

update cpprealm installation and cmake examples #45

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ Package.resolved
# Qt demo apps
examples/*.pro.user
docs/

# other builds
*cmake-build*
#ide
*.idea
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...)

<img style="width: 0px; height: 0px;" src="https://3eaz4mshcd.execute-api.us-east-1.amazonaws.com/prod?s=https://github.com/realm/realm-cocoa#README.md">
46 changes: 43 additions & 3 deletions examples/cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,46 @@ project (hello)

set(CMAKE_CXX_STANDARD 17)
set(REALM_CPP_NO_TESTS 1)
add_subdirectory(../../ cmake-build-debug)
add_executable(hello helloworld.cpp)
target_link_libraries(hello cpprealm)

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")

set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
find_package(OpenSSL REQUIRED)
find_package(CURL REQUIRED)

find_library(REALMCPP_LIB
NAMES cpprealm
)
find_library(REALM_LIB
NAMES realm
)
message(STATUS "REALMCPP_LIB = ${REALMCPP_LIB}")
message(STATUS "REALM_LIB = ${REALM_LIB}")


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()

39 changes: 39 additions & 0 deletions examples/cmake/example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <cpprealm/sdk.hpp>

struct Dog : realm::object<Dog> {
realm::persisted<std::string> name;
realm::persisted<int64_t> age;
static constexpr auto schema = realm::schema("Dog",
realm::property<&Dog::name>("name"),
realm::property<&Dog::age>("age"));
};
struct Person : realm::object<Person> {
realm::persisted<std::string> _id;
realm::persisted<std::string> name;
realm::persisted<int64_t> age;
// Create relationships by pointing an Object field to another Class
realm::persisted<std::optional<Dog>> 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<Person, Dog>();
// Persist your data in a write transaction
realm.write([&realm, &dog] {
realm.add(dog);
});
}

int main() {
run_realm();
return 0;
}

8 changes: 4 additions & 4 deletions examples/cmake/helloworld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

struct Foo: realm::object<Foo> {

realm::persisted<int> _id;
realm::persisted<int64_t> _id;
realm::persisted<bool> bool_col;
realm::persisted<std::string> str_col;
realm::persisted<std::chrono::time_point<std::chrono::system_clock>> date_col;
Expand All @@ -28,11 +28,11 @@ void run_realm() {
auto tsr = realm::async_open<Foo>(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>("foo-strings", [](auto& obj) {
return obj.str_col != "alex"; // sync all objects where name does not equal 'alex'
});
Expand All @@ -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";
Expand Down
12 changes: 10 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion tests/list_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ TEST_CASE("list", "[list]") {
auto realm = realm::open<AllTypesObject, AllTypesObjectLink, AllTypesObjectEmbedded, Dog>({path});

auto obj = AllTypesObject();
obj.list_mixed_col.push_back(42);
obj.list_mixed_col.push_back(static_cast<int64_t>(42));
obj.list_mixed_col.push_back(true);
obj.list_mixed_col.push_back("hello world");
obj.list_mixed_col.push_back(42.42);
Expand Down
2 changes: 1 addition & 1 deletion tests/mixed_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int64_t>(42);
CHECK(obj.mixed_col == static_cast<int64_t>(42));
auto realm = realm::open<AllTypesObject, AllTypesObjectLink, AllTypesObjectEmbedded>({path});
realm.write([&realm, &obj] {
Expand Down