Realm C++ SDK Version v2.2.0

managed_string.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_MANAGED_STRING_HPP
20#define CPPREALM_MANAGED_STRING_HPP
21
22#include <cpprealm/macros.hpp>
23
24namespace realm {
25 class rbool;
26}
27
28namespace realm {
29 //MARK: - char reference
31 char_reference& operator =(char c);
32 private:
33 char_reference(managed<std::string>* parent, size_t idx);
34 template <typename, typename> friend struct realm::managed;
35 friend struct char_pointer;
36 managed<std::string>* m_parent;
37 size_t m_idx;
38 };
39 //MARK: - const char reference
41 operator char() const; //NOLINT(google-explicit-constructor)
42 private:
43 const_char_reference(const managed<std::string>* parent, size_t idx);
44 template <typename, typename> friend struct realm::managed;
45 friend struct const_char_pointer;
46 const managed<std::string>* m_parent;
47 size_t m_idx;
48 };
49 //MARK: - char pointer
50 struct char_pointer {
51 char_reference operator [](size_t idx);
52 private:
53 explicit char_pointer(managed<std::string>* parent);
54 template <typename, typename> friend struct realm::managed;
55 managed<std::string>* m_parent;
56 };
57 //MARK: - const char pointer
59 const_char_reference operator [](size_t idx) const;
60 private:
61 explicit const_char_pointer(const managed<std::string>* parent);
62 template <typename, typename> friend struct realm::managed;
63 const managed<std::string>* m_parent;
64 };
65
66 //MARK: - managed string
67 template <> struct managed<std::string> : managed_base {
68 using value_type = std::string;
69 using managed<std::string>::managed_base::managed_base;
70 using managed<std::string>::managed_base::operator=;
71
72 [[nodiscard]] std::string detach() const;
73
76
77 managed& operator =(std::string&& v) { set(std::move(v)); return *this; }
78 managed& operator =(const std::string& v) { set(v); return *this; }
79 managed& operator =(const char* v) { set(v); return *this; }
80
81 //MARK: - element access
83 reference at(size_t idx);
85 reference operator[](size_t idx);
86
88 [[nodiscard]] const_reference front() const;
89 reference front();
91 [[nodiscard]] const_reference back() const;
92 reference back();
94 char_pointer data();
95 [[nodiscard]] const_char_pointer data() const;
96
98 [[nodiscard]] const_char_pointer c_str() const;
99
101 operator std::string_view() const = delete;
102 operator std::string() const;
103
104
105 [[nodiscard]] size_t size() const noexcept;
106 //MARK: - operations
107 void clear() noexcept;
108 void push_back(char c);
110 managed& append(const std::string&);
112 managed& operator+=(const std::string&);
114 void pop_back();
115
116 //MARK: - comparison operators
117 rbool operator==(const std::string& rhs) const noexcept;
118 rbool operator==(const char* rhs) const noexcept;
119 rbool operator!=(const std::string& rhs) const noexcept;
120 rbool operator!=(const char* rhs) const noexcept;
121 rbool contains(const std::string &s, bool case_sensitive = true) const noexcept;
122 rbool empty() const noexcept;
123 private:
124 friend struct char_reference;
125 friend struct const_char_reference;
126 void inline set(const std::string& v) { m_obj->template set<std::string>(m_key, v); }
127 [[nodiscard]] inline std::string get() const { return m_obj->get<std::string>(m_key); }
128 managed() = default;
129 managed(const managed&) = delete;
130 managed(managed &&) = delete;
131 managed& operator=(const managed&) = delete;
132 managed& operator=(managed&&) = delete;
133 template<typename, typename>
134 friend struct managed;
135 };
136
137 template <> struct managed<std::optional<std::string>> final : public managed<std::string> {
138 using value_type = std::optional<std::string>;
139 using managed<std::string>::operator=;
140 managed& operator =(std::optional<std::string>&& v) { set(std::move(v)); return *this; }
141 managed& operator =(const std::optional<std::string>& v) { set(v); return *this; }
142 managed& operator =(const char* v) { set(v); return *this; }
143
144 [[nodiscard]] std::optional<std::string> detach() const {
145 return m_obj->template get_optional<std::string>(m_key);
146 }
147
148 [[nodiscard]] std::optional<std::string> operator *() const {
149 return detach();
150 }
151
152 [[nodiscard]] operator std::optional<std::string>() const {
153 return detach();
154 }
155
156 rbool operator==(const std::optional<std::string>& rhs) const noexcept;
157 rbool operator!=(const std::optional<std::string>& rhs) const noexcept;
158 private:
159 void inline set(const std::optional<std::string>& v) { m_obj->template set<std::optional<std::string>>(m_key, v); }
160 managed() = default;
161 managed(const managed&) = delete;
162 managed(managed &&) = delete;
163 managed& operator=(const managed&) = delete;
164 managed& operator=(managed&&) = delete;
165 template<typename, typename>
166 friend struct managed;
167 };
168}
169
170#endif //CPPREALM_MANAGED_STRING_HPP
Definition: rbool.hpp:36
Definition: managed_string.hpp:50
Definition: managed_string.hpp:30
Definition: managed_string.hpp:58
Definition: managed_string.hpp:40
Definition: managed_string.hpp:67
Definition: macros.hpp:286
Definition: obj.hpp:62