c++ 虚幻引擎5宏FORVALUE(检测成员,检查字段)

agxfikkp  于 2022-12-20  发布在  其他
关注(0)|答案(1)|浏览(146)

虚幻引擎5
宏代码解决了从int32 Id数组中读取变量的问题

template< class T >
static bool IsArray(T A){return std::is_array<A>::value;}

template< class T >
static bool IsStruct(T A){return std::is_struct<A>::value;}

#define FORVALUE(Value, Field) bool Fv(){ \
bool B=false; \
if(UILbr::IsStruct(Value)){FString S=#Struct; B=S.Find(#Field)>-1; if(B){Value=Value.Field;}} \
bool B=UILbr::IsArray(Value) && Value.IsValidIndex(Id); \
if(B){Value=Value[Id];} \
return B; \
}
/* Library (Instance Variable Saver) */
UCLASS(BlueprintType) class IVS_API UILbr:public UBlueprintFunctionLibrary{GENERATED_UCLASS_BODY()
static TArray<FString> InStrings(FBlns V){return BlnsToStrs(V.V);}
static TArray<FString> InStrings(FBlnsA V){return BlnsAToStrs(V);}
static TArray<FString> InStrings(FBrss V){return BrssToStrs(V.V);}
static TArray<FString> InStrings(FBrssA V){return BrssAToStrs(V);}
template <typename A>
static TArray<FString> InStrings(A V){return {InString(V)};}
static FString InString(bool V){return BlnToStr(V);}
static FString InString(FBlns V){return V.ToString();}
static FString InString(FBlnsA V){return V.ToJson();}
static FString InString(FSlateBrush V){return BrsToStr(V);}
static FString InString(FBrss V){return V.ToString();}
static FString InString(FBrssA V){return V.ToJson();}
}
#define ISSI(T) TArray < FString > UV## T::InStringsIds_Implementation(FI32Ss Ids, bool & B) { \
  int32 C = Ids.Num(); \
  auto A = V; \
  for (int32 & Id: Ids.V) { \
    B = FORVALUE(A, V); \
    if (B) { \
      if (Id == C) { \
        break; \
      } \
    } else { \
      break; \
    } \
  } \
  if (C == 0) { \
    B = true; \
  } \
  return UILbr::InStrings(A); \
}

请更正代码FString S =#Struct; B = S,求(场)〉-1;如果(B){值=值字段;}
代码解决了从int32 Id数组读取变量的问题

template< class T >
static bool IsArray(T A){return std::is_array<A>::value;}
template< class T >
static bool IsStruct(T A){return std::is_struct<A>::value;}

template< class T, typename R >
static bool ForValue(T &A, R V, int32 Id){
if(UILbr::IsStruct(A)){A=A.V;} //structvar.member
bool B=UILbr::IsArray(A) && A.IsValidIndex(Id);
if(B){A=A[Id];}
return B;
}

#define ISSI(T) TArray < FString > UV## T::InStringsIds_Implementation(FI32Ss Ids, bool & B) {
  int32 C = Ids.Num();
  auto A = V;
  for (int32 & Id: Ids.V) {
    B = UILbr::ForValue(A, V, Id);
    if (B) {
      if (Id == C) {
        break;
      }
    } else {
      break;
    }
  }
  if (C == 0) {
    B = true;
  }
  return UILbr::InStrings(A);
}

最小化输入数据,使其更容易解决问题https://www.onlinegdb.com/online_c++_compiler

#include <iostream>
#include <type_traits>
 
using namespace std;
 
 
#define DETECTED_MEMBER(member_name, member_field) bool detected_member(member_name){return member_name.member_field? true: false;}                                            

struct A{A(){}; float v;};
 
struct B{B(){}; int v;};

int main()
{
    //---- true/false
    cout<< "DETECTED_MEMBER B.v ? "
        << DETECTED_MEMBER(B,v)     <<endl; //true
     //---- true/false
    cout<< "DETECTED_MEMBER A.v ? "
        << DETECTED_MEMBER(A,v)     <<endl;  //true
             //---- true/false
    cout<< "DETECTED_MEMBER A.w ? "
        << DETECTED_MEMBER(A,w)     <<endl;  //false
 }
mrzz3bfm

mrzz3bfm1#

#pragma once
#include "Kismet/BlueprintFunctionLibrary.h"
#include "GameFramework/SaveGame.h"
#include "Kismet/KismetStringLibrary.h"
#include "Kismet/GameplayStatics.h"
#include <type_traits>
#include "testBPLibrary.generated.h"
UCLASS() class UILbr: public UBlueprintFunctionLibrary{GENERATED_UCLASS_BODY()
template< class T >
static bool IsArray(T A){return std::is_array<A>::value;}
template< class T >
static bool IsStruct(T A){return std::is_struct<A>::value;}
template<typename T>
static bool HasV(T X){
template<class T> struct HV{ 
struct Fallback{int V;}; // introduce member name "V"
struct Derived:T, Fallback{};
template<typename C, C> struct ChT; 
template<typename C> static char (&f(ChT<int Fallback::*, &C::V>*))[1]; 
template<typename C> static char (&f(...))[2]; 
static bool const value=sizeof(f<Derived>(0))==2;
}; 
return HV<T>::value;
}
template< class T>
static bool ForValue(T &A, int32 Id){
if(UILbr::IsStruct(A) && UILbr::HasV(A)){A=A.V;}
bool B=UILbr::IsArray(A) && A.IsValidIndex(Id);
if(B){A=A[Id];}
return B;
}
UCLASS(BlueprintType) class UVBln:public USaveGame{GENERATED_BODY()
public:
UPROPERTY(EditAnywhere, BlueprintReadWrite) bool V;
virtual FString InStringIds_Implementation(FI32Ss Ids, bool &Succes);
};
#define ISI(T) FString UV##T::InStringIds_Implementation(FI32Ss Ids, bool &B){FBlns R; \
int32 C=Ids.Num(); auto A=V; for(int32 &Id: Ids.V){ \
if(UILbr::ForValue(A, Id)){if(Id==C){return "";}}else{break;}} if(C==0){B=true;} return "";}
};
We get a class from a variable, but A=A.V; but error C2228

相关问题