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.arch.emm; 26 27 import des.util.arch.tree; 28 29 /// 30 interface ExternalMemoryManager : TNode!(ExternalMemoryManager,"","EMM") 31 { 32 /// 33 mixin template EMM(string file=__FILE__,size_t line=__LINE__) 34 { 35 static if( !is(typeof(__EMM_BASE_IMPLEMENT)) ) 36 { 37 protected enum __EMM_BASE_IMPLEMENT = true; 38 39 mixin TNodeHelperEMM!(true,true,true); 40 41 private bool is_destroyed = false; 42 public final bool isDestroyed() const { return is_destroyed; } 43 protected final void isDestroyed( bool d ) 44 { 45 bool change = is_destroyed != d; 46 is_destroyed = d; 47 if( change && !is_destroyed ) 48 selfConstruct(); 49 } 50 51 import std.traits; 52 53 protected override 54 { 55 static if( isAbstractFunction!selfConstruct ) void selfConstruct() {} 56 static if( isAbstractFunction!selfDestroy ) void selfDestroy() {} 57 static if( isAbstractFunction!preChildsDestroy ) void preChildsDestroy() {} 58 } 59 } 60 else 61 { 62 version(emmcheck) 63 pragma(msg, format( "WARNING: duplicate mixin EMM at %s:%d", file, line ) ); 64 } 65 } 66 67 protected 68 { 69 /// 70 @property void isDestroyed( bool d ); 71 72 /// 73 void selfConstruct(); 74 /// 75 void selfDestroy(); 76 /// 77 void preChildsDestroy(); 78 } 79 80 /// 81 @property bool isDestroyed() const; 82 83 final 84 { 85 /// 86 T registerChildEMM(T)( T obj ) 87 if( is( T == class ) || is( T == interface ) ) 88 { 89 auto cemm = cast(ExternalMemoryManager)obj; 90 if( cemm ) attachChildsEMM( cemm ); 91 return obj; 92 } 93 94 /// 95 T[] registerChildEMM(T)( T[] objs ) 96 if( is( T == class ) || is( T == interface ) ) 97 { 98 foreach( obj; objs ) 99 registerChildEMM( obj ); 100 return objs; 101 } 102 103 /// 104 T newEMM(T,Args...)( Args args ) 105 { return registerChildEMM( new T(args) ); } 106 107 /// 108 void destroy() 109 { 110 if( isDestroyed ) return; 111 preChildsDestroy(); 112 foreach( cemm; childsEMM ) 113 cemm.destroy(); 114 selfDestroy(); 115 isDestroyed = true; 116 } 117 } 118 } 119 120 version(todos) pragma(msg,__FILE__, "TODO: add unittests");