Skip to content

Commit a848452

Browse files
author
Kevin Welton
committed
Interoperability Helper Functions document updated to correctly describe the use of the renamed abi helper functions
1 parent ea04c2a commit a848452

File tree

1 file changed

+32
-26
lines changed

1 file changed

+32
-26
lines changed

Docs/Interoperability Helper Functions.md

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,28 @@ Interoperability Helper Functions
33

44
The base library provides a set of helper functions that are used within the projection to deal with type conversion generically between the ABI and the projected API surface. While this remains their primary purpose, they are also useful in cases where you might need to interop between C++/WinRT and other APIs or libraries that may not have any knowledge of C++/WinRT. This document briefly describes these helper functions and how you might use them to perform various common operations related to interop. Unless otherwise noted, all of the functions below are free functions available in the winrt namespace.
55

6-
`winrt::get`
6+
`winrt::get_abi`
77
---
88

9-
The `get` function returns the ABI representation for a given value. C++/WinRT does not provide implicit conversions to the ABI types, so you might call this function to get a raw pointer to the COM interface backing a runtime class or interface. For example:
9+
The `get_abi` function returns the ABI representation for a given value. C++/WinRT does not provide implicit conversions to the ABI types, so you might call this function to get a raw pointer to the COM interface backing a runtime class or interface. For example:
1010

1111
```C++
1212
using namespace Windows::Foundation;
1313
IAsyncInfo info = ...
1414

15-
ABI::Windows::Foundation::IAsyncInfo * ptr = winrt::get(info);
15+
ABI::Windows::Foundation::IAsyncInfo * ptr = winrt::get_abi(info);
1616
```
1717
18-
Note that the `get` function does not increment the reference count so it is a useful and efficient way to pass the pointer to some other function, provided it is synchronous in nature. You can also save some typing by using the `abi` alias template to avoid uttering the name of the ABI type:
18+
Note that the `get_abi` function does not increment the reference count so it is a useful and efficient way to pass the pointer to some other function, provided it is synchronous in nature. You can also save some typing by using the `abi` alias template to avoid uttering the name of the ABI type:
1919
2020
```C++
2121
using namespace Windows::Foundation;
2222
IAsyncInfo info;
2323
24-
abi<IAsyncInfo> * ptr = winrt::get(info);
24+
abi<IAsyncInfo> * ptr = winrt::get_abi(info);
2525
```
2626

27-
The `get` function works equally with other types like hstring:
27+
The `get_abi` function works equally with other types like hstring:
2828

2929
```C++
3030
HRESULT LogMessage(HSTRING value);
@@ -33,24 +33,24 @@ int main()
3333
{
3434
winrt::hstring message = L"hello world";
3535

36-
HRESULT hr = LogMessage(winrt::get(message));
36+
HRESULT hr = LogMessage(winrt::get_abi(message));
3737
}
3838
```
3939
40-
`winrt::put`
40+
`winrt::put_abi`
4141
---
4242
43-
The `put` function returns the address of the ABI representation for a given value. This supports the COM convention of returning references as `out` parameters and is useful for safely taking ownership of a value returned from a classic COM or C-style function. For example:
43+
The `put_abi` function returns the address of the ABI representation for a given value. This supports the COM convention of returning references as `out` parameters and is useful for safely taking ownership of a value returned from a classic COM or C-style function. For example:
4444
4545
```C++
46-
HRESULT GetAsyncInfo(ABI::Windows::Foundation::IAsyncInfo ** value);
46+
HRESULT GetAsyncInfo(/* [out, retval] */ ABI::Windows::Foundation::IAsyncInfo ** value);
4747
4848
int main()
4949
{
5050
using namespace Windows::Foundation;
5151
IAsyncInfo info;
5252
53-
HRESULT hr = GetAsyncInfo(winrt::put(info));
53+
HRESULT hr = GetAsyncInfo(winrt::put_abi(info));
5454
}
5555
```
5656

@@ -63,56 +63,62 @@ int main()
6363
{
6464
winrt::hstring message;
6565

66-
HRESULT hr = GetMessage(winrt::put(message));
66+
HRESULT hr = GetMessage(winrt::put_abi(message));
6767
}
6868
```
6969
70-
`winrt::attach` & `winrt::detach`
70+
`winrt::attach_abi` & `winrt::detach_abi`
7171
---------------
7272
73-
The `attach` function takes ownership of an ABI value. Assuming you have a value, such as a COM interface pointer that is not owned, you can use the `attach` function to give ownership of it to a C++/WinRT type. The `attach` function is complemented by the `detach` function that does the inverse. For example:
73+
The `attach_abi` function takes ownership of an ABI value. Assuming you have a value, such as a COM interface pointer that is not owned, you can use the `attach_abi` function to give ownership of it to a C++/WinRT type. The `attach_abi` function is complemented by the `detach_abi` function that does the inverse.
74+
75+
For example:
7476
7577
```C++
7678
IAsyncInfo info = ...
7779
78-
abi<IAsyncInfo> * ptr = winrt::detach(info);
80+
abi<IAsyncInfo> * ptr = winrt::detach_abi(info);
7981
80-
winrt::attach(info, ptr);
82+
winrt::attach_abi(info, ptr);
8183
```
8284

83-
This may be useful if you need to pass ownership between C++/WinRT and some other library like WRL or C++/CX. The same is possible with other types like `winrt::hstring`:
85+
This may be useful if you need to pass ownership between C++/WinRT and some other library like WRL or C++/CX.
86+
87+
The same is possible with other types like `winrt::hstring`:
8488

8589
```C++
8690
winrt::hstring message = L"hello world";
8791

88-
HSTRING handle = winrt::detach(message);
92+
HSTRING handle = winrt::detach_abi(message);
8993
uint32_t size = WindowsGetStringLen(handle);
9094

91-
winrt::attach(message, handle);
95+
winrt::attach_abi(message, handle);
9296
assert(message.size() == size);
9397
```
9498
95-
`winrt::copy_to` & `winrt::copy_from`
99+
`winrt::copy_to_abi` & `winrt::copy_from_abi`
96100
-------------------
97101
98-
The `copy_to` and `copy_from` functions are similar to `attach` and `detach` except that they don't transfer ownership. Instead, they copy the underlying reference effectively calling `AddRef` or some equivalent. This is useful if you need to hold on to a reference but also need to return a reference-counted object to the caller. For example:
102+
The `copy_to_abi` and `copy_from_abi` functions are similar to `attach_abi` and `detach_abi` except that they don't transfer ownership. Instead, they copy the underlying reference effectively calling `AddRef` or some equivalent. This is useful if you need to hold on to a reference but also need to return a reference-counted object to the caller.
103+
104+
For example:
99105
100106
```C++
101107
abi<IAsyncInfo> * ptr = ...
102108
103109
IAsyncInfo info;
104-
winrt::copy_from(info, ptr);
110+
winrt::copy_from_abi(info, ptr);
105111
```
106112

107-
In this example, the `copy_from` function first releases any reference held by the `info` object before acquiring a copy of `ptr`. If `ptr` does not hold a `nullptr` then `AddRef` is called so that the `info` variable has its own reference to the instance.
113+
In the preceding example, the `copy_from_abi` function first releases any reference held by the `info` object before acquiring a copy of `ptr`. If `ptr` does not hold a `nullptr` then `AddRef` is called so that the `info` variable has its own reference to the instance.
108114

109115
```C++
110116
IAsyncInfo info = ...
111117

112118
abi<IAsyncInfo> * ptr = nullptr;
113-
winrt::copy_to(info, ptr);
119+
winrt::copy_to_abi(info, ptr);
114120
```
115121
116-
In this example, the `info` object presumably holds a reference to some instance and would like to give `ptr` its own reference. If `info` is not holding a reference then `ptr` will be assigned a `nullptr` value.
122+
In the preceding example, the `info` object presumably holds a reference to some instance and would like to give `ptr` its own reference. If `info` is not holding a reference then `ptr` will be assigned a `nullptr` value.
117123
118-
`copy_to` and `copy_from` are thus very useful for dealing with ABI parameters as they follow the rules of COM and make it very convenient for interoperability.
124+
The `copy_to_abi` and `copy_from_abi` functions are thus very useful for dealing with ABI parameters as they follow the rules of COM and make it very convenient for interoperability.

0 commit comments

Comments
 (0)