1 // This file may be redistributed and modified only under the terms of
2 // the GNU Lesser General Public License (See COPYING for details).
3 // Copyright (C) 2000 Stefanus Du Toit
4 
5 // $Id$
6 
7 #include <Atlas/Message/DecoderBase.h>
8 
9 #include <Atlas/Message/Element.h>
10 
11 #include <Atlas/Debug.h>
12 
13 #include <iostream>
14 
15 #include <cassert>
16 
17 
18 
19 namespace Atlas { namespace Message {
20 
DecoderBase()21 DecoderBase::DecoderBase() : m_state(), m_maps(), m_lists(), m_names()
22 {
23 }
24 
~DecoderBase()25 DecoderBase::~DecoderBase()
26 {
27 }
28 
streamBegin()29 void DecoderBase::streamBegin()
30 {
31     ATLAS_DEBUG(std::cout << "DecoderBase::streamBegin" << std::endl)
32     m_state.push(STATE_STREAM);
33 }
34 
streamMessage()35 void DecoderBase::streamMessage()
36 {
37     ATLAS_DEBUG(std::cout << "DecoderBase::streamMessage" << std::endl)
38     MapType m;
39     m_maps.push(m);
40     m_state.push(STATE_MAP);
41 }
42 
streamEnd()43 void DecoderBase::streamEnd()
44 {
45     ATLAS_DEBUG(std::cout << "DecoderBase::streamEnd" << std::endl)
46     assert(!m_state.empty());
47     m_state.pop();
48 }
49 
mapMapItem(const std::string & name)50 void DecoderBase::mapMapItem(const std::string& name)
51 {
52     ATLAS_DEBUG(std::cout << "DecoderBase::mapMapItem Map" << std::endl)
53     MapType m;
54     m_names.push(name);
55     m_maps.push(m);
56     m_state.push(STATE_MAP);
57 }
58 
mapListItem(const std::string & name)59 void DecoderBase::mapListItem(const std::string& name)
60 {
61     ATLAS_DEBUG(std::cout << "DecoderBase::mapListItem List" << std::endl)
62     ListType l;
63     m_names.push(name);
64     m_lists.push(l);
65     m_state.push(STATE_LIST);
66 }
67 
mapIntItem(const std::string & name,long i)68 void DecoderBase::mapIntItem(const std::string& name, long i)
69 {
70     ATLAS_DEBUG(std::cout << "DecoderBase::mapIntItem" << std::endl)
71     assert(!m_maps.empty());
72     m_maps.top()[name] = i;
73 }
74 
mapFloatItem(const std::string & name,double d)75 void DecoderBase::mapFloatItem(const std::string& name, double d)
76 {
77     ATLAS_DEBUG(std::cout << "DecoderBase::mapFloatItem" << std::endl)
78     assert(!m_maps.empty());
79     m_maps.top()[name] = d;
80 }
81 
mapStringItem(const std::string & name,const std::string & s)82 void DecoderBase::mapStringItem(const std::string& name, const std::string& s)
83 {
84     ATLAS_DEBUG(std::cout << "DecoderBase::mapStringItem" << std::endl)
85     assert(!m_maps.empty());
86     m_maps.top()[name] = s;
87 }
88 
mapEnd()89 void DecoderBase::mapEnd()
90 {
91     ATLAS_DEBUG(std::cout << "DecoderBase::mapEnd" << std::endl)
92     assert(!m_maps.empty());
93     assert(!m_state.empty());
94     m_state.pop();
95     switch (m_state.top()) {
96         case STATE_MAP:
97             {
98                 MapType map = m_maps.top();
99                 m_maps.pop();
100                 assert(!m_maps.empty());
101                 assert(!m_names.empty());
102                 m_maps.top()[m_names.top()] = map;
103                 m_names.pop();
104             }
105             break;
106         case STATE_LIST:
107             {
108                 MapType map = m_maps.top();
109                 m_maps.pop();
110                 assert(!m_lists.empty());
111                 m_lists.top().insert(m_lists.top().end(), map);
112             }
113             break;
114         case STATE_STREAM:
115             {
116                 MapType & map = m_maps.top();
117                 messageArrived(map);
118                 m_maps.pop();
119             }
120             break;
121         default:
122             {
123                 // MapType map = m_maps.top();
124                 m_maps.pop();
125             }
126             break;
127     }
128 }
129 
listMapItem()130 void DecoderBase::listMapItem()
131 {
132     ATLAS_DEBUG(std::cout << "DecoderBase::listMapItem" << std::endl)
133     MapType map;
134     m_maps.push(map);
135     m_state.push(STATE_MAP);
136 }
137 
listListItem()138 void DecoderBase::listListItem()
139 {
140     ATLAS_DEBUG(std::cout << "DecoderBase::listListItem" << std::endl)
141     ListType list;
142     m_lists.push(list);
143     m_state.push(STATE_LIST);
144 }
145 
listIntItem(long i)146 void DecoderBase::listIntItem(long i)
147 {
148     ATLAS_DEBUG(std::cout << "DecoderBase::listIntItem" << std::endl)
149     assert(!m_lists.empty());
150     m_lists.top().push_back(i);
151 }
152 
listFloatItem(double d)153 void DecoderBase::listFloatItem(double d)
154 {
155     ATLAS_DEBUG(std::cout << "DecoderBase::listFloatItem" << std::endl)
156     m_lists.top().push_back(d);
157 }
158 
listStringItem(const std::string & s)159 void DecoderBase::listStringItem(const std::string& s)
160 {
161     ATLAS_DEBUG(std::cout << "DecoderBase::listStringItem" << std::endl)
162     assert(!m_lists.empty());
163     m_lists.top().push_back(s);
164 }
165 
listEnd()166 void DecoderBase::listEnd()
167 {
168     ATLAS_DEBUG(std::cout << "DecoderBase::listEnd" << std::endl)
169     assert(!m_lists.empty());
170     assert(!m_state.empty());
171     ListType list = m_lists.top();
172     m_lists.pop();
173     m_state.pop();
174     switch (m_state.top()) {
175         case STATE_MAP:
176             assert(!m_maps.empty());
177             assert(!m_names.empty());
178             m_maps.top()[m_names.top()] = list;
179             m_names.pop();
180             break;
181         case STATE_LIST:
182             assert(!m_lists.empty());
183             m_lists.top().push_back(list);
184             break;
185         case STATE_STREAM:
186             std::cerr << "DecoderBase::listEnd: Error" << std::endl;
187             // XXX - report error?
188             break;
189     }
190 }
191 
192 } } // namespace Atlas::Message
193