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.FloatElement;
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 
21 import hunt.proton.codec.Data;
22 import hunt.Float;
23 
24 class FloatElement : AtomicElement!Float
25 {
26 
27     private float _value;
28 
29     this(IElement parent, IElement prev, float f)
30     {
31         super(parent, prev);
32         _value = f;
33     }
34 
35     public int size()
36     {
37         return isElementOfArray() ? 4 : 5;
38     }
39 
40     public Object getValue()
41     {
42         return  new Float( _value);
43     }
44 
45     public Data.DataType getDataType()
46     {
47         return Data.DataType.FLOAT;
48     }
49 
50     public int encode(ByteBuffer b)
51     {
52         int size = size();
53         if(b.remaining()>=size)
54         {
55             if(size == 5)
56             {
57                 b.put(cast(byte)0x72);
58             }
59             b.putInt(cast(int)_value);
60             return size;
61         }
62         else
63         {
64             return 0;
65         }
66     }
67 }