1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
2 // Licensed under the MIT License:
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21 
22 module capnproto.SegmentReader;
23 
24 import java.nio.ByteBuffer;
25 
26 import capnproto.Arena;
27 import capnproto.Constants;
28 
29 struct SegmentReader
30 {
31 public: //Variables.
32 	static immutable SegmentReader EMPTY = cast(immutable SegmentReader)SegmentReader(ByteBuffer(new ubyte[](8)), null);
33 	
34 	ByteBuffer buffer;
35 
36 public: //Methods.
37 	this(ByteBuffer buffer, Arena arena)
38 	{
39 		this.buffer = buffer;
40 		this.arena = arena;
41 	}
42 	
43 	long get(size_t index) const
44 	{
45 		return buffer.get!long(index * Constants.BYTES_PER_WORD);
46 	}
47 
48 package: //Variables.
49 	Arena arena;
50 }
51 
52 union FloatIntBits
53 {
54 	int intBits;
55 	float floatBits;
56 }
57 
58 float intBitsToFloat(int x)
59 {
60 	FloatIntBits bits;
61 	bits.intBits = x;
62 	return bits.floatBits;
63 }
64 
65 int floatToIntBits(float x)
66 {
67 	FloatIntBits bits;
68 	bits.floatBits = x;
69 	return bits.intBits;
70 }
71 
72 union DoubleLongBits
73 {
74 	long intBits;
75 	double floatBits;
76 }
77 
78 double longBitsToDouble(long x)
79 {
80 	DoubleLongBits bits;
81 	bits.intBits = x;
82 	return bits.floatBits;
83 }
84 
85 long doubleToLongBits(double x)
86 {
87 	DoubleLongBits bits;
88 	bits.floatBits = x;
89 	return bits.intBits;
90 }