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.AbstractElement;
13 import hunt.proton.codec.impl.ArrayElement;
14 import hunt.proton.codec.impl.Element;
15 abstract class AbstractElement(T) : Element!T
16 {
17     private IElement _parent;
18     private IElement _next;
19     private IElement _prev;
20 
21     this(IElement parent, IElement prev)
22     {
23         _parent = parent;
24         _prev = prev;
25     }
26 
27     protected bool isElementOfArray()
28     {
29         ArrayElement toArry = cast(ArrayElement)_parent;
30         if (toArry !is null)
31         {  // _parent instanceof ArrayElement && !(((ArrayElement)parent()).isDescribed() && this == _parent.child());
32             if(!((cast(ArrayElement)parent()).isDescribed() && this is _parent.child()))
33             {
34                 return true;
35             }
36         }
37         return false;
38     }
39 
40     override
41     public IElement next()
42     {
43         // TODO
44         return _next;
45     }
46 
47     override
48     public IElement prev()
49     {
50         // TODO
51         return _prev;
52     }
53 
54     override
55     public IElement parent()
56     {
57         // TODO
58         return _parent;
59     }
60 
61     override
62     public void setNext(IElement elt)
63     {
64         _next = cast(Element!T)elt;
65     }
66 
67     override
68     public void setPrev(IElement elt)
69     {
70 
71         _prev = cast(Element!T)elt;
72     }
73 
74     override
75     public void setParent(IElement elt)
76     {
77         _parent = cast(Element!T)elt;
78     }
79 
80     override
81     public IElement replaceWith(IElement elt)
82     {
83 
84         if (_parent !is null) {
85             elt = _parent.checkChild(elt);
86         }
87 
88         elt.setPrev(_prev);
89         elt.setNext(_next);
90         elt.setParent(_parent);
91 
92         if (_prev !is null) {
93             _prev.setNext(elt);
94         }
95         if (_next !is null) {
96             _next.setPrev(elt);
97         }
98 
99         if (_parent !is null && _parent.child() is this) {
100             _parent.setChild(elt);
101         }
102 
103         return elt;
104     }
105 
106     //override
107     //public String toString()
108     //{
109     //    return String.format("%s[%h]{parent=%h, prev=%h, next=%h}",
110     //                         this.getClass().getSimpleName(),
111     //                         System.identityHashCode(this),
112     //                         System.identityHashCode(_parent),
113     //                         System.identityHashCode(_prev),
114     //                         System.identityHashCode(_next));
115     //}
116 
117     abstract string startSymbol();
118 
119     abstract string stopSymbol();
120 
121     override
122     public void render(string sb)
123     {
124         //if (canEnter()) {
125         //    sb ~= startSymbol();
126         //    Element el = child();
127         //    bool first = true;
128         //    while (el != null) {
129         //        if (first) {
130         //            first = false;
131         //        } else {
132         //            sb ~= ", ";
133         //        }
134         //        el.render(sb);
135         //        el = el.next();
136         //    }
137         //    sb ~= stopSymbol();
138         //} else {
139         //  //  sb.append(getDataType()).append(" ").append(getValue());
140         //}
141     }
142 
143 }