1 module injected.resolver;
2 
3 /**
4  * Interface for object that can resolve instances of a type, possibly with a name.
5  */
6 interface Resolver {
7 
8     /**
9      * Resolve an instance of the type of TypeInfo, and execute a delegate with
10      * a pointer to that instance (as a `void*`).
11      *
12      * A name can be used to disambiguate between multiple providers
13      */
14     protected void withResolvedPtr(TypeInfo info, void delegate(void*) dg);
15     /// ditto
16     protected void withResolvedPtr(TypeInfo info, string name, void delegate(void*) dg);
17 
18     /**
19      * Resolve an instance of type T. If name is provided
20      * use that to disambiguate between multiple providers.
21      */
22     final T resolve(T)() {
23         assert(canResolve(typeid(T)));
24         T result;
25         withResolvedPtr(typeid(T), delegate(ptr) {
26                 result = *(cast(T*) ptr);
27             });
28         return result;
29     }
30 
31     /// ditto
32     final T resolve(T)(string name) {
33         assert(canResolve(typeid(T)));
34         T result;
35         withResolvedPtr(typeid(T), name, delegate(ptr) {
36                 result = *(cast(T*) ptr);
37             });
38         return result;
39     }
40 
41     /**
42      * Check if the resolver can resolve a type (possibly with a name).
43      */
44     bool canResolve(TypeInfo type);
45     /// ditto
46     bool canResolve(TypeInfo type,  string name);
47 }