|
| 1 | +/* |
| 2 | + * Copyright 2018 Google |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +// TODO(rsgowman): This file isn't intended to be used just yet. It's just an |
| 18 | +// outline of what the API might eventually look like. Most of this was |
| 19 | +// shamelessly stolen and modified from rtdb's header file, melded with the |
| 20 | +// (java) firestore api. |
| 21 | + |
| 22 | +#ifndef FIRESTORE_CORE_INCLUDE_FIREBASE_FIRESTORE_DOCUMENT_REFERENCE_H_ |
| 23 | +#define FIRESTORE_CORE_INCLUDE_FIREBASE_FIRESTORE_DOCUMENT_REFERENCE_H_ |
| 24 | + |
| 25 | +#include <string> |
| 26 | +#include <unordered_map> |
| 27 | + |
| 28 | +#if defined(FIREBASE_USE_STD_FUNCTION) |
| 29 | +#include <functional> |
| 30 | +#endif |
| 31 | + |
| 32 | +// TODO(rsgowman): Note that RTDB uses: |
| 33 | +// #if defined(FIREBASE_USE_MOVE_OPERATORS) || defined(DOXYGEN |
| 34 | +// to protect move operators from older compilers. But all our supported |
| 35 | +// compilers support this, so we've skipped the #if guard. This TODO comment is |
| 36 | +// here so we don't forget to mention this during the API review, and should be |
| 37 | +// removed once this note has migrated to the API review doc. |
| 38 | + |
| 39 | +// TODO(rsgowman): replace these forward decl's with appropriate includes (once |
| 40 | +// they exist) |
| 41 | +namespace firebase { |
| 42 | +class App; |
| 43 | +template <typename T> |
| 44 | +class Future; |
| 45 | +} // namespace firebase |
| 46 | + |
| 47 | +namespace firebase { |
| 48 | +namespace firestore { |
| 49 | + |
| 50 | +// TODO(rsgowman): replace these forward decl's with appropriate includes (once |
| 51 | +// they exist) |
| 52 | +class FieldValue; |
| 53 | +class DocumentSnapshot; |
| 54 | +class Firestore; |
| 55 | +class Error; |
| 56 | +template <typename T> |
| 57 | +class EventListener; |
| 58 | +class ListenerRegistration; |
| 59 | +class CollectionReference; |
| 60 | +class DocumentListenOptions; |
| 61 | +// TODO(rsgowman): not quite a forward decl, but required to make the default |
| 62 | +// parameter to Set() "compile". |
| 63 | +class SetOptions { |
| 64 | + public: |
| 65 | + SetOptions(); |
| 66 | +}; |
| 67 | + |
| 68 | +// TODO(rsgowman): move this into the FieldValue header |
| 69 | +#ifdef STLPORT |
| 70 | +using MapFieldValue = std::tr1::unordered_map<std::string, FieldValue>; |
| 71 | +#else |
| 72 | +using MapFieldValue = std::unordered_map<std::string, FieldValue>; |
| 73 | +#endif |
| 74 | + |
| 75 | +/** |
| 76 | + * A DocumentReference refers to a document location in a Firestore database and |
| 77 | + * can be used to write, read, or listen to the location. There may or may not |
| 78 | + * exist a document at the referenced location. A DocumentReference can also be |
| 79 | + * used to create a CollectionReference to a subcollection. |
| 80 | + * |
| 81 | + * Create a DocumentReference via Firebase::Document(const string& path). |
| 82 | + * |
| 83 | + * Subclassing Note: Firestore classes are not meant to be subclassed except for |
| 84 | + * use in test mocks. Subclassing is not supported in production code and new |
| 85 | + * SDK releases may break code that does so. |
| 86 | + */ |
| 87 | +class DocumentReference { |
| 88 | + public: |
| 89 | + /** |
| 90 | + * @brief Default constructor. This creates an invalid DocumentReference. |
| 91 | + * Attempting to perform any operations on this reference will fail (and cause |
| 92 | + * a crash) unless a valid DocumentReference has been assigned to it. |
| 93 | + */ |
| 94 | + DocumentReference(); |
| 95 | + |
| 96 | + /** |
| 97 | + * @brief Copy constructor. It's totally okay (and efficient) to copy |
| 98 | + * DocumentReference instances, as they simply point to the same location in |
| 99 | + * the database. |
| 100 | + * |
| 101 | + * @param[in] reference DocumentReference to copy from. |
| 102 | + */ |
| 103 | + DocumentReference(const DocumentReference& reference); |
| 104 | + |
| 105 | + /** |
| 106 | + * @brief Move constructor. Moving is an efficient operation for |
| 107 | + * DocumentReference instances. |
| 108 | + * |
| 109 | + * @param[in] reference DocumentReference to move data from. |
| 110 | + */ |
| 111 | + DocumentReference(DocumentReference&& reference); |
| 112 | + |
| 113 | + virtual ~DocumentReference(); |
| 114 | + |
| 115 | + /** |
| 116 | + * @brief Copy assignment operator. It's totally okay (and efficient) to copy |
| 117 | + * DocumentReference instances, as they simply point to the same location in |
| 118 | + * the database. |
| 119 | + * |
| 120 | + * @param[in] reference DocumentReference to copy from. |
| 121 | + * |
| 122 | + * @returns Reference to the destination DocumentReference. |
| 123 | + */ |
| 124 | + DocumentReference& operator=(const DocumentReference& reference); |
| 125 | + |
| 126 | + /** |
| 127 | + * @brief Move assignment operator. Moving is an efficient operation for |
| 128 | + * DocumentReference instances. |
| 129 | + * |
| 130 | + * @param[in] reference DocumentReference to move data from. |
| 131 | + * |
| 132 | + * @returns Reference to the destination DocumentReference. |
| 133 | + */ |
| 134 | + DocumentReference& operator=(DocumentReference&& reference); |
| 135 | + |
| 136 | + /** |
| 137 | + * @brief Returns the Firestore instance associated with this document |
| 138 | + * reference. |
| 139 | + * |
| 140 | + * The pointer will remain valid indefinitely. |
| 141 | + * |
| 142 | + * @returns Firebase Firestore instance that this DocumentReference refers to. |
| 143 | + */ |
| 144 | + virtual const Firestore* firestore() const; |
| 145 | + |
| 146 | + /** |
| 147 | + * @brief Returns the Firestore instance associated with this document |
| 148 | + * reference. |
| 149 | + * |
| 150 | + * The pointer will remain valid indefinitely. |
| 151 | + * |
| 152 | + * @returns Firebase Firestore instance that this DocumentReference refers to. |
| 153 | + */ |
| 154 | + virtual Firestore* firestore(); |
| 155 | + |
| 156 | + /** |
| 157 | + * @brief Returns the string id of this document location. |
| 158 | + * |
| 159 | + * The pointer is only valid while the DocumentReference remains in memory. |
| 160 | + * |
| 161 | + * @returns String id of this document location, which will remain valid in |
| 162 | + * memory until the DocumentReference itself goes away. |
| 163 | + */ |
| 164 | + virtual const char* id() const; |
| 165 | + |
| 166 | + /** |
| 167 | + * @brief Returns the string id of this document location. |
| 168 | + * |
| 169 | + * @returns String id of this document location. |
| 170 | + */ |
| 171 | + virtual std::string id_string() const; |
| 172 | + |
| 173 | + /** |
| 174 | + * @brief Returns the path of this document (relative to the root of the |
| 175 | + * database) as a slash-separated string. |
| 176 | + * |
| 177 | + * The pointer is only valid while the DocumentReference remains in memory. |
| 178 | + * |
| 179 | + * @returns String path of this document location, which will remain valid in |
| 180 | + * memory until the DocumentReference itself goes away. |
| 181 | + */ |
| 182 | + virtual const char* path() const; |
| 183 | + |
| 184 | + /** |
| 185 | + * @brief Returns the path of this document (relative to the root of the |
| 186 | + * database) as a slash-separated string. |
| 187 | + * |
| 188 | + * @returns String path of this document location. |
| 189 | + */ |
| 190 | + virtual std::string path_string() const; |
| 191 | + |
| 192 | + /** |
| 193 | + * @brief Returns a CollectionReference to the collection that contains this |
| 194 | + * document. |
| 195 | + */ |
| 196 | + virtual CollectionReference get_parent() const; |
| 197 | + |
| 198 | + /** |
| 199 | + * @brief Returns a CollectionReference instance that refers to the |
| 200 | + * subcollection at the specified path relative to this document. |
| 201 | + * |
| 202 | + * @param[in] collection_path A slash-separated relative path to a |
| 203 | + * subcollection. The pointer only needs to be valid during this call. |
| 204 | + * |
| 205 | + * @return The CollectionReference instance. |
| 206 | + */ |
| 207 | + virtual CollectionReference Collection(const char* collection_path) const; |
| 208 | + |
| 209 | + /** |
| 210 | + * @brief Returns a CollectionReference instance that refers to the |
| 211 | + * subcollection at the specified path relative to this document. |
| 212 | + * |
| 213 | + * @param[in] collection_path A slash-separated relative path to a |
| 214 | + * subcollection. |
| 215 | + * |
| 216 | + * @return The CollectionReference instance. |
| 217 | + */ |
| 218 | + virtual CollectionReference Collection( |
| 219 | + const std::string& collection_path) const; |
| 220 | + |
| 221 | + /** |
| 222 | + * @brief Reads the document referenced by this DocumentReference. |
| 223 | + * |
| 224 | + * @return A Future that will be resolved with the contents of the Document at |
| 225 | + * this DocumentReference. |
| 226 | + */ |
| 227 | + virtual Future<DocumentSnapshot> Get() const; |
| 228 | + |
| 229 | + /** |
| 230 | + * @brief Writes to the document referred to by this DocumentReference. |
| 231 | + * |
| 232 | + * If the document does not yet exist, it will be created. If you pass |
| 233 | + * SetOptions, the provided data can be merged into an existing document. |
| 234 | + * |
| 235 | + * @param[in] data A map of the fields and values for the document. |
| 236 | + * @param[in] options An object to configure the set behavior. |
| 237 | + * |
| 238 | + * @return A Future that will be resolved when the write finishes. |
| 239 | + */ |
| 240 | + virtual Future<void> Set(const MapFieldValue& data, |
| 241 | + const SetOptions& options = SetOptions()); |
| 242 | + |
| 243 | + /** |
| 244 | + * @brief Updates fields in the document referred to by this |
| 245 | + * DocumentReference. |
| 246 | + * |
| 247 | + * If no document exists yet, the update will fail. |
| 248 | + * |
| 249 | + * @param[in] data A map of field / value pairs to update. Fields can contain |
| 250 | + * dots to reference nested fields within the document. |
| 251 | + * |
| 252 | + * @return A Future that will be resolved when the write finishes. |
| 253 | + */ |
| 254 | + virtual Future<void> Update(const MapFieldValue& data); |
| 255 | + |
| 256 | + /** |
| 257 | + * @brief Removes the document referred to by this DocumentReference. |
| 258 | + * |
| 259 | + * @return A Task that will be resolved when the delete completes. |
| 260 | + */ |
| 261 | + virtual Future<void> Delete(); |
| 262 | + |
| 263 | + /** |
| 264 | + * @brief Starts listening to the document referenced by this |
| 265 | + * DocumentReference. |
| 266 | + * |
| 267 | + * @param[in] listener The event listener that will be called with the |
| 268 | + * snapshots, which must remain in memory until you remove the listener from |
| 269 | + * this DocumentReference. (Ownership is not transferred; you are responsible |
| 270 | + * for making sure that listener is valid as long as this DocumentReference is |
| 271 | + * valid and the listener is registered.) |
| 272 | + * |
| 273 | + * @return A registration object that can be used to remove the listener. |
| 274 | + */ |
| 275 | + virtual ListenerRegistration AddSnapshotListener( |
| 276 | + EventListener<DocumentSnapshot>* listener); |
| 277 | + |
| 278 | + /** |
| 279 | + * @brief Starts listening to the document referenced by this |
| 280 | + * DocumentReference. |
| 281 | + * |
| 282 | + * @param[in] options The options to use for this listen. |
| 283 | + * @param[in] listener The event listener that will be called with the |
| 284 | + * snapshots, which must remain in memory until you remove the listener from |
| 285 | + * this DocumentReference. (Ownership is not transferred; you are responsible |
| 286 | + * for making sure that listener is valid as long as this DocumentReference is |
| 287 | + * valid and the listener is registered.) |
| 288 | + * |
| 289 | + * @return A registration object that can be used to remove the listener. |
| 290 | + */ |
| 291 | + virtual ListenerRegistration AddSnapshotListener( |
| 292 | + const DocumentListenOptions& options, |
| 293 | + EventListener<DocumentSnapshot>* listener); |
| 294 | + |
| 295 | +#if defined(FIREBASE_USE_STD_FUNCTION) || defined(DOXYGEN) |
| 296 | + /** |
| 297 | + * @brief Starts listening to the document referenced by this |
| 298 | + * DocumentReference. |
| 299 | + * |
| 300 | + * @param[in] callback function or lambda to call. When this function is |
| 301 | + * called, exactly one of the parameters will be non-null. |
| 302 | + * |
| 303 | + * @return A registration object that can be used to remove the listener. |
| 304 | + * |
| 305 | + * @note This method is not available when using STLPort on Android, as |
| 306 | + * std::function is not supported on STLPort. |
| 307 | + */ |
| 308 | + virtual ListenerRegistration AddSnapshotListener( |
| 309 | + std::function<void(const DocumentSnapshot*, const Error*)> callback); |
| 310 | + |
| 311 | + /** |
| 312 | + * @brief Starts listening to the document referenced by this |
| 313 | + * DocumentReference. |
| 314 | + * |
| 315 | + * @param[in] options The options to use for this listen. |
| 316 | + * @param[in] callback function or lambda to call. When this function is |
| 317 | + * called, exactly one of the parameters will be non-null. |
| 318 | + * |
| 319 | + * @return A registration object that can be used to remove the listener. |
| 320 | + * |
| 321 | + * @note This method is not available when using STLPort on Android, as |
| 322 | + * std::function is not supported on STLPort. |
| 323 | + */ |
| 324 | + virtual ListenerRegistration AddSnapshotListener( |
| 325 | + const DocumentListenOptions& options, |
| 326 | + std::function<void(const DocumentSnapshot*, const Error*)> callback); |
| 327 | +#endif // defined(FIREBASE_USE_STD_FUNCTION) || defined(DOXYGEN) |
| 328 | +}; |
| 329 | + |
| 330 | +// TODO(rsgowman): probably define and inline here. |
| 331 | +bool operator==(const DocumentReference& lhs, const DocumentReference& rhs); |
| 332 | + |
| 333 | +inline bool operator!=(const DocumentReference& lhs, |
| 334 | + const DocumentReference& rhs) { |
| 335 | + return !(lhs == rhs); |
| 336 | +} |
| 337 | + |
| 338 | +// TODO(rsgowman): probably define and inline here. |
| 339 | +bool operator<(const DocumentReference& lhs, const DocumentReference& rhs); |
| 340 | + |
| 341 | +inline bool operator>(const DocumentReference& lhs, |
| 342 | + const DocumentReference& rhs) { |
| 343 | + return rhs < lhs; |
| 344 | +} |
| 345 | + |
| 346 | +inline bool operator<=(const DocumentReference& lhs, |
| 347 | + const DocumentReference& rhs) { |
| 348 | + return !(lhs > rhs); |
| 349 | +} |
| 350 | + |
| 351 | +inline bool operator>=(const DocumentReference& lhs, |
| 352 | + const DocumentReference& rhs) { |
| 353 | + return !(lhs < rhs); |
| 354 | +} |
| 355 | + |
| 356 | +} // namespace firestore |
| 357 | +} // namespace firebase |
| 358 | + |
| 359 | +namespace std { |
| 360 | +// TODO(rsgowman): NB that specialization of std::hash deviates from the Google |
| 361 | +// C++ style guide. But we think this is probably ok in this case since: |
| 362 | +// a) It's the standard way of doing this outside of Google (as the style guide |
| 363 | +// itself points out), and |
| 364 | +// b) This has a straightfoward hash function anyway (just hash the path) so I |
| 365 | +// don't think the concerns in the style guide are going to bite us. |
| 366 | +// |
| 367 | +// Raise this concern during the API review. |
| 368 | +template <> |
| 369 | +struct hash<firebase::firestore::DocumentReference> { |
| 370 | + std::size_t operator()( |
| 371 | + const firebase::firestore::DocumentReference& doc_ref) const; |
| 372 | +}; |
| 373 | +} // namespace std |
| 374 | + |
| 375 | +#endif // FIRESTORE_CORE_INCLUDE_FIREBASE_FIRESTORE_DOCUMENT_REFERENCE_H_ |
0 commit comments