Skip to content

Commit 9b6c99c

Browse files
committed
Add a couple of workarounds for Swift on Windows
The C++ Interop efforts in Swift currently have some limitations. In particular, it cannot support trivial types with non-trivial destructors. As a workaround, provide a copy constructor which can be used by the Swift interop while using the regular semantics for all other cases. A second issue arises in the handling of futures. Unfortunately, it is not currently possible to pass an indirect block parameter which prevents the construction of a callback. Workaround this by providing an inline shim to use a direct parameter (i.e. indirect value through a pointer) which then allows a callback to be formed. Both of these items are being tracked upstream but seem to be potentially sufficient to enable the use of Swift for using the C++ SDK for desktop scenarios.
1 parent 5df80a2 commit 9b6c99c

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

app/src/include/firebase/future.h

+15
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,11 @@ class Future : public FutureBase {
407407
/// when you set up the callback.
408408
typedef void (*TypedCompletionCallback)(const Future<ResultType>& result_data,
409409
void* user_data);
410+
#if defined(__swift__)
411+
// TODO(apple/swift#67662) indirect block parameters are unsupported
412+
typedef void (*TypedCompletionCallback_SwiftWorkaround)(
413+
const Future<ResultType>* result_data, void* user_data);
414+
#endif
410415

411416
/// Construct a future.
412417
Future() {}
@@ -464,6 +469,16 @@ class Future : public FutureBase {
464469
inline void OnCompletion(TypedCompletionCallback callback,
465470
void* user_data) const;
466471

472+
#if defined(__swift__)
473+
// TODO(apple/swift#67662) indirect block parameters are unsupported
474+
inline void OnCompletion_SwiftWorkaround(
475+
TypedCompletionCallback_SwiftWorkaround callback, void *user_data) const {
476+
OnCompletion([callback, user_data](const Future<ResultType>& future) {
477+
callback(&future, user_data);
478+
});
479+
}
480+
#endif
481+
467482
#if defined(FIREBASE_USE_STD_FUNCTION) || defined(DOXYGEN)
468483
/// Register a single callback that will be called at most once, when the
469484
/// future is completed.

auth/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ build_flatbuffers("${flatbuffer_schemas}"
5151
# Common source files used by all platforms
5252
set(common_SRCS
5353
src/auth.cc
54+
src/auth_swift.cc
5455
src/credential.cc
5556
src/common.cc
5657
src/common.h

auth/src/auth_swift.cc

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2023 Google LLC
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+
#define __swift__ 50000
18+
#include "auth/src/include/firebase/auth.h"
19+
20+
#if FIREBASE_PLATFORM_WINDOWS
21+
namespace firebase {
22+
namespace auth {
23+
Auth::Auth(const Auth &) noexcept = default;
24+
}
25+
} // namespace firebase
26+
#endif

auth/src/include/firebase/auth.h

+7
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,13 @@ class Auth {
147147

148148
~Auth();
149149

150+
#if defined(__swift__)
151+
#if FIREBASE_PLATFORM_WINDOWS
152+
// TODO(apple/swift#67288) support trivial C++ types with non-trivial dtors
153+
Auth(const Auth&) noexcept;
154+
#endif
155+
#endif
156+
150157
/// Synchronously gets the cached current user, or returns an object where
151158
/// is_valid() == false if there is none.
152159
///

0 commit comments

Comments
 (0)