1 /* 2 * zlib License 3 * 4 * (C) 2016 jython234 5 * 6 * This software is provided 'as-is', without any express or implied 7 * warranty. In no event will the authors be held liable for any damages 8 * arising from the use of this software. 9 * 10 * Permission is granted to anyone to use this software for any purpose, 11 * including commercial applications, and to alter it and redistribute it 12 * freely, subject to the following restrictions: 13 * 14 * 1. The origin of this software must not be misrepresented; you must not 15 * claim that you wrote the original software. If you use this software 16 * in a product, an acknowledgment in the product documentation would be 17 * appreciated but is not required. 18 * 2. Altered source versions must be plainly marked as such, and must not be 19 * misrepresented as being the original software. 20 * 3. This notice may not be removed or altered from any source distribution. 21 */ 22 module blocksound.util; 23 24 /// Vector 3 struct with floats. 25 struct Vec3 { 26 private shared float _x; 27 private shared float _y; 28 private shared float _z; 29 30 @property float x() @safe nothrow const { return _x; } 31 32 @property float y() @safe nothrow const { return _y; } 33 34 @property float z() @safe nothrow const { return _z; } 35 36 this(float x, float y, float z) @safe nothrow { 37 this._x = x; 38 this._y = y; 39 this._z = z; 40 } 41 } 42 43 /// Represents an exception when an End-Of-File is reached. 44 class EOFException : Exception { 45 /// Default constructor 46 this(in string msg) { 47 super(msg); 48 } 49 } 50 51 /++ 52 Helper class which emulates an ArrayList due to 53 dynamic arrays not having a remove function for 54 elements. 55 56 Uses an associative array to emulate. 57 +/ 58 synchronized class ArrayList(T) { 59 private shared size_t counter = 0; 60 private shared T[size_t] list; 61 62 /// Representation of the ArrayList as an Array. Returns a copy. 63 @property T[] array() @trusted { 64 return cast(T[]) list.values(); 65 } 66 67 /++ 68 Adds the element to the array at the next 69 position. 70 71 Params: 72 element = The element to be added. 73 +/ 74 void add(T element) @trusted { 75 import core.atomic; 76 atomicOp!"+="(counter, 1); 77 78 list[counter] = cast(shared) element; 79 } 80 81 /++ 82 Removes the element from the array. 83 84 Params: 85 element = The element to be removed. 86 +/ 87 void remove(T element) @trusted { 88 size_t posToRemove; 89 foreach(key, val; list) { 90 if((cast(T) val) == element) { 91 posToRemove = key; 92 break; 93 } 94 } 95 list.remove(posToRemove); 96 } 97 } 98 99 /++ 100 Converts a D string (immutable(char[])) to a C string 101 (char*). 102 103 Params: 104 dString = The D string to be converted. 105 106 Returns: A C string (char array). 107 +/ 108 char* toCString(in string dString) @trusted { 109 import std.string : toStringz; 110 return cast(char*) toStringz(dString); 111 } 112 113 /++ 114 Converts a C string (char array) to a D string 115 (immutable(char[])) 116 117 Params: 118 cString = The C string to be converted. 119 120 Returns: A D string (immutable(char[])) 121 +/ 122 string toDString(in char* cString) @trusted { 123 import std.string : fromStringz; 124 return cast(string) fromStringz(cString); 125 }