1 /* 2 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 */ 28 module derelict.util.exception; 29 30 private 31 { 32 import derelict.util.compat; 33 } 34 35 /** 36 * Base class for all exceptions thrown by Derelict packages. 37 */ 38 class DerelictException : Exception 39 { 40 public: 41 this(string msg) 42 { 43 super(msg); 44 } 45 } 46 47 /** 48 * Helper struct to facilitate throwing a single SharedLibException after failing 49 * to load a library using multiple names. 50 */ 51 struct FailedSharedLib 52 { 53 string name; 54 string reason; 55 } 56 57 /** 58 * This exception is thrown when a shared library cannot be loaded 59 * because it is either missing or not on the system path. 60 */ 61 class SharedLibLoadException : DerelictException 62 { 63 public: 64 static void throwNew(in char[][] libNames, in char[][] reasons) 65 { 66 string msg = "Failed to load one or more shared libraries:"; 67 foreach(i, n; libNames) 68 { 69 msg ~= "\n\t" ~ n ~ " - "; 70 if(i < reasons.length) 71 msg ~= reasons[i]; 72 else 73 msg ~= "Unknown"; 74 } 75 throw new SharedLibLoadException(msg); 76 } 77 78 this(string msg) 79 { 80 super(msg); 81 _sharedLibName = ""; 82 } 83 84 this(string msg, string sharedLibName) 85 { 86 super(msg); 87 _sharedLibName = sharedLibName; 88 } 89 90 string sharedLibName() 91 { 92 return _sharedLibName; 93 } 94 95 private: 96 string _sharedLibName; 97 } 98 99 /** 100 * This exception is thrown when a symbol cannot be loaded from a shared library, 101 * either because it does not exist in the library or because the library is corrupt. 102 */ 103 class SymbolLoadException : DerelictException 104 { 105 public: 106 this(string msg) 107 { 108 super(msg); 109 } 110 111 this(string sharedLibName, string symbolName) 112 { 113 super("Failed to load symbol " ~ symbolName ~ " from shared library " ~ sharedLibName); 114 _symbolName = symbolName; 115 } 116 117 string symbolName() 118 { 119 return _symbolName; 120 } 121 122 private: 123 string _symbolName; 124 } 125 126 alias SymbolLoadException SharedLibProcLoadException; 127 128 //****************************************************************************** 129 130 /** 131 * The MissingSymbolCallback allows the app to handle the case of missing symbols. By default, 132 * a SharedLibSymbolLoadException is thrown. However, if a the app determines that 133 * particular symbol is not needed, the callback can return true. This will cause 134 * the shared library to continue loading. Returning false will cause the exception 135 * to be thrown. 136 */ 137 alias bool function(string libName, string symbolName) MissingSymbolCallback; 138 alias MissingSymbolCallback MissingProcCallback; 139 140 private MissingSymbolCallback missingSymbolCallback; 141 142 public void Derelict_HandleMissingSymbol(string libName, string symbolName) 143 { 144 bool result = false; 145 if(missingSymbolCallback !is null) 146 result = missingSymbolCallback(libName, symbolName); 147 if(!result) 148 throw new SymbolLoadException(libName, symbolName); 149 } 150 alias Derelict_HandleMissingSymbol Derelict_HandleMissingProc; 151 152 void Derelict_SetMissingSymbolCallback(MissingSymbolCallback callback) 153 { 154 missingSymbolCallback = callback; 155 } 156 alias Derelict_SetMissingSymbolCallback Derelict_SetMissingProcCallback;