llsdserialize.h

Go to the documentation of this file.
00001 
00034 #ifndef LL_LLSDSERIALIZE_H
00035 #define LL_LLSDSERIALIZE_H
00036 
00037 #include <iosfwd>
00038 #include "llsd.h"
00039 #include "llmemory.h"
00040 
00045 class LLSDParser : public LLRefCount
00046 {
00047 protected:
00051         virtual ~LLSDParser();
00052 
00053 public:
00057         LLSDParser();
00058 
00072         virtual S32 parse(std::istream& istr, LLSD& data) const = 0;
00073 
00074 protected:
00075 
00076 };
00077 
00082 class LLSDNotationParser : public LLSDParser
00083 {
00084 protected:
00088         virtual ~LLSDNotationParser();
00089 
00090 public:
00094         LLSDNotationParser() {}
00095 
00110         virtual S32 parse(std::istream& istr, LLSD& data) const;
00111 
00122         static LLSD parse(std::istream& istr);
00123 
00124 private:
00132         S32 parseMap(std::istream& istr, LLSD& map) const;
00133 
00141         S32 parseArray(std::istream& istr, LLSD& array) const;
00142 
00149         void parseString(std::istream& istr, LLSD& data) const;
00150 
00157         void parseBinary(std::istream& istr, LLSD& data) const;
00158 };
00159 
00164 class LLSDXMLParser : public LLSDParser
00165 {
00166 protected:
00170         virtual ~LLSDXMLParser();
00171 
00172 public:
00176         LLSDXMLParser();
00177 
00191         virtual S32 parse(std::istream& istr, LLSD& data) const;
00192 
00193 private:
00194         class Impl;
00195         Impl& impl;
00196 
00197         void parsePart(const char *buf, int len);
00198         friend class LLSDSerialize;
00199 };
00200 
00205 class LLSDBinaryParser : public LLSDParser
00206 {
00207 protected:
00211         virtual ~LLSDBinaryParser();
00212 
00213 public:
00217         LLSDBinaryParser();
00218 
00232         virtual S32 parse(std::istream& istr, LLSD& data) const;
00233 
00244         static LLSD parse(std::istream& istr);
00245 
00246 private:
00254         S32 parseMap(std::istream& istr, LLSD& map) const;
00255 
00263         S32 parseArray(std::istream& istr, LLSD& array) const;
00264 
00271         void parseString(std::istream& istr, std::string& value) const;
00272 };
00273 
00274 
00279 class LLSDFormatter : public LLRefCount
00280 {
00281 protected:
00285         virtual ~LLSDFormatter();
00286 
00287 public:
00291         enum e_formatter_options_type
00292         {
00293                 OPTIONS_NONE = 0,
00294                 OPTIONS_PRETTY = 1
00295         } EFormatterOptions;
00296 
00300         LLSDFormatter();
00301 
00307         void boolalpha(bool alpha);
00308 
00321         void realFormat(const std::string& format);
00322 
00330         virtual S32 format(const LLSD& data, std::ostream& ostr, U32 options = LLSDFormatter::OPTIONS_NONE) const = 0;
00331 
00332 protected:
00339         void formatReal(LLSD::Real real, std::ostream& ostr) const;
00340 
00341 protected:
00342         bool mBoolAlpha;
00343         std::string mRealFormat;
00344 };
00345 
00346 
00351 class LLSDNotationFormatter : public LLSDFormatter
00352 {
00353 protected:
00357         virtual ~LLSDNotationFormatter();
00358 
00359 public:
00363         LLSDNotationFormatter();
00364 
00375         static std::string escapeString(const std::string& in);
00376 
00384         virtual S32 format(const LLSD& data, std::ostream& ostr, U32 options = LLSDFormatter::OPTIONS_NONE) const;
00385 };
00386 
00387 
00392 class LLSDXMLFormatter : public LLSDFormatter
00393 {
00394 protected:
00398         virtual ~LLSDXMLFormatter();
00399 
00400 public:
00404         LLSDXMLFormatter();
00405 
00412         static std::string escapeString(const std::string& in);
00413 
00421         virtual S32 format(const LLSD& data, std::ostream& ostr, U32 options = LLSDFormatter::OPTIONS_NONE) const;
00422 
00423 protected:
00424 
00432         S32 format_impl(const LLSD& data, std::ostream& ostr, U32 options, U32 level) const;
00433 };
00434 
00435 
00460 class LLSDBinaryFormatter : public LLSDFormatter
00461 {
00462 protected:
00466         virtual ~LLSDBinaryFormatter();
00467 
00468 public:
00472         LLSDBinaryFormatter();
00473 
00481         virtual S32 format(const LLSD& data, std::ostream& ostr, U32 options = LLSDFormatter::OPTIONS_NONE) const;
00482 
00483 protected:
00492         void formatString(const std::string& string, std::ostream& ostr) const;
00493 };
00494 
00495 
00511 template <class Formatter>
00512 class LLSDOStreamer : public Formatter
00513 {
00514 public:
00518         LLSDOStreamer(const LLSD& data, U32 options = LLSDFormatter::OPTIONS_NONE) :
00519                 mSD(data), mOptions(options) {}
00520 
00529         friend std::ostream& operator<<(
00530                 std::ostream& str,
00531                 const LLSDOStreamer<Formatter>& formatter)
00532         {
00533                 formatter.format(formatter.mSD, str, formatter.mOptions);
00534                 return str;
00535         }
00536 
00537 protected:
00538         LLSD mSD;
00539         U32 mOptions;
00540 };
00541 
00542 typedef LLSDOStreamer<LLSDNotationFormatter>    LLSDNotationStreamer;
00543 typedef LLSDOStreamer<LLSDXMLFormatter>                 LLSDXMLStreamer;
00544 
00549 class LLSDSerialize
00550 {
00551 public:
00552         enum ELLSD_Serialize
00553         {
00554                 LLSD_BINARY, LLSD_XML
00555         };
00556 
00557         /*
00558          * Generic in/outs
00559          */
00560         static void serialize(const LLSD& sd, std::ostream& str, ELLSD_Serialize,
00561                 U32 options = LLSDFormatter::OPTIONS_NONE);
00562         static bool deserialize(LLSD& sd, std::istream& str);
00563 
00564         /*
00565          * Notation Methods
00566          */
00567         static S32 toNotation(const LLSD& sd, std::ostream& str)
00568         {
00569                 LLPointer<LLSDNotationFormatter> f = new LLSDNotationFormatter;
00570                 return f->format(sd, str, LLSDFormatter::OPTIONS_NONE);
00571         }
00572         static S32 fromNotation(LLSD& sd, std::istream& str)
00573         {
00574                 LLPointer<LLSDNotationParser> p = new LLSDNotationParser;
00575                 return p->parse(str, sd);
00576         }
00577         
00578         /*
00579          * XML Methods
00580          */
00581         static S32 toXML(const LLSD& sd, std::ostream& str)
00582         {
00583                 LLPointer<LLSDXMLFormatter> f = new LLSDXMLFormatter;
00584                 return f->format(sd, str, LLSDFormatter::OPTIONS_NONE);
00585         }
00586         static S32 toPrettyXML(const LLSD& sd, std::ostream& str)
00587         {
00588                 LLPointer<LLSDXMLFormatter> f = new LLSDXMLFormatter;
00589                 return f->format(sd, str, LLSDFormatter::OPTIONS_PRETTY);
00590         }
00591         static S32 fromXML(LLSD& sd, std::istream& str)
00592         {
00593                 LLPointer<LLSDXMLParser> p = new LLSDXMLParser;
00594                 return p->parse(str, sd);
00595         }
00596 
00597         /*
00598          * Binary Methods
00599          */
00600         static S32 toBinary(const LLSD& sd, std::ostream& str)
00601         {
00602                 LLPointer<LLSDBinaryFormatter> f = new LLSDBinaryFormatter;
00603                 return f->format(sd, str, LLSDFormatter::OPTIONS_NONE);
00604         }
00605         static S32 fromBinary(LLSD& sd, std::istream& str)
00606         {
00607                 LLPointer<LLSDBinaryParser> p = new LLSDBinaryParser;
00608                 return p->parse(str, sd);
00609         }
00610 private:
00611         static const char *LLSDBinaryHeader;
00612         static const char *LLSDXMLHeader;
00613 };
00614 
00615 #endif // LL_LLSDSERIALIZE_H

Generated on Thu Jul 1 06:09:09 2010 for Second Life Viewer by  doxygen 1.4.7