Atlas  0.7.0
Networking protocol for the Worldforge system.
FEncoder.h
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 // Much inspiration, the original idea and name suggestion by Mike Day.
8 
9 #ifndef ATLAS_FUNKY_ENCODER_H
10 #define ATLAS_FUNKY_ENCODER_H
11 
12 #include <string>
13 
14 namespace Atlas { namespace Funky {
15 
53 class BeginMessage {};
59 class EndMessage {};
65 class BeginMap {};
71 class EndMap {};
77 class BeginList {};
83 class EndList {};
84 
85 template<class B> class FunkyEncoder;
86 template<class B, class T> class EncMap;
87 template<class B, class T> class EncList;
88 template<class B, class T> class EncMapValue;
89 
95 template<class B, class T>
96 class EncMapValue {
97 public:
98  EncMapValue(B& b, const std::string& name) : b(b), name(name) { }
99 
102  {
103  b.mapMapItem(name);
104  return EncMap<B, T>(b);
105  }
106 
108  EncList<B, T> operator<<(const BeginList&)
109  {
110  b.mapListItem(name);
111  return EncList<B, T>(b);
112  }
113 
115  T operator<<(std::int64_t i)
116  {
117  b.mapIntItem(name, i);
118  return T(b);
119  }
120 
122  T operator<<(double d)
123  {
124  b.mapFloatItem(name, d);
125  return T(b);
126  }
127 
129  T operator<<(const std::string& s)
130  {
131  b.mapStringItem(name, s);
132  return T(b);
133  }
134 
136  template<typename Arg>
137  T operator<<(const Arg& a)
138  {
139  b.mapItem(name, a);
140  return T(b);
141  }
142 
143 protected:
145  B& b;
147  std::string name;
148 };
149 
155 template<class B, class T>
156 class EncMap {
157 public:
158  EncMap(B& b) : b(b) { }
159 
161  EncMapValue< B, EncMap<B, T> > operator<<(const std::string& name)
162  {
163  return EncMapValue< B, EncMap<B, T> >(b, name);
164  }
165 
168  {
169  b.mapEnd();
170  return T(b);
171  }
172 
173 protected:
175  B& b;
176 };
177 
183 template<class B, class T>
184 class EncList {
185 public:
186  EncList(B& b) : b(b) { }
187 
190  {
191  b.listMapItem();
192  return EncMap<B, EncList<B, T> >(b);
193  }
194 
196  EncList<B, EncList<B, T> > operator<<(const BeginList&)
197  {
198  b.listListItem();
199  return EncList<B, EncList<B, T> >(b);
200  }
201 
203  EncList<B, T> operator<<(std::int64_t i)
204  {
205  b.listIntItem(i);
206  return *this;
207  }
208 
210  EncList<B, T> operator<<(double d)
211  {
212  b.listFloatItem(d);
213  return *this;
214  }
215 
217  EncList<B, T> operator<<(const std::string& s)
218  {
219  b.listStringItem(s);
220  return *this;
221  }
222 
224  template<typename Arg>
225  EncList<B, T> operator<<(const Arg& a)
226  {
227  b.listItem(a);
228  return *this;
229  }
230 
233  {
234  b.listEnd();
235  return T(b);
236  }
237 
238 protected:
240  B& b;
241 };
242 
248 template <class B>
250 {
251 public:
252  FunkyEncoder(B& b) : b(b) { }
253 
256  b.streamMessage();
257  return EncMap<B, FunkyEncoder>(b);
258  }
259 
261  template<typename Arg>
262  FunkyEncoder<B> operator<<(const Arg& a)
263  {
264  b.streamObjectsMessage(a);
265  return *this;
266  }
267 
268 protected:
270  B& b;
271 };
272 
280 class Tokens {
281 public:
282  static BeginMap begin_map;
283  static EndMap end_map;
284  static BeginList begin_list;
285  static EndList end_list;
286 };
287 
288 
289 } } // Atlas::Funky namespace
290 
291 #endif
B & b
The bridge or encoder that is written to.
Definition: FEncoder.h:240
T operator<<(EndList)
End this list.
Definition: FEncoder.h:232
EncMap< B, EncList< B, T > > operator<<(const BeginMap &)
Start a map.
Definition: FEncoder.h:189
EncMap< B, T > operator<<(const BeginMap &)
Begin a map.
Definition: FEncoder.h:101
std::string name
The name of this item.
Definition: FEncoder.h:147
T operator<<(std::int64_t i)
Send an integer value.
Definition: FEncoder.h:115
B & b
The bridge or encoder that is written to.
Definition: FEncoder.h:145
T operator<<(const Arg &a)
If the encoder supports it, send any kind of value.
Definition: FEncoder.h:137
T operator<<(double d)
Send a double value.
Definition: FEncoder.h:122
T operator<<(const std::string &s)
Send a string value.
Definition: FEncoder.h:129
EncMapValue< B, EncMap< B, T > > operator<<(const std::string &name)
Start a value with its name.
Definition: FEncoder.h:161
T operator<<(EndMap)
End this map.
Definition: FEncoder.h:167
B & b
The bridge or encoder that is written to.
Definition: FEncoder.h:175
B & b
The bridge or encoder that is written to.
Definition: FEncoder.h:270
EncMap< B, FunkyEncoder > operator<<(const BeginMap &)
Start a message (as a map).
Definition: FEncoder.h:255
Definition: Bridge.h:20