Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Can I use implicit sharing within the Undo Framework?



  • Hi,
    I'm wondering if there is a way of mixing implicit sharing with the Undo Framework.

    Here is my use case:
    I've a DataSet of business objects. The DataSet is owning the objects. Those objects can reference each others but never own each others. However if one object is deleted (from the DataSet), I would like that all others that reference it don't stay with a dangling pointer.
    To achieve this, I'm not using normal C++ class attributes for my objects but instead a Map of meta objects Property (Link11Property, Link0NProperty, OrderedLink01Property...) that will know their reverse property in order to remove themself from it if they are delete (kind of reproducing the e-opposite of Ecore model)
    I'm also doing this cause I need to have an Undo/Redo feature for every property update. It is narrowing down my Undo framework to only 3 single commands: CreateObject, DeleteObject (which won't destroy the object as it could come back) and UpdateProperty.

    This is working fine, please let me know what you think about this design.

    I now have a new use case to add on top of that: I should be able to do some snapshots of my DataSet. The snapshots would be fixed: meaning once they're done we can't modify them, but the main DataSet would still be able to be modified.
    This make me think that implicit sharing would be perfect. I could make my objects inherit from QSharedData, and my DataSet holding QSharedDataPointers on them.
    Thus if I've done some snapshots and then modify my DataSet, the objects would be detached.
    Am I right?

    My issue is that I should be able to load back a snapshot into the DataSet by overwriting the objects that have been modified since then and I would like this operation to be UndoAble. So I can't just loose the objects of the DataSet in this operation, I need somehow to get the UndoCommands implied in case I wish to revert the operation.

    If I make the operation of taking a snapshot a Command would this be enough to just jump back in the UndoStack at this point? A command that does nothing but would mark a place in the UndoStack so I could come back there are any modification later. Is that possible?
    But to work I imagine I should have a QExplicitlySharedDataPointer between my UndoStack and the DataSet whereas it is supposed to be a QSharedDataPointer to be shared with the Snapshots...
    Is there a way to mix ExplicitlyShared with ImplicitlyShared pointers? Should I just use the raw pointers in my UndoStack?

    Let me know if you need some clarification and what how you would implement such thing.
    Cheers :)



  • well in fact I can't rely on making my Snapshot a Command and jump back to it as both my DataSet and Snaphot can be saved in a file and load in the application (at new start for exemple) so I won't have my UndoStack.
    I guess I thus need to be able to recreate all the Commands of the differences between my Snapshot and the DataSet... This could be complicated...
    Maybe the Snapshot could store a copy of them... What you think?
    I still have the issue of mixing implicit and explicit sharing of an object...


Log in to reply