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.BooleanElement;
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.Boolean;
23 
24 class BooleanElement : AtomicElement!Boolean
25 {
26     private bool _value;
27 
28     this(IElement parent, IElement current, bool b)
29     {
30         super(parent, current);
31         _value = b;
32     }
33 
34     public int size()
35     {
36         // in non-array parent then there is a single byte encoding, in an array there is a 1-byte encoding but no
37         // constructor
38         return 1;
39     }
40 
41     public Object getValue()
42     {
43         return new Boolean( _value);
44     }
45 
46     public Data.DataType getDataType()
47     {
48         return Data.DataType.BOOL;
49     }
50 
51     public int encode(ByteBuffer b)
52     {
53         if(b.hasRemaining())
54         {
55             if(isElementOfArray())
56             {
57                 b.put(_value ? cast(byte) 1 : cast(byte) 0);
58             }
59             else
60             {
61                 b.put(_value ? cast(byte) 0x41 : cast(byte) 0x42);
62             }
63             return 1;
64         }
65         return 0;
66     }
67 
68 }