Main Page | Modules | Class Hierarchy | Class List | File List | Class Members | Related Pages

Encoder.h

00001 // This file may be redistributed and modified only under the terms of
00002 // the GNU Lesser General Public License (See COPYING for details).
00003 // Copyright (C) 2000 Stefanus Du Toit
00004 
00005 // Much inspiration, the original idea and name suggestion by Mike Day.
00006 
00007 #ifndef ATLAS_FUNKY_ENCODER_H
00008 #define ATLAS_FUNKY_ENCODER_H
00009 
00010 #include <string>
00011 
00012 namespace Atlas { namespace Funky {
00013 
00051 class BeginMessage {};
00057 class EndMessage {};
00063 class BeginMap {};
00069 class EndMap {};
00075 class BeginList {};
00081 class EndList {};
00082 
00083 template<class B> class Encoder;
00084 template<class B, class T> class EncMap;
00085 template<class B, class T> class EncList;
00086 template<class B, class T> class EncMapValue;
00087 
00093 template<class B, class T>
00094 class EncMapValue {
00095 public:
00096     EncMapValue(B& b, const std::string& name) : b(b), name(name) { }
00097     
00099     EncMap<B, T> operator<<(const BeginMap&)
00100     {
00101         b.MapItem(name, B::MapBegin);
00102         return EncMap<B, T>(b);
00103     }
00104 
00106     EncList<B, T> operator<<(const BeginList&)
00107     {
00108         b.MapItem(name, B::ListBegin);
00109         return EncList<B, T>(b);
00110     }
00111 
00113     T operator<<(int i)
00114     {
00115         b.MapItem(name, i);
00116         return T(b);
00117     }
00118 
00120     T operator<<(double d)
00121     {
00122         b.MapItem(name, d);
00123         return T(b);
00124     }
00125 
00127     T operator<<(const std::string& s)
00128     {
00129         b.MapItem(name, s);
00130         return T(b);
00131     }
00132 
00134     template<typename Arg>
00135     T operator<<(const Arg& a)
00136     {
00137         b.MapItem(name, a);
00138         return T(b);
00139     }
00140 
00141 protected:
00143     B& b;
00145     std::string name;
00146 };
00147 
00153 template<class B, class T>
00154 class EncMap {
00155 public:
00156     EncMap(B& b) : b(b) { }
00157 
00159     EncMapValue< B, EncMap<B, T> > operator<<(const std::string& name)
00160     {
00161         return EncMapValue< B, EncMap<B, T> >(b, name);
00162     }
00163 
00165     T operator<<(EndMap)
00166     {
00167         b.MapEnd();
00168         return T(b);
00169     }
00170     
00171 protected:
00173     B& b;
00174 };
00175 
00181 template<class B, class T>
00182 class EncList {
00183 public:
00184     EncList(B& b) : b(b) { }
00185 
00187     EncMap<B, EncList<B, T> > operator<<(const BeginMap&)
00188     {
00189         b.ListItem(B::MapBegin);
00190         return EncMap<B, EncList<B, T> >(b);
00191     }
00192 
00194     EncList<B, EncList<B, T> > operator<<(const BeginList&)
00195     {
00196         b.ListItem(B::ListBegin);
00197         return EncList<B, EncList<B, T> >(b);
00198     }
00199 
00201     EncList<B, T> operator<<(int i)
00202     {
00203         b.ListItem(i);
00204         return *this;
00205     }
00206 
00208     EncList<B, T> operator<<(double d)
00209     {
00210         b.ListItem(d);
00211         return *this;
00212     }
00213 
00215     EncList<B, T> operator<<(const std::string& s)
00216     {
00217         b.ListItem(s);
00218         return *this;
00219     }
00220 
00222     template<typename Arg>
00223     EncList<B, T> operator<<(const Arg& a)
00224     {
00225         b.ListItem(a);
00226         return *this;
00227     }
00228     
00230     T operator<<(EndList)
00231     {
00232         b.ListEnd();
00233         return T(b);
00234     }
00235     
00236 protected:
00238     B& b;
00239 };
00240 
00246 template <class B>
00247 class Encoder
00248 {
00249 public:
00250     Encoder(B& b) : b(b) { }
00251     
00253     EncMap<B, Encoder> operator<<(const BeginMap&) {
00254         b.StreamMessage(B::MapBegin);
00255         return EncMap<B, Encoder>(b);
00256     }
00257 
00259     template<typename Arg>
00260     Encoder<B> operator<<(const Arg& a)
00261     {
00262         b.StreamMessage(a);
00263         return *this;
00264     }
00265 
00266 protected:
00268     B& b;
00269 };
00270 
00278 class Tokens {
00279 public:
00280     static BeginMap begin_map;
00281     static EndMap end_map;
00282     static BeginList begin_list;
00283     static EndList end_list;
00284 };
00285 
00286 
00287 } } // Atlas::Funky namespace
00288 
00289 #endif

Copyright 2000 the respective authors.

This document is licensed under the terms of the GNU Free Documentation License and may be freely distributed under the conditions given by this license.