MCRO
C++23 utilities for Unreal Engine.
Loading...
Searching...
No Matches
Cast.h
Go to the documentation of this file.
1/** @noop License Comment
2 * @file
3 * @copyright
4 * This Source Code is subject to the terms of the Mozilla Public License, v2.0.
5 * If a copy of the MPL was not distributed with this file You can obtain one at
6 * https://mozilla.org/MPL/2.0/
7 *
8 * @author David Mórász
9 * @date 2025
10 */
11
12#pragma once
13
14#include "CoreMinimal.h"
16#include "Mcro/TextMacros.h"
17#include "Microsoft/COMPointer.h"
18
20{
21 using namespace Mcro::Windows::Error;
22
23 template <typename From, typename To>
24 FCanFail ComCast(From* from, TComPtr<To>& to, bool fastError = false)
25 {
26 HRESULT hr = from->QueryInterface(IID_PPV_ARGS(&to));
27 if (UNLIKELY(hr != S_OK))
28 return IError::Make(new FHresultError(hr, fastError))
29 ->AsRecoverable()
30 ->WithMessageF(
31 TEXT_"Object of type {0} did not implement {1}",
34 );
35 return Success();
36 }
37
38 template <typename From, typename To>
39 FCanFail ComCast(TComPtr<From> const& from, TComPtr<To>& to, bool fastError = false)
40 {
41 return ComCast(from.Get(), to, fastError);
42 }
43
44 template <typename From, typename To>
45 TMaybe<TComPtr<To>> ComCast(From* from, bool fastError = false)
46 {
47 TComPtr<To> to;
48 ComCast(from, to, fastError);
49 return MoveTemp(to);
50 }
51
52 template <typename From, typename To>
53 TMaybe<TComPtr<To>> ComCast(TComPtr<From> const& from, bool fastError = false)
54 {
55 return ComCast(from.Get(), fastError);
56 }
57}
Use leading TEXT_ without parenthesis for Unreal compatible text literals.
#define TEXT_
A convenience alternative to Unreal's own TEXT macro but this one doesn't require parenthesis around ...
Definition TextMacros.h:53
static TSharedRef< T > Make(T *newError, Args &&... args)
To ensure automatic type reflection use IError::Make instead of manually constructing error objects.
Definition Error.h:148
An error wrapping HRESULT code returned by many Microsoft APIs. It will also collect human readable m...
FORCEINLINE FCanFail Success()
Return an FCanFail or FTrueOrReason indicating a success or truthy output.
Definition Error.h:685
constexpr FStringView TTypeName
Get a friendly string of an input type without using typeid(T).name().
Definition TypeName.h:161
FCanFail ComCast(From *from, TComPtr< To > &to, bool fastError=false)
Definition Cast.h:24
A TValueOrError alternative for IError which allows implicit conversion from values and errors (no ne...
Definition Error.h:590