Realm C++ SDK Version v2.1.0

dictionary.hpp

1
2//
3// Copyright 2024 Realm Inc.
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16//
18
19#ifndef CPPREALM_BRIDGE_DICTIONARY_HPP
20#define CPPREALM_BRIDGE_DICTIONARY_HPP
21
22#include <functional>
23#include <memory>
24#include <string>
25#include <vector>
26#include <cpprealm/internal/bridge/utils.hpp>
27
28namespace realm::object_store {
29 class Dictionary;
30}
31
32namespace realm {
33 class Dictionary;
34 using CoreDictionary = Dictionary;
39 dictionary_change_set& operator=(dictionary_change_set&&) = default;
40
41 // Keys which were removed from the _old_ dictionary
42 std::vector<std::string> deletions;
43
44 // Keys in the _new_ dictionary which are new insertions
45 std::vector<std::string> insertions;
46
47 // Keys of objects/values which were modified
48 std::vector<std::string> modifications;
49
50 bool collection_root_was_deleted = false;
51 };
52
55
56
57 dictionary_collection_change(std::vector<std::string>&& i,
58 std::vector<std::string>&& m,
59 std::vector<std::string>&& d,
60 bool c)
61 : insertions(std::move(i)),
62 modifications(std::move(m)),
63 deletions(std::move(d)),
64 collection_root_was_deleted(c) {}
65
66 std::vector<std::string> insertions;
67 std::vector<std::string> modifications;
68 std::vector<std::string> deletions;
69 // This flag indicates whether the underlying object which is the source of this
70 // collection was deleted. This applies to lists, dictionaries and sets.
71 // This enables notifiers to report a change on empty collections that have been deleted.
72 bool collection_root_was_deleted = false;
73
74 [[nodiscard]] bool empty() const noexcept {
75 return deletions.empty() && insertions.empty() && modifications.empty() &&
76 !collection_root_was_deleted;
77 }
78 };
79
81 std::function<void(dictionary_collection_change)> handler;
82 bool ignore_changes_in_initial_notification;
83
85 bool ignore_changes_in_initial_notification)
86 : handler(handler)
87 , ignore_changes_in_initial_notification(ignore_changes_in_initial_notification)
88 {}
89 };
90}
91
92namespace realm::internal::bridge {
93 using Dictionary = object_store::Dictionary;
94 using CoreDictionary = CoreDictionary;
95
96 struct mixed;
97 struct binary;
98 struct timestamp;
99 struct obj_key;
100 struct uuid;
101 struct object_id;
102 struct notification_token;
103 struct collection_change_callback;
104 struct obj;
105
108 core_dictionary(const core_dictionary &other);
109 core_dictionary &operator=(const core_dictionary &other);
111 core_dictionary &operator=(core_dictionary &&other);
113
114 core_dictionary(const CoreDictionary& v); //NOLINT(google-explicit-constructor)
115 operator CoreDictionary () const; //NOLINT(google-explicit-constructor)
116 void insert(const std::string& key, const mixed& value);
117 void insert(const std::string& key, const std::string& value);
118 obj create_and_insert_linked_object(const std::string& key);
119 obj create_and_insert_linked_object(const std::string& key, const internal::bridge::mixed& pk);
120 mixed get(const std::string& key) const;
121 void erase(const std::string& key);
122 obj get_object(const std::string& key);
123 std::pair<mixed, mixed> get_pair(size_t ndx) const;
124 size_t find_any_key(const std::string& value) const noexcept;
125
126 size_t size() const;
127 private:
128 const CoreDictionary* get_dictionary() const;
129 CoreDictionary* get_dictionary();
130#ifdef CPPREALM_HAVE_GENERATED_BRIDGE_TYPES
131 storage::CoreDictionary m_dictionary[1];
132#else
133 std::shared_ptr<CoreDictionary> m_dictionary;
134#endif
135 };
136
137
138 struct dictionary {
139 dictionary();
140 dictionary(const dictionary& other) ;
141 dictionary& operator=(const dictionary& other) ;
142 dictionary(dictionary&& other);
143 dictionary& operator=(dictionary&& other);
144 ~dictionary();
145 dictionary(const Dictionary& v); //NOLINT(google-explicit-constructor)
146 operator Dictionary() const; //NOLINT(google-explicit-constructor)
147 void insert(const std::string& key, const mixed& value);
148 void insert(const std::string &key, const std::string &value);
149 [[nodiscard]] size_t size() const;
150 void remove_all();
151 void remove(const std::string&);
152 std::pair<std::string, mixed> get_pair(size_t);
153 [[nodiscard]] size_t get_key_index(const std::string&);
154 void clear();
155 [[nodiscard]] size_t find(const std::string&);
156 obj insert_embedded(const std::string&);
157 notification_token add_notification_callback(std::shared_ptr<collection_change_callback>&& cb);
158 notification_token add_key_based_notification_callback(std::shared_ptr<dictionary_callback_wrapper>&& cb);
159 private:
160 const Dictionary* get_dictionary() const;
161 Dictionary* get_dictionary();
162 friend inline Dictionary * get_dictionary(dictionary& lst);
163 friend inline const Dictionary* get_dictionary(const dictionary& lst);
164 template <typename T>
165 friend T get(dictionary&, const std::string&);
166#ifdef CPPREALM_HAVE_GENERATED_BRIDGE_TYPES
167 storage::Dictionary m_dictionary[1];
168#else
169 std::shared_ptr<Dictionary> m_dictionary;
170#endif
171 };
172
173 template <typename T>
174 [[nodiscard]] T get(dictionary&, const std::string&);
175 template <>
176 [[nodiscard]] std::string get(dictionary&, const std::string&);
177 template <>
178 [[nodiscard]] uuid get(dictionary&, const std::string&);
179 template <>
180 [[nodiscard]] object_id get(dictionary&, const std::string&);
181 template <>
182 [[nodiscard]] timestamp get(dictionary&, const std::string&);
183 template <>
184 [[nodiscard]] binary get(dictionary&, const std::string&);
185 template <>
186 [[nodiscard]] obj get(dictionary&, const std::string&);
187 template <>
188 [[nodiscard]] obj_key get(dictionary&, const std::string&);
189 template <>
190 [[nodiscard]] int64_t get(dictionary&, const std::string&);
191 template <>
192 [[nodiscard]] double get(dictionary&, const std::string&);
193}
194
195#endif //CPPREALM_BRIDGE_DICTIONARY_HPP
Definition: dictionary.hpp:80
Definition: dictionary.hpp:35
Definition: dictionary.hpp:53
Definition: binary.hpp:30
Definition: dictionary.hpp:106
Definition: dictionary.hpp:138
Definition: mixed.hpp:69
Definition: obj_key.hpp:33
Definition: obj.hpp:123
Definition: object_id.hpp:31
Definition: timestamp.hpp:30
Definition: uuid.hpp:32