1 /*******************************************************************************
2 
3     Pool class template which stores struct instances, has the
4     following features:
5         * Get and recycle items. Recycled items will be re-used before creating
6           new items.
7         * The total number of items, as well as the number of idle or busy items
8           in the pool can be queried.
9         * A limit can be applied to the pool, which prevents more than the
10           specified number of items from being created.
11         * A specified number of items can be pre-allocated in the pool using the
12           fill() method.
13         * The entire pool can be emptied, returning all items to the idle state,
14           with clear().
15         * Iteration over all items in the pool, or all busy or idle items. (See
16           further notes in the super class.)
17         * get() and fill() methods exist which automatically create new pool
18           items, without requiring them to be passed via a lazy argument.
19 
20     Also see: ocean.util.container.pool.model.IAggregatePool, for more detailed
21     documentation and usage examples.
22 
23     Copyright:
24         Copyright (c) 2009-2016 dunnhumby Germany GmbH.
25         All rights reserved.
26 
27     License:
28         Boost Software License Version 1.0. See LICENSE_BOOST.txt for details.
29         Alternatively, this file may be distributed under the terms of the Tango
30         3-Clause BSD License (see LICENSE_BSD.txt for details).
31 
32 *******************************************************************************/
33 
34 module ocean.util.container.pool.StructPool;
35 
36 
37 
38 
39 import ocean.util.container.pool.model.IAggregatePool;
40 
41 import ocean.meta.traits.Aggregates /* : hasMember */;
42 
43 
44 /*******************************************************************************
45 
46     Manages a pool of struct instances of type T.
47 
48     Params:
49         T = type stored in pool
50 
51 *******************************************************************************/
52 
53 public class StructPool ( T ) : IAggregatePool!(T)
54 {
55     /***************************************************************************
56 
57         Asserts that T is a struct.
58 
59     ***************************************************************************/
60 
61     static assert(is(T == struct));
62 
63     /**************************************************************************
64 
65         Resets item.
66 
67         If T has a method of type:
68 
69             void reset ( )
70 
71         then this method will be called for the given item.
72 
73         Params:
74             item = item to reset
75 
76      **************************************************************************/
77 
78     protected override void resetItem ( Item item )
79     {
80         static if (hasMember!(T, "reset"))
81         {
82             static assert(
83                    is(typeof(&T.init.reset) == void delegate())
84                 || is(typeof(&T.init.reset) == void function()),
85                 T.stringof ~ ".reset() must be 'void reset()'"
86             );
87 
88             this.fromItem(item).reset();
89         }
90     }
91 }
92 
93 
94 
95 version (unittest)
96 {
97     struct Struct
98     {
99         size_t object_pool_index;
100 
101         size_t i;
102         char[] s;
103 
104         void reset ( )
105         {
106 
107         }
108     }
109 
110     import ocean.core.Test;
111 
112     alias StructPool!(Struct) MyPool;
113     class StructPoolTester : IAggregatePoolTester!(Struct)
114     {
115         public this ( )
116         {
117             super(new MyPool);
118         }
119 
120         protected override Item newItem ( )
121         {
122             return new Struct;
123         }
124 
125         protected override void setItem ( ref Item item, size_t i )
126         {
127             item.i = i;
128             item.s.length = 1;
129             item.s[0] = cast(char)(i + 32);
130         }
131 
132         protected override void checkItem ( ref Item item, size_t i )
133         {
134             .test!("==")(item.i, i, "item integer wrong");
135             .test!("==")(item.s.length, 1, "item string length wrong");
136             .test!("==")(item.s[0], cast(char)(i + 32), "item string content wrong");
137         }
138     }
139 }
140 
141 unittest
142 {
143     scope sp = new StructPoolTester;
144     sp.test();
145 }
146