Trailer: backtrack easily and efficiently
The trailer is the object that keeps track of anything you want to keep track of. Some objects will take a trailer
as a parameter in their constructor. When it does, it means that their state can be saved and restored on demand using the functions described below.
State manipulation
Those functions are used to change the current state, save and or restore it.
Note that during your "state exploration", you can only restore higher. It is not possible to restore some deeper state, or state that could be in the same level. For example, if you have a state A at some point, you call SeaPearl.saveState!
to store it. You edit some SeaPearl.StateObject
, making you at some state B. Then you call SeaPearl.restoreState!
that will restore every SeaPearl.StateObject
to the state A. At that point, there is no way to go back to the state B using the trailer.
SeaPearl.StateObject
— TypeStateObject{T}(value::T, trailer::Trailer)
A reversible object of value value
that has a type T
, storing its modification into trailer
.
SeaPearl.StateEntry
— TypeStateEntry{T}(value::T, object::StateObject{T})
An entry that can be stacked in the trailer, containing the former value of the object, and a reference to the
object` so that it can be restored by the trailer.
SeaPearl.trail!
— Functiontrail!(var::StateObject{T})
Store the current value of var
into its trailer.
SeaPearl.setValue!
— FunctionsetValue!(var::StateObject{T}, value::T) where {T}
Change the value of var
, replacing it with value
, and if needed, store the former value into var
's trailer.
SeaPearl.saveState!
— FunctionsaveState!(trailer::Trailer)
Store the current state into the trailer, replacing the current stack with an empty one.
SeaPearl.restoreState!
— FunctionrestoreState!(trailer::Trailer)
Iterate over the last state to restore every former value, used to backtrack every change made after the last call to saveState!
.
SeaPearl.withNewState!
— FunctionwithNewState!(func, trailer::Trailer)
Call the func
function with a new state, restoring it after. Aimed to be used with the do
block syntax.
Examples
using SeaPearl
trailer = SeaPearl.Trailer()
reversibleInt = SeaPearl.StateObject{Int}(3, trailer)
SeaPearl.withNewState!(trailer) do
SeaPearl.setValue!(reversibleInt, 5)
end
reversibleInt.value # 3
SeaPearl.restoreInitialState!
— FunctionrestoreInitialState!(trailer::Trailer)
Restore every linked object to its initial state. Basically call restoreState!
until not possible.