- Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathquery_ios.h
253 lines (210 loc) · 9.52 KB
/
query_ios.h
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// Copyright 2016 Google LLC
//
// 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 FIREBASE_DATABASE_SRC_IOS_QUERY_IOS_H_
#defineFIREBASE_DATABASE_SRC_IOS_QUERY_IOS_H_
#include<memory>
#include"app/src/include/firebase/future.h"
#include"app/src/include/firebase/internal/common.h"
#include"app/src/reference_counted_future_impl.h"
#include"app/src/util_ios.h"
#include"database/src/common/query_spec.h"
#include"database/src/include/firebase/database.h"
#include"database/src/include/firebase/database/listener.h"
#ifdef __OBJC__
#include<Foundation/Foundation.h>
#import"FIRDatabase.h"
#endif// __OBJC__
#ifdef __OBJC__
// Object which stores references to C++ objects that are used by observer
// blocks.
@interfaceFIRCPPDatabaseQueryCallbackState : NSObject
// Guarded by lock.
@property(nonatomic, nullable) firebase::database::internal::DatabaseInternal*
databaseInternal;
// Guarded by lock.
@property(nonatomic, nullable) FIRDatabaseQuery* databaseQuery;
// Guarded by lock.
@property(nonatomic, nullable) firebase::database::ValueListener* valueListener;
// Guarded by lock.
@property(nonatomic, nullable) firebase::database::ChildListener* childListener;
// Callers should lock the lock property while using any properties of this
// object.
@property(nonatomic, readonly) NSRecursiveLock* _Nonnull lock;
-(_Nullable instancetype) init NS_UNAVAILABLE;
// Initialize this state.
-(_Nonnull instancetype)
initWithDatabase:(firebase::database::internal::DatabaseInternal* _Nonnull)
databaseInternal
andQuery:(FIRDatabaseQuery* _Nonnull)databaseQuery
andValueListener:(firebase::database::ValueListener* _Nullable)valueListener
andChildListener:(firebase::database::ChildListener* _Nullable)
childListener NS_DESIGNATED_INITIALIZER;
// Associate an observer with this state.
-(void)addObserverHandle:(FIRDatabaseHandle)handle;
// Remove all observer handles managed by this state from the database and
// remove references to C++ objects.
-(void)removeAllObservers;
@end
#endif// __OBJC__
namespace firebase {
class ReferenceCountedFutureImpl;
namespace database {
namespace internal {
// This defines the class FIRDatabaseQueryPointer, which is a C++-compatible
// wrapper around the FIRDatabaseQuery Obj-C class.
OBJ_C_PTR_WRAPPER(FIRDatabaseQuery);
// This defines the class FIRCPPDatabaseQueryCallbackStatePointer, which is a
// C++-compatible wrapper around the FIRCPPDatabaseQueryCallbackState Obj-C
// class.
OBJ_C_PTR_WRAPPER(FIRCPPDatabaseQueryCallbackState);
#pragma clang assume_nonnull begin
/// The iOS implementation of the Query class, used for reading data.
class QueryInternal {
public:
QueryInternal(DatabaseInternal* database,
std::unique_ptr<FIRDatabaseQueryPointer> query);
QueryInternal(DatabaseInternal* database,
std::unique_ptr<FIRDatabaseQueryPointer> query,
const internal::QuerySpec& query_spec);
QueryInternal(const QueryInternal& query);
QueryInternal& operator=(const QueryInternal& query);
#if defined(FIREBASE_USE_MOVE_OPERATORS) || defined(DOXYGEN)
QueryInternal(QueryInternal&& query);
QueryInternal& operator=(QueryInternal&& query);
#endif// defined(FIREBASE_USE_MOVE_OPERATORS) || defined(DOXYGEN)
virtual ~QueryInternal();
// Gets the value of the query for the given location a single time.
Future<DataSnapshot> GetValue();
// Gets the result of the most recent call to GetValue().
Future<DataSnapshot> GetValueLastResult();
// Adds a listener that will be called immediately and then again any time the
// data changes.
voidAddValueListener(ValueListener* listener);
// Removes a listener that was previously added with AddValueListener().
voidRemoveValueListener(ValueListener* listener);
// Removes all value listeners that were added with AddValueListener().
voidRemoveAllValueListeners();
// Adds a listener that will be called any time a child is added, removed,
// modified, or reordered.
voidAddChildListener(ChildListener* listener);
// Removes a listener that was previously added with AddChildListener().
voidRemoveChildListener(ChildListener* listener);
// Removes all child listeners that were added by AddChildListener().
voidRemoveAllChildListeners();
// Gets a DatabaseReference corresponding to the given location.
//
// The returned pointer should be passed to a DatabaseReference for lifetime
// management.
DatabaseReferenceInternal* GetReference();
// Sets whether this location's data should be kept in sync even if there are
// no active Listeners.
voidSetKeepSynchronized(bool keep_sync);
// Gets a query in which child nodes are ordered by the values of the
// specified path. Any previous OrderBy directive will be replaced in the
// returned Query.
//
// The returned pointer should be passed to a Query for lifetime management.
QueryInternal* OrderByChild(constchar* _Nonnull path);
// Gets a query in which child nodes are ordered by the values of the
// specified path. Any previous OrderBy directive will be replaced in the
// returned Query.
//
// The returned pointer should be passed to a Query for lifetime management.
QueryInternal* OrderByKey();
// Gets a query in which child nodes are ordered by their priority. Any
// previous OrderBy directive will be replaced in the returned Query.
//
// The returned pointer should be passed to a Query for lifetime management.
QueryInternal* OrderByPriority();
// Create a query in which nodes are ordered by their value.
//
// The returned pointer should be passed to a Query for lifetime management.
QueryInternal* OrderByValue();
// Get a Query constrained to nodes with the given sort value or higher.
//
// The returned pointer should be passed to a Query for lifetime management.
QueryInternal* StartAt(Variant order_value);
// Get a Query constrained to nodes with the given sort value or higher, and
// the given key or higher.
//
// The returned pointer should be passed to a Query for lifetime management.
QueryInternal* StartAt(Variant order_value, constchar* child_key);
// Get a Query constrained to nodes with the given sort value or lower.
//
// The returned pointer should be passed to a Query for lifetime management.
QueryInternal* EndAt(Variant order_value);
// Get a Query constrained to nodes with the given sort value or lower, and
// the given key or lower.
//
// The returned pointer should be passed to a Query for lifetime management.
QueryInternal* EndAt(Variant order_value, constchar* child_key);
// Get a Query constrained to nodes with the exact given sort value.
//
// The returned pointer should be passed to a Query for lifetime management.
QueryInternal* EqualTo(Variant order_value);
// Get a Query constrained to nodes with the exact given sort value, and the
// exact given key.
//
// The returned pointer should be passed to a Query for lifetime management.
QueryInternal* EqualTo(Variant order_value, constchar* child_key);
// Gets a Query limited to only the first results.
//
// The returned pointer should be passed to a Query for lifetime management.
QueryInternal* LimitToFirst(size_t limit);
// Gets a Query limited to only the last results.
//
// The returned pointer should be passed to a Query for lifetime management.
QueryInternal* LimitToLast(size_t limit);
const internal::QuerySpec& query_spec() const { return query_spec_; }
DatabaseInternal* database_internal() const { return database_; }
protected:
#ifdef __OBJC__
FIRDatabaseQuery* impl() const { return impl_->get(); }
#endif// __OBJC__
internal::QuerySpec query_spec_;
DatabaseInternal* database_;
private:
// Get the Future for the QueryInternal.
ReferenceCountedFutureImpl* query_future();
// Object lifetime managed by Objective C ARC.
std::unique_ptr<FIRDatabaseQueryPointer> impl_;
// The memory location of this member variable is used to look up our
// ReferenceCountedFutureImpl. We can't use "this" because QueryInternal and
// DatabaseReferenceInternal require two separate ReferenceCountedFutureImpl
// instances, but have the same "this" pointer as one is a subclass of the
// other.
int future_api_id_;
};
#pragma clang assume_nonnull end
// Used by Query::GetValue().
class SingleValueListener : public ValueListener {
public:
SingleValueListener(
ReferenceCountedFutureImpl* _Nonnull future,
const SafeFutureHandle<DataSnapshot>& handle,
const FIRCPPDatabaseQueryCallbackStatePointer& callback_state);
// Unregister ourselves from the database.
virtual ~SingleValueListener();
virtual voidOnValueChanged(const DataSnapshot& snapshot);
virtual voidOnCancelled(const Error& error_code,
constchar* _Nullable error_message);
private:
ReferenceCountedFutureImpl* _Nonnull future_;
SafeFutureHandle<DataSnapshot> handle_;
std::unique_ptr<FIRCPPDatabaseQueryCallbackStatePointer> callback_state_;
};
} // namespace internal
} // namespace database
} // namespace firebase
#endif// FIREBASE_DATABASE_SRC_IOS_QUERY_IOS_H_