Atlas-C++
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 
109  {
110  b.mapListItem(name);
111  return EncList<B, T>(b);
112  }
113 
115  T operator<<(long 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 
197  {
198  b.listListItem();
199  return EncList<B, EncList<B, T> >(b);
200  }
201 
204  {
205  b.listIntItem(i);
206  return *this;
207  }
208 
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>
249 class FunkyEncoder
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>
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
Token class representing the end of a list.
Definition: FEncoder.h:83
Encoder in map value state.
Definition: FEncoder.h:88
std::string name
The name of this item.
Definition: FEncoder.h:147
Token class representing the beginning of a map.
Definition: FEncoder.h:65
EncMap< B, T > operator<<(const BeginMap &)
Begin a map.
Definition: FEncoder.h:101
Token class representing the beginning of a list.
Definition: FEncoder.h:77
Token class representing the beginning of a message.
Definition: FEncoder.h:53
T operator<<(const Arg &a)
If the encoder supports it, send any kind of value.
Definition: FEncoder.h:137
Token class representing the end of a map.
Definition: FEncoder.h:71
B & b
The bridge or encoder that is written to.
Definition: FEncoder.h:145
The Atlas namespace.
Definition: Bridge.h:20
T operator<<(long i)
Send an integer value.
Definition: FEncoder.h:115
EncMap< B, EncList< B, T > > operator<<(const BeginMap &)
Start a map.
Definition: FEncoder.h:189
Token class representing the end of a message.
Definition: FEncoder.h:59
B & b
The bridge or encoder that is written to.
Definition: FEncoder.h:240
Tokens representing beginnings and ends of maps/lists.
Definition: FEncoder.h:280
EncMapValue< B, EncMap< B, T > > operator<<(const std::string &name)
Start a value with its name.
Definition: FEncoder.h:161
T operator<<(EndList)
End this list.
Definition: FEncoder.h:232
T operator<<(const std::string &s)
Send a string value.
Definition: FEncoder.h:129
T operator<<(EndMap)
End this map.
Definition: FEncoder.h:167
T operator<<(double d)
Send a double value.
Definition: FEncoder.h:122
EncMap< B, FunkyEncoder > operator<<(const BeginMap &)
Start a message (as a map).
Definition: FEncoder.h:255
Encoder in List state.
Definition: FEncoder.h:87
B & b
The bridge or encoder that is written to.
Definition: FEncoder.h:270
The root encoder in "stream" state.
Definition: FEncoder.h:85
Encoder in Map state.
Definition: FEncoder.h:86
B & b
The bridge or encoder that is written to.
Definition: FEncoder.h:175

Copyright 2000-2004 the respective authors.

This document can be licensed under the terms of the GNU Free Documentation License or the GNU General Public License and may be freely distributed under the terms given by one of these licenses.