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.tuple_container;
31 
32 import aermicioi.aedi.container.container;
33 import aermicioi.aedi.storage.storage;
34 import aermicioi.aedi.storage.locator;
35 import aermicioi.aedi.storage.object_storage;
36 import aermicioi.aedi.exception.not_found_exception;
37 import std.range.interfaces;
38 import std.typecons;
39 import std.container;
40 
41 /**
42 An aggregate container with knowledge of concrete types of aggregated containers.
43 It delegates the task of serving an object to contained containers.
44 **/
45 class TupleContainer(T...) : Container {
46 
47     public {
48         T containers;
49         
50         alias containers this;
51         
52         /**
53         Construct tuple container with a set of aggregate containers.
54         
55         Params:
56             containers = a sequence of containers
57         **/
58         this(T containers) {
59             this.containers = containers;
60         }
61         
62         /**
63         Get a container, or an object that is contained by managed containers.
64         
65         Get a container, or an object that is contained by managed containers.
66         
67         Params:
68         	identity = identity of object that is to be supplied.
69         
70         Throws:
71         	NotFoundException when no requested object by identity is present in container
72         
73         Returns:
74         	Object the object contained in one of containers or a container itself.
75         **/
76         Object get(string identity) {
77             
78         	foreach (container; this.containers) {
79         	    if (container.has(identity)) {
80         	        return container.get(identity);
81         	    }
82         	}
83         	
84         	throw new NotFoundException("Object by id " ~ identity ~ " not found.");
85         }
86         
87         /**
88         Check if an object is present in one of containers, or it is a container itself.
89         
90         Check if an object is present in one of containers, or it is a container itself.
91         
92         Params:
93         	identity = identity of object to be checked
94         
95         Returns:
96         	bool true if exists, false otherwise
97         **/
98         bool has(in string identity) inout {
99 
100             foreach (container; this.containers) {
101                 if (container.has(identity)) {
102                     return true;
103                 }
104             }
105             
106             return false;
107         }
108         
109         /**
110         Finalize all unfinished initialization work in containers.
111         
112         Finalize all unfinished initialization work in containers.
113         
114         Returns:
115         	TupleContainer
116         **/
117         TupleContainer instantiate() {
118             
119             foreach (container; this.containers) {
120                 container.instantiate;
121             }
122             
123             return this;
124         }
125     }
126 }