1 /+
2 The MIT License (MIT)
3 
4     Copyright (c) <2013> <Oleg Butko (deviator), Anton Akzhigitov (Akzwar)>
5 
6     Permission is hereby granted, free of charge, to any person obtaining a copy
7     of this software and associated documentation files (the "Software"), to deal
8     in the Software without restriction, including without limitation the rights
9     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10     copies of the Software, and to permit persons to whom the Software is
11     furnished to do so, subject to the following conditions:
12 
13     The above copyright notice and this permission notice shall be included in
14     all copies or substantial portions of the Software.
15 
16     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22     THE SOFTWARE.
23 +/
24 
25 module des.util.emm;
26 
27 interface ExternalMemoryManager
28 {
29     mixin template DirectEMM()
30     {
31         private ExternalMemoryManager[] chemm;
32         protected final ref ExternalMemoryManager[] childEMM() { return chemm; }
33     }
34 
35     mixin template ParentEMM()
36     {
37         mixin DirectEMM;
38         protected void selfDestroy() {}
39     }
40 
41     protected
42     {
43         @property ref ExternalMemoryManager[] childEMM();
44         void selfDestroy();
45     }
46 
47     final
48     {
49         T registerChildEMM(T)( T obj )
50             if( is( T == class ) || is( T == interface ) )
51         {
52             auto cemm = cast(ExternalMemoryManager)obj;
53             if( cemm ) childEMM ~= cemm; 
54             return obj;
55         }
56 
57         T[] registerChildEMM(T)( T[] objs )
58             if( is( T == class ) || is( T == interface ) )
59         {
60             foreach( obj; objs )
61                 registerChildEMM( obj );
62             return objs;
63         }
64 
65         T newEMM(T,Args...)( Args args )
66         { return registerChildEMM( new T(args) ); }
67 
68         void destroy()
69         {
70             foreach( cemm; childEMM )
71                 cemm.destroy();
72             selfDestroy();
73         }
74     }
75 }