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