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             op = operation to perform
100             rhs = array or element to append
101 
102     ***************************************************************************/
103 
104     void opOpAssign (string op : "~") ( in ubyte rhs )
105     {
106         this.length = this.data.length + 1;
107         this.data[$-1] = rhs;
108     }
109 
110     /***************************************************************************
111 
112         ditto
113 
114     ***************************************************************************/
115 
116     void opOpAssign (string op: "~") ( in ubyte[] rhs )
117     {
118         this.length = this.data.length + rhs.length;
119         this.data[$-rhs.length .. $] = rhs[];
120     }
121 
122     /***************************************************************************
123 
124         Buffer element iteration
125 
126         Params:
127             dg = foreach loop body
128 
129     ***************************************************************************/
130 
131     int opApply ( scope int delegate(ref const(ubyte)) dg ) const
132     {
133         foreach (elem; cast(ubyte[]) this.data)
134         {
135             auto status = dg(elem);
136             if (status != 0)
137                 return status;
138         }
139 
140         return 0;
141     }
142 
143     /***************************************************************************
144 
145         Buffer element iteration (with index)
146 
147         Params:
148             dg = foreach loop body
149 
150     ***************************************************************************/
151 
152     int opApply ( scope int delegate(ref size_t, ref const(ubyte)) dg ) const
153     {
154         foreach (index, elem; cast(ubyte[]) this.data)
155         {
156             auto status = dg(index, elem);
157             if (status != 0)
158                 return status;
159         }
160 
161         return 0;
162     }
163 }