1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
2 // Licensed under the MIT License:
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
12 // all 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
20 // THE SOFTWARE.
21 
22 module capnproto.ElementSize;
23 
24 struct ElementSize
25 {
26 	static enum ubyte VOID = 0;
27 	static enum ubyte BIT = 1;
28 	static enum ubyte BYTE = 2;
29 	static enum ubyte TWO_BYTES = 3;
30 	static enum ubyte FOUR_BYTES = 4;
31 	static enum ubyte EIGHT_BYTES = 5;
32 	static enum ubyte POINTER = 6;
33 	static enum ubyte INLINE_COMPOSITE = 7;
34 	
35 	static uint dataBitsPerElement(ubyte size)
36 	{
37 		switch(size)
38 		{
39 			case VOID:
40 				return 0;
41 			case BIT:
42 				return 1;
43 			case BYTE:
44 				return 8;
45 			case TWO_BYTES:
46 				return 16;
47 			case FOUR_BYTES:
48 				return 32;
49 			case EIGHT_BYTES:
50 				return 64;
51 			case POINTER:
52 				return 0;
53 			case INLINE_COMPOSITE:
54 				return 0;
55 			default:
56 				break;
57 		}
58 		import std.conv : to;
59 		assert(0, "Impossible field size: " ~ to!string(size));
60 	}
61 	
62 	static ushort pointersPerElement(ubyte size)
63 	{
64 		switch(size)
65 		{
66 			case POINTER:
67 				return 1;
68 			default:
69 				break;
70 		}
71 		return 0;
72 	}
73 }