Atlas  0.7.0
Networking protocol for the Worldforge system.
DecoderBase.cpp
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 static const bool debug_flag = false;
18 
19 namespace Atlas { namespace Message {
20 
21 DecoderBase::DecoderBase() : m_state(), m_maps(), m_lists(), m_names()
22 {
23 }
24 
25 void DecoderBase::streamBegin()
26 {
27  ATLAS_DEBUG(std::cout << "DecoderBase::streamBegin" << std::endl)
28  m_state.push(STATE_STREAM);
29 }
30 
31 void DecoderBase::streamMessage()
32 {
33  ATLAS_DEBUG(std::cout << "DecoderBase::streamMessage" << std::endl)
34  m_maps.emplace();
35  m_state.push(STATE_MAP);
36 }
37 
38 void DecoderBase::streamEnd()
39 {
40  ATLAS_DEBUG(std::cout << "DecoderBase::streamEnd" << std::endl)
41  assert(!m_state.empty());
42  m_state.pop();
43 }
44 
45 void DecoderBase::mapMapItem(std::string name)
46 {
47  ATLAS_DEBUG(std::cout << "DecoderBase::mapMapItem Map" << std::endl)
48  m_names.push(std::move(name));
49  m_maps.emplace();
50  m_state.push(STATE_MAP);
51 }
52 
53 void DecoderBase::mapListItem(std::string name)
54 {
55  ATLAS_DEBUG(std::cout << "DecoderBase::mapListItem List" << std::endl)
56  m_names.push(std::move(name));
57  m_lists.emplace();
58  m_state.push(STATE_LIST);
59 }
60 
61 void DecoderBase::mapIntItem(std::string name, std::int64_t i)
62 {
63  ATLAS_DEBUG(std::cout << "DecoderBase::mapIntItem" << std::endl)
64  assert(!m_maps.empty());
65  m_maps.top().emplace(std::move(name), i);
66 }
67 
68 void DecoderBase::mapFloatItem(std::string name, double d)
69 {
70  ATLAS_DEBUG(std::cout << "DecoderBase::mapFloatItem" << std::endl)
71  assert(!m_maps.empty());
72  m_maps.top().emplace(std::move(name), d);
73 }
74 
75 void DecoderBase::mapStringItem(std::string name, std::string s)
76 {
77  ATLAS_DEBUG(std::cout << "DecoderBase::mapStringItem" << std::endl)
78  assert(!m_maps.empty());
79  m_maps.top().emplace(std::move(name), std::move(s));
80 }
81 
82 void DecoderBase::mapNoneItem(std::string name) {
83  ATLAS_DEBUG(std::cout << "DecoderBase::mapNoneItem" << std::endl)
84  assert(!m_maps.empty());
85  m_maps.top().emplace(std::move(name), Atlas::Message::Element());
86 }
87 
88 void DecoderBase::mapEnd()
89 {
90  ATLAS_DEBUG(std::cout << "DecoderBase::mapEnd" << std::endl)
91  assert(!m_maps.empty());
92  assert(!m_state.empty());
93  m_state.pop();
94  switch (m_state.top()) {
95  case STATE_MAP:
96  {
97  MapType map = std::move(m_maps.top());
98  m_maps.pop();
99  assert(!m_maps.empty());
100  assert(!m_names.empty());
101  m_maps.top().emplace(std::move(m_names.top()), std::move(map));
102  m_names.pop();
103  }
104  break;
105  case STATE_LIST:
106  {
107  assert(!m_lists.empty());
108  m_lists.top().emplace_back(std::move(m_maps.top()));
109  m_maps.pop();
110  }
111  break;
112  case STATE_STREAM:
113  {
114  messageArrived(std::move(m_maps.top()));
115  m_maps.pop();
116  }
117  break;
118  default:
119  {
120  // MapType map = m_maps.top();
121  m_maps.pop();
122  }
123  break;
124  }
125 }
126 
127 void DecoderBase::listMapItem()
128 {
129  ATLAS_DEBUG(std::cout << "DecoderBase::listMapItem" << std::endl)
130  m_maps.emplace();
131  m_state.push(STATE_MAP);
132 }
133 
134 void DecoderBase::listListItem()
135 {
136  ATLAS_DEBUG(std::cout << "DecoderBase::listListItem" << std::endl)
137  m_lists.emplace();
138  m_state.push(STATE_LIST);
139 }
140 
141 void DecoderBase::listIntItem(std::int64_t i)
142 {
143  ATLAS_DEBUG(std::cout << "DecoderBase::listIntItem" << std::endl)
144  assert(!m_lists.empty());
145  m_lists.top().emplace_back(i);
146 }
147 
148 void DecoderBase::listFloatItem(double d)
149 {
150  ATLAS_DEBUG(std::cout << "DecoderBase::listFloatItem" << std::endl)
151  m_lists.top().emplace_back(d);
152 }
153 
154 void DecoderBase::listStringItem(std::string s)
155 {
156  ATLAS_DEBUG(std::cout << "DecoderBase::listStringItem" << std::endl)
157  assert(!m_lists.empty());
158  m_lists.top().emplace_back(std::move(s));
159 }
160 
161 void DecoderBase::listNoneItem() {
162  ATLAS_DEBUG(std::cout << "DecoderBase::listNoneItem" << std::endl)
163  assert(!m_lists.empty());
164  m_lists.top().emplace_back(Atlas::Message::Element());
165 }
166 
167 void DecoderBase::listEnd()
168 {
169  ATLAS_DEBUG(std::cout << "DecoderBase::listEnd" << std::endl)
170  assert(!m_lists.empty());
171  assert(!m_state.empty());
172  m_state.pop();
173  switch (m_state.top()) {
174  case STATE_MAP:
175  assert(!m_maps.empty());
176  assert(!m_names.empty());
177  m_maps.top().emplace(m_names.top(), std::move(m_lists.top()));
178  m_names.pop();
179  m_lists.pop();
180  break;
181  case STATE_LIST:
182  {
183  ListType list = std::move(m_lists.top());
184  m_lists.pop();
185  assert(!m_lists.empty());
186  m_lists.top().emplace_back(std::move(list));
187  }
188  break;
189  case STATE_STREAM:
190  std::cerr << "DecoderBase::listEnd: Error" << std::endl;
191  m_lists.pop();
192  // XXX - report error?
193  break;
194  }
195 }
196 
197 } } // namespace Atlas::Message
std::stack< State > m_state
The state stack.
Definition: DecoderBase.h:78
std::stack< std::string > m_names
Names for maps and lists.
Definition: DecoderBase.h:84
std::stack< ListType > m_lists
The list stack.
Definition: DecoderBase.h:82
std::stack< MapType > m_maps
The map stack.
Definition: DecoderBase.h:80
void messageArrived(Atlas::Message::MapType) override
Overridden by to retrieve the message from DecoderBase.
Definition: Decoder.cpp:20
Definition: Bridge.h:20