/* ----------------------------------------------------------------------------- Copyright 2021 Kevin P. Barry Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ----------------------------------------------------------------------------- */ // Author: Kevin P. Barry [ta0kira@gmail.com] define Routines { $ReadOnlyExcept[machine]$ refines StateMachine @category ReferenceMachine dropWeak <- IfElseDecrWeakIsZero.new( /*if*/ FreeRef.new(), /*else*/ empty) @category ReferenceMachine sharedThenDrop <- LockRef.new() `Run.then` IfElseIncrRefIsOne.new( /*if*/ DecrRef.new() `Run.then` UnlockRef.new() `Run.then` dropWeak, /*else*/ UnlockRef.new() `Run.then` dropWeak `Run.then` dropShared) @category ReferenceMachine dropShared <- LockRef.new() `Run.then` IfElseDecrRefIsZero.new( /*if*/ FreeObject.new() `Run.then` UnlockRef.new() `Run.then` IfElseDecrWeakIsZero.new( /*if*/ FreeRef.new(), /*else*/ empty), /*else*/ UnlockRef.new()) @category ReferenceMachine sharedThenDropBroken <- IfElsePlusLockModLockZero.new( /*if*/ MinusLock.new() `Run.then` dropWeak, /*else*/ MinusLockPlusOne.new() `Run.then` dropWeak `Run.then` dropSharedBroken) @category ReferenceMachine dropSharedBroken <- IfElseDecrRefIsZero.new( /*if*/ FreeObject.new() `Run.then` IfElseDecrWeakIsZero.new( /*if*/ FreeRef.new(), /*else*/ empty), /*else*/ empty) @value String name @value ReferenceState state @value optional ReferenceMachine machine newShared (name,state) { if (state.addStrong() == 1) { \ state.addWeak() } return #self{ name, state, dropShared } } newWeak (name,state) { \ state.addWeak() return #self{ name, state, sharedThenDrop } } newSharedBroken (name,state) { if (state.addStrong() == 1) { \ state.addWeak() } return #self{ name, state, dropSharedBroken } } newWeakBroken (name,state) { \ state.addWeak() return #self{ name, state, sharedThenDropBroken } } transition () { if (!present(machine)) { return empty } elif (present((machine <- require(machine).runWith(name,state)))) { return self } else { return empty } } }