1 /**
2 License:
3 	Boost Software License - Version 1.0 - August 17th, 2003
4 
5 	Permission is hereby granted, free of charge, to any person or organization
6 	obtaining a copy of the software and accompanying documentation covered by
7 	this license (the "Software") to use, reproduce, display, distribute,
8 	execute, and transmit the Software, and to prepare derivative works of the
9 	Software, and to permit third-parties to whom the Software is furnished to
10 	do so, all subject to the following:
11 	
12 	The copyright notices in the Software and this entire statement, including
13 	the above license grant, this restriction and the following disclaimer,
14 	must be included in all copies of the Software, in whole or in part, and
15 	all derivative works of the Software, unless such copies or derivative
16 	works are solely in the form of machine-executable object code generated by
17 	a source language processor.
18 	
19 	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 	FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
22 	SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
23 	FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
24 	ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 	DEALINGS IN THE SOFTWARE.
26 
27 Authors:
28 	aermicioi
29 **/
30 module aermicioi.aedi.container.interface_container;
31 
32 import aermicioi.aedi.container.container;
33 import aermicioi.aedi.exception.not_found_exception;
34 import aermicioi.aedi.factory.factory;
35 
36 import std.algorithm;
37 import std.array;
38 import std.range;
39 import std.typecons;
40 
41 /**
42 A container that can serve objects based on interfaces that they request.
43 
44 A container that can serve objects based on interfaces that they request. Unfortunately there is a inconsistency between name, and
45 ClassInfo system in dlang, objects registered in container which have inhereted a templated interface/class, won't be found by inherited template
46 **/
47 class InheritanceContainer : ConfigurableContainer {
48 
49     private {
50         ConfigurableContainer container_;
51         
52         string[][string] candidates;
53     }
54     
55     public {
56         
57         this(ConfigurableContainer container) {
58             this.container = container;
59         }
60         
61         InheritanceContainer container(ConfigurableContainer container) @safe nothrow {
62         	this.container_ = container;
63         
64         	return this;
65         }
66         
67         inout(ConfigurableContainer) container() @safe nothrow inout {
68         	return this.container_;
69         }
70         
71         InheritanceContainer set(ObjectFactory object, string key) {
72             this.container.set(object, key);
73             
74             ClassInfo info = cast(ClassInfo) object.type;
75             
76             if (info !is null) {
77                 this.mapBase(info, info.base);
78                 
79                 this.mapInterface(
80                     info, 
81                     info.interfaces
82                     );
83             }
84             
85             return this;
86         }
87         
88         InheritanceContainer remove(string key) {
89             this.container.remove(key);
90             
91             return this;
92         }
93         
94         Object get(string key) {
95             if (this.container.has(key)) {
96                 return this.container.get(key);
97             }
98             
99             import std.stdio;
100             if (key in this.candidates) {
101                 auto res = this.candidates[key].find!(
102                     (a) => this.container.has(a)
103                 );
104                 
105                 if (!res.empty) {
106                     return this.container.get(res[0]);
107                 }
108             }
109             
110             throw new NotFoundException("Object with id " ~ key ~ " not found.");
111         }
112         
113         bool has(in string key) inout {
114             return this.container.has(key) || (key in this.candidates);
115         }
116         
117         InheritanceContainer instantiate() {
118             this.container.instantiate();
119             
120             return this;
121         }
122         
123         InheritanceContainer link(string key, string alias_) {
124             this.container.link(key, alias_);
125             
126             return this;
127         }
128         
129         InheritanceContainer unlink(string alias_) {
130             this.container.unlink(alias_);
131             
132             return this;
133         }
134         
135         const(string) resolve(in string key) const {
136             return this.container.resolve(key);
137         }
138         
139         ObjectFactory getFactory(string identity) {
140             return this.container.getFactory(identity);
141         }
142         
143         InputRange!(Tuple!(ObjectFactory, string)) getFactories() {
144             import std.algorithm;
145             
146             return this.container.getFactories();
147         }
148     }
149     
150     private {
151         
152         void mapBase(ClassInfo original, ClassInfo inherited) {
153             this.candidates[inherited.name] ~= original.name;
154             
155             if (inherited.base !is null) {
156                 this.mapBase(original, inherited.base);
157             }
158             
159             this.mapInterface(
160                 inherited,
161                 inherited.interfaces
162             );
163         }
164         
165         void mapInterface(ClassInfo original, Interface[] inherited) {
166             foreach (iface; inherited) {
167                 if ((iface.classinfo.name !in this.candidates) || !this.candidates[iface.classinfo.name].canFind(original.name)) {
168                     this.candidates[iface.classinfo.name] ~= original.name;
169                     
170                     this.mapInterface(
171                         iface.classinfo, 
172                         iface.classinfo.interfaces
173                     );
174                 }
175             }
176         }
177     }
178 }