Docs Menu

Docs HomeRealm

C++ SDK (Alpha)

On this page

  • Limitations
  • Requirements
  • Install
  • Add Package Dependency
  • Specify the Repository
  • Select the Package Products
  • Usage
  • Include the Header
  • Define Your Data Model
  • Use Realm
  • Further Reading

Warning

Alpha Release

This SDK is currently offered as an alpha release. We encourage you to try out the feature and give feedback. However, be aware that APIs and functionality are subject to change.

The Realm C++ SDK enables client applications written in C++ to access data stored in realms and sync data with Atlas.

The C++ SDK does not yet support all Realm database features or Atlas App Services integrations.

  • Minimum C++ standard: C++20.

  • For development on macOS: Xcode 11.x or later.

  • Otherwise, we recommend git and CMake.

Make the Realm C++ SDK available in your code by including the cpprealm/sdk.hpp header:

#include <cpprealm/sdk.hpp>

You can define your models as classes or structs that inherit from realm::object.

You must declare any property you want to store (persist) in a realm as realm::persisted. Your model must also expose a public type definition called schema. Use the realm::schema and realm::property templates to describe your schema.

Realm ignores any members not declared as realm::persisted and not declared in the schema.

// Define your models like regular structs.
struct Dog : realm::object {
realm::persisted<std::string> name;
realm::persisted<int> age;
using schema = realm::schema<"Dog",
realm::property<"name", &Dog::name>,
realm::property<"age", &Dog::age>
>;
};
struct Person : realm::object {
realm::persisted<std::string> _id;
realm::persisted<std::string> name;
realm::persisted<int> age;
// Create relationships by pointing an Object field to another Class
realm::persisted<std::optional<Dog>> dog;
using schema = realm::schema<"Person",
realm::property<"_id", &Person::_id, true>, // primary key
realm::property<"name", &Person::name>,
realm::property<"age", &Person::age>,
realm::property<"dog", &Person::dog>
>;
};

You can use your data models as you would normal classes, except:

  • You may not modify any properties outside of a write transaction.

A realm is a collection of persisted objects. When opening a realm, you must specify which models are available by passing the models into the template parameter list of the realm::open() function.

To open a write transaction, pass a function to realm.write(). Make all modifications within that function.

// 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);
});
←  Flutter TutorialFlutter SDK (Beta) →
Give Feedback
© 2022 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2022 MongoDB, Inc.