Skip to content

Commit

Permalink
Add a couple of workarounds for Swift on Windows
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
compnerd committed Aug 7, 2023
1 parent f198c6f commit c912e46
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
15 changes: 15 additions & 0 deletions app/src/include/firebase/future.h
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,11 @@ class Future : public FutureBase {
/// when you set up the callback.
typedef void (*TypedCompletionCallback)(const Future<ResultType>& result_data,
void* user_data);
#if defined(__swift__)
// TODO(apple/swift#67662) indirect block parameters are unsupported
typedef void (*TypedCompletionCallback_SwiftWorkaround)(
const Future<ResultType>* result_data, void* user_data);
#endif

/// Construct a future.
Future() {}
Expand Down Expand Up @@ -464,6 +469,16 @@ class Future : public FutureBase {
inline void OnCompletion(TypedCompletionCallback callback,
void* user_data) const;

#if defined(__swift__)
// TODO(apple/swift#67662) indirect block parameters are unsupported
inline void OnCompletion_SwiftWorkaround(
TypedCompletionCallback_SwiftWorkaround callback, void* user_data) const {
OnCompletion([callback, user_data](const Future<ResultType>& future) {
callback(&future, user_data);
});
}
#endif

#if defined(FIREBASE_USE_STD_FUNCTION) || defined(DOXYGEN)
/// Register a single callback that will be called at most once, when the
/// future is completed.
Expand Down
1 change: 1 addition & 0 deletions auth/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ build_flatbuffers("${flatbuffer_schemas}"
# Common source files used by all platforms
set(common_SRCS
src/auth.cc
src/auth_swift.cc
src/credential.cc
src/common.cc
src/common.h
Expand Down
26 changes: 26 additions & 0 deletions auth/src/auth_swift.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2023 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.
*/

#define __swift__ 50000
#include "auth/src/include/firebase/auth.h"

#if FIREBASE_PLATFORM_WINDOWS
namespace firebase {
namespace auth {
Auth::Auth(const Auth &) noexcept = default;
}
} // namespace firebase
#endif
7 changes: 7 additions & 0 deletions auth/src/include/firebase/auth.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@ class Auth {

~Auth();

#if defined(__swift__)
#if FIREBASE_PLATFORM_WINDOWS
// TODO(apple/swift#67288) support trivial C++ types with non-trivial dtors
Auth(const Auth&) noexcept;
#endif
#endif

/// Synchronously gets the cached current user, or returns an object where
/// is_valid() == false if there is none.
///
Expand Down

0 comments on commit c912e46

Please sign in to comment.