Atlas-C++
FEncoder.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 // $Id$
00006 
00007 // Much inspiration, the original idea and name suggestion by Mike Day.
00008 
00009 #ifndef ATLAS_FUNKY_ENCODER_H
00010 #define ATLAS_FUNKY_ENCODER_H
00011 
00012 #include <string>
00013 
00014 namespace Atlas { namespace Funky {
00015 
00053 class BeginMessage {};
00059 class EndMessage {};
00065 class BeginMap {};
00071 class EndMap {};
00077 class BeginList {};
00083 class EndList {};
00084 
00085 template<class B> class FunkyEncoder;
00086 template<class B, class T> class EncMap;
00087 template<class B, class T> class EncList;
00088 template<class B, class T> class EncMapValue;
00089 
00095 template<class B, class T>
00096 class EncMapValue {
00097 public:
00098     EncMapValue(B& b, const std::string& name) : b(b), name(name) { }
00099     
00101     EncMap<B, T> operator<<(const BeginMap&)
00102     {
00103         b.mapMapItem(name);
00104         return EncMap<B, T>(b);
00105     }
00106 
00108     EncList<B, T> operator<<(const BeginList&)
00109     {
00110         b.mapListItem(name);
00111         return EncList<B, T>(b);
00112     }
00113 
00115     T operator<<(long i)
00116     {
00117         b.mapIntItem(name, i);
00118         return T(b);
00119     }
00120 
00122     T operator<<(double d)
00123     {
00124         b.mapFloatItem(name, d);
00125         return T(b);
00126     }
00127 
00129     T operator<<(const std::string& s)
00130     {
00131         b.mapStringItem(name, s);
00132         return T(b);
00133     }
00134 
00136     template<typename Arg>
00137     T operator<<(const Arg& a)
00138     {
00139         b.mapItem(name, a);
00140         return T(b);
00141     }
00142 
00143 protected:
00145     B& b;
00147     std::string name;
00148 };
00149 
00155 template<class B, class T>
00156 class EncMap {
00157 public:
00158     EncMap(B& b) : b(b) { }
00159 
00161     EncMapValue< B, EncMap<B, T> > operator<<(const std::string& name)
00162     {
00163         return EncMapValue< B, EncMap<B, T> >(b, name);
00164     }
00165 
00167     T operator<<(EndMap)
00168     {
00169         b.mapEnd();
00170         return T(b);
00171     }
00172     
00173 protected:
00175     B& b;
00176 };
00177 
00183 template<class B, class T>
00184 class EncList {
00185 public:
00186     EncList(B& b) : b(b) { }
00187 
00189     EncMap<B, EncList<B, T> > operator<<(const BeginMap&)
00190     {
00191         b.listMapItem();
00192         return EncMap<B, EncList<B, T> >(b);
00193     }
00194 
00196     EncList<B, EncList<B, T> > operator<<(const BeginList&)
00197     {
00198         b.listListItem();
00199         return EncList<B, EncList<B, T> >(b);
00200     }
00201 
00203     EncList<B, T> operator<<(long i)
00204     {
00205         b.listIntItem(i);
00206         return *this;
00207     }
00208 
00210     EncList<B, T> operator<<(double d)
00211     {
00212         b.listFloatItem(d);
00213         return *this;
00214     }
00215 
00217     EncList<B, T> operator<<(const std::string& s)
00218     {
00219         b.listStringItem(s);
00220         return *this;
00221     }
00222 
00224     template<typename Arg>
00225     EncList<B, T> operator<<(const Arg& a)
00226     {
00227         b.listItem(a);
00228         return *this;
00229     }
00230     
00232     T operator<<(EndList)
00233     {
00234         b.listEnd();
00235         return T(b);
00236     }
00237     
00238 protected:
00240     B& b;
00241 };
00242 
00248 template <class B>
00249 class FunkyEncoder
00250 {
00251 public:
00252     FunkyEncoder(B& b) : b(b) { }
00253     
00255     EncMap<B, FunkyEncoder> operator<<(const BeginMap&) {
00256         b.streamMessage();
00257         return EncMap<B, FunkyEncoder>(b);
00258     }
00259 
00261     template<typename Arg>
00262     FunkyEncoder<B> operator<<(const Arg& a)
00263     {
00264         b.streamObjectsMessage(a);
00265         return *this;
00266     }
00267 
00268 protected:
00270     B& b;
00271 };
00272 
00280 class Tokens {
00281 public:
00282     static BeginMap begin_map;
00283     static EndMap end_map;
00284     static BeginList begin_list;
00285     static EndList end_list;
00286 };
00287 
00288 
00289 } } // Atlas::Funky namespace
00290 
00291 #endif

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.