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.StructList; 23 24 import capnproto.ElementSize; 25 import capnproto.ListBuilder; 26 import capnproto.ListReader; 27 import capnproto.SegmentReader; 28 import capnproto.SegmentBuilder; 29 import capnproto.StructBuilder; 30 import capnproto.StructReader; 31 import capnproto.WireHelpers; 32 33 struct StructList(T) 34 { 35 enum elementSize = ElementSize.INLINE_COMPOSITE; 36 alias structSize = T.structSize; 37 38 public: //Types. 39 static struct Reader 40 { 41 public: //Methods. 42 this(SegmentReader* segment, int ptr, int elementCount, int step, int structDataSize, short structPointerCount, int nestingLimit) 43 { 44 b = ListReader(segment, ptr, elementCount, step, structDataSize, structPointerCount, nestingLimit); 45 } 46 47 size_t length() 48 { 49 return b.length; 50 } 51 52 T.Reader get(size_t index) 53 { 54 return b._getStructElement!T(cast(int)index); 55 } 56 57 T.Reader opIndex(size_t index) 58 { 59 return b._getStructElement!T(cast(int)index); 60 } 61 62 int opApply(scope int delegate(T.Reader) dg) 63 { 64 int result = 0; 65 foreach(i; 0..b.length) 66 { 67 result = dg(b._getStructElement!T(i)); 68 if(result) 69 break; 70 } 71 return result; 72 } 73 74 int opApply(scope int delegate(size_t,T.Reader) dg) 75 { 76 int result = 0; 77 foreach(i; 0..b.length) 78 { 79 result = dg(i, b._getStructElement!T(i)); 80 if(result) 81 break; 82 } 83 return result; 84 } 85 86 package: //Variables. 87 ListReader b; 88 } 89 90 static struct Builder 91 { 92 public: //Methods. 93 this(SegmentBuilder* segment, int ptr, int elementCount, int step, int structDataSize, short structPointerCount) 94 { 95 b = ListBuilder(segment, ptr, elementCount, step, structDataSize, structPointerCount); 96 } 97 98 size_t length() 99 { 100 return b.length; 101 } 102 103 T.Builder get(size_t index) 104 { 105 return b._getStructElement!T(cast(int)index); 106 } 107 108 T.Builder opIndex(size_t index) 109 { 110 return b._getStructElement!T(cast(int)index); 111 } 112 113 int opApply(scope int delegate(T.Builder) dg) 114 { 115 int result = 0; 116 foreach(i; 0..b.length) 117 { 118 result = dg(b._getStructElement!T(i)); 119 if(result) 120 break; 121 } 122 return result; 123 } 124 125 private: //Variables. 126 ListBuilder b; 127 } 128 }