Notes_Assertion_Shared Pointer_Shared Reference_Conversion_Weak Pointer_Unicast/Multicast/Dynamic Proxy

1. Assertion
1.checkNoEntry();

Check if the program has entered this function, and if it enters, it will be disconnected

2.checkNoReentry();

Check if the function is executed twice;

3.checkNoRecursion();

Recursion detection, crash if recursion.

4.ensure(0 && "Print information");

Among them, 0 is bool, if it is zero, it will be interrupted here, and the program will be blocked here for debugging


2. Shared pointer (TSharedPtr)

1. Advantages

1> Can avoid circular references

2> Released by system management without manual operation

3> There is a reference count

2. How to use

1> Create a shared pointer

TSharedPtr<类名>TempPtr (new 类名)

It can also be created like this:

TSharedPtr<类名>TempPtr;

TempPtr = MakeSharedPtr(new 类名);

2> Dereference

TempPtr.Get();

3> Reset the shared pointer

TempPtr.Rest();

TempPtr=NULL;

4> Get reference count

TempPtr.GetSharedReferenceCount();

5>Judge whether it is unique

TempPtr.IsUnique();

2. Shared reference (TSharedRef)

1. Concept

Usage is similar to TSharedPtr, compared with TSharedPtr, shared references are more secure. Because the reference cannot be empty , the initialization of the shared reference is generally assigned by MakeShared or the ToSharedRef function of the shared pointer, so it is safer.

It is generally used when passing function parameters and as a return value. Shared references do not have IsValid for effective judgment. If you want to judge the validity, you can get the native pointer judgment through Get.

2. How to use

1>Create

TSharedRef<类名> TempRef = MakeShared<类名>();

2> Conversion with shared pointers (.ToSharedRef())

//共享指针转共享引用

TSharedPtr<类名> TempPtr= MakeShareable(new 类名());

TempRef = TempPtr.ToSharedRef();
//共享引用转共享指针

TempPtr = TempRef;//可以直接隐式转换

3. Weak pointer (TWeakPtr)

1. Concept

Weak pointers do not participate in reference counting. At the same time, if there is no other pointer to the object pointed by the weak pointer, the weak pointer is invalid, so judge before using it

2. How to use

1>Create

TWeakPtr<类名>TempWPr;

2> Valid Judgment

TempWPr.IsVaild();

3> Convert to a shared pointer

//弱指针的.Pin(),会返回一个智能指针

TSharedPtr<类名>newPtr(TempWPr.Pin());

Four: TSharedFromThis template class

Once an ordinary c++ class inherits this class, it will save a weak pointer in the class ,

When the ordinary pointer of the class is obtained through the smart pointer, the smart pointer of the class can be obtained by calling the AsShared method.

1> example

class TestA:public TSharedFromThis<TestA>

{

}

TSharedPtr<TestA> TestPtr = MakeShareable(new TestA());
TestA* NormalPtr = TestPtr.Get();
TSharedPtr<TestA> TestPtr2 = NormalPtr ->AsShared();

Note: Only MakeShareable creates the ordinary pointer assigned to the smart pointer before you can use AsShared to convert it into a smart pointer. Ordinary new one is not acceptable, because the weak pointer saved in it will be initialized only through MakeShareable creation , otherwise the failure to call AsShared will cause a crash .


5. Unicast Proxy

1>Create proxy

DECLARE_DELEGATE(xxx);//单播无参xxx代理
DECLARE_DELEGATE_OneParam(xxx,bool);//单播有参为bool的xxx代理;

DECLARE_DELEGATE_RetVal(bool,xxx);//单播无参返回值为bool的xxx代理;
DECLARE_DELEGATE_RetVal_OneParam(int32,xxx,FString&);//单播有参数FString&,返回值为Int32的xxx代理;

2>Instantiation

XXX TA;//创建一个代理实例

3> Binding method to the agent

TA.BindRaw(this,&方法名);//绑定原生C的方法;
TA.BindSP(this,&方法名);//绑定智能指针
TA.BindUObject(this,&方法名);//绑定Object类;
TA.BindStatic(函数名);//绑定静态函数
TA.BindFunction(this,TEXT("函数名"))//同上

4> call proxy

TA.ExecuteIfBound(此处有参就传参);

6. Multicast agent

It is basically the same as the unicast agent, but there is no return value

DECLARE_MULTICAST_DELEGATE(FDelegateTaskE);
FDelegateTaskE DelegateTaskE;
DelegateTaskE.AddRaw(this,&方法名);//绑定方法


call proxy

  DelegateTaskE.Broadcast(有参传参);
  //区别是可以绑定多个函数,对目标为弱引用,可以和结构体一起使用,可以很方便地进行拷贝,等等。

7. Dynamic Multicast Proxy

1>Create

DECLARE_DYNAMIC_DELEGATE_OneParam(FDelegateTaskG,FString&,Str_C );
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FDelegateTaskH,FString&,Str_C);

2>Instantiation

FDelegateTaskG DelegateTaskG;
FDelegateTaskH DelegateTaskH;	

3> Binding method

DelegateTaskG.BindDynamic(this,&方法名);
DelegateTaskH AddDynamic(this,&方法名);

4> call proxy

DelegateTaskG.ExecuteIfBound(TempF);
DelegateTaskH.Broadcast(TempF);

If the dynamic unicast proxy is bound to multiple methods, only the last one will be executed, that is, only one can be bound.


Guess you like

Origin blog.csdn.net/qq_35337794/article/details/123164997