blob: 134bda515b82f37788a295c637dca500abe8b52b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
/*
* Copyright 2018 Google
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_DOCUMENT_KEY_H_
#define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_DOCUMENT_KEY_H_
#include <initializer_list>
#include <memory>
#include <string>
#if defined(__OBJC__)
#import "Firestore/Source/Model/FSTDocumentKey.h"
#endif // defined(__OBJC__)
#include "Firestore/core/src/firebase/firestore/model/resource_path.h"
#include "absl/strings/string_view.h"
namespace firebase {
namespace firestore {
namespace model {
/**
* DocumentKey represents the location of a document in the Firestore database.
*/
class DocumentKey {
public:
/** Creates a "blank" document key not associated with any document. */
DocumentKey() : path_{std::make_shared<ResourcePath>()} {
}
/** Creates a new document key containing a copy of the given path. */
explicit DocumentKey(const ResourcePath& path);
/** Creates a new document key, taking ownership of the given path. */
explicit DocumentKey(ResourcePath&& path);
#if defined(__OBJC__)
DocumentKey(FSTDocumentKey* key) // NOLINT(runtime/explicit)
: path_(std::make_shared<ResourcePath>(key.path)) {
}
operator FSTDocumentKey*() const {
return [FSTDocumentKey keyWithPath:path()];
}
std::string ToString() const {
return path().CanonicalString();
}
#endif
/**
* Creates and returns a new document key using '/' to split the string into
* segments.
*/
static DocumentKey FromPathString(const absl::string_view path) {
return DocumentKey{ResourcePath::FromString(path)};
}
/** Creates and returns a new document key with the given segments. */
static DocumentKey FromSegments(std::initializer_list<std::string> list) {
return DocumentKey{ResourcePath{list}};
}
/** Returns a shared instance of an empty document key. */
static const DocumentKey& Empty();
/** Returns true iff the given path is a path to a document. */
static bool IsDocumentKey(const ResourcePath& path) {
return path.size() % 2 == 0;
}
/** The path to the document. */
const ResourcePath& path() const {
return path_ ? *path_ : Empty().path();
}
private:
// This is an optimization to make passing DocumentKey around cheaper (it's
// copied often).
std::shared_ptr<const ResourcePath> path_;
};
inline bool operator==(const DocumentKey& lhs, const DocumentKey& rhs) {
return lhs.path() == rhs.path();
}
inline bool operator!=(const DocumentKey& lhs, const DocumentKey& rhs) {
return lhs.path() != rhs.path();
}
inline bool operator<(const DocumentKey& lhs, const DocumentKey& rhs) {
return lhs.path() < rhs.path();
}
inline bool operator<=(const DocumentKey& lhs, const DocumentKey& rhs) {
return lhs.path() <= rhs.path();
}
inline bool operator>(const DocumentKey& lhs, const DocumentKey& rhs) {
return lhs.path() > rhs.path();
}
inline bool operator>=(const DocumentKey& lhs, const DocumentKey& rhs) {
return lhs.path() >= rhs.path();
}
} // namespace model
} // namespace firestore
} // namespace firebase
#endif // FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_MODEL_DOCUMENT_KEY_H_
|