1 /* 2 * hunt-proton: AMQP Protocol library for D programming language. 3 * 4 * Copyright (C) 2018-2019 HuntLabs 5 * 6 * Website: https://www.huntlabs.net/ 7 * 8 * Licensed under the Apache-2.0 License. 9 * 10 */ 11 12 module hunt.proton.engine.ExtendableAccessor; 13 14 import hunt.proton.engine.Extendable; 15 import hunt.proton.engine.Record; 16 import hunt.Exceptions; 17 18 /** 19 * A typesafe convenience class for associating additional data with {@link Extendable} classes. 20 * <p> 21 * An instance of <code>ExtendableAccessor</code> uses itself as the key in the {@link Extendable#attachments()} 22 * so it's best instantiated as a static member. 23 * <pre><code> 24 * class Foo extends BaseHandler { 25 * private static ExtendableAccessor<Link, Bar> LINK_BAR = new ExtendableAccessor<>(Bar.class); 26 * void onLinkRemoteOpen(Event e) { 27 * Bar bar = LINK_BAR.get(e.getLink()); 28 * if (bar is null) { 29 * bar = new Bar(); 30 * LINK_BAR.set(e.getLink(), bar); 31 * } 32 * } 33 * } 34 * </code></pre> 35 * 36 * @param <E> An {@link Extendable} type where the data is to be stored 37 * @param <T> The type of the data to be stored 38 */ 39 class ExtendableAccessor(E,T) : Extendable { 40 private TypeInfo klass; 41 this() { 42 this.klass = typeid(T); 43 } 44 45 public T get(E e, string key) { 46 return cast(T)(e.attachments().get(key)); 47 } 48 49 public void set(E e, T value ,string key) { 50 e.attachments().set(key, cast(Object)value); 51 } 52 53 Record attachments() { 54 implementationMissing(false); 55 return null; 56 } 57 }