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 FunkyEncoder; 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.mapMapItem(name); 00102 return EncMap<B, T>(b); 00103 } 00104 00106 EncList<B, T> operator<<(const BeginList&) 00107 { 00108 b.mapListItem(name); 00109 return EncList<B, T>(b); 00110 } 00111 00113 T operator<<(long i) 00114 { 00115 b.mapIntItem(name, i); 00116 return T(b); 00117 } 00118 00120 T operator<<(double d) 00121 { 00122 b.mapFloatItem(name, d); 00123 return T(b); 00124 } 00125 00127 T operator<<(const std::string& s) 00128 { 00129 b.mapStringItem(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.listMapItem(); 00190 return EncMap<B, EncList<B, T> >(b); 00191 } 00192 00194 EncList<B, EncList<B, T> > operator<<(const BeginList&) 00195 { 00196 b.listListItem(); 00197 return EncList<B, EncList<B, T> >(b); 00198 } 00199 00201 EncList<B, T> operator<<(long i) 00202 { 00203 b.listIntItem(i); 00204 return *this; 00205 } 00206 00208 EncList<B, T> operator<<(double d) 00209 { 00210 b.listFloatItem(d); 00211 return *this; 00212 } 00213 00215 EncList<B, T> operator<<(const std::string& s) 00216 { 00217 b.listStringItem(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 FunkyEncoder 00248 { 00249 public: 00250 FunkyEncoder(B& b) : b(b) { } 00251 00253 EncMap<B, FunkyEncoder> operator<<(const BeginMap&) { 00254 b.streamMessage(); 00255 return EncMap<B, FunkyEncoder>(b); 00256 } 00257 00259 template<typename Arg> 00260 FunkyEncoder<B> operator<<(const Arg& a) 00261 { 00262 b.streamObjectsMessage(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-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.