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 "Microsoft/COMPointer.h"
17
19{
20 using namespace Mcro::Windows::Error;
21
22 template <typename From, typename To>
23 FCanFail ComCast(From* from, TComPtr<To>& to, bool fastError = false)
24 {
25 HRESULT hr = from->QueryInterface(IID_PPV_ARGS(&to));
26 if (UNLIKELY(hr != S_OK))
27 return IError::Make(new FHresultError(hr, fastError))
28 ->AsRecoverable()
29 ->WithMessageF(
30 TEXT("Object of type %s did not implement %s"),
33 );
34 return Success();
35 }
36
37 template <typename From, typename To>
38 FCanFail ComCast(TComPtr<From> const& from, TComPtr<To>& to, bool fastError = false)
39 {
40 return ComCast(from.Get(), to, fastError);
41 }
42
43 template <typename From, typename To>
44 TMaybe<TComPtr<To>> ComCast(From* from, bool fastError = false)
45 {
46 TComPtr<To> to;
47 ComCast(from, to, fastError);
48 return MoveTemp(to);
49 }
50
51 template <typename From, typename To>
52 TMaybe<TComPtr<To>> ComCast(TComPtr<From> const& from, bool fastError = false)
53 {
54 return ComCast(from.Get(), fastError);
55 }
56}
static TSharedRef< T > Make(T *newError, Args &&... args)
Definition Error.h:136
FORCEINLINE FCanFail Success()
Definition Error.h:590
const FString TTypeString
Definition TypeName.h:146
FCanFail ComCast(From *from, TComPtr< To > &to, bool fastError=false)
Definition Cast.h:23