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 /// X coordinate 27 float x; 28 /// Y coordinate 29 float y; 30 /// Z coordinate 31 float z; 32 } 33 34 /++ 35 Helper class which emulates an ArrayList due to 36 dynamic arrays not having a remove function for 37 elements. 38 39 Uses an associative array to emulate. 40 +/ 41 synchronized class ArrayList(T) { 42 private shared size_t counter = 0; 43 private shared T[size_t] list; 44 45 /// Representation of the ArrayList as an Array. Returns a copy. 46 @property T[] array() @trusted { 47 return cast(T[]) list.values(); 48 } 49 50 /++ 51 Adds the element to the array at the next 52 position. 53 54 Params: 55 element = The element to be added. 56 +/ 57 void add(T element) @trusted { 58 import core.atomic; 59 atomicOp!"+="(counter, 1); 60 61 list[counter] = cast(shared) element; 62 } 63 64 /++ 65 Removes the element from the array. 66 67 Params: 68 element = The element to be removed. 69 +/ 70 void remove(T element) @trusted { 71 size_t posToRemove; 72 foreach(key, val; list) { 73 if((cast(T) val) == element) { 74 posToRemove = key; 75 break; 76 } 77 } 78 list.remove(posToRemove); 79 } 80 } 81 82 /++ 83 Converts a D string (immutable(char[])) to a C string 84 (char*). 85 86 Params: 87 dString = The D string to be converted. 88 89 Returns: A C string (char array). 90 +/ 91 char* toCString(in string dString) @trusted { 92 import std.string : toStringz; 93 return cast(char*) toStringz(dString); 94 } 95 96 /++ 97 Converts a C string (char array) to a D string 98 (immutable(char[])) 99 100 Params: 101 cString = The C string to be converted. 102 103 Returns: A D string (immutable(char[])) 104 +/ 105 string toDString(in char* cString) @trusted { 106 import std.string : fromStringz; 107 return cast(string) fromStringz(cString); 108 }