jsonval.h

Go to the documentation of this file.
00001 
00002 // Name:        jsonval.h
00003 // Purpose:     the wxJSONValue class: it holds a JSON value
00004 // Author:      Luciano Cattani
00005 // Created:     2007/09/15
00006 // RCS-ID:      $Id: jsonval.h,v 1.4 2008/01/10 21:27:15 luccat Exp $
00007 // Copyright:   (c) 2007 Luciano Cattani
00008 // Licence:     wxWidgets licence
00010 
00011 #if !defined( _WX_JSONVAL_H )
00012 #define _WX_JSONVAL_H
00013 
00014 #ifdef __GNUG__
00015     #pragma interface "jsonval.h"
00016 #endif
00017 
00018 // For compilers that support precompilation, includes "wx/wx.h".
00019 #include "wx/wxprec.h"
00020  
00021 #ifdef __BORLANDC__
00022     #pragma hdrstop
00023 #endif
00024 
00025 // for all others, include the necessary headers (this file is usually all you
00026 // need because it includes almost all "standard" wxWidgets headers)
00027 #ifndef WX_PRECOMP
00028     #include <wx/object.h>
00029     #include <wx/hashmap.h>
00030     #include <wx/dynarray.h>
00031     #include <wx/arrstr.h>
00032 #endif
00033 
00034 
00035 #include "json_defs.h"
00036 
00037 // forward declarations
00038 class WXDLLIMPEXP_JSON wxJSONReader;
00039 class WXDLLIMPEXP_JSON wxJSONRefData;
00040 class WXDLLIMPEXP_JSON wxJSONInternalMap;
00041 class WXDLLIMPEXP_JSON wxJSONInternalArray;
00042 
00043 
00045 enum wxJSONType {
00046     wxJSONTYPE_INVALID = 0,  
00047     wxJSONTYPE_NULL,       
00048     wxJSONTYPE_INT,        
00049     wxJSONTYPE_UINT,       
00050     wxJSONTYPE_DOUBLE,     
00051     wxJSONTYPE_STRING,     
00052     wxJSONTYPE_CSTRING,    
00053     wxJSONTYPE_BOOL,       
00054     wxJSONTYPE_ARRAY,      
00055     wxJSONTYPE_OBJECT,     
00056     wxJSONTYPE_LONG,       
00057     wxJSONTYPE_INT64,      
00058     wxJSONTYPE_ULONG,      
00059     wxJSONTYPE_UINT64,     
00060     wxJSONTYPE_SHORT,      
00061     wxJSONTYPE_USHORT      
00062 };
00063 
00064 // the comment position: every value only has one comment position
00065 // althrough comments may be splitted into several lines
00066 enum {
00067   wxJSONVALUE_COMMENT_DEFAULT = 0,
00068   wxJSONVALUE_COMMENT_BEFORE,
00069   wxJSONVALUE_COMMENT_AFTER,
00070   wxJSONVALUE_COMMENT_INLINE,
00071 };
00072 
00073 /***********************************************************************
00074 
00075                         class wxJSONValue
00076 
00077 ***********************************************************************/
00078 
00079 
00080 // class WXDLLIMPEXP_JSON wxJSONValue : public wxObject
00081 class WXDLLIMPEXP_JSON wxJSONValue
00082 {
00083   friend class wxJSONReader;
00084 
00085 public:
00086 
00087   // ctors and dtor
00088   wxJSONValue();
00089   wxJSONValue( wxJSONType type );
00090   wxJSONValue( int i );
00091   wxJSONValue( unsigned int i );
00092   wxJSONValue( short i );
00093   wxJSONValue( unsigned short i );
00094   wxJSONValue( long int i );
00095   wxJSONValue( unsigned long int i );
00096 #if defined( wxJSON_64BIT_INT)
00097   wxJSONValue( wxInt64 i );
00098   wxJSONValue( wxUint64 ui );
00099 #endif
00100   wxJSONValue( bool b  );
00101   wxJSONValue( double d );
00102   wxJSONValue( const wxChar* str );     // assume static ASCIIZ strings
00103   wxJSONValue( const wxString& str );
00104   wxJSONValue( const wxJSONValue& other );
00105   virtual ~wxJSONValue();
00106 
00107   // get the value type
00108   wxJSONType  GetType() const;
00109   bool IsValid() const;
00110   bool IsNull() const;
00111   bool IsInt() const;
00112   bool IsUInt() const;
00113   bool IsShort() const;
00114   bool IsUShort() const;
00115   bool IsLong() const;
00116   bool IsULong() const;
00117 #if defined( wxJSON_64BIT_INT)
00118   bool IsInt32() const;
00119   bool IsInt64() const;
00120   bool IsUInt32() const;
00121   bool IsUInt64() const;
00122 #endif
00123   bool IsBool() const;
00124   bool IsDouble() const;
00125   bool IsString() const;
00126   bool IsCString() const;
00127   bool IsArray() const;
00128   bool IsObject() const;
00129 
00130   // get the value as ...
00131   int            AsInt() const;
00132   unsigned int   AsUInt() const;
00133   short          AsShort() const;
00134   unsigned short AsUShort() const;
00135   long int       AsLong() const;
00136   unsigned long  AsULong() const;
00137 #if defined( wxJSON_64BIT_INT)
00138   wxInt32        AsInt32() const;
00139   wxUint32       AsUInt32() const;
00140   wxInt64        AsInt64() const;
00141   wxUint64       AsUInt64() const;
00142 #endif
00143   bool           AsBool() const;
00144   double         AsDouble() const;
00145   wxString       AsString() const;
00146   const wxChar*  AsCString() const;
00147 
00148   const wxJSONInternalMap*   AsMap() const;
00149   const wxJSONInternalArray* AsArray() const;
00150 
00151   // get members names, size and other info
00152   bool      HasMember( unsigned index ) const;
00153   bool      HasMember( const wxString& key ) const;
00154   int       Size() const;
00155   wxArrayString  GetMemberNames() const;
00156 
00157   // appending items, resizing and deleting items
00158   wxJSONValue& Append( const wxJSONValue& value );
00159   wxJSONValue& Append( bool b );
00160   wxJSONValue& Append( int i );
00161   wxJSONValue& Append( unsigned int ui );
00162   wxJSONValue& Append( short int i );
00163   wxJSONValue& Append( unsigned short int ui );
00164   wxJSONValue& Append( long int l );
00165   wxJSONValue& Append( unsigned long int ul );
00166 #if defined( wxJSON_64BIT_INT )
00167   wxJSONValue& Append( wxInt64 i );
00168   wxJSONValue& Append( wxUint64 ui );
00169 #endif
00170   wxJSONValue& Append( double d );
00171   wxJSONValue& Append( const wxChar* str );
00172   wxJSONValue& Append( const wxString& str );
00173   bool         Remove( int index );
00174   bool         Remove( const wxString& key );
00175   void         Clear();
00176   bool         Cat( const wxChar* str );
00177   bool         Cat( const wxString& str );
00178 
00179   // retrieve an item
00180   wxJSONValue& Item( unsigned index );
00181   wxJSONValue& Item( const wxString& key );
00182   wxJSONValue  ItemAt( unsigned index ) const;
00183   wxJSONValue  ItemAt( const wxString& key ) const;
00184 
00185   wxJSONValue& operator [] ( unsigned index );
00186   wxJSONValue& operator [] ( const wxString& key );
00187 
00188   wxJSONValue& operator = ( int i );
00189   wxJSONValue& operator = ( unsigned int ui );
00190   wxJSONValue& operator = ( short int i );
00191   wxJSONValue& operator = ( unsigned short int ui );
00192   wxJSONValue& operator = ( long int l );
00193   wxJSONValue& operator = ( unsigned long int ul );
00194 #if defined( wxJSON_64BIT_INT )
00195   wxJSONValue& operator = ( wxInt64 i );
00196   wxJSONValue& operator = ( wxUint64 ui );
00197 #endif
00198   wxJSONValue& operator = ( bool b );
00199   wxJSONValue& operator = ( double d );
00200   wxJSONValue& operator = ( const wxChar* str );
00201   wxJSONValue& operator = ( const wxString& str );
00202   wxJSONValue& operator = ( const wxJSONValue& value );
00203 
00204   // get the value of a default value
00205   wxJSONValue  Get( const wxString& key, const wxJSONValue& defaultValue ) const;
00206 
00207   // comparison function
00208   bool         IsSameAs( const wxJSONValue& other ) const;
00209 
00210   // comment-related functions
00211   int      AddComment( const wxString& str, int position = wxJSONVALUE_COMMENT_DEFAULT );
00212   int      AddComment( const wxArrayString& comments, int position = wxJSONVALUE_COMMENT_DEFAULT );
00213   wxString GetComment( int idx = -1 ) const;
00214   int      GetCommentPos() const;
00215   int      GetCommentCount() const;
00216   void     ClearComments();
00217   const wxArrayString& GetCommentArray() const;
00218 
00219   // debugging functions
00220   wxString         GetInfo() const;
00221   wxString         Dump( bool deep = false, int mode = 0 ) const;
00222 
00223   wxJSONRefData*   GetRefData() const;
00224   wxJSONRefData*   SetType( wxJSONType type );
00225 
00226   int              GetLineNo() const;
00227   void             SetLineNo( int num );
00228 
00229   static  wxString TypeToString( wxJSONType type );
00230 
00231 protected:
00232   wxJSONValue*  Find( unsigned index ) const;
00233   wxJSONValue*  Find( const wxString& key ) const;
00234   //  void          DeleteObj();
00235   void          DeepCopy( const wxJSONValue& other );
00236 
00237   wxJSONRefData*  Init( wxJSONType type );
00238   wxJSONRefData*  COW();
00239 
00240   // overidden from wxObject
00241   virtual wxJSONRefData*  CloneRefData(const wxJSONRefData *data) const;
00242   virtual wxJSONRefData*  CreateRefData() const;
00243 
00244   void            SetRefData(wxJSONRefData* data);
00245   void            Ref(const wxJSONValue& clone);
00246   void            UnRef();
00247   void            UnShare();
00248   void            AllocExclusive();
00249 
00251   wxJSONRefData*  m_refData;
00252 
00253   // used for debugging purposes: only in debug builds.
00254 #if defined( WXJSON_USE_VALUE_COUNTER )
00255   int         m_progr;
00256   static int  sm_progr;
00257 #endif
00258 };
00259 
00260 
00261 /***********************************************************************
00262 
00263                         class wxJSONRefData
00264 
00265 ***********************************************************************/
00266 
00267 
00268 
00269 WX_DECLARE_OBJARRAY( wxJSONValue, wxJSONInternalArray );
00270 WX_DECLARE_STRING_HASH_MAP( wxJSONValue, wxJSONInternalMap );
00271 
00272 
00274 
00284 union wxJSONValueHolder  {
00285     int             m_valInt;
00286     unsigned int    m_valUInt;
00287     short int       m_valShort;
00288     unsigned short  m_valUShort;
00289     long int        m_valLong;
00290     unsigned long   m_valULong;
00291     double          m_valDouble;
00292     const wxChar*   m_valCString;
00293     bool            m_valBool;
00294 #if defined( wxJSON_64BIT_INT )
00295     wxInt64         m_valInt64;
00296     wxUint64        m_valUInt64;
00297 #endif
00298   };
00299 
00300 //
00301 // access to the (unsigned) integer value is done through
00302 // the VAL_INT macro which expands to the 'long' integer
00303 // data member of the 'long long' integer if 64-bits integer
00304 // support is enabled
00305 #if defined( wxJSON_64BIT_INT )
00306  #define VAL_INT  m_valInt64
00307  #define VAL_UINT m_valUInt64
00308 #else
00309  #define VAL_INT  m_valLong
00310  #define VAL_UINT m_valULong
00311 #endif
00312 
00313 
00314 
00315 // class WXDLLIMPEXP_JSON wxJSONRefData : public wxObjectRefData
00316 class WXDLLIMPEXP_JSON wxJSONRefData
00317 {
00318   // friend class wxJSONReader;
00319   friend class wxJSONValue;
00320 
00321 public:
00322 
00323   wxJSONRefData();
00324   virtual ~wxJSONRefData();
00325 
00326   int GetRefCount() const;
00327 
00328   // there is no need to define copy ctor
00329   // wxJSONRefData( const wxJSONRefData& other );
00330 
00331 protected:
00333   int               m_refCount;
00334 
00336   wxJSONType        m_type;
00337 
00339 
00345   wxJSONValueHolder m_value;
00346 
00348   wxString            m_valString;
00349 
00351   wxJSONInternalArray m_valArray;
00352 
00354   wxJSONInternalMap   m_valMap;
00355 
00356 
00358 
00364   int               m_commentPos;
00365 
00367   wxArrayString     m_comments;
00368 
00370 
00377   int               m_lineNo;
00378 
00379 
00380   // used for debugging purposes: only in debug builds.
00381   #if defined( WXJSON_USE_VALUE_COUNTER )
00382     int         m_progr;
00383     static int  sm_progr;
00384   #endif
00385 };
00386 
00387 
00388 
00389 #endif                  // not defined _WX_JSONVAL_H
00390 
00391 

Generated on Mon Aug 18 22:54:22 2008 for wxJSON by  doxygen 1.4.7