1 /**
2 
3 License:
4 	Boost Software License - Version 1.0 - August 17th, 2003
5 
6     Permission is hereby granted, free of charge, to any person or organization
7     obtaining a copy of the software and accompanying documentation covered by
8     this license (the "Software") to use, reproduce, display, distribute,
9     execute, and transmit the Software, and to prepare derivative works of the
10     Software, and to permit third-parties to whom the Software is furnished to
11     do so, all subject to the following:
12 
13     The copyright notices in the Software and this entire statement, including
14     the above license grant, this restriction and the following disclaimer,
15     must be included in all copies of the Software, in whole or in part, and
16     all derivative works of the Software, unless such copies or derivative
17     works are solely in the form of machine-executable object code generated by
18     a source language processor.
19 
20     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22     FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
23     SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
24     FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
25     ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26     DEALINGS IN THE SOFTWARE.
27 
28 Authors:
29 	Alexandru Ermicioi
30 **/
31 module aermicioi.aedi.container.container;
32 
33 import aermicioi.aedi.factory.factory;
34 import aermicioi.aedi.storage.locator;
35 import aermicioi.aedi.storage.storage;
36 import aermicioi.aedi.storage.alias_aware;
37 
38 /**
39 Interface for objects that instantiate and manage the lifetime of objects in it.
40 **/
41 @safe interface Container : Locator!(Object, string) {
42 
43     public {
44 
45         /**
46         Sets up the internal state of container.
47 
48         Sets up the internal state of container (Ex, for singleton container it will spawn all objects that locator contains).
49         **/
50         Container instantiate();
51 
52         /**
53         Destruct all managed components.
54 
55         Destruct all managed components. The method denotes the end of container lifetime, and therefore destruction of all managed components
56         by it.
57         **/
58         Container terminate();
59     }
60 }
61 
62 /**
63  Buildable/configurable instantiatiator with factories, and aliasing.
64 **/
65 @safe interface ConfigurableContainer : Container, Storage!(ObjectFactory, string), AliasAware!(string), FactoryLocator!ObjectFactory {
66 
67 }
68 
69 /**
70 Mix in container interface implementation that delegates
71 it to decorated container.
72 **/
73 @safe mixin template ContainerMixin(T : Container) {
74 
75     /**
76     Sets up the internal state of container.
77 
78     Sets up the internal state of container (Ex, for singleton container it will spawn all objects that locator contains).
79     **/
80     typeof(this) instantiate()
81     in (this.decorated !is null, "A container that decorates another container cannot instantiate components when no decorated container has been set")
82     {
83         this.decorated.instantiate();
84 
85         return this;
86     }
87 
88 
89     /**
90     Destruct all managed components.
91 
92     Destruct all managed components. The method denotes the end of container lifetime, and therefore destruction of all managed components
93     by it.
94     **/
95     typeof(this) terminate()
96     in (this.decorated !is null, "A container that decorates another container cannot terminate components when no decorated container has been set.")
97     {
98         this.decorated.terminate();
99 
100         return this;
101     }
102 }
103 
104 /**
105 Provide an interface for accessing factories used by containers to instantiate component.
106 **/
107 @safe interface FactoryLocator(T : Factory!Z, Z) {
108     import std.range.interfaces : InputRange;
109     import aermicioi.aedi.util.typecons : Pair, pair;
110 
111     public {
112 
113         /**
114         Get factory for constructed component identified by identity.
115 
116         Get factory for constructed component identified by identity.
117         Params:
118         	identity = the identity of component that factory constructs.
119 
120         Throws:
121         	NotFoundException when factory for it is not found.
122 
123         Returns:
124         	T the factory for constructed component.
125         **/
126         T getFactory(string identity);
127 
128         /**
129         Get all factories available in container.
130 
131         Get all factories available in container.
132 
133         Returns:
134         	InputRange!(Tuple!(T, string)) a tuple of factory => identity.
135         **/
136         InputRange!(Pair!(T, string)) getFactories();
137     }
138 }
139 
140 /**
141 Mix in factory locator interface implementation that delegates
142 the logic to decorated container.
143 **/
144 @safe mixin template FactoryLocatorMixin(T : FactoryLocator!W, W) {
145     import std.range.interfaces : InputRange;
146     import aermicioi.aedi.util.typecons : Pair, pair;
147 
148     /**
149     Get factory for constructed component identified by identity.
150 
151     Get factory for constructed component identified by identity.
152     Params:
153         identity = the identity of component that factory constructs.
154 
155     Throws:
156         NotFoundException when factory for it is not found.
157 
158     Returns:
159         ObjectFactory the factory for constructed component.
160     **/
161     W getFactory(string identity)
162     in (decorated !is null, "A container that is decorating factory locator interface cannot operate without a decorated container")
163     {
164         return this.decorated.getFactory(identity);
165     }
166 
167     /**
168     Get all factories available in container.
169 
170     Get all factories available in container.
171 
172     Returns:
173         InputRange!(Tuple!(ObjectFactory, string)) a tuple of factory => identity.
174     **/
175     InputRange!(Pair!(W, string)) getFactories()
176     in (decorated !is null, "A container that is decorating factory locator interface cannot operate without a decorated container")
177     {
178         return this.decorated.getFactories();
179     }
180 }