osgEarth 2.1.1

/home/cube/sources/osgearth/src/osgEarth/JsonUtils

Go to the documentation of this file.
00001 /* -*-c++-*- */
00002 /* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
00003  * Copyright 2008-2010 Pelican Mapping
00004  * http://osgearth.org
00005  *
00006  * osgEarth is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU Lesser General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public License
00017  * along with this program.  If not, see <http://www.gnu.org/licenses/>
00018  */
00019 
00029 #ifndef OSGEARTH_JSONUTILS_H
00030 #define OSGEARTH_JSONUTILS_H 1
00031 
00032 #include "Common"
00033 #include <utility>
00034 #include <stdio.h>
00035 #include <assert.h>
00036 #include <iostream>
00037 #include <stdexcept>
00038 #include <utility>
00039 #include <stdexcept>
00040 #include "assert.h"
00041 #include <stddef.h>
00042 #include <deque>
00043 #include <stack>
00044 #include <string>
00045 #include <vector>
00046 #include <map>
00047 
00048 #define JSON_API OSGEARTH_EXPORT
00049 
00050 namespace osgEarth {
00051 
00052 namespace Json {
00053 
00054    class FastWriter;
00055    class Reader;
00056    class StyledWriter;
00057 
00058    // value.h
00059    class StaticString;
00060    class Path;
00061    class PathArgument;
00062    class Value;
00063    class ValueIteratorBase;
00064    class ValueIterator;
00065    class ValueConstIterator;
00066 #ifdef JSON_VALUE_USE_INTERNAL_MAP
00067    class ValueAllocator;
00068    class ValueMapAllocator;
00069    class ValueInternalLink;
00070    class ValueInternalArray;
00071    class ValueInternalMap;
00072 #endif // #ifdef JSON_VALUE_USE_INTERNAL_MAP
00073 
00074 } // namespace Json
00075 
00076 namespace Json {
00077 
00080    enum ValueType
00081    {
00082       nullValue = 0, 
00083       intValue,      
00084       uintValue,     
00085       realValue,     
00086       stringValue,   
00087       booleanValue,  
00088       arrayValue,    
00089       objectValue    
00090    };
00091 
00092    enum CommentPlacement
00093    {
00094       commentBefore = 0,        
00095       commentAfterOnSameLine,   
00096       commentAfter,             
00097       numberOfCommentPlacement
00098    };
00099 
00100 //# ifdef JSON_USE_CPPTL
00101 //   typedef CppTL::AnyEnumerator<const char *> EnumMemberNames;
00102 //   typedef CppTL::AnyEnumerator<const Value &> EnumValues;
00103 //# endif
00104 
00119    class JSON_API StaticString
00120    {
00121    public:
00122       explicit StaticString( const char *czstring )
00123          : str_( czstring )
00124       {
00125       }
00126 
00127       operator const char *() const
00128       {
00129          return str_;
00130       }
00131 
00132       const char *c_str() const
00133       {
00134          return str_;
00135       }
00136 
00137    private:
00138       const char *str_;
00139    };
00140 
00168    class JSON_API Value 
00169    {
00170       friend class ValueIteratorBase;
00171 # ifdef JSON_VALUE_USE_INTERNAL_MAP
00172       friend class ValueInternalLink;
00173       friend class ValueInternalMap;
00174 # endif
00175    public:
00176       typedef std::vector<std::string> Members;
00177       typedef int Int;
00178       typedef unsigned int UInt;
00179       typedef ValueIterator iterator;
00180       typedef ValueConstIterator const_iterator;
00181       typedef UInt ArrayIndex;
00182 
00183       static const Value null;
00184       static const Int minInt;
00185       static const Int maxInt;
00186       static const UInt maxUInt;
00187 
00188    private:
00189 #ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
00190 # ifndef JSON_VALUE_USE_INTERNAL_MAP
00191       class CZString 
00192       {
00193       public:
00194          enum DuplicationPolicy 
00195          {
00196             noDuplication = 0,
00197             duplicate,
00198             duplicateOnCopy
00199          };
00200          CZString( int index );
00201          CZString( const char *cstr, DuplicationPolicy allocate );
00202          CZString( const CZString &other );
00203          ~CZString();
00204          CZString &operator =( const CZString &other );
00205          bool operator<( const CZString &other ) const;
00206          bool operator==( const CZString &other ) const;
00207          int index() const;
00208          const char *c_str() const;
00209          bool isStaticString() const;
00210       private:
00211          void swap( CZString &other );
00212          const char *cstr_;
00213          int index_;
00214       };
00215 
00216    public:
00217 #  ifndef JSON_USE_CPPTL_SMALLMAP
00218       typedef std::map<CZString, Value> ObjectValues;
00219 #  else
00220       typedef CppTL::SmallMap<CZString, Value> ObjectValues;
00221 #  endif // ifndef JSON_USE_CPPTL_SMALLMAP
00222 # endif // ifndef JSON_VALUE_USE_INTERNAL_MAP
00223 #endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
00224 
00225    public:
00241       Value( ValueType type = nullValue );
00242       Value( Int value );
00243       Value( UInt value );
00244       Value( double value );
00245       Value( const char *value );
00256       Value( const StaticString &value );
00257       Value( const std::string &value );
00258 # ifdef JSON_USE_CPPTL
00259       Value( const CppTL::ConstString &value );
00260 # endif
00261       Value( bool value );
00262       Value( const Value &other );
00263       ~Value();
00264 
00265       Value &operator=( const Value &other );
00269       void swap( Value &other );
00270 
00271       ValueType type() const;
00272 
00273       bool operator <( const Value &other ) const;
00274       bool operator <=( const Value &other ) const;
00275       bool operator >=( const Value &other ) const;
00276       bool operator >( const Value &other ) const;
00277 
00278       bool operator ==( const Value &other ) const;
00279       bool operator !=( const Value &other ) const;
00280 
00281       int compare( const Value &other );
00282 
00283       const char *asCString() const;
00284       std::string asString() const;
00285 # ifdef JSON_USE_CPPTL
00286       CppTL::ConstString asConstString() const;
00287 # endif
00288       Int asInt() const;
00289       UInt asUInt() const;
00290       double asDouble() const;
00291       bool asBool() const;
00292 
00293       bool isNull() const;
00294       bool isBool() const;
00295       bool isInt() const;
00296       bool isUInt() const;
00297       bool isIntegral() const;
00298       bool isDouble() const;
00299       bool isNumeric() const;
00300       bool isString() const;
00301       bool isArray() const;
00302       bool isObject() const;
00303 
00304       bool isConvertibleTo( ValueType other ) const;
00305 
00307       UInt size() const;
00308 
00311       bool empty() const;
00312 
00314       bool operator!() const;
00315 
00319       void clear();
00320 
00326       void resize( UInt size );
00327 
00333       Value &operator[]( UInt index );
00337       const Value &operator[]( UInt index ) const;
00340       Value get( UInt index, 
00341                  const Value &defaultValue ) const;
00343       bool isValidIndex( UInt index ) const;
00347       Value &append( const Value &value );
00348 
00350       Value &operator[]( const char *key );
00352       const Value &operator[]( const char *key ) const;
00354       Value &operator[]( const std::string &key );
00356       const Value &operator[]( const std::string &key ) const;
00368       Value &operator[]( const StaticString &key );
00369 # ifdef JSON_USE_CPPTL
00370 
00371       Value &operator[]( const CppTL::ConstString &key );
00373       const Value &operator[]( const CppTL::ConstString &key ) const;
00374 # endif
00375 
00376       Value get( const char *key, 
00377                  const Value &defaultValue ) const;
00379       Value get( const std::string &key,
00380                  const Value &defaultValue ) const;
00381 # ifdef JSON_USE_CPPTL
00382 
00383       Value get( const CppTL::ConstString &key,
00384                  const Value &defaultValue ) const;
00385 # endif
00386 
00387 
00388 
00389 
00390 
00391 
00392       Value removeMember( const char* key );
00394       Value removeMember( const std::string &key );
00395 
00397       bool isMember( const char *key ) const;
00399       bool isMember( const std::string &key ) const;
00400 # ifdef JSON_USE_CPPTL
00401 
00402       bool isMember( const CppTL::ConstString &key ) const;
00403 # endif
00404 
00410       Members getMemberNames() const;
00411 
00412 //# ifdef JSON_USE_CPPTL
00413 //      EnumMemberNames enumMemberNames() const;
00414 //      EnumValues enumValues() const;
00415 //# endif
00416 
00418       void setComment( const char *comment,
00419                        CommentPlacement placement );
00421       void setComment( const std::string &comment,
00422                        CommentPlacement placement );
00423       bool hasComment( CommentPlacement placement ) const;
00425       std::string getComment( CommentPlacement placement ) const;
00426 
00427       std::string toStyledString() const;
00428 
00429       const_iterator begin() const;
00430       const_iterator end() const;
00431 
00432       iterator begin();
00433       iterator end();
00434 
00435    private:
00436       Value &resolveReference( const char *key, 
00437                                bool isStatic );
00438 
00439 # ifdef JSON_VALUE_USE_INTERNAL_MAP
00440       inline bool isItemAvailable() const
00441       {
00442          return itemIsUsed_ == 0;
00443       }
00444 
00445       inline void setItemUsed( bool isUsed = true )
00446       {
00447          itemIsUsed_ = isUsed ? 1 : 0;
00448       }
00449 
00450       inline bool isMemberNameStatic() const
00451       {
00452          return memberNameIsStatic_ == 0;
00453       }
00454 
00455       inline void setMemberNameIsStatic( bool isStatic )
00456       {
00457          memberNameIsStatic_ = isStatic ? 1 : 0;
00458       }
00459 # endif // # ifdef JSON_VALUE_USE_INTERNAL_MAP
00460 
00461    private:
00462       struct CommentInfo
00463       {
00464          CommentInfo();
00465          ~CommentInfo();
00466 
00467          void setComment( const char *text );
00468 
00469          char *comment_;
00470       };
00471 
00472       //struct MemberNamesTransform
00473       //{
00474       //   typedef const char *result_type;
00475       //   const char *operator()( const CZString &name ) const
00476       //   {
00477       //      return name.c_str();
00478       //   }
00479       //};
00480 
00481       union ValueHolder
00482       {
00483          Int int_;
00484          UInt uint_;
00485          double real_;
00486          bool bool_;
00487          char *string_;
00488 # ifdef JSON_VALUE_USE_INTERNAL_MAP
00489          ValueInternalArray *array_;
00490          ValueInternalMap *map_;
00491 #else
00492          ObjectValues *map_;
00493 # endif
00494       } value_;
00495       ValueType type_ : 8;
00496       int allocated_ : 1;     // Notes: if declared as bool, bitfield is useless.
00497 # ifdef JSON_VALUE_USE_INTERNAL_MAP
00498       unsigned int itemIsUsed_ : 1;      // used by the ValueInternalMap container.
00499       int memberNameIsStatic_ : 1;       // used by the ValueInternalMap container.
00500 # endif
00501       CommentInfo *comments_;
00502    };
00503 
00504 
00507    class PathArgument
00508    {
00509    public:
00510       friend class Path;
00511 
00512       PathArgument();
00513       PathArgument( Value::UInt index );
00514       PathArgument( const char *key );
00515       PathArgument( const std::string &key );
00516 
00517    private:
00518       enum Kind
00519       {
00520          kindNone = 0,
00521          kindIndex,
00522          kindKey
00523       };
00524       std::string key_;
00525       Value::UInt index_;
00526       Kind kind_;
00527    };
00528 
00540    class Path
00541    {
00542    public:
00543       Path( const std::string &path,
00544             const PathArgument &a1 = PathArgument(),
00545             const PathArgument &a2 = PathArgument(),
00546             const PathArgument &a3 = PathArgument(),
00547             const PathArgument &a4 = PathArgument(),
00548             const PathArgument &a5 = PathArgument() );
00549 
00550       const Value &resolve( const Value &root ) const;
00551       Value resolve( const Value &root, 
00552                      const Value &defaultValue ) const;
00554       Value &make( Value &root ) const;
00555 
00556    private:
00557       typedef std::vector<const PathArgument *> InArgs;
00558       typedef std::vector<PathArgument> Args;
00559 
00560       void makePath( const std::string &path,
00561                      const InArgs &in );
00562       void addPathInArg( const std::string &path, 
00563                          const InArgs &in, 
00564                          InArgs::const_iterator &itInArg, 
00565                          PathArgument::Kind kind );
00566       void invalidPath( const std::string &path, 
00567                         int location );
00568 
00569       Args args_;
00570    };
00571 
00579    class ValueAllocator
00580    {
00581    public:
00582       enum { unknown = (unsigned)-1 };
00583 
00584       virtual ~ValueAllocator();
00585 
00586       virtual char *makeMemberName( const char *memberName ) = 0;
00587       virtual void releaseMemberName( char *memberName ) = 0;
00588       virtual char *duplicateStringValue( const char *value, 
00589                                           unsigned int length = unknown ) = 0;
00590       virtual void releaseStringValue( char *value ) = 0;
00591    };
00592 
00593 #ifdef JSON_VALUE_USE_INTERNAL_MAP
00594 
00638    class JSON_API ValueMapAllocator
00639    {
00640    public:
00641       virtual ~ValueMapAllocator();
00642       virtual ValueInternalMap *newMap() = 0;
00643       virtual ValueInternalMap *newMapCopy( const ValueInternalMap &other ) = 0;
00644       virtual void destructMap( ValueInternalMap *map ) = 0;
00645       virtual ValueInternalLink *allocateMapBuckets( unsigned int size ) = 0;
00646       virtual void releaseMapBuckets( ValueInternalLink *links ) = 0;
00647       virtual ValueInternalLink *allocateMapLink() = 0;
00648       virtual void releaseMapLink( ValueInternalLink *link ) = 0;
00649    };
00650 
00654    class JSON_API ValueInternalLink
00655    {
00656    public:
00657       enum { itemPerLink = 6 };  // sizeof(ValueInternalLink) = 128 on 32 bits architecture.
00658       enum InternalFlags { 
00659          flagAvailable = 0,
00660          flagUsed = 1
00661       };
00662 
00663       ValueInternalLink();
00664 
00665       ~ValueInternalLink();
00666 
00667       Value items_[itemPerLink];
00668       char *keys_[itemPerLink];
00669       ValueInternalLink *previous_;
00670       ValueInternalLink *next_;
00671    };
00672 
00673 
00686    class JSON_API ValueInternalMap
00687    {
00688       friend class ValueIteratorBase;
00689       friend class Value;
00690    public:
00691       typedef unsigned int HashKey;
00692       typedef unsigned int BucketIndex;
00693 
00694 # ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
00695       struct IteratorState
00696       {
00697          ValueInternalMap *map_;
00698          ValueInternalLink *link_;
00699          BucketIndex itemIndex_;
00700          BucketIndex bucketIndex_;
00701       };
00702 # endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
00703 
00704       ValueInternalMap();
00705       ValueInternalMap( const ValueInternalMap &other );
00706       ValueInternalMap &operator =( const ValueInternalMap &other );
00707       ~ValueInternalMap();
00708 
00709       void swap( ValueInternalMap &other );
00710 
00711       BucketIndex size() const;
00712 
00713       void clear();
00714 
00715       bool reserveDelta( BucketIndex growth );
00716 
00717       bool reserve( BucketIndex newItemCount );
00718 
00719       const Value *find( const char *key ) const;
00720 
00721       Value *find( const char *key );
00722 
00723       Value &resolveReference( const char *key, 
00724                                bool isStatic );
00725 
00726       void remove( const char *key );
00727 
00728       void doActualRemove( ValueInternalLink *link, 
00729                            BucketIndex index,
00730                            BucketIndex bucketIndex );
00731 
00732       ValueInternalLink *&getLastLinkInBucket( BucketIndex bucketIndex );
00733 
00734       Value &setNewItem( const char *key, 
00735                          bool isStatic, 
00736                          ValueInternalLink *link, 
00737                          BucketIndex index );
00738 
00739       Value &unsafeAdd( const char *key, 
00740                         bool isStatic, 
00741                         HashKey hashedKey );
00742 
00743       HashKey hash( const char *key ) const;
00744 
00745       int compare( const ValueInternalMap &other ) const;
00746 
00747    private:
00748       void makeBeginIterator( IteratorState &it ) const;
00749       void makeEndIterator( IteratorState &it ) const;
00750       static bool equals( const IteratorState &x, const IteratorState &other );
00751       static void increment( IteratorState &iterator );
00752       static void incrementBucket( IteratorState &iterator );
00753       static void decrement( IteratorState &iterator );
00754       static const char *key( const IteratorState &iterator );
00755       static const char *key( const IteratorState &iterator, bool &isStatic );
00756       static Value &value( const IteratorState &iterator );
00757       static int distance( const IteratorState &x, const IteratorState &y );
00758 
00759    private:
00760       ValueInternalLink *buckets_;
00761       ValueInternalLink *tailLink_;
00762       BucketIndex bucketsSize_;
00763       BucketIndex itemCount_;
00764    };
00765 
00777    class JSON_API ValueInternalArray
00778    {
00779       friend class Value;
00780       friend class ValueIteratorBase;
00781    public:
00782       enum { itemsPerPage = 8 };    // should be a power of 2 for fast divide and modulo.
00783       typedef Value::ArrayIndex ArrayIndex;
00784       typedef unsigned int PageIndex;
00785 
00786 # ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
00787       struct IteratorState // Must be a POD
00788       {
00789          ValueInternalArray *array_;
00790          Value **currentPageIndex_;
00791          unsigned int currentItemIndex_;
00792       };
00793 # endif // ifndef JSONCPP_DOC_EXCLUDE_IMPLEMENTATION
00794 
00795       ValueInternalArray();
00796       ValueInternalArray( const ValueInternalArray &other );
00797       ValueInternalArray &operator =( const ValueInternalArray &other );
00798       ~ValueInternalArray();
00799       void swap( ValueInternalArray &other );
00800 
00801       void clear();
00802       void resize( ArrayIndex newSize );
00803 
00804       Value &resolveReference( ArrayIndex index );
00805 
00806       Value *find( ArrayIndex index ) const;
00807 
00808       ArrayIndex size() const;
00809 
00810       int compare( const ValueInternalArray &other ) const;
00811 
00812    private:
00813       static bool equals( const IteratorState &x, const IteratorState &other );
00814       static void increment( IteratorState &iterator );
00815       static void decrement( IteratorState &iterator );
00816       static Value &dereference( const IteratorState &iterator );
00817       static Value &unsafeDereference( const IteratorState &iterator );
00818       static int distance( const IteratorState &x, const IteratorState &y );
00819       static ArrayIndex indexOf( const IteratorState &iterator );
00820       void makeBeginIterator( IteratorState &it ) const;
00821       void makeEndIterator( IteratorState &it ) const;
00822       void makeIterator( IteratorState &it, ArrayIndex index ) const;
00823 
00824       void makeIndexValid( ArrayIndex index );
00825 
00826       Value **pages_;
00827       ArrayIndex size_;
00828       PageIndex pageCount_;
00829    };
00830 
00890    class JSON_API ValueArrayAllocator
00891    {
00892    public:
00893       virtual ~ValueArrayAllocator();
00894       virtual ValueInternalArray *newArray() = 0;
00895       virtual ValueInternalArray *newArrayCopy( const ValueInternalArray &other ) = 0;
00896       virtual void destructArray( ValueInternalArray *array ) = 0;
00908       virtual void reallocateArrayPageIndex( Value **&indexes, 
00909                                              ValueInternalArray::PageIndex &indexCount,
00910                                              ValueInternalArray::PageIndex minNewIndexCount ) = 0;
00911       virtual void releaseArrayPageIndex( Value **indexes, 
00912                                           ValueInternalArray::PageIndex indexCount ) = 0;
00913       virtual Value *allocateArrayPage() = 0;
00914       virtual void releaseArrayPage( Value *value ) = 0;
00915    };
00916 #endif // #ifdef JSON_VALUE_USE_INTERNAL_MAP
00917 
00918 
00922    class ValueIteratorBase
00923    {
00924    public:
00925       typedef unsigned int size_t;
00926       typedef int difference_type;
00927       typedef ValueIteratorBase SelfType;
00928 
00929       ValueIteratorBase();
00930 #ifndef JSON_VALUE_USE_INTERNAL_MAP
00931       explicit ValueIteratorBase( const Value::ObjectValues::iterator &current );
00932 #else
00933       ValueIteratorBase( const ValueInternalArray::IteratorState &state );
00934       ValueIteratorBase( const ValueInternalMap::IteratorState &state );
00935 #endif
00936 
00937       bool operator ==( const SelfType &other ) const
00938       {
00939          return isEqual( other );
00940       }
00941 
00942       bool operator !=( const SelfType &other ) const
00943       {
00944          return !isEqual( other );
00945       }
00946 
00947       difference_type operator -( const SelfType &other ) const
00948       {
00949          return computeDistance( other );
00950       }
00951 
00953       Value key() const;
00954 
00956       Value::UInt index() const;
00957 
00959       const char *memberName() const;
00960 
00961    protected:
00962       Value &deref() const;
00963 
00964       void increment();
00965 
00966       void decrement();
00967 
00968       difference_type computeDistance( const SelfType &other ) const;
00969 
00970       bool isEqual( const SelfType &other ) const;
00971 
00972       void copy( const SelfType &other );
00973 
00974    private:
00975 #ifndef JSON_VALUE_USE_INTERNAL_MAP
00976       Value::ObjectValues::iterator current_;
00977 #else
00978       union
00979       {
00980          ValueInternalArray::IteratorState array_;
00981          ValueInternalMap::IteratorState map_;
00982       } iterator_;
00983       bool isArray_;
00984 #endif
00985    };
00986 
00990    class ValueConstIterator : public ValueIteratorBase
00991    {
00992       friend class Value;
00993    public:
00994       typedef unsigned int size_t;
00995       typedef int difference_type;
00996       typedef const Value &reference;
00997       typedef const Value *pointer;
00998       typedef ValueConstIterator SelfType;
00999 
01000       ValueConstIterator();
01001    private:
01004 #ifndef JSON_VALUE_USE_INTERNAL_MAP
01005       explicit ValueConstIterator( const Value::ObjectValues::iterator &current );
01006 #else
01007       ValueConstIterator( const ValueInternalArray::IteratorState &state );
01008       ValueConstIterator( const ValueInternalMap::IteratorState &state );
01009 #endif
01010    public:
01011       SelfType &operator =( const ValueIteratorBase &other );
01012 
01013       SelfType operator++( int )
01014       {
01015          SelfType temp( *this );
01016          ++*this;
01017          return temp;
01018       }
01019 
01020       SelfType operator--( int )
01021       {
01022          SelfType temp( *this );
01023          --*this;
01024          return temp;
01025       }
01026 
01027       SelfType &operator--()
01028       {
01029          decrement();
01030          return *this;
01031       }
01032 
01033       SelfType &operator++()
01034       {
01035          increment();
01036          return *this;
01037       }
01038 
01039       reference operator *() const
01040       {
01041          return deref();
01042       }
01043    };
01044 
01045 
01048    class ValueIterator : public ValueIteratorBase
01049    {
01050       friend class Value;
01051    public:
01052       typedef unsigned int size_t;
01053       typedef int difference_type;
01054       typedef Value &reference;
01055       typedef Value *pointer;
01056       typedef ValueIterator SelfType;
01057 
01058       ValueIterator();
01059       ValueIterator( const ValueConstIterator &other );
01060       ValueIterator( const ValueIterator &other );
01061    private:
01064 #ifndef JSON_VALUE_USE_INTERNAL_MAP
01065       explicit ValueIterator( const Value::ObjectValues::iterator &current );
01066 #else
01067       ValueIterator( const ValueInternalArray::IteratorState &state );
01068       ValueIterator( const ValueInternalMap::IteratorState &state );
01069 #endif
01070    public:
01071 
01072       SelfType &operator =( const SelfType &other );
01073 
01074       SelfType operator++( int )
01075       {
01076          SelfType temp( *this );
01077          ++*this;
01078          return temp;
01079       }
01080 
01081       SelfType operator--( int )
01082       {
01083          SelfType temp( *this );
01084          --*this;
01085          return temp;
01086       }
01087 
01088       SelfType &operator--()
01089       {
01090          decrement();
01091          return *this;
01092       }
01093 
01094       SelfType &operator++()
01095       {
01096          increment();
01097          return *this;
01098       }
01099 
01100       reference operator *() const
01101       {
01102          return deref();
01103       }
01104    };
01105 
01106 
01107 } // namespace Json
01108 
01109 
01110 
01111 namespace Json {
01112 
01113    class Value;
01114 
01117    class JSON_API Writer
01118    {
01119    public:
01120       virtual ~Writer();
01121 
01122       virtual std::string write( const Value &root ) = 0;
01123    };
01124 
01131    class JSON_API FastWriter : public Writer
01132    {
01133    public:
01134       FastWriter();
01135       virtual ~FastWriter(){}
01136 
01137       void enableYAMLCompatibility();
01138 
01139    public: // overridden from Writer
01140       virtual std::string write( const Value &root );
01141 
01142    private:
01143       void writeValue( const Value &value );
01144 
01145       std::string document_;
01146       bool yamlCompatiblityEnabled_;
01147    };
01148 
01167    class JSON_API StyledWriter: public Writer
01168    {
01169    public:
01170       StyledWriter();
01171       virtual ~StyledWriter(){}
01172 
01173    public: // overridden from Writer
01178       virtual std::string write( const Value &root );
01179 
01180    private:
01181       void writeValue( const Value &value );
01182       void writeArrayValue( const Value &value );
01183       bool isMultineArray( const Value &value );
01184       void pushValue( const std::string &value );
01185       void writeIndent();
01186       void writeWithIndent( const std::string &value );
01187       void indent();
01188       void unindent();
01189       void writeCommentBeforeValue( const Value &root );
01190       void writeCommentAfterValueOnSameLine( const Value &root );
01191       bool hasCommentForValue( const Value &value );
01192       static std::string normalizeEOL( const std::string &text );
01193 
01194       typedef std::vector<std::string> ChildValues;
01195 
01196       ChildValues childValues_;
01197       std::string document_;
01198       std::string indentString_;
01199       int rightMargin_;
01200       int indentSize_;
01201       bool addChildValues_;
01202    };
01203 
01224    class JSON_API StyledStreamWriter
01225    {
01226    public:
01227       StyledStreamWriter( std::string indentation="\t" );
01228       ~StyledStreamWriter(){}
01229 
01230    public:
01236       void write( std::ostream &out, const Value &root );
01237 
01238    private:
01239       void writeValue( const Value &value );
01240       void writeArrayValue( const Value &value );
01241       bool isMultineArray( const Value &value );
01242       void pushValue( const std::string &value );
01243       void writeIndent();
01244       void writeWithIndent( const std::string &value );
01245       void indent();
01246       void unindent();
01247       void writeCommentBeforeValue( const Value &root );
01248       void writeCommentAfterValueOnSameLine( const Value &root );
01249       bool hasCommentForValue( const Value &value );
01250       static std::string normalizeEOL( const std::string &text );
01251 
01252       typedef std::vector<std::string> ChildValues;
01253 
01254       ChildValues childValues_;
01255       std::ostream* document_;
01256       std::string indentString_;
01257       int rightMargin_;
01258       std::string indentation_;
01259       bool addChildValues_;
01260    };
01261 
01262    std::string JSON_API valueToString( Value::Int value );
01263    std::string JSON_API valueToString( Value::UInt value );
01264    std::string JSON_API valueToString( double value );
01265    std::string JSON_API valueToString( bool value );
01266    std::string JSON_API valueToQuotedString( const char *value );
01267 
01270    std::ostream& operator<<( std::ostream&, const Value &root );
01271 
01272 } // namespace Json
01273 
01274 
01275 namespace Json {
01276 
01277    class Value;
01278 
01283    class JSON_API Reader
01284    {
01285    public:
01286       typedef char Char;
01287       typedef const Char *Location;
01288 
01289       Reader();
01290 
01299       bool parse( const std::string &document, 
01300                   Value &root,
01301                   bool collectComments = true );
01302 
01311       bool parse( const char *beginDoc, const char *endDoc, 
01312                   Value &root,
01313                   bool collectComments = true );
01314 
01317       bool parse( std::istream&,
01318                   Value &root,
01319                   bool collectComments = true );
01320 
01326       std::string getFormatedErrorMessages() const;
01327 
01328    private:
01329       enum TokenType
01330       {
01331          tokenEndOfStream = 0,
01332          tokenObjectBegin,
01333          tokenObjectEnd,
01334          tokenArrayBegin,
01335          tokenArrayEnd,
01336          tokenString,
01337          tokenNumber,
01338          tokenTrue,
01339          tokenFalse,
01340          tokenNull,
01341          tokenArraySeparator,
01342          tokenMemberSeparator,
01343          tokenComment,
01344          tokenError
01345       };
01346 
01347       class Token
01348       {
01349       public:
01350          TokenType type_;
01351          Location start_;
01352          Location end_;
01353       };
01354 
01355       class ErrorInfo
01356       {
01357       public:
01358          Token token_;
01359          std::string message_;
01360          Location extra_;
01361       };
01362 
01363       typedef std::deque<ErrorInfo> Errors;
01364 
01365       bool expectToken( TokenType type, Token &token, const char *message );
01366       bool readToken( Token &token );
01367       void skipSpaces();
01368       bool match( Location pattern, 
01369                   int patternLength );
01370       bool readComment();
01371       bool readCStyleComment();
01372       bool readCppStyleComment();
01373       bool readString();
01374       void readNumber();
01375       bool readValue();
01376       bool readObject( Token &token );
01377       bool readArray( Token &token );
01378       bool decodeNumber( Token &token );
01379       bool decodeString( Token &token );
01380       bool decodeString( Token &token, std::string &decoded );
01381       bool decodeDouble( Token &token );
01382       bool decodeUnicodeEscapeSequence( Token &token, 
01383                                         Location &current, 
01384                                         Location end, 
01385                                         unsigned int &unicode );
01386       bool addError( const std::string &message, 
01387                      Token &token,
01388                      Location extra = 0 );
01389       bool recoverFromError( TokenType skipUntilToken );
01390       bool addErrorAndRecover( const std::string &message, 
01391                                Token &token,
01392                                TokenType skipUntilToken );
01393       void skipUntilSpace();
01394       Value &currentValue();
01395       Char getNextChar();
01396       void getLocationLineAndColumn( Location location,
01397                                      int &line,
01398                                      int &column ) const;
01399       std::string getLocationLineAndColumn( Location location ) const;
01400       void addComment( Location begin, 
01401                        Location end, 
01402                        CommentPlacement placement );
01403       void skipCommentTokens( Token &token );
01404    
01405       typedef std::stack<Value *> Nodes;
01406       Nodes nodes_;
01407       Errors errors_;
01408       std::string document_;
01409       Location begin_;
01410       Location end_;
01411       Location current_;
01412       Location lastValueEnd_;
01413       Value *lastValue_;
01414       std::string commentsBefore_;
01415       bool collectComments_;
01416    };
01417 
01442    std::istream& operator>>( std::istream&, Value& );
01443 
01444 } // namespace Json
01445 
01446 } // namespace osgEarth
01447 
01448 #endif // OSGEARTH_JSONUTILS_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines