| | | 1 | | namespace Elsa.Expressions.Models; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Represents a register of memory. |
| | | 5 | | /// </summary> |
| | | 6 | | public class MemoryRegister |
| | | 7 | | { |
| | | 8 | | /// <summary> |
| | | 9 | | /// Constructor. |
| | | 10 | | /// </summary> |
| | 3209 | 11 | | public MemoryRegister(IDictionary<string, MemoryBlock>? blocks = null) |
| | | 12 | | { |
| | 3209 | 13 | | Blocks = blocks ?? new Dictionary<string, MemoryBlock>(); |
| | 3209 | 14 | | } |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// The memory blocks declared in this register. |
| | | 18 | | /// </summary> |
| | 17061 | 19 | | public IDictionary<string, MemoryBlock> Blocks { get; } |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Returns true if the specified memory block is declared in this register. |
| | | 23 | | /// </summary> |
| | 0 | 24 | | public bool IsDeclared(MemoryBlockReference reference) => HasBlock(reference.Id); |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Returns true if the specified memory block is declared in this register. |
| | | 28 | | /// </summary> |
| | 331 | 29 | | public bool HasBlock(string id) => Blocks.ContainsKey(id); |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Returns the memory block with the specified ID. |
| | | 33 | | /// </summary> |
| | | 34 | | public bool TryGetBlock(string id, out MemoryBlock block) |
| | | 35 | | { |
| | 11618 | 36 | | block = null!; |
| | 11618 | 37 | | return Blocks.TryGetValue(id, out block!); |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Declares the memory for the specified memory block references. |
| | | 42 | | /// </summary> |
| | | 43 | | public void Declare(IEnumerable<MemoryBlockReference> references) |
| | | 44 | | { |
| | 1478 | 45 | | foreach (var reference in references) |
| | 0 | 46 | | Declare(reference); |
| | 739 | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Declares the memory for the specified memory block reference. |
| | | 51 | | /// </summary> |
| | | 52 | | public MemoryBlock Declare(MemoryBlockReference blockReference) |
| | | 53 | | { |
| | 2492 | 54 | | if(Blocks.TryGetValue(blockReference.Id, out var block)) |
| | 17 | 55 | | return block; |
| | | 56 | | |
| | 2475 | 57 | | block = blockReference.Declare(); |
| | 2475 | 58 | | Blocks[blockReference.Id] = block; |
| | 2475 | 59 | | return block; |
| | | 60 | | } |
| | | 61 | | } |