PolicyFunction

- Switched to a delegate rather such that we can be passed a context pointer as well

SmartPolicy

- Working on a stateful policy

Queue

- Removed `maxSize`
- Updated the constructor to use the nop policy function but need a delegate firstly
- Added `makeSmart(size_t)` and `makeSmart()` which are static factories for `SmartPolicy`-based (stateful) policy functions
This commit is contained in:
Tristan B. Velloza Kildaire 2023-11-22 14:31:11 +02:00
parent 71ed7223ea
commit 57b4f930b0
1 changed files with 40 additions and 4 deletions

View File

@ -69,12 +69,12 @@ public enum PolicyDecision
}
/**
* Describes a function which can be used
* Describes a delegate which can be used
* for examining the latest item incoming
* the queue, along with the queue itself
* and return a verdict based on it
*/
public alias PolicyFunction = PolicyDecision function(Message, QueueIntrospective);
public alias PolicyFunction = PolicyDecision delegate(Message, QueueIntrospective);
/**
* NOP policy does nothing and always
@ -88,6 +88,28 @@ public PolicyDecision nop(Message, QueueIntrospective)
return PolicyDecision.ACCEPT;
}
public struct SmartPolicy
{
private size_t maxSize;
@disable
private this();
this(size_t maxSize)
{
this.maxSize = maxSize;
}
public PolicyDecision enact(Message message, QueueIntrospective queue)
{
// TODO: Implement me
return PolicyDecision.ACCEPT;
}
}
/**
* Defines the interface which policy
* functions can use in order to access
@ -105,17 +127,31 @@ public enum QUEUE_DEFAULT_SIZE = 100;
// TODO: Templatize in the future on the T element type
public class Queue : QueueIntrospective
{
private size_t maxSize; // TODO: Remove this and make a policy which respects it
private PolicyFunction policy;
private DList!(Message) queue;
private Mutex lock;
public this(size_t maxSize = QUEUE_DEFAULT_SIZE, PolicyFunction policy = &nop)
import std.functional : toDelegate;
public this(PolicyFunction policy = toDelegate(&nop))
{
this.lock = new Mutex();
this.policy = policy;
}
public static Queue makeSmart(size_t maxSize)
{
SmartPolicy smartPolicy = SmartPolicy(maxSize);
PolicyFunction smartPolicyFunction = &smartPolicy.enact;
Queue smartQueue = new Queue(smartPolicyFunction);
return smartQueue;
}
public static makeSmart()
{
return makeSmart(QUEUE_DEFAULT_SIZE);
}
public void enqueue(Message message)
{
// Lock the queue