Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
Change.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "Containers/Array.h"
6#include "Containers/UnrealString.h"
7#include "HAL/Platform.h"
8#include "HAL/PlatformCrt.h"
9#include "Misc/AssertionMacros.h"
10#include "Templates/UniquePtr.h"
11#include "Templates/UnrealTemplate.h"
12
14class FReferenceCollector;
15struct UObject;
16
17/**
18 * FChange modifies a UObject and is meant to be used to implement undo/redo.
19 * The change is embedded in an FTransaction which executes it *instead* of the standard
20 * serialization transaction (cannot be combined - see FTransaction).
21 *
22 * The original FChange style (used by MeshEditor) was that calling Execute() would return a new
23 * FChange that applies the opposite action, and FTransaction would swap the two at each undo/redo
24 * step (eg a "DeleteObject" FChange would return a "CreateObject" FChange)
25 *
26 * The alternative "Command Pattern"-style FChange calls Apply() and Revert() on a single FChange.
27 *
28 * FChange may eventually be deprecated. You should subclass
29 * FSwapChange and FCommandChange to implement these different styles.
30 */
32{
33
34public:
35 enum class EChangeStyle
36 {
37 InPlaceSwap, // Call Execute() which returns new "opposite" FChange (default)
38 CommandPattern // call Revert() to Undo and Apply() to Redo
39 };
40
41 /** What style of change is this */
43
44 /** Makes the change to the object, returning a new change that can be used to perfectly roll back this change */
46
47 /** Makes the change to the object */
48 virtual void Apply( UObject* Object ) = 0;
49
50 /** Reverts change to the object */
51 virtual void Revert( UObject* Object ) = 0;
52
53 /** @return true if this Change has Expired, ie it will no longer have any effect and could be skipped by undo/redo */
54 virtual bool HasExpired( UObject* Object ) const { return false; }
55
56 /** Used by GC to collect referenced objects. */
57 virtual void AddReferencedObjects( FReferenceCollector& Collector ) { }
58
59 /** Describes this change (for debugging) */
60 virtual FString ToString() const = 0;
61
62 /** Prints this change to the log, including sub-changes if there are any. For compound changes, there might be multiple lines. You should not need to override this function. */
63 virtual void PrintToLog( FFeedbackContext& FeedbackContext, const int32 IndentLevel = 0 );
64
65 /** Virtual destructor */
66 virtual ~FChange()
67 {
68 }
69
70protected:
71
72 /** Protected default constructor */
74 {
75 }
76
77private:
78 // Non-copyable
79 FChange( const FChange& ) = delete;
80 FChange& operator=( const FChange& ) = delete;
81
82};
83
84
85
86/**
87 * To use FSwapChange you must implement Execute().
88 * This function must do two things:
89 * 1) apply the change to the given UObject
90 * 2) return a new FSwapChange that does the "opposite" action
91 */
92class FSwapChange : public FChange
93{
94
95public:
97 {
99 }
100
101 /** Makes the change to the object */
102 virtual void Apply(UObject* Object) final
103 {
104 check(false);
105 }
106
107 /** Reverts change to the object */
108 virtual void Revert(UObject* Object) final
109 {
110 check(false);
111 }
112
113};
114
115
116/**
117 * To use FCommandChange you must implement Apply() and Revert()
118 * Revert() is called to "Undo" and Apply() is called to "Redo"
119 */
121{
122
123public:
125 {
127 }
128
130 {
131 check(false);
132 return nullptr;
133 }
134
135};
136
137
138
139
140
141
143{
145 {
146 }
147
150 {
151 }
152
153 /** Ordered list of changes that comprise everything needed to describe this change */
155
156private:
157 // Non-copyable
160};
161
162
163
164/**
165 * FCompoundChange applies a sequence of FSwapChanges.
166 * The changes are executed in reverse order (this is like a mini undo stack)
167 */
169{
170
171public:
172
173 /** Constructor */
174 explicit FCompoundChange( FCompoundChangeInput&& InitInput )
175 : Input( MoveTemp( InitInput ) )
176 {
177 }
178
179 // Parent class overrides
180 virtual TUniquePtr<FChange> Execute( UObject* Object ) override;
181 virtual FString ToString() const override;
182 virtual void PrintToLog( class FFeedbackContext& FeedbackContext, const int32 IndentLevel = 0 ) override;
183
184
185private:
186
187 /** The data we need to make this change */
189
190private:
191 // Non-copyable
194
195};
#define check(expr)
virtual void Apply(UObject *Object)=0
virtual void PrintToLog(FFeedbackContext &FeedbackContext, const int32 IndentLevel=0)
virtual EChangeStyle GetChangeType()=0
virtual ~FChange()
Definition Change.h:66
FChange & operator=(const FChange &)=delete
virtual void AddReferencedObjects(FReferenceCollector &Collector)
Definition Change.h:57
FChange()
Definition Change.h:73
FChange(const FChange &)=delete
virtual void Revert(UObject *Object)=0
virtual bool HasExpired(UObject *Object) const
Definition Change.h:54
EChangeStyle
Definition Change.h:36
virtual FString ToString() const =0
virtual TUniquePtr< FChange > Execute(UObject *Object)=0
virtual EChangeStyle GetChangeType() final
Definition Change.h:124
virtual TUniquePtr< FChange > Execute(UObject *Object) final
Definition Change.h:129
FCompoundChange(FCompoundChangeInput &&InitInput)
Definition Change.h:174
virtual FString ToString() const override
virtual TUniquePtr< FChange > Execute(UObject *Object) override
virtual void PrintToLog(class FFeedbackContext &FeedbackContext, const int32 IndentLevel=0) override
FCompoundChange(const FCompoundChange &)=delete
FCompoundChangeInput Input
Definition Change.h:188
FCompoundChange & operator=(const FCompoundChange &)=delete
virtual EChangeStyle GetChangeType() final
Definition Change.h:96
virtual void Revert(UObject *Object) final
Definition Change.h:108
virtual void Apply(UObject *Object) final
Definition Change.h:102
FCompoundChangeInput & operator=(const FCompoundChangeInput &)=delete
TArray< TUniquePtr< FChange > > Subchanges
Definition Change.h:154
FCompoundChangeInput(const FCompoundChangeInput &)=delete
FCompoundChangeInput(FCompoundChangeInput &&RHS)
Definition Change.h:148
Definition UE.h:432