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.ListList;
23 
24 import capnproto.ElementSize;
25 import capnproto.ListBuilder;
26 import capnproto.ListReader;
27 import capnproto.SegmentBuilder;
28 import capnproto.SegmentReader;
29 
30 struct ListList(T)
31 {
32 	enum elementSize = ElementSize.POINTER;
33 
34 public: //Types.
35 	static struct Reader
36 	{
37 	public: //Methods.
38 		this(SegmentReader* segment, int ptr, int elementCount, int step, int structDataSize, short structPointerCount, int nestingLimit)
39 		{
40 			b = ListReader(segment, ptr, elementCount, step, structDataSize, structPointerCount, nestingLimit);
41 		}
42 		
43 		T.Reader get(int index)
44 		{
45 			return b._getPointerElement!T(index);
46 		}
47 		
48 		int opApply(scope int delegate(T.Reader) dg)
49 		{
50 			int result = 0;
51 			foreach(i; 0..b.length)
52 			{
53 				result = dg(b._getPointerElement!T(i));
54 				if(result)
55 					break;
56 			}
57 			return result;
58 		}
59 		
60 		size_t length() const
61 		{
62 			return b.length;
63 		}
64 	
65 	package: //Variables.
66 		ListReader b;
67 	}
68 	
69 	static struct Builder
70 	{
71 	public: //Methods.
72 		this(SegmentBuilder* segment, int ptr, int elementCount, int step, int structDataSize, short structPointerCount)
73 		{
74 			b = ListBuilder(segment, ptr, elementCount, step, structDataSize, structPointerCount);
75 		}
76 		
77 		T.Builder init(int index, int size)
78 		{
79 			return b._initPointerElement!T(index, size);
80 		}
81 		
82 		T.Builder get(int index)
83 		{
84 			return b._getPointerElement!T(index);
85 		}
86 		
87 		int opApply(scope int delegate(T.Builder) dg)
88 		{
89 			int result = 0;
90 			foreach(i; 0..b.length)
91 			{
92 				result = dg(b._getPointerElement!T(i));
93 				if(result)
94 					break;
95 			}
96 			return result;
97 		}
98 		
99 		size_t length() const
100 		{
101 			return b.length;
102 		}
103 	
104 	package: //Variables.
105 		ListBuilder b;
106 	}
107 }