1 /*
2  * hunt-proton: AMQP Protocol library for D programming language.
3  *
4  * Copyright (C) 2018-2019 HuntLabs
5  *
6  * Website: https://www.huntlabs.net/
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11 
12 module hunt.proton.codec.impl.DoubleElement;
13 
14 import hunt.io.ByteBuffer;
15 import std.conv;
16 import hunt.proton.codec.impl.AtomicElement;
17 import hunt.proton.codec.impl.Element;
18 import hunt.proton.codec.impl.ArrayElement;
19 import hunt.proton.codec.impl.AbstractElement;
20 import hunt.Double;
21 import hunt.proton.codec.Data;
22 
23 class DoubleElement : AtomicElement!Double
24 {
25 
26     private double _value;
27 
28     this(IElement parent, IElement prev, double d)
29     {
30         super(parent, prev);
31         _value = d;
32     }
33 
34     public int size()
35     {
36         return isElementOfArray() ? 8 : 9;
37     }
38 
39     public Object getValue()
40     {
41         return new Double( _value);
42     }
43 
44     public Data.DataType getDataType()
45     {
46         return Data.DataType.DOUBLE;
47     }
48 
49     public int encode(ByteBuffer b)
50     {
51         int size = size();
52         if(b.remaining()>=size)
53         {
54             if(size == 9)
55             {
56                 b.put(cast(byte)0x82);
57             }
58             b.putLong(cast(long)_value);
59             return size;
60         }
61         else
62         {
63             return 0;
64         }
65     }
66 }