1 /*******************************************************************************
2 
3     Types concerning the values stored in cache.
4 
5     Value is the type stored in the cache.
6 
7     ValueRef is the type of a reference to a value as it is returned by
8     createRaw(), getRaw() and getOrCreateRaw().
9 
10     For values of fixed size (not dynamic, ValueSize != 0) createRaw() and
11     getOrCreateRaw() return a dynamic array which slices to the value in the
12     cache and therefore has always a length of ValueSize. getRaw() returns
13     either such a slice or null.
14 
15     For values of dynamic size createRaw() and getOrCreateRaw() return a pointer
16     to a Value struct instance while getRaw() returns either a pointer or null.
17     The Value struct wraps a dynamic array and provides access via struct
18     methods.
19 
20     Copyright:
21         Copyright (c) 2009-2016 dunnhumby Germany GmbH.
22         All rights reserved.
23 
24     License:
25         Boost Software License Version 1.0. See LICENSE_BOOST.txt for details.
26         Alternatively, this file may be distributed under the terms of the Tango
27         3-Clause BSD License (see LICENSE_BSD.txt for details).
28 
29 *******************************************************************************/
30 
31 module ocean.util.container.cache.model.Value;
32 
33 /******************************************************************************/
34 
35 template Value ( size_t ValueSize )
36 {
37     /***************************************************************************
38 
39         Tells if the values are of dynamic size; if not, they are of fixed size.
40 
41     ***************************************************************************/
42 
43     static immutable is_dynamic = !ValueSize;
44 
45     static if (is_dynamic)
46     {
47         struct Value
48         {
49             /*******************************************************************
50 
51                 Value array. This may be an allocated buffer or a slice to an
52                 external array, depending on whether it is assigned by
53                 opSliceAssign() (allocates or reuses a buffer) or opSlice()
54                 (slices an external array).
55 
56             *******************************************************************/
57 
58             private void[] array;
59 
60             /*******************************************************************
61 
62                 Sets the value array instance to val.
63 
64                 Params:
65                     val = new value array instance
66 
67                 Returns:
68                     val.
69 
70             *******************************************************************/
71 
72             public void[] opAssign ( void[] val )
73             {
74                 return this.array = val;
75             }
76 
77             /*******************************************************************
78 
79                 Allocates a buffer for the value array and copies the content of
80                 val into the value array. If the value array already exists (is
81                 not null), it is resized and reused.
82 
83                 Params:
84                     val = value content to copy to the value array of this
85                           instance
86 
87                 Returns:
88                     the value array of this instance.
89 
90             *******************************************************************/
91 
92             public void[] opSliceAssign ( void[] val )
93             {
94                 if (this.array is null)
95                 {
96                     this.array = new ubyte[val.length];
97                 }
98                 else
99                 {
100                     this.array.length = val.length;
101                 }
102 
103                 return this.array[] = val[];
104             }
105 
106             /*******************************************************************
107 
108                 Returns:
109                     the value array.
110 
111             *******************************************************************/
112 
113             public void[] opSlice ( )
114             {
115                 return this.array;
116             }
117 
118             /*******************************************************************
119 
120                 Obtains a pointer to the value array instance.
121                 Should only be used in special situations where it would be
122                 unreasonably difficult to use the other access methods.
123 
124                 Returns:
125                     a pointer to the value array instance.
126 
127             *******************************************************************/
128 
129             public void[]* opCast ( )
130             {
131                 return &this.array;
132             }
133 
134             /*******************************************************************
135 
136                 Returns:
137                     length of underlying array
138 
139             *******************************************************************/
140 
141             public size_t length ( )
142             {
143                 return this.array.length;
144             }
145         }
146 
147         public alias Value* ValueRef;
148     }
149     else
150     {
151         public alias ubyte[ValueSize] Value;
152         public alias void[]           ValueRef;
153     }
154 }