1 /*
2  * Copyright (c) 2016-2018 sel-project
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all
12  * copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  *
22  */
23 module soupply.util;
24 
25 import std..string : split, join;
26 import std.uuid : _UUID = UUID;
27 
28 import xpacket : Packet, Exclude;
29 
30 import xbuffer.buffer : Buffer;
31 
32 template Pad(size_t padding, T:Packet) {
33 
34 	private ubyte[] __padding = new ubyte[padding];
35 
36 	class Pad : T {
37 	
38 		override ubyte[] encode(Buffer buffer) {
39 			encodeId(buffer);
40 			buffer.writeData(__padding);
41 			encodeBody(buffer);
42 			return buffer.data!ubyte;
43 		}
44 		
45 		alias encode = typeof(super).encode;
46 		
47 		override void decode(Buffer buffer) {
48 			decodeId(buffer);
49 			buffer.readData(padding);
50 			decodeBody(buffer);
51 		}
52 		
53 		alias decode = typeof(super).decode;
54 	
55 	}
56 
57 }
58 
59 struct Vector(T, string variables) if(variables.length)
60 {
61 
62 	union
63 	{
64 	
65 		T[variables.length] array;
66 		struct
67 		{
68 			mixin("@Exclude T " ~ variables.split("").join(";@Exclude T ") ~ ";");
69 		}
70 	
71 	}
72 	
73 	this(T[] values...)
74 	{
75 		this.array = values;
76 	}
77 
78 }
79 
80 struct UUID {
81 
82 	_UUID uuid;
83 	
84 	void serialize(Buffer buffer) {
85 		buffer.writeData(uuid.data);
86 	}
87 	
88 	void deserialize(Buffer buffer) {
89 		ubyte[16] data = cast(ubyte[])buffer.readData(16);
90 		uuid = _UUID(data);
91 	}
92 	
93 	alias uuid this;
94 
95 }