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