Skip to content

Commit

Permalink
Less clever #includes for IWeakReference (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanlabelle authored Apr 1, 2024
1 parent d7683d3 commit 54f6187
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 6 deletions.
56 changes: 56 additions & 0 deletions swiftwinrt/Resources/CWinRT/WeakReference.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// The real weakreference.h will provide different definitions in C vs C++ compilation mode.
// Override it with ours so we ensure we get the C definitions.

#include <objidl.h>
#include <inspectable.h>

#ifndef _IWEAKREFERENCE_DEFINED
#define _IWEAKREFERENCE_DEFINED
typedef interface IWeakReference IWeakReference;
#endif

typedef struct IWeakReferenceVtbl
{
BEGIN_INTERFACE

HRESULT (STDMETHODCALLTYPE* QueryInterface)(__RPC__in IWeakReference* This,
REFIID riid,
_COM_Outptr_ void** ppvObject);
ULONG (STDMETHODCALLTYPE* AddRef)(__RPC__in IWeakReference* This);
ULONG (STDMETHODCALLTYPE* Release)(__RPC__in IWeakReference* This);
HRESULT (STDMETHODCALLTYPE* Resolve)(__RPC__in IWeakReference* This,
__RPC__in REFIID riid,
__RPC__deref_out_opt IInspectable **objectReference);

END_INTERFACE
} IWeakReferenceVtbl;

interface IWeakReference
{
CONST_VTBL struct IWeakReferenceVtbl* lpVtbl;
};

#ifndef _IWEAKREFERENCESOURCE_DEFINED
#define _IWEAKREFERENCESOURCE_DEFINED
typedef interface IWeakReferenceSource IWeakReferenceSource;
#endif

typedef struct IWeakReferenceSourceVtbl
{
BEGIN_INTERFACE

HRESULT (STDMETHODCALLTYPE* QueryInterface)(__RPC__in IWeakReferenceSource* This,
REFIID riid,
_COM_Outptr_ void** ppvObject);
ULONG (STDMETHODCALLTYPE* AddRef)(__RPC__in IWeakReferenceSource* This);
ULONG (STDMETHODCALLTYPE* Release)(__RPC__in IWeakReferenceSource* This);
HRESULT (STDMETHODCALLTYPE* GetWeakReference)(__RPC__in IWeakReferenceSource* This,
__RPC__deref_out_opt IWeakReference **weakReference);

END_INTERFACE
} IWeakReferenceSourceVtbl;

interface IWeakReferenceSource
{
CONST_VTBL struct IWeakReferenceSourceVtbl* lpVtbl;
};
5 changes: 2 additions & 3 deletions swiftwinrt/abi_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,12 +291,11 @@ namespace swiftwinrt
w.c_mod = settings.get_c_module_name();
write_preamble(w, /* swift_code: */ false);
w.write(R"(#pragma once
#define CINTERFACE
#include <wtypesbase.h>
#include <minwindef.h>
#include <winnt.h>
#include <combaseapi.h> // IUnknown, CoCreateInstance
#include <inspectable.h> // IInspectable
#include <oleauto.h> // BSTR, Sys***String***
#include <roapi.h> // Ro***
#include "RestrictedErrorInfo.h" // IRestrictedErrorInfo (C definition)
Expand All @@ -315,8 +314,8 @@ namespace swiftwinrt
#pragma clang diagnostic ignored "-Wmicrosoft-enum-forward-reference"
#include "CppInteropWorkaround.h" // TODO(WIN-860): Remove workaround once C++ interop issues with WinSDK.GUID are fixed.
#include <weakreference.h>
#include "MemoryBuffer.h" // IMemoryBufferByteAccess (C definition)
#include "WeakReference.h" // IWeakReference[Source] (C definition)
#include "robuffer.h" // IBufferByteAccess (C definition)
)");
for (auto& [ns, members] : namespaces)
Expand Down
2 changes: 2 additions & 0 deletions swiftwinrt/file_writers.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ namespace swiftwinrt

auto memorybuffer_h_template = find_resource(RESOURCE_TYPE_OTHER_FILE_STR, RESOURCE_NAME_CWINRT_MEMORYBUFFER_H_STR);
fill_template_placeholders_to_file(memorybuffer_h_template, dir_path / "include" / "MemoryBuffer.h");
auto weakreference_h_template = find_resource(RESOURCE_TYPE_OTHER_FILE_STR, RESOURCE_NAME_CWINRT_WEAKREFERENCE_H_STR);
fill_template_placeholders_to_file(weakreference_h_template, dir_path / "include" / "WeakReference.h");

if (settings.has_project_type(project_type::spm))
{
Expand Down
3 changes: 3 additions & 0 deletions swiftwinrt/resources.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
#define RESOURCE_NAME_CWINRT_MEMORYBUFFER_H CWINRT_MEMORYBUFFER
#define RESOURCE_NAME_CWINRT_MEMORYBUFFER_H_STR "CWINRT_MEMORYBUFFER"

#define RESOURCE_NAME_CWINRT_WEAKREFERENCE_H CWINRT_WEAKREFERENCE
#define RESOURCE_NAME_CWINRT_WEAKREFERENCE_H_STR "CWINRT_WEAKREFERENCE"

#ifndef RC_INVOKED

#include <span>
Expand Down
1 change: 1 addition & 0 deletions swiftwinrt/resources.rc
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ RESOURCE_NAME_CWINRT_PACKAGE_SWIFT RESOURCE_TYPE_OTHER_FILE "Resources\\CWinRT\\

RESOURCE_NAME_CWINRT_ROBUFFER_H RESOURCE_TYPE_OTHER_FILE "Resources\\CWinRT\\robuffer.h"
RESOURCE_NAME_CWINRT_MEMORYBUFFER_H RESOURCE_TYPE_OTHER_FILE "Resources\\CWinRT\\MemoryBuffer.h"
RESOURCE_NAME_CWINRT_WEAKREFERENCE_H RESOURCE_TYPE_OTHER_FILE "Resources\\CWinRT\\WeakReference.h"
5 changes: 2 additions & 3 deletions tests/test_component/Sources/CWinRT/include/Ctest_component.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// WARNING: Please don't edit this file. It was generated by Swift/WinRT v0.0.1

#pragma once
#define CINTERFACE

#include <wtypesbase.h>
#include <minwindef.h>
#include <winnt.h>
#include <combaseapi.h> // IUnknown, CoCreateInstance
#include <inspectable.h> // IInspectable
#include <oleauto.h> // BSTR, Sys***String***
#include <roapi.h> // Ro***
#include "RestrictedErrorInfo.h" // IRestrictedErrorInfo (C definition)
Expand All @@ -25,8 +24,8 @@
#pragma clang diagnostic ignored "-Wmicrosoft-enum-forward-reference"

#include "CppInteropWorkaround.h" // TODO(WIN-860): Remove workaround once C++ interop issues with WinSDK.GUID are fixed.
#include <weakreference.h>
#include "MemoryBuffer.h" // IMemoryBufferByteAccess (C definition)
#include "WeakReference.h" // IWeakReference[Source] (C definition)
#include "robuffer.h" // IBufferByteAccess (C definition)
#include "Windows.AI.MachineLearning.h"
#include "Windows.AI.MachineLearning.Preview.h"
Expand Down

0 comments on commit 54f6187

Please sign in to comment.