WSF
WsfPProxyUndo.hpp
Go to the documentation of this file.
1// ****************************************************************************
2// CUI
3//
4// The Advanced Framework for Simulation, Integration, and Modeling (AFSIM)
5//
6// Copyright 2003-2015 The Boeing Company. All rights reserved.
7//
8// The use, dissemination or disclosure of data in this file is subject to
9// limitation or restriction. See accompanying README and LICENSE for details.
10// ****************************************************************************
11// ****************************************************************************
12// Updated by Infoscitex, a DCS Company.
13// ****************************************************************************
14
15#ifndef WSFPPROXYUNDO_HPP
16#define WSFPPROXYUNDO_HPP
17
18#include "wsf_parser_export.h"
19
20#include <cassert>
21#include <vector>
22
23#include "UtReferenceCount.hpp"
24#include "WsfPProxyPath.hpp"
25#include "WsfPProxyValue.hpp"
26#include "WsfParseType.hpp"
27
28// Provides feedback when a proxy object is modified
29class WSF_PARSER_EXPORT WsfPProxyDeserializeObserver
30{
31public:
33
34 virtual void BeforeCopy(const WsfPProxyPath& aCopyFrom) = 0;
35
36 virtual void BeforeAssignment(const WsfPProxyPath& aPath, WsfPProxyValue& aNewValue) = 0;
37 virtual void BeforeMapInsert(const WsfPProxyPath& aMapPath, const std::string& aMapKey, WsfPProxyValue aValue) = 0;
38
39 virtual void BeforeMapDelete(const WsfPProxyPath& aMapPath, const std::string& aMapKey) = 0;
40
41 virtual void BeforeListPush(const WsfPProxyPath& aListPath) = 0;
42
43 virtual void AfterLoad(const WsfPProxyPath& aLoadPath) = 0;
44};
45
46class WsfPProxyUndo_ActionAssign;
47class WsfPProxyUndo_ActionMapOp;
48class WsfPProxyUndo_ActionListOp;
49
50// Implements an undo system for the proxy.
51// Changes to the proxy can be reverted to a previous state
52class WSF_PARSER_EXPORT WsfPProxyUndo
53{
54public:
58
60 : mRoot(aRoot)
61 , mKeepUndoData(true)
62 {
63 }
64 WsfPProxyUndo(const WsfPProxyUndo&) = delete;
66
69 {
70 public:
71 virtual ~ActionBase() {}
72 virtual void Undo(WsfPProxyUndo* aThis) = 0;
73 };
74
75 size_t GetHistorySize() { return mActions.size(); }
76
77 void Rollback(size_t aHistoryIndex);
78 void Commit(size_t aHistoryIndex = 0);
79 bool Assign(const WsfPProxyPath& aPath, WsfPProxyValue aNewValue);
80 bool MapInsert(const WsfPProxyPath& aMapPath, const std::string& aMapKey, WsfPProxyValue aValue);
81 bool MapDelete(const WsfPProxyPath& aMapPath, const std::string& aMapKey);
82 bool ListPush(const WsfPProxyPath& aListPath);
83
84 const WsfPProxyValue& Root() const { return mRoot; }
85
86 bool SetUnset(const WsfPProxyPath& aAttributePath, bool aIsUnset = true);
87
88
89 void DisableUndo() { mKeepUndoData = false; }
90
91private:
92 void PushAction(std::unique_ptr<ActionBase> aAction);
93
94 std::vector<std::unique_ptr<ActionBase>> mActions;
95 WsfPProxyValue mRoot;
96 bool mKeepUndoData;
97 UT_MEMORY_DEBUG_MARKER(cMDB_PARSE_UNDO);
98};
99
100// Wraps a WsfPProxyValue object so that changes may be undone
101class WSF_PARSER_EXPORT WsfPProxyUndoValue
102{
103public:
105 : mUndoPtr(nullptr)
106 , mReferenceCount(nullptr)
107 {
108 }
110 {
111 mUndoPtr = new WsfPProxyUndo(aValue);
112 mReferenceCount = new UtReferenceCount(1);
113 }
115 : mPath(aSrc.mPath)
116 , mUndoPtr(aSrc.mUndoPtr)
118 {
119 if (mReferenceCount)
120 {
121 mReferenceCount->AddStrongRef();
122 assert(mUndoPtr);
123 }
124 else
125 {
126 assert(!mUndoPtr);
127 }
128 }
130 {
131 if (mUndoPtr)
132 {
133 mUndoPtr->DisableUndo();
134 }
135 }
138 {
139 if (this == &aRhs)
140 {
141 return *this;
142 }
143 Cleanup();
144 mPath = aRhs.mPath;
145 mUndoPtr = aRhs.mUndoPtr;
147 if (mReferenceCount)
148 {
149 mReferenceCount->AddStrongRef();
150 }
151 return *this;
152 }
153
155 bool Assign(WsfPProxyValue aNewValue) { return mUndoPtr->Assign(mPath, aNewValue); }
156
159 bool MapInsert(const std::string& aMapKey, WsfPProxyValue aValue)
160 {
161 return mUndoPtr ? mUndoPtr->MapInsert(mPath, aMapKey, aValue) : false;
162 }
163 bool MapDelete(const std::string& aMapKey) { return mUndoPtr ? mUndoPtr->MapDelete(mPath, aMapKey) : false; }
164 bool ListPush() { return mUndoPtr ? mUndoPtr->ListPush(mPath) : false; }
165 bool MoveToAttr(size_t aIndex)
166 {
167 if (*this)
168 {
169 mPath += aIndex;
170 return true;
171 }
172 return false;
173 }
174 bool MoveToAttr(const std::string& aKey)
175 {
176 bool ok = mPath.Push(Get(), aKey);
177 if (!ok)
178 {
179 Cleanup();
180 }
181 return ok;
182 }
183 bool MoveToAttr(const WsfPProxyKey& aKey)
184 {
185 if (aKey.IsString())
186 {
187 return MoveToAttr(aKey.GetMapKey());
188 }
189 else
190 {
191 return MoveToAttr(aKey.GetIndex());
192 }
193 }
194 bool MoveToAttr(const WsfPProxyPath& aPath)
195 {
196 for (size_t i = 0; i < aPath.size(); ++i)
197 {
198 if (!MoveToAttr(aPath[i]))
199 {
200 return false;
201 }
202 }
203 return true;
204 }
205
206 bool MoveToAttr(const WsfParseTypePath& aLookupPath)
207 {
208 bool ok = true;
209 for (size_t i = 0; i < aLookupPath.size() && ok; ++i)
210 {
211 const std::string& addr = aLookupPath[i].Get();
212 ok = MoveToAttr(addr);
213 }
214 return ok;
215 }
216
218 {
219 WsfPProxyUndoValue rval = *this;
220 if (rval.MoveToAttr(aLookupPath))
221 {
222 return rval;
223 }
224 return WsfPProxyUndoValue();
225 }
226
227 bool SetUnset(bool aIsUnset) { return mUndoPtr ? mUndoPtr->SetUnset(mPath, aIsUnset) : false; }
228 WsfPProxyUndoValue GetAttr(const std::string& aKey) const
229 {
230 WsfPProxyUndoValue rval(*this);
231 rval.MoveToAttr(aKey);
232 return rval;
233 }
234 WsfPProxyUndoValue GetAttr(size_t aIndex) const
235 {
236 WsfPProxyUndoValue rval(*this);
237 rval.MoveToAttr(aIndex);
238 return rval;
239 }
241 {
242 WsfPProxyUndoValue rval(*this);
243 rval.MoveToAttr(aPath);
244 return rval;
245 }
246 void MoveToParent() { mPath.Pop(); }
248 {
249 WsfPProxyUndoValue rval = *this;
250 rval.MoveToParent();
251 return rval;
252 }
253
255 {
256 if (mUndoPtr)
257 {
258 return mUndoPtr->Root().Lookup(mPath);
259 }
260 return WsfPProxyValue();
261 }
262 bool Exists() const { return (bool)Get(); }
263 void Rollback()
264 {
265 if (mUndoPtr)
266 {
267 mUndoPtr->Rollback(0);
268 }
269 }
270 void Rollback(size_t aHistoryIndex)
271 {
272 if (aHistoryIndex <= GetHistorySize())
273 {
274 if (mUndoPtr)
275 {
276 mUndoPtr->Rollback(aHistoryIndex);
277 }
278 }
279 else
280 {
281 assert(!"INVALID INDEX");
282 }
283 }
284 void Commit(size_t aHistoryIndex = 0)
285 {
286 assert(mUndoPtr != nullptr);
287 mUndoPtr->Commit(aHistoryIndex);
288 }
289 size_t GetHistorySize() const { return mUndoPtr ? mUndoPtr->GetHistorySize() : 0; }
290 operator bool() const { return mUndoPtr != nullptr; }
291 std::string DebugAddr() const;
292
293 const WsfPProxyPath& GetPath() const { return mPath; }
294
295 WsfPProxyUndo* GetUndo() const { return mUndoPtr; }
296
297protected:
298 void Cleanup()
299 {
300 if (mReferenceCount)
301 {
302 if (mReferenceCount->RemoveStrongRef())
303 {
304 mUndoPtr->Commit();
305 delete mUndoPtr;
306 }
307 }
308 mUndoPtr = nullptr;
309 mReferenceCount = nullptr;
310 }
313 UtReferenceCount* mReferenceCount;
314};
315
316#endif
@ cMDB_PARSE_UNDO
Definition WsfParseDebugMarkers.hpp:31
std::vector< UtStringRef > WsfParseTypePath
Definition WsfParseTypePath.hpp:21
bool ok
Definition WsfScriptWeaponClass.cpp:211
Definition WsfPProxyUndo.hpp:30
virtual ~WsfPProxyDeserializeObserver()
Definition WsfPProxyUndo.hpp:32
virtual void BeforeAssignment(const WsfPProxyPath &aPath, WsfPProxyValue &aNewValue)=0
virtual void BeforeListPush(const WsfPProxyPath &aListPath)=0
virtual void AfterLoad(const WsfPProxyPath &aLoadPath)=0
virtual void BeforeMapInsert(const WsfPProxyPath &aMapPath, const std::string &aMapKey, WsfPProxyValue aValue)=0
virtual void BeforeCopy(const WsfPProxyPath &aCopyFrom)=0
virtual void BeforeMapDelete(const WsfPProxyPath &aMapPath, const std::string &aMapKey)=0
Definition WsfPProxyKey.hpp:24
size_t GetIndex() const
Definition WsfPProxyKey.hpp:42
const std::string & GetMapKey() const
Definition WsfPProxyKey.hpp:43
bool IsString() const
Definition WsfPProxyKey.cpp:47
Represents a path or unique key to a value in the proxy.
Definition WsfPProxyPath.hpp:64
size_t size() const
Definition WsfPProxyPath.hpp:142
WsfPProxyUndoValue GetParent() const
Definition WsfPProxyUndo.hpp:247
bool SetUnset(bool aIsUnset)
Definition WsfPProxyUndo.hpp:227
bool MoveToAttr(const std::string &aKey)
Definition WsfPProxyUndo.hpp:174
void MoveToParent()
Definition WsfPProxyUndo.hpp:246
WsfPProxyUndoValue GetAttr(const WsfParseTypePath &aLookupPath) const
Definition WsfPProxyUndo.hpp:217
UtReferenceCount * mReferenceCount
Definition WsfPProxyUndo.hpp:313
WsfPProxyUndoValue GetAttr(size_t aIndex) const
Definition WsfPProxyUndo.hpp:234
void Cleanup()
Definition WsfPProxyUndo.hpp:298
void Rollback()
Definition WsfPProxyUndo.hpp:263
bool MoveToAttr(const WsfPProxyKey &aKey)
Definition WsfPProxyUndo.hpp:183
bool MoveToAttr(const WsfParseTypePath &aLookupPath)
Definition WsfPProxyUndo.hpp:206
bool MapInsert(const std::string &aMapKey, WsfPProxyValue aValue)
Definition WsfPProxyUndo.hpp:159
const WsfPProxyPath & GetPath() const
Definition WsfPProxyUndo.hpp:293
void Commit(size_t aHistoryIndex=0)
Definition WsfPProxyUndo.hpp:284
void DisableUndo()
Definition WsfPProxyUndo.hpp:129
WsfPProxyUndoValue(WsfPProxyValue aValue)
Definition WsfPProxyUndo.hpp:109
WsfPProxyUndoValue & operator=(const WsfPProxyUndoValue &aRhs)
Definition WsfPProxyUndo.hpp:137
bool MapDelete(const std::string &aMapKey)
Definition WsfPProxyUndo.hpp:163
WsfPProxyUndoValue GetAttr(const WsfPProxyPath &aPath) const
Definition WsfPProxyUndo.hpp:240
void Rollback(size_t aHistoryIndex)
Definition WsfPProxyUndo.hpp:270
bool MoveToAttr(size_t aIndex)
Definition WsfPProxyUndo.hpp:165
WsfPProxyPath mPath
Definition WsfPProxyUndo.hpp:311
WsfPProxyUndoValue()
Definition WsfPProxyUndo.hpp:104
bool MoveToAttr(const WsfPProxyPath &aPath)
Definition WsfPProxyUndo.hpp:194
WsfPProxyValue Get() const
Definition WsfPProxyUndo.hpp:254
bool Assign(WsfPProxyValue aNewValue)
Assigns a new value. aNewValue's memory is now maintained by WsfPProxyUndo and should not be used dir...
Definition WsfPProxyUndo.hpp:155
WsfPProxyUndoValue GetAttr(const std::string &aKey) const
Definition WsfPProxyUndo.hpp:228
WsfPProxyUndo * GetUndo() const
Definition WsfPProxyUndo.hpp:295
bool ListPush()
Definition WsfPProxyUndo.hpp:164
WsfPProxyUndo * mUndoPtr
Definition WsfPProxyUndo.hpp:312
WsfPProxyUndoValue(const WsfPProxyUndoValue &aSrc)
Definition WsfPProxyUndo.hpp:114
bool Exists() const
Definition WsfPProxyUndo.hpp:262
~WsfPProxyUndoValue()
Definition WsfPProxyUndo.hpp:136
size_t GetHistorySize() const
Definition WsfPProxyUndo.hpp:289
Definition WsfPProxyUndo.hpp:69
virtual void Undo(WsfPProxyUndo *aThis)=0
virtual ~ActionBase()
Definition WsfPProxyUndo.hpp:71
Definition WsfPProxyUndo.hpp:53
WsfPProxyUndo(const WsfPProxyUndo &)=delete
size_t GetHistorySize()
Definition WsfPProxyUndo.hpp:75
WsfPProxyUndo(WsfPProxyValue aRoot)
Definition WsfPProxyUndo.hpp:59
friend class WsfPProxyUndo_ActionMapOp
Definition WsfPProxyUndo.hpp:56
friend class WsfPProxyUndo_ActionAssign
Definition WsfPProxyUndo.hpp:55
friend class WsfPProxyUndo_ActionListOp
Definition WsfPProxyUndo.hpp:57
WsfPProxyUndo & operator=(const WsfPProxyUndo &)=delete
~WsfPProxyUndo()
Definition WsfPProxyUndo.hpp:67
const WsfPProxyValue & Root() const
Definition WsfPProxyUndo.hpp:84
void Commit(size_t aHistoryIndex=0)
Definition WsfPProxyUndo.cpp:295
void DisableUndo()
Definition WsfPProxyUndo.hpp:89
Definition WsfPProxyValue.hpp:33
Copyrights Multiple, All Rights Reserved