1 2 module autoloader.base; 3 4 abstract class Autoloader 5 { 6 private: 7 /++ 8 + Cached ClassInfo objects. 9 ++/ 10 ClassInfo[string] _cache; 11 12 public: 13 /++ 14 + Loads and constructs an Object given by name. 15 + Optionally casts the Object to a type given by template parameter. 16 + 17 + Returns: 18 + The newly created Object, or null if not found or the cast failed. 19 ++/ 20 T create(T : Object = Object)(string name) 21 { 22 auto result = load(name); 23 24 if(result !is null) 25 { 26 return cast(T) result.create; 27 } 28 else 29 { 30 return null; 31 } 32 } 33 34 /++ 35 + Tries to load a class given by name, searching through the registered paths. 36 + This function calls lookup internally, but caches results once found. 37 + 38 + Returns: 39 + The ClassInfo object of the class given by name, or null if not found. 40 ++/ 41 const(ClassInfo) load(string name) 42 { 43 auto cachePtr = name in _cache; 44 45 if(cachePtr !is null) 46 { 47 return cast(const) *cachePtr; 48 } 49 else 50 { 51 const(ClassInfo) result = lookup(name); 52 if(result) _cache[name] = cast(ClassInfo) result; 53 54 return result; 55 } 56 } 57 58 /++ 59 + Tries to load a class given by name. This function does not cache located 60 + classes, hence the lookup is performed for every call. 61 + 62 + Returns: 63 + The ClassInfo object of the class given by name, or null if not found. 64 ++/ 65 abstract const(ClassInfo) lookup(string name); 66 67 /++ 68 + Clears any cached results. 69 ++/ 70 void purgeCache() 71 { 72 _cache = typeof(_cache).init; 73 } 74 }