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.PrimitiveList;
23 
24 import capnproto.ElementSize;
25 import capnproto.ListBuilder;
26 import capnproto.ListReader;
27 import capnproto.SegmentBuilder;
28 import capnproto.SegmentReader;
29 import capnproto.Void;
30 
31 struct PrimitiveList(Type)
32 {
33 	static if(is(Type == Void))
34 		enum elementSize = ElementSize.VOID;
35 	else static if(is(Type == bool))
36 		enum elementSize = ElementSize.BIT;
37 	else static if(is(Type == byte) || is(Type == ubyte))
38 		enum elementSize = ElementSize.BYTE;
39 	else static if(is(Type == short) || is(Type == ushort))
40 		enum elementSize = ElementSize.TWO_BYTES;
41 	else static if(is(Type == int) || is(Type == uint) || is(Type == float))
42 		enum elementSize = ElementSize.FOUR_BYTES;
43 	else static if(is(Type == long) || is(Type == ulong) || is(Type == double))
44 		enum elementSize = ElementSize.EIGHT_BYTES;
45 	
46 	static struct Reader
47 	{
48 	public: //Methods.
49 		this(SegmentReader* segment, int ptr, int elementCount, int step, int structDataSize, short structPointerCount, int nestingLimit)
50 		{
51 			b = ListReader(segment, ptr, elementCount, step, structDataSize, structPointerCount, nestingLimit);
52 		}
53 		
54 		size_t length()
55 		{
56 			return b.length;
57 		}
58 		
59 		Type get(size_t index)
60 		{
61 			static if(is(Type == Void))
62 				return .Void.VOID;
63 			else static if(is(Type == bool))
64 				return b._getBooleanElement(index);
65 			else static if(is(Type == byte))
66 				return b._getByteElement(index);
67 			else static if(is(Type == ubyte))
68 				return cast(ubyte)b._getByteElement(index);
69 			else static if(is(Type == short))
70 				return b._getShortElement(index);
71 			else static if(is(Type == ushort))
72 				return cast(ushort)b._getShortElement(index);
73 			else static if(is(Type == int))
74 				return b._getIntElement(index);
75 			else static if(is(Type == uint))
76 				return cast(uint)b._getIntElement(index);
77 			else static if(is(Type == float))
78 				return b._getFloatElement(index);
79 			else static if(is(Type == double))
80 				return b._getDoubleElement(index);
81 			else static if(is(Type == long))
82 				return b._getLongElement(index);
83 			else static if(is(Type == ulong))
84 				return cast(ulong)b._getLongElement(index);
85 		}
86 		
87 		Type opIndex(size_t index)
88 		{
89 			return get(index);
90 		}
91 		
92 		int opApply(scope int delegate(Type) dg)
93 		{
94 			int result = 0;
95 			foreach(i; 0..b.length)
96 			{
97 				result = dg(opIndex(i));
98 				if(result)
99 					break;
100 			}
101 			return result;
102 		}
103 		
104 		int opApply(scope int delegate(size_t,Type) dg)
105 		{
106 			int result = 0;
107 			foreach(i; 0..b.length)
108 			{
109 				result = dg(i, opIndex(i));
110 				if(result)
111 					break;
112 			}
113 			return result;
114 		}
115 	
116 	package: //Variables.
117 		ListReader b;
118 	}
119 	
120 	static struct Builder
121 	{
122 	public: //Methods.
123 		this(SegmentBuilder* segment, int ptr, int elementCount, int step, int structDataSize, short structPointerCount)
124 		{
125 			b = ListBuilder(segment, ptr, elementCount, step, structDataSize, structPointerCount);
126 		}
127 		
128 		size_t length()
129 		{
130 			return b.length;
131 		}
132 		
133 		Type get(size_t index)
134 		{
135 			static if(is(Type == Void))
136 				return .Void.VOID;
137 			else static if(is(Type == bool))
138 				return b._getBooleanElement(index);
139 			else static if(is(Type == byte))
140 				return b._getByteElement(index);
141 			else static if(is(Type == ubyte))
142 				return cast(ubyte)b._getByteElement(index);
143 			else static if(is(Type == short))
144 				return b._getShortElement(index);
145 			else static if(is(Type == ushort))
146 				return cast(ushort)b._getShortElement(index);
147 			else static if(is(Type == int))
148 				return b._getIntElement(index);
149 			else static if(is(Type == uint))
150 				return cast(uint)b._getIntElement(index);
151 			else static if(is(Type == float))
152 				return b._getFloatElement(index);
153 			else static if(is(Type == double))
154 				return b._getDoubleElement(index);
155 			else static if(is(Type == long))
156 				return b._getLongElement(index);
157 			else static if(is(Type == ulong))
158 				return cast(ulong)b._getLongElement(index);
159 		}
160 		
161 		Type opIndex(size_t index)
162 		{
163 			return get(index);
164 		}
165 		
166 		void set(int index, Type value)
167 		{
168 			static if(is(Type == Void)) {}
169 				//static assert(0, "Cannot set Void!");
170 			else static if(is(Type == bool))
171 				b._setBooleanElement(index, value);
172 			else static if(is(Type == byte))
173 				b._setByteElement(index, value);
174 			else static if(is(Type == ubyte))
175 				b._setByteElement(index, value);
176 			else static if(is(Type == short))
177 				b._setShortElement(index, value);
178 			else static if(is(Type == ushort))
179 				b._setShortElement(index, value);
180 			else static if(is(Type == int))
181 				b._setIntElement(index, value);
182 			else static if(is(Type == uint))
183 				b._setIntElement(index, value);
184 			else static if(is(Type == float))
185 				b._setFloatElement(index, value);
186 			else static if(is(Type == double))
187 				b._setDoubleElement(index, value);
188 			else static if(is(Type == long))
189 				b._setLongElement(index, value);
190 			else static if(is(Type == ulong))
191 				b._setLongElement(index, value);
192 		}
193 	
194 	package: //Variables.
195 		ListBuilder b;
196 	}
197 }