File: FEncoder.h

package info (click to toggle)
atlas-cpp 0.5.98-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,652 kB
  • ctags: 2,284
  • sloc: sh: 8,305; cpp: 6,372; python: 1,471; makefile: 216
file content (289 lines) | stat: -rw-r--r-- 6,270 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// This file may be redistributed and modified only under the terms of
// the GNU Lesser General Public License (See COPYING for details).
// Copyright (C) 2000 Stefanus Du Toit

// Much inspiration, the original idea and name suggestion by Mike Day.

#ifndef ATLAS_FUNKY_ENCODER_H
#define ATLAS_FUNKY_ENCODER_H

#include <string>

namespace Atlas { namespace Funky {

/** @defgroup funky_encoder Atlas Funky Encoder
 *
 * A compile-time-semantics-checking-<<-style encoder.
 *
 * This encoder is composed of several classes which each have different
 * operator<<. You can use it to send a message through a bridge in a format
 * similar to the following:
 *
 * <PRE>
 * using namespace Funky;
 * Funky::Encoder enc(&myBridge);
 * enc << Token::begin_message
 *     << Token::begin_map
 *        << "an int" << 1234
 *        << "a float" << 3.142
 *        << "a string" << "Hello World!"
 *        << "a list" << Token::begin_list
 *           << 5678
 *           << 2.181
 *           << "another string!"
 *        << Token::end_list
 *     << Token::end_map
 *     << Token::end_message;
 * </PRE>
 * 
 * The special thing is that it will perform semantic checking automatically
 * <I>at compile time</I> via a template stack.
 *
 * @author Stefanus Du Toit <sdt@gmx.net>, with help of Mike Day <mikeday@corplink.com.au>
 * @see Atlas::Bridge
*/

/** Token class representing the beginning of a message.
 * 
 *  @ingroup funky_encoder
 * @see funky_encoder
 */
class BeginMessage {};
/** Token class representing the end of a message.
 *
 * @ingroup funky_encoder
 * @see funky_encoder
 */
class EndMessage {};
/** Token class representing the beginning of a map.
 *
 * @ingroup funky_encoder
 * @see funky_encoder
 */
class BeginMap {};
/** Token class representing the end of a map.
 *
 * @ingroup funky_encoder
 * @see funky_encoder
 */
class EndMap {};
/** Token class representing the beginning of a list.
 *
 * @ingroup funky_encoder
 * @see funky_encoder
 */
class BeginList {};
/** Token class representing the end of a list.
 *
 * @ingroup funky_encoder
 * @see funky_encoder
 */
class EndList {};

template<class B> class FunkyEncoder;
template<class B, class T> class EncMap;
template<class B, class T> class EncList;
template<class B, class T> class EncMapValue;

/** Encoder in map value state
 *
 * @ingroup funky_encoder
 * @see funky_encoder
 */
template<class B, class T>
class EncMapValue {
public:
    EncMapValue(B& b, const std::string& name) : b(b), name(name) { }
    
    /// Begin a map.
    EncMap<B, T> operator<<(const BeginMap&)
    {
        b.mapMapItem(name);
        return EncMap<B, T>(b);
    }

    /// Begin a list.
    EncList<B, T> operator<<(const BeginList&)
    {
        b.mapListItem(name);
        return EncList<B, T>(b);
    }

    /// Send an integer value.
    T operator<<(long i)
    {
        b.mapIntItem(name, i);
        return T(b);
    }

    /// Send a double value.
    T operator<<(double d)
    {
        b.mapFloatItem(name, d);
        return T(b);
    }

    /// Send a string value.
    T operator<<(const std::string& s)
    {
        b.mapStringItem(name, s);
        return T(b);
    }

    /// If the encoder supports it, send any kind of value.
    template<typename Arg>
    T operator<<(const Arg& a)
    {
        b.mapItem(name, a);
        return T(b);
    }

protected:
    /// The bridge or encoder that is written to.
    B& b;
    /// The name of this item
    std::string name;
};

/** Encoder in Map state
 *
 * @ingroup funky_encoder
 * @see funky_encoder
 */
template<class B, class T>
class EncMap {
public:
    EncMap(B& b) : b(b) { }

    /// Start a value with its name
    EncMapValue< B, EncMap<B, T> > operator<<(const std::string& name)
    {
        return EncMapValue< B, EncMap<B, T> >(b, name);
    }

    /// End this map
    T operator<<(EndMap)
    {
        b.mapEnd();
        return T(b);
    }
    
protected:
    /// The bridge or encoder that is written to.
    B& b;
};

/** Encoder in List state
 *
 * @ingroup funky_encoder
 * @see funky_encoder
 */
template<class B, class T>
class EncList {
public:
    EncList(B& b) : b(b) { }

    /// Start a map.
    EncMap<B, EncList<B, T> > operator<<(const BeginMap&)
    {
        b.listMapItem();
        return EncMap<B, EncList<B, T> >(b);
    }

    /// Start a list.
    EncList<B, EncList<B, T> > operator<<(const BeginList&)
    {
        b.listListItem();
        return EncList<B, EncList<B, T> >(b);
    }

    /// Send an integer value.
    EncList<B, T> operator<<(long i)
    {
        b.listIntItem(i);
        return *this;
    }

    /// Send a double value.
    EncList<B, T> operator<<(double d)
    {
        b.listFloatItem(d);
        return *this;
    }

    /// Send a string value.
    EncList<B, T> operator<<(const std::string& s)
    {
        b.listStringItem(s);
        return *this;
    }

    /// If the encoder supports it, send any kind of value.
    template<typename Arg>
    EncList<B, T> operator<<(const Arg& a)
    {
        b.listItem(a);
        return *this;
    }
    
    /// End this list.
    T operator<<(EndList)
    {
        b.listEnd();
        return T(b);
    }
    
protected:
    /// The bridge or encoder that is written to.
    B& b;
};

/** The root encoder in "stream" state.
 *
 * @ingroup funky_encoder
 * @see funky_encoder
 */
template <class B>
class FunkyEncoder
{
public:
    FunkyEncoder(B& b) : b(b) { }
    
    /// Start a message (as a map).
    EncMap<B, FunkyEncoder> operator<<(const BeginMap&) {
        b.streamMessage();
        return EncMap<B, FunkyEncoder>(b);
    }

    /// If the encoder supports it, send a different kind of message.
    template<typename Arg>
    FunkyEncoder<B> operator<<(const Arg& a)
    {
        b.streamObjectsMessage(a);
        return *this;
    }

protected:
    /// The bridge or encoder that is written to.
    B& b;
};

/** Tokens representing beginnings and ends of maps/lists.
 *
 * Use these as parameters to operator<< for the Funky::Encoder classes.
 * 
 * @see funky_encoder
 * @ingroup funky_encoder
 */
class Tokens {
public:
    static BeginMap begin_map;
    static EndMap end_map;
    static BeginList begin_list;
    static EndList end_list;
};


} } // Atlas::Funky namespace

#endif