Skip to content

Commit

Permalink
Add std::set support (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
leemaguire authored Oct 17, 2023
1 parent 4acec73 commit ab2fc5f
Show file tree
Hide file tree
Showing 28 changed files with 1,686 additions and 41 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ app_config.metadata_encryption_key = example_key;
auto encrypted_app = realm::App(app_config);
```
* Add ability to encrypt a Realm. Usage: `realm::config::set_encryption_key(const std::array<char, 64>&)`.
* Add support for `std::set` in object models.
### Breaking Changes
* `realm::App(const std::string &app_id, const std::optional<std::string> &base_url,
Expand Down
6 changes: 5 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ set(SOURCES
cpprealm/internal/bridge/realm.cpp
cpprealm/internal/bridge/results.cpp
cpprealm/internal/bridge/schema.cpp
cpprealm/internal/bridge/set.cpp
cpprealm/internal/bridge/status.cpp
cpprealm/internal/bridge/sync_error.cpp
cpprealm/internal/bridge/sync_manager.cpp
Expand Down Expand Up @@ -72,6 +73,7 @@ set(HEADERS
cpprealm/experimental/managed_numeric.hpp
cpprealm/experimental/managed_objectid.hpp
cpprealm/experimental/managed_primary_key.hpp
cpprealm/experimental/managed_set.hpp
cpprealm/experimental/managed_string.hpp
cpprealm/experimental/managed_timestamp.hpp
cpprealm/experimental/managed_uuid.hpp
Expand All @@ -97,6 +99,7 @@ set(HEADERS
cpprealm/internal/bridge/realm.hpp
cpprealm/internal/bridge/results.hpp
cpprealm/internal/bridge/schema.hpp
cpprealm/internal/bridge/set.hpp
cpprealm/internal/bridge/status.hpp
cpprealm/internal/bridge/sync_error.hpp
cpprealm/internal/bridge/sync_manager.hpp
Expand All @@ -114,7 +117,8 @@ set(HEADERS
cpprealm/schema.hpp
cpprealm/task.hpp
cpprealm/thread_safe_reference.hpp
cpprealm/sdk.hpp cpprealm/alpha_support.hpp) # REALM_INSTALL_HEADERS
cpprealm/sdk.hpp
cpprealm/alpha_support.hpp) # REALM_INSTALL_HEADERS

if(ENABLE_ALPHA_SDK)
list(APPEND HEADERS
Expand Down
86 changes: 78 additions & 8 deletions src/cpprealm/experimental/accessors.hpp

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/cpprealm/experimental/db.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ namespace realm::experimental {
m_obj = table.create_object();
}

std::apply([&m_obj, &v](auto && ...p) {
std::apply([&m_obj, &v, this](auto && ...p) {
(accessor<typename std::decay_t<decltype(p)>::Result>::set(
m_obj, m_obj.get_table().get_column_key(p.name), v.*(std::decay_t<decltype(p)>::ptr)
m_obj, m_obj.get_table().get_column_key(p.name), m_realm, v.*(std::decay_t<decltype(p)>::ptr)
), ...);
}, managed<T>::schema.ps);
auto m = managed<T>(std::move(m_obj), m_realm);
Expand Down
4 changes: 2 additions & 2 deletions src/cpprealm/experimental/link.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ namespace realm {
m_obj->set(m_key, obj.get_key());
}

std::apply([&obj, &o](auto && ...p) {
std::apply([&obj, &o, realm = *m_realm](auto && ...p) {
(accessor<typename std::decay_t<decltype(p)>::Result>::set(
obj, obj.get_table().get_column_key(p.name), (*o).*(std::decay_t<decltype(p)>::ptr)
obj, obj.get_table().get_column_key(p.name), realm, (*o).*(std::decay_t<decltype(p)>::ptr)
), ...);
}, managed<T>::schema.ps);
auto m = managed<T>(std::move(obj), *m_realm);
Expand Down
3 changes: 2 additions & 1 deletion src/cpprealm/experimental/macros.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,8 @@ rbool managed<std::optional<type>>::operator op(const std::optional<type>& rhs)
\
return a.get_table() == b->get_table() \
&& a.get_key() == b->get_key(); \
} \
} \
bool operator < (const managed<cls>& rhs) const { return m_obj.get_key() < rhs.m_obj.get_key(); } \
}; \
struct meta_schema_##cls { \
meta_schema_##cls() { \
Expand Down
4 changes: 2 additions & 2 deletions src/cpprealm/experimental/managed_dictionary.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,9 +409,9 @@ namespace realm::experimental {
m_obj = const_cast<box<managed<V*>> *>(this)->m_backing_map.create_and_insert_linked_object(const_cast<box<managed<V*>> *>(this)->m_key);
}

std::apply([&m_obj, &o](auto && ...p) {
std::apply([&m_obj, &o, realm = this->m_realm](auto && ...p) {
(accessor<typename std::decay_t<decltype(p)>::Result>::set(
m_obj, m_obj.get_table().get_column_key(p.name),
m_obj, m_obj.get_table().get_column_key(p.name), realm,
(*o).*(std::decay_t<decltype(p)>::ptr)), ...);
}, managed<V>::schema.ps);
return *this;
Expand Down
70 changes: 67 additions & 3 deletions src/cpprealm/experimental/managed_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,70 @@ namespace realm::experimental {
return ret;
}

class iterator {
public:
using value_type = managed<T>;

using difference_type = std::ptrdiff_t;
using pointer = T*;
using reference = T&;
using iterator_category = std::forward_iterator_tag;

bool operator!=(const iterator& other) const
{
return !(*this == other);
}

bool operator==(const iterator& other) const
{
return (m_parent == other.m_parent) && (m_i == other.m_i);
}

managed<T> operator*() const noexcept
{
auto list = realm::internal::bridge::list(*m_parent->m_realm, *m_parent->m_obj, m_parent->m_key);
managed<T> m(realm::internal::bridge::get<realm::internal::bridge::obj>(list, m_i), *m_parent->m_realm);
std::apply([&m](auto &&...ptr) {
std::apply([&](auto &&...name) {
((m.*ptr).assign(&m.m_obj, &m.m_realm, m.m_obj.get_table().get_column_key(name)), ...);
}, managed<T>::managed_pointers_names);
}, managed<T>::managed_pointers());
return {std::move(m)};
}

iterator& operator++()
{
this->m_i++;
return *this;
}

const iterator& operator++(int i)
{
this->m_i += i;
return *this;
}
private:
template<typename, typename>
friend struct managed;

iterator(size_t i, managed<std::vector<T*>>* parent)
: m_i(i), m_parent(parent)
{
}
size_t m_i;
managed<std::vector<T*>>* m_parent;
};

iterator begin()
{
return iterator(0, this);
}

iterator end()
{
return iterator(size(), this);
}

void pop_back() {
internal::bridge::list(*m_realm, *m_obj, m_key).remove(size() - 1);
}
Expand All @@ -186,9 +250,9 @@ namespace realm::experimental {
} else {
m_obj = table.create_object();
}
std::apply([&m_obj, &value](auto && ...p) {
std::apply([&m_obj, &value, realm = *m_realm](auto && ...p) {
(accessor<typename std::decay_t<decltype(p)>::Result>::set(
m_obj, m_obj.get_table().get_column_key(p.name),
m_obj, m_obj.get_table().get_column_key(p.name), realm,
(*value).*(std::decay_t<decltype(p)>::ptr)), ...);
}, managed<T, void>::schema.ps);
if (!managed<T>::schema.is_embedded_experimental()) {
Expand All @@ -214,7 +278,7 @@ namespace realm::experimental {
}
}

size_t size()
size_t size() const
{
return internal::bridge::list(*m_realm, *m_obj, m_key).size();
}
Expand Down
Loading

0 comments on commit ab2fc5f

Please sign in to comment.