@@ -17,14 +17,16 @@ namespace dspx {
1717 }
1818
1919 CreateEntityCommand::~CreateEntityCommand () {
20- if (m_object && m_object->parent () != m_strategy) {
21- delete m_object;
20+ // Delete object if command is in undone state
21+ if (m_object && m_undone) {
22+ delete m_object.data ();
2223 }
2324 }
2425
2526 void CreateEntityCommand::undo () {
2627 // Detach object from strategy but keep it alive
2728 m_object->setParent (nullptr );
29+ m_undone = true ;
2830 Q_EMIT m_strategy->destroyEntityNotified (entity ());
2931 }
3032
@@ -36,6 +38,7 @@ namespace dspx {
3638 // Subsequent times: re-parent the existing object
3739 m_object->setParent (m_strategy);
3840 }
41+ m_undone = false ;
3942 Q_EMIT m_strategy->createEntityNotified (entity (), m_entityType);
4043 }
4144
@@ -49,8 +52,9 @@ namespace dspx {
4952 }
5053
5154 DestroyEntityCommand::~DestroyEntityCommand () {
52- if (m_object && m_object->parent () != m_strategy) {
53- delete m_object;
55+ // Delete object if command is NOT in undone state (i.e., destroy was executed)
56+ if (m_object && !m_undone) {
57+ delete m_object.data ();
5458 }
5559 }
5660
@@ -94,10 +98,12 @@ namespace dspx {
9498
9599 void DestroyEntityCommand::undo () {
96100 recursivelyCreate (m_object);
101+ m_undone = true ;
97102 }
98103
99104 void DestroyEntityCommand::redo () {
100105 recursivelyDestroy (m_object);
106+ m_undone = false ;
101107 }
102108
103109 // ================================================================
@@ -134,24 +140,27 @@ namespace dspx {
134140 }
135141
136142 TakeFromSequenceContainerCommand::~TakeFromSequenceContainerCommand () {
137- if (m_object && m_object->parent () != handle_cast<QObject>(m_container)) {
138- delete m_object;
143+ // Delete object if command is NOT in undone state (i.e., take was executed)
144+ if (m_object && !m_undone) {
145+ delete m_object.data ();
139146 }
140147 }
141148
142149 void TakeFromSequenceContainerCommand::undo () {
143150 auto containerObj = handle_cast<BasicModelStrategySequenceContainerEntity>(m_container);
144151 containerObj->sequence .insert (m_object);
145152 m_object->setParent (containerObj);
146- Q_EMIT m_strategy->insertIntoSequenceContainerNotified (m_container, {reinterpret_cast <quintptr>(m_object)});
153+ m_undone = true ;
154+ Q_EMIT m_strategy->insertIntoSequenceContainerNotified (m_container, {reinterpret_cast <quintptr>(m_object.data ())});
147155 }
148156
149157 void TakeFromSequenceContainerCommand::redo () {
150158 auto containerObj = handle_cast<BasicModelStrategySequenceContainerEntity>(m_container);
151159 containerObj->sequence .remove (m_object);
152160 m_object->setParent (m_strategy);
153- Q_EMIT m_strategy->takeFromContainerNotified ({reinterpret_cast <quintptr>(m_object)}, m_container,
154- {reinterpret_cast <quintptr>(m_object)});
161+ m_undone = false ;
162+ Q_EMIT m_strategy->takeFromContainerNotified ({reinterpret_cast <quintptr>(m_object.data ())}, m_container,
163+ {reinterpret_cast <quintptr>(m_object.data ())});
155164 }
156165
157166 InsertIntoListContainerCommand::InsertIntoListContainerCommand (
@@ -185,15 +194,17 @@ namespace dspx {
185194 }
186195
187196 TakeFromListContainerCommand::~TakeFromListContainerCommand () {
188- if (m_object && m_object->parent () != handle_cast<QObject>(m_container)) {
189- delete m_object;
197+ // Delete object if command is NOT in undone state (i.e., take was executed)
198+ if (m_object && !m_undone) {
199+ delete m_object.data ();
190200 }
191201 }
192202
193203 void TakeFromListContainerCommand::undo () {
194204 auto containerObj = handle_cast<BasicModelStrategyListContainerEntity>(m_container);
195205 containerObj->list .insert (m_index, m_object);
196206 m_object->setParent (containerObj);
207+ m_undone = true ;
197208 Q_EMIT m_strategy->insertIntoListContainerNotified (m_container, entity (), m_index);
198209 }
199210
@@ -205,11 +216,12 @@ namespace dspx {
205216 auto containerObj = handle_cast<BasicModelStrategyListContainerEntity>(m_container);
206217 containerObj->list .removeAt (m_index);
207218 m_object->setParent (m_strategy);
219+ m_undone = false ;
208220 Q_EMIT m_strategy->takeFromListContainerNotified (entity (), m_container, m_index);
209221 }
210222
211223 Handle TakeFromListContainerCommand::entity () const {
212- return {reinterpret_cast <quintptr>(m_object)};
224+ return {reinterpret_cast <quintptr>(m_object. data () )};
213225 }
214226
215227 InsertIntoMapContainerCommand::InsertIntoMapContainerCommand (UndoableModelStrategy *strategy,
@@ -222,7 +234,7 @@ namespace dspx {
222234
223235 InsertIntoMapContainerCommand::~InsertIntoMapContainerCommand () {
224236 if (m_oldObject) {
225- delete m_oldObject;
237+ delete m_oldObject. data () ;
226238 }
227239 }
228240
@@ -238,7 +250,7 @@ namespace dspx {
238250 containerObj->map .insert (m_key, m_oldObject);
239251 m_oldObject->setParent (containerObj);
240252 Q_EMIT m_strategy->insertIntoMapContainerNotified (m_container,
241- {reinterpret_cast <quintptr>(m_oldObject)}, m_key);
253+ {reinterpret_cast <quintptr>(m_oldObject. data () )}, m_key);
242254 m_oldObject = nullptr ; // Transfer ownership back to container
243255 }
244256 }
@@ -250,7 +262,7 @@ namespace dspx {
250262 if (containerObj->map .contains (m_key)) {
251263 m_oldObject = containerObj->map .take (m_key);
252264 m_oldObject->setParent (nullptr ); // Take ownership
253- Q_EMIT m_strategy->takeFromMapContainerNotified ({reinterpret_cast <quintptr>(m_oldObject)}, m_container,
265+ Q_EMIT m_strategy->takeFromMapContainerNotified ({reinterpret_cast <quintptr>(m_oldObject. get () )}, m_container,
254266 m_key);
255267 }
256268
@@ -266,15 +278,17 @@ namespace dspx {
266278 }
267279
268280 TakeFromMapContainerCommand::~TakeFromMapContainerCommand () {
269- if (m_object && m_object->parent () != handle_cast<QObject>(m_container)) {
270- delete m_object;
281+ // Delete object if command is NOT in undone state (i.e., take was executed)
282+ if (m_object && !m_undone) {
283+ delete m_object.data ();
271284 }
272285 }
273286
274287 void TakeFromMapContainerCommand::undo () {
275288 auto containerObj = handle_cast<BasicModelStrategyMapContainerEntity>(m_container);
276289 containerObj->map .insert (m_key, m_object);
277290 m_object->setParent (containerObj);
291+ m_undone = true ;
278292 Q_EMIT m_strategy->insertIntoMapContainerNotified (m_container, entity (), m_key);
279293 }
280294
@@ -286,11 +300,12 @@ namespace dspx {
286300 auto containerObj = handle_cast<BasicModelStrategyMapContainerEntity>(m_container);
287301 containerObj->map .remove (m_key);
288302 m_object->setParent (m_strategy);
303+ m_undone = false ;
289304 Q_EMIT m_strategy->takeFromMapContainerNotified (entity (), m_container, m_key);
290305 }
291306
292307 Handle TakeFromMapContainerCommand::entity () const {
293- return {reinterpret_cast <quintptr>(m_object)};
308+ return {reinterpret_cast <quintptr>(m_object. data () )};
294309 }
295310
296311 RotateListContainerCommand::RotateListContainerCommand (UndoableModelStrategy *strategy,
0 commit comments