Skip to content

Commit 34d1aee

Browse files
var-consta-maurice
authored andcommitted
Change the signature of RunTransaction callback to Error (Transaction&, std::string&).
Mutable references are preferable in this case to pointers because they clearly indicate that the given argument is always valid (not null), potentially saving our users the trouble of checking explicitly. The C++ style guide now (cr/312129146) allows using mutable references as function parameters. Note that our users never invoke the callback (this is done by Firestore), so the absence of an ampersand at the call site will not affect readability for them in any way. PiperOrigin-RevId: 312197508
1 parent 4f03835 commit 34d1aee

File tree

12 files changed

+22
-21
lines changed

12 files changed

+22
-21
lines changed

firestore/src/android/firestore_android.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ Future<void> FirestoreInternal::RunTransaction(TransactionFunction* update,
370370

371371
#if defined(FIREBASE_USE_STD_FUNCTION) || defined(DOXYGEN)
372372
Future<void> FirestoreInternal::RunTransaction(
373-
std::function<Error(Transaction*, std::string*)> update) {
373+
std::function<Error(Transaction&, std::string&)> update) {
374374
LambdaTransactionFunction* lambda_update =
375375
new LambdaTransactionFunction(firebase::Move(update));
376376
return RunTransaction(lambda_update, /*is_lambda=*/true);

firestore/src/android/firestore_android.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class FirestoreInternal {
9191
bool is_lambda = false);
9292
#if defined(FIREBASE_USE_STD_FUNCTION) || defined(DOXYGEN)
9393
Future<void> RunTransaction(
94-
std::function<Error(Transaction*, std::string*)> update);
94+
std::function<Error(Transaction&, std::string&)> update);
9595
#endif // defined(FIREBASE_USE_STD_FUNCTION) || defined(DOXYGEN)
9696
Future<void> RunTransactionLastResult();
9797

firestore/src/android/lambda_transaction_function.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ class LambdaTransactionFunction
2828
public Promise<void, void, FirestoreFn>::Completion<void> {
2929
public:
3030
LambdaTransactionFunction(
31-
std::function<Error(Transaction*, std::string*)> update)
31+
std::function<Error(Transaction&, std::string&)> update)
3232
: update_(firebase::Move(update)) {
3333
FIREBASE_ASSERT(update_);
3434
}
3535

3636
// Override TransactionFunction::Apply().
37-
Error Apply(Transaction* transaction, std::string* error_message) override {
37+
Error Apply(Transaction& transaction, std::string& error_message) override {
3838
Error result = update_(transaction, error_message);
3939
return result;
4040
}
@@ -46,7 +46,7 @@ class LambdaTransactionFunction
4646
}
4747

4848
private:
49-
std::function<Error(Transaction*, std::string*)> update_;
49+
std::function<Error(Transaction&, std::string&)> update_;
5050
};
5151

5252
} // namespace firestore

firestore/src/android/transaction_android.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ jobject TransactionInternal::TransactionFunctionNativeApply(
215215
Transaction cpp_transaction(new TransactionInternal{firestore, transaction});
216216
cpp_transaction.internal_->ClearException();
217217
std::string message;
218-
Error code = transaction_function->Apply(&cpp_transaction, &message);
218+
Error code = transaction_function->Apply(cpp_transaction, message);
219219

220220
jobject first_exception =
221221
env->NewLocalRef(*(cpp_transaction.internal_->first_exception_));

firestore/src/common/firestore.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ Future<void> Firestore::RunTransaction(TransactionFunction* update) {
230230

231231
#if defined(FIREBASE_USE_STD_FUNCTION) || defined(DOXYGEN)
232232
Future<void> Firestore::RunTransaction(
233-
std::function<Error(Transaction*, std::string*)> update) {
233+
std::function<Error(Transaction&, std::string&)> update) {
234234
FIREBASE_ASSERT_MESSAGE(update, "invalid update parameter is passed in.");
235235
if (!internal_) return FailedFuture<void>();
236236
return internal_->RunTransaction(firebase::Move(update));

firestore/src/include/firebase/csharp/unity_transaction_function.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ ::firebase::Mutex* UnityTransactionFunction::mutex_ = new ::firebase::Mutex();
1010
UnityTransactionFunctionCallback
1111
UnityTransactionFunction::transaction_function_callback_ = nullptr;
1212

13-
Error UnityTransactionFunction::Apply(Transaction* transaction,
14-
std::string* error_message) {
13+
Error UnityTransactionFunction::Apply(Transaction& transaction,
14+
std::string& error_message) {
1515
MutexLock lock(*mutex_);
1616
if (transaction_function_callback_ != nullptr) {
17-
return transaction_function_callback_(callback_id_, transaction,
18-
error_message);
17+
return transaction_function_callback_(callback_id_, &transaction,
18+
&error_message);
1919
} else {
2020
FIREBASE_ASSERT_MESSAGE(
2121
false, "C++ transaction callback called before C# registered.");
@@ -42,8 +42,8 @@ Future<void> UnityTransactionFunction::RunTransactionOn(int32_t callback_id,
4242
Firestore* firestore) {
4343
UnityTransactionFunction unity_transaction_function(callback_id);
4444
return firestore->RunTransaction(
45-
[unity_transaction_function](Transaction* transaction,
46-
std::string* error_message) mutable {
45+
[unity_transaction_function](Transaction& transaction,
46+
std::string& error_message) mutable {
4747
return unity_transaction_function.Apply(transaction, error_message);
4848
});
4949
}

firestore/src/include/firebase/csharp/unity_transaction_function.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class UnityTransactionFunction : public TransactionFunction {
5252
* Implementation of TransactionFunction::Apply() that forwards to the C#
5353
* global delegate, passing along this->callback_id_ for context.
5454
*/
55-
Error Apply(Transaction* transaction, std::string* error_message) override;
55+
Error Apply(Transaction& transaction, std::string& error_message) override;
5656

5757
private:
5858
explicit UnityTransactionFunction(int32_t callback_id)

firestore/src/include/firebase/firestore.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,14 +242,15 @@ class Firestore {
242242
* 5 attempts, the transaction will fail.
243243
*
244244
* @param update function or lambda to execute within the transaction context.
245+
* The string reference parameter can be used to set the error message.
245246
*
246247
* @return A Future that will be resolved when the transaction finishes.
247248
*
248249
* @note This method is not available when using the STLPort C++ runtime
249250
* library.
250251
*/
251252
virtual Future<void> RunTransaction(
252-
std::function<Error(Transaction*, std::string*)> update);
253+
std::function<Error(Transaction&, std::string&)> update);
253254
#endif // defined(FIREBASE_USE_STD_FUNCTION) || defined(DOXYGEN)
254255

255256
/**

firestore/src/include/firebase/firestore/transaction.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ class TransactionFunction {
153153
* @return Either Error::kOk if successful or the error code from Error that
154154
* most closely matches the failure.
155155
*/
156-
virtual Error Apply(Transaction* transaction, std::string* error_message) = 0;
156+
virtual Error Apply(Transaction& transaction, std::string& error_message) = 0;
157157
};
158158

159159
} // namespace firestore

firestore/src/ios/firestore_ios.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,13 @@ WriteBatch FirestoreInternal::batch() const {
123123

124124
Future<void> FirestoreInternal::RunTransaction(TransactionFunction* update) {
125125
return RunTransaction(
126-
[update](Transaction* transaction, std::string* error_message) {
126+
[update](Transaction& transaction, std::string& error_message) {
127127
return update->Apply(transaction, error_message);
128128
});
129129
}
130130

131131
Future<void> FirestoreInternal::RunTransaction(
132-
std::function<Error(Transaction*, std::string*)> update) {
132+
std::function<Error(Transaction&, std::string&)> update) {
133133
auto executor = absl::ShareUniquePtr(Executor::CreateConcurrent(
134134
"com.google.firebase.firestore.transaction", /*threads=*/5));
135135
auto promise =
@@ -151,7 +151,7 @@ Future<void> FirestoreInternal::RunTransaction(
151151
new TransactionInternal(core_transaction, this);
152152
Transaction transaction{transaction_internal};
153153

154-
Error error_code = update(&transaction, &error_message);
154+
Error error_code = update(transaction, error_message);
155155
if (error_code == Error::kOk) {
156156
eventual_result_callback(Status::OK());
157157
} else {

0 commit comments

Comments
 (0)