1 /******************************************************************************* 2 3 Implementation of buffer version compatible with void[]. 4 5 Copyright: 6 Copyright (c) 2016 dunnhumby Germany GmbH. 7 All rights reserved. 8 9 License: 10 Boost Software License Version 1.0. See LICENSE_BOOST.txt for details. 11 Alternatively, this file may be distributed under the terms of the Tango 12 3-Clause BSD License (see LICENSE_BSD.txt for details). 13 14 *******************************************************************************/ 15 16 module ocean.core.buffer.Void; 17 18 template VoidBufferImpl ( ) 19 { 20 /*************************************************************************** 21 22 Plain D array internally used as buffer storage 23 24 ***************************************************************************/ 25 26 private ubyte[] data; 27 28 /*************************************************************************** 29 30 Assigns data to stored data from other slice 31 32 Params: 33 rhs = slice to assign from 34 35 ***************************************************************************/ 36 37 void opAssign ( in void[] rhs ) 38 { 39 this.length = rhs.length; 40 this.data[] = (cast(ubyte[]) rhs)[]; 41 } 42 43 /*************************************************************************** 44 45 Assigns data to stored data from other slice 46 47 Params: 48 rhs = slice to assign from 49 begin = starting index for local data 50 end = end index for local data 51 52 ***************************************************************************/ 53 54 void opSliceAssign ( in void[] rhs, ptrdiff_t begin = 0, 55 ptrdiff_t end = -1 ) 56 { 57 if (end < 0) 58 end = this.length(); 59 this.data[begin .. end] = (cast(ubyte[]) rhs)[]; 60 } 61 62 /*************************************************************************** 63 64 Individual element access. 65 66 Params: 67 i = element index 68 69 Returns: 70 Requested element 71 72 ***************************************************************************/ 73 74 ubyte* opIndex ( size_t i ) 75 { 76 return &this.data[i]; 77 } 78 79 /*************************************************************************** 80 81 Invidual element assignment 82 83 Params: 84 value = new element value 85 i = element index 86 87 ***************************************************************************/ 88 89 void opIndexAssign ( ubyte value, size_t i ) 90 { 91 this.data[i] = value; 92 } 93 94 /*************************************************************************** 95 96 Appends to current buffer 97 98 Params: 99 rhs = array or element to append 100 101 ***************************************************************************/ 102 103 void opCatAssign ( in ubyte rhs ) 104 { 105 this.length = this.data.length + 1; 106 this.data[$-1] = rhs; 107 } 108 109 /*************************************************************************** 110 111 ditto 112 113 ***************************************************************************/ 114 115 void opCatAssign ( in ubyte[] rhs ) 116 { 117 this.length = this.data.length + rhs.length; 118 this.data[$-rhs.length .. $] = rhs[]; 119 } 120 121 /*************************************************************************** 122 123 Buffer element iteration 124 125 Params: 126 dg = foreach loop body 127 128 ***************************************************************************/ 129 130 int opApply ( scope int delegate(ref Const!(ubyte)) dg ) const 131 { 132 foreach (elem; cast(ubyte[]) this.data) 133 { 134 auto status = dg(elem); 135 if (status != 0) 136 return status; 137 } 138 139 return 0; 140 } 141 142 /*************************************************************************** 143 144 Buffer element iteration (with index) 145 146 Params: 147 dg = foreach loop body 148 149 ***************************************************************************/ 150 151 int opApply ( scope int delegate(ref size_t, ref Const!(ubyte)) dg ) const 152 { 153 foreach (index, elem; cast(ubyte[]) this.data) 154 { 155 auto status = dg(index, elem); 156 if (status != 0) 157 return status; 158 } 159 160 return 0; 161 } 162 }