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.value_container; 31 32 import aermicioi.aedi.container.container; 33 import aermicioi.aedi.storage.storage; 34 import aermicioi.aedi.storage.alias_aware; 35 import aermicioi.aedi.storage.locator; 36 import aermicioi.aedi.storage.object_storage; 37 import aermicioi.aedi.exception.not_found_exception; 38 39 /** 40 Value container for instantiated components. 41 42 **/ 43 @safe class ValueContainer : Container, Storage!(Object, string) { 44 45 private { 46 ObjectStorage!(Object, string) values; 47 } 48 49 public { 50 51 /** 52 Default constructor for ValueContainer 53 **/ 54 this() { 55 this.values = new ObjectStorage!(Object, string); 56 } 57 58 /** 59 Save an object in ValueContainer by identity. 60 61 Params: 62 identity = identity of element in Storage. 63 object = element which is to be saved in Storage. 64 65 Return: 66 ValueContainer 67 **/ 68 ValueContainer set(Object object, string identity) { 69 this.values.set(object, identity); 70 71 return this; 72 } 73 74 /** 75 Remove an object from ValueContainer with identity. 76 77 Remove an object from ValueContainer with identity. If there is no element by provided identity, then no action is performed. 78 79 Params: 80 identity = the identity of element to be removed. 81 82 Return: 83 ValueContainer 84 **/ 85 ValueContainer remove(string identity) { 86 this.values.remove(identity); 87 88 return this; 89 } 90 91 /** 92 Get an Object that is associated with identity. 93 94 Params: 95 identity = the object id. 96 97 Throws: 98 NotFoundException in case if the element wasn't found. 99 100 Returns: 101 Object element if it is available. 102 **/ 103 Object get(string identity) { 104 105 if (this.values.has(identity)) { 106 return this.values.get(identity); 107 } 108 109 throw new NotFoundException("Component by ${identity} not found.", identity); 110 } 111 112 /** 113 Check if an object is present in Locator by identity. 114 115 Note: 116 This check should be done for elements that locator actually contains, and 117 not in chained locator. 118 Params: 119 identity = identity of element. 120 121 Returns: 122 bool true if an element by key is present in Locator. 123 **/ 124 bool has(in string identity) inout { 125 126 return this.values.has(identity); 127 } 128 129 /** 130 Sets up the internal state of container. 131 132 Sets up the internal state of container (Ex, for singleton container it will spawn all objects that locator contains). 133 In case of value container it does nothing. 134 **/ 135 ValueContainer instantiate() { 136 137 // We do nothing since all components is already instantiated. 138 return this; 139 } 140 141 /** 142 Destruct all managed components. 143 144 Destruct all managed components. The method denotes the end of container lifetime, and therefore destruction of all managed components 145 by it. 146 **/ 147 ValueContainer terminate() { 148 149 // We do nothing since none of contained are instantiated by container. 150 return this; 151 } 152 } 153 }