MCRO
C++23 utilities for Unreal Engine.
Loading...
Searching...
No Matches
Mcro::Any::FAny Struct Reference

A simplistic but type-safe and RAII compliant storage for anything. Enclosed data is owned by this type. More...

#include <Any.h>

Public Member Functions

template<typename T >
 FAny (T *newObject, TAnyTypeFacilities< T > const &facilities={})
 
FORCEINLINE FAny ()
 
 FAny (FAny const &other)
 
 FAny (FAny &&other)
 
 ~FAny ()
 
template<typename T >
const T * TryGet () const
 
template<typename T >
T * TryGet ()
 
template<typename T , typename Self >
decltype(auto) WithAlias (this Self &&self)
 Specify one type the enclosed value can be safely cast to, and is valid to be used with TryGet.
 
template<typename Self , typename... T>
decltype(auto) With (this Self &&self, TTypes< T... > &&)
 Specify multiple types the enclosed value can be safely cast to, and are valid to be used with TryGet.
 
FORCEINLINE bool IsValid () const
 
FORCEINLINE FType GetType () const
 
FORCEINLINE TSet< FType > const & GetValidTypes () const
 

Detailed Description

A simplistic but type-safe and RAII compliant storage for anything. Enclosed data is owned by this type.

Use this with care, the underlying data can be only accessed with the same type as it has been constructed with, or with types provided by ValidAs. This means derived classes cannot be accessed with their base types safely and implicitly. MCRO however provides methods for classes to allow them exposing base types to FAny (and other facilities):

class FMyThing : public TInherit<IFoo, IBar, IEtc>
{
// ...
}
Inherit via this template to allow other API to reflect upon the base types of deriving class....

TInherit has a member alias using Bases = TTypes<...> and that can be used by FAny to automatically register base classes as compatible ones.

Enclosed value is recommended to be copy constructible. It may yield a runtime error otherwise. Moving an FAny will just transfer ownership of the wrapped object but will not move construct a new object. The source FAny will be reset to an invalid state.

Todo
C++ 26 has promising proposal for static value-based reflection, which can gather metadata from classes or even emit them. The best summary I found so far is a stack-overflow answer https://stackoverflow.com/a/77477029 Once that's available we can gather base classes in compile time, and do dynamic casting of objects without the need for intrusive extra syntax, or extra work at construction. Currently GCC's __bases would be perfect for the job, but other popular compilers don't have similar intrinsics. Once such a feature becomes widely available base classes can be automatically added as aliases for types wrapped in FAny.

Definition at line 92 of file Any.h.

Constructor & Destructor Documentation

◆ FAny() [1/4]

template<typename T >
Mcro::Any::FAny::FAny ( T * newObject,
TAnyTypeFacilities< T > const & facilities = {} )
inline

Definition at line 95 of file Any.h.

◆ FAny() [2/4]

FORCEINLINE Mcro::Any::FAny::FAny ( )
inline

Definition at line 124 of file Any.h.

◆ FAny() [3/4]

Mcro::Any::FAny::FAny ( FAny const & other)

◆ FAny() [4/4]

Mcro::Any::FAny::FAny ( FAny && other)

◆ ~FAny()

Mcro::Any::FAny::~FAny ( )

Member Function Documentation

◆ GetType()

FORCEINLINE FType Mcro::Any::FAny::GetType ( ) const
inline

Definition at line 170 of file Any.h.

◆ GetValidTypes()

FORCEINLINE TSet< FType > const & Mcro::Any::FAny::GetValidTypes ( ) const
inline

Definition at line 171 of file Any.h.

◆ IsValid()

FORCEINLINE bool Mcro::Any::FAny::IsValid ( ) const
inline

Definition at line 169 of file Any.h.

◆ TryGet() [1/2]

template<typename T >
T * Mcro::Any::FAny::TryGet ( )
inline

Definition at line 138 of file Any.h.

◆ TryGet() [2/2]

template<typename T >
const T * Mcro::Any::FAny::TryGet ( ) const
inline

Definition at line 130 of file Any.h.

◆ With()

template<typename Self , typename... T>
decltype(auto) Mcro::Any::FAny::With ( this Self && self,
TTypes< T... > &&  )
inline

Specify multiple types the enclosed value can be safely cast to, and are valid to be used with TryGet.

Definition at line 163 of file Any.h.

◆ WithAlias()

template<typename T , typename Self >
decltype(auto) Mcro::Any::FAny::WithAlias ( this Self && self)
inline

Specify one type the enclosed value can be safely cast to, and is valid to be used with TryGet.

Definition at line 147 of file Any.h.


The documentation for this struct was generated from the following file: