-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(DOCSP-29739): C++: Add documentation for Set data type (#3065)
## Pull Request Info ### Jira - https://jira.mongodb.org/browse/DOCSP-29739 ### Staged Changes - [Supported Types](https://preview-mongodbdacharyc.gatsbyjs.io/realm/DOCSP-29739/sdk/cpp/model-data/supported-types/#set) - [CRUD/Create](https://preview-mongodbdacharyc.gatsbyjs.io/realm/DOCSP-29739/sdk/cpp/crud/create/#create-an-object-with-a-set-property) - [CRUD/Read](https://preview-mongodbdacharyc.gatsbyjs.io/realm/DOCSP-29739/sdk/cpp/crud/read/#read-a-set-property) - [CRUD/Update](https://preview-mongodbdacharyc.gatsbyjs.io/realm/DOCSP-29739/sdk/cpp/crud/update/#update-a-set-property) - [CRUD/Delete](https://preview-mongodbdacharyc.gatsbyjs.io/realm/DOCSP-29739/sdk/cpp/crud/delete/#delete-set-values) - [React to Changes](https://preview-mongodbdacharyc.gatsbyjs.io/realm/DOCSP-29739/sdk/cpp/react-to-changes/#register-a-collection-change-listener) ### Reminder Checklist If your PR modifies the docs, you might need to also update some corresponding pages. Check if completed or N/A. - [x] Create Jira ticket for corresponding docs-app-services update(s), if any - [x] Checked/updated Admin API - [x] Checked/updated CLI reference ### Review Guidelines [REVIEWING.md](https://github.com/mongodb/docs-realm/blob/master/REVIEWING.md)
- Loading branch information
Showing
16 changed files
with
368 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
source/examples/generated/cpp/crud.snippet.beta-model-with-set-property.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
struct Repository { | ||
std::string ownerAndName; | ||
std::set<int64_t> openPullRequestNumbers; | ||
}; | ||
REALM_SCHEMA(Repository, ownerAndName, openPullRequestNumbers) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Remove an element from the set with erase() | ||
auto it3064 = managedDocsRealm.openPullRequestNumbers.find(3064); | ||
CHECK(it3064 != managedDocsRealm.openPullRequestNumbers.end()); | ||
realm.write([&] { | ||
managedDocsRealm.openPullRequestNumbers.erase(it3065); | ||
}); | ||
CHECK(managedDocsRealm.openPullRequestNumbers.size() == 4); | ||
|
||
// Clear the entire contents of the set | ||
realm.write([&] { | ||
managedDocsRealm.openPullRequestNumbers.clear(); | ||
}); | ||
CHECK(managedDocsRealm.openPullRequestNumbers.size() == 0); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
auto repositories = realm.objects<Repository>(); | ||
|
||
auto repositoriesNamedDocsRealm = repositories.where([](auto &repository) { | ||
return repository.ownerAndName == "mongodb/docs-realm"; | ||
}); | ||
|
||
auto docsRealm = repositoriesNamedDocsRealm[0]; | ||
|
||
// You can check the size of the set | ||
auto numberOfPullRequests = docsRealm.openPullRequestNumbers.size(); | ||
|
||
// Find an element in the set whose value is 3064 | ||
auto it = managedDocsRealm.openPullRequestNumbers.find(3064); | ||
|
||
// Get a copy of the set that exists independent of the managed set | ||
auto openRealmPullRequests = docsRealm.openPullRequestNumbers.detach(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Add elements to the set in a write transaction | ||
realm.write([&] { | ||
managedDocsRealm.openPullRequestNumbers.insert(3066); | ||
}); | ||
CHECK(managedDocsRealm.openPullRequestNumbers.size() == 4); | ||
|
||
// Use std::set algorithms to update a set | ||
// In this example, use std::set_union to add elements to the set | ||
// 3064 already exists, so it won't be added, but 3065 and 3067 are unique | ||
// values and will be added to the set. | ||
auto newOpenPullRequests = std::set<int64_t>({ 3064, 3065, 3067 }); | ||
realm.write([&] { | ||
std::set_union( | ||
docsRealm.openPullRequestNumbers.begin(), | ||
docsRealm.openPullRequestNumbers.end(), | ||
newOpenPullRequests.begin(), | ||
newOpenPullRequests.end(), | ||
std::inserter( | ||
managedDocsRealm.openPullRequestNumbers, | ||
managedDocsRealm.openPullRequestNumbers.end())); | ||
}); | ||
CHECK(managedDocsRealm.openPullRequestNumbers.size() == 6); | ||
|
||
// Erase elements from a set | ||
auto it3065 = managedDocsRealm.openPullRequestNumbers.find(3065); | ||
CHECK(it3065 != managedDocsRealm.openPullRequestNumbers.end()); | ||
realm.write([&] { | ||
managedDocsRealm.openPullRequestNumbers.erase(it3065); | ||
}); |
23 changes: 23 additions & 0 deletions
23
source/examples/generated/cpp/crud.snippet.write-set-object.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
auto realm = db(std::move(config)); | ||
|
||
// Create an object that has a set property | ||
auto docsRealmRepo = Repository { | ||
.ownerAndName = "mongodb/docs-realm" | ||
}; | ||
|
||
// Add the object to the realm and get the managed object | ||
auto managedDocsRealm = realm.write([&]() { | ||
return realm.add(std::move(docsRealmRepo)); | ||
}); | ||
|
||
// Insert items into the set | ||
auto openPullRequestNumbers = { 3059, 3062, 3064 }; | ||
|
||
realm.write([&] { | ||
for (auto number: openPullRequestNumbers) { | ||
// You can only mutate the set in a write transaction. | ||
// This means you can't set values at initialization, | ||
// but must do it during a write. | ||
managedDocsRealm.openPullRequestNumbers.insert(number); | ||
} | ||
}); |
1 change: 1 addition & 0 deletions
1
source/examples/generated/cpp/supported-types.snippet.beta-required-set.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
std::set<std::string> setTypeName; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.