1 
2 module handle.handle;
3 
4 import std.traits;
5 
6 struct Handle(T)
7 {
8 private:
9     ushort _index;
10     ushort _counter;
11 
12 public:
13     /++
14      + Constructs a new handle from an index and update counter state.
15      ++/
16     this(ushort index, ushort counter)
17     {
18         _index   = index;
19         _counter = counter;
20     }
21 
22     /++
23      + Reconstructs a packed handle.
24      ++/
25     this(uint handle)
26     {
27         _index   = cast(ushort)((handle >>  0) & 0xFFFF);
28         _counter = cast(ushort)((handle >> 16) & 0xFFFF);
29     }
30 
31     /++
32      + The update counter of the handle, that guarantees the validity of a handle.
33      +
34      + Returns:
35      +   The update counter.
36      ++/
37     @property
38     ushort counter() const
39     {
40         return _counter;
41     }
42 
43     /++
44      + The index of the stored element into the handle manager.
45      +
46      + Returns:
47      +   The index of the handle.
48      ++/
49     @property
50     ushort index() const
51     {
52         return _index;
53     }
54 
55     /++
56      + Packs a handle into a uint.
57      ++/
58     I opCast(I : uint)() const
59     {
60         return cast(I) _counter << 16 | _index;
61     }
62 
63     /++
64      + Computes the hash of a handle (its packed value).
65      ++/
66     hash_t toHash() const
67     {
68         return cast(uint) this;
69     }
70 }