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.EnumList; 23 24 import capnproto.ElementSize; 25 import capnproto.ListBuilder; 26 import capnproto.ListReader; 27 import capnproto.SegmentBuilder; 28 import capnproto.SegmentReader; 29 30 struct EnumList(T) 31 { 32 enum elementSize = ElementSize.TWO_BYTES; 33 34 public: 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 get(int index) 44 { 45 return clampOrdinal!T(b._getShortElement(index)); 46 } 47 48 int opApply(scope int delegate(T) dg) 49 { 50 int result = 0; 51 foreach(i; 0..b.length) 52 { 53 result = dg(clampOrdinal!T(b._getShortElement(i))); 54 if(result) 55 break; 56 } 57 return result; 58 } 59 60 package: //Variables. 61 ListReader b; 62 } 63 64 static struct Builder 65 { 66 public: //Methods. 67 this(SegmentBuilder* segment, int ptr, int elementCount, int step, int structDataSize, short structPointerCount) 68 { 69 b = ListBuilder(segment, ptr, elementCount, step, structDataSize, structPointerCount); 70 } 71 72 T get(int index) 73 { 74 return clampOrdinal!T(b._getShortElement(index)); 75 } 76 77 void set(int index, T value) 78 { 79 b._setShortElement(index, cast(short)value); 80 } 81 82 int opApply(scope int delegate(T) dg) 83 { 84 int result = 0; 85 foreach(i; 0..b.length) 86 { 87 result = dg(clampOrdinal!T(b._getShortElement(i))); 88 if(result) 89 break; 90 } 91 return result; 92 } 93 94 package: //Variables. 95 ListBuilder b; 96 } 97 } 98 99 private T clampOrdinal(T)(ushort ordinal) 100 { 101 size_t index = ordinal; 102 if(ordinal < 0 || ordinal >= T.max) 103 index = T.max - 1; 104 return cast(T)index; 105 }