Browse Source

Reference Counting zu JSONValues hinzugefügt

kolja 5 years ago
parent
commit
43af463b13
2 changed files with 50 additions and 28 deletions
  1. 38 20
      JSON.cpp
  2. 12 8
      JSON.h

+ 38 - 20
JSON.cpp

@@ -6,11 +6,16 @@ using namespace JSON;
 JSONValue::JSONValue()
 {
     this->type = NULL_;
+    ref = 1;
 }
 
+JSONValue::~JSONValue()
+{}
+
 JSONValue::JSONValue( JSONType type )
 {
     this->type = type;
+    ref = 1;
 }
 
 JSONType JSONValue::getType() const
@@ -23,6 +28,19 @@ Text JSONValue::toString() const
     return Text( "null" );
 }
 
+JSONValue *JSONValue::getThis()
+{
+    ref++;
+    return this;
+}
+
+JSONValue *JSONValue::release()
+{
+    if( !--ref )
+        delete this;
+    return 0;
+}
+
 
 JSONBool::JSONBool( bool b )
     : JSONValue( BOOLEAN )
@@ -81,13 +99,13 @@ Text JSONString::toString() const
 JSONArray::JSONArray()
     : JSONValue( ARRAY )
 {
-    array = new Array< JSONValue >();
+    array = new RCArray< JSONValue >();
 }
 
 JSONArray::JSONArray( Text string )
     : JSONValue( ARRAY )
 {
-    array = new Array< JSONValue >();
+    array = new RCArray< JSONValue >();
     string = Parser::removeWhitespace( string );
     if( string.getText()[ 0 ] == '[' && string.getText()[ string.getLength() - 1 ] == ']' )
     {
@@ -122,12 +140,12 @@ JSONArray &JSONArray::operator=( const JSONArray &arr )
     return *this;
 }
 
-void JSONArray::addValue( JSONValue value )
+void JSONArray::addValue( JSONValue *value )
 {
     array->add( value );
 }
 
-JSONValue JSONArray::getValue( int i ) const
+JSONValue *JSONArray::getValue( int i ) const
 {
     return array->get( i );
 }
@@ -142,7 +160,7 @@ Text JSONArray::toString() const
     Text str = "[";
     for( auto i = array->getIterator(); i; i++ )
     {
-        str += i._.toString();
+        str += i->toString();
         if( i.hasNext() )
             str += ",";
     }
@@ -155,14 +173,14 @@ JSONObject::JSONObject()
     : JSONValue( OBJECT )
 {
     fields = new Array< Text >();
-    values = new Array< JSONValue >();
+    values = new RCArray< JSONValue >();
 }
 
 JSONObject::JSONObject( Text string )
     : JSONValue( OBJECT )
 {
     fields = new Array< Text >();
-    values = new Array< JSONValue >();
+    values = new RCArray< JSONValue >();
     string = Parser::removeWhitespace( string );
     if( string.getText()[ 0 ] == '{' && string.getText()[ string.getLength() - 1 ] == '}' )
     {
@@ -210,7 +228,7 @@ JSONObject &JSONObject::operator=( const JSONObject &obj )
 }
 
 
-bool JSONObject::addValue( Text field, JSONValue value )
+bool JSONObject::addValue( Text field, JSONValue *value )
 {
     if( hasValue( field ) )
         return 0;
@@ -243,14 +261,14 @@ bool JSONObject::hasValue( Text field )
     return 0;
 }
 
-JSONValue JSONObject::getValue( Text field )
+JSONValue *JSONObject::getValue( Text field )
 {
     for( int i = 0; i < fields->getEintragAnzahl(); i++ )
     {
         if( fields->get( i ).istGleich( field ) )
             return values->get( i );
     }
-    return JSONValue();
+    return new JSONValue();
 }
 
 Iterator< Text > JSONObject::getFields()
@@ -258,7 +276,7 @@ Iterator< Text > JSONObject::getFields()
     return fields->getIterator();
 }
 
-Iterator< JSONValue > JSONObject::getValues()
+Iterator< JSONValue* > JSONObject::getValues()
 {
     return values->getIterator();
 }
@@ -272,7 +290,7 @@ Text JSONObject::toString() const
         str += "\"";
         str += k._.getText();
         str += "\":";
-        str += v._.toString().getText();
+        str += v->toString().getText();
         if( v.hasNext() )
             str += ",";
     }
@@ -357,26 +375,26 @@ Text Parser::removeWhitespace( const char *str )
     return ret;
 }
 
-JSONValue Parser::getValue( const char *str )
+JSONValue *Parser::getValue( const char *str )
 {
     Text string = Parser::removeWhitespace( str );
     if( string.istGleich( "true" ) )
-        return JSONBool( 1 );
+        return new JSONBool( 1 );
     if( string.istGleich( "false" ) )
-        return JSONBool( 0 );
+        return new JSONBool( 0 );
     if( string.getText()[ 0 ] == '"' )
     {
         string.remove( 0, 1 );
         string.remove( string.getLength() - 1, string.getLength() );
-        return JSONString( string );
+        return new JSONString( string );
     }
     if( string.getText()[ 0 ] == '[' )
-        return JSONArray( string );
+        return new JSONArray( string );
     if( string.getText()[ 0 ] == '{' )
-        return JSONObject( string );
+        return new JSONObject( string );
     if( Text( (double)string ).istGleich( string.getText() ) )
-        return JSONNumber( string );
-    return JSONValue();
+        return new JSONNumber( string );
+    return new JSONValue();
 }
 
 int Parser::findFieldEndInObject( const char *str )

+ 12 - 8
JSON.h

@@ -24,14 +24,18 @@ namespace Framework
         {
         private:
             JSONType type;
+            int ref;
 
         protected:
             __declspec( dllexport ) JSONValue( JSONType type );
 
         public:
             __declspec( dllexport ) JSONValue();
+            __declspec( dllexport ) virtual ~JSONValue();
             __declspec( dllexport ) JSONType getType() const;
             __declspec( dllexport ) virtual Text toString() const;
+            __declspec( dllexport ) JSONValue *getThis();
+            __declspec( dllexport ) JSONValue *release();
         };
 
         class JSONBool : public JSONValue
@@ -73,7 +77,7 @@ namespace Framework
         class JSONArray : public JSONValue
         {
         private:
-            Array< JSONValue > *array;
+            RCArray< JSONValue > *array;
 
         public:
             __declspec( dllexport ) JSONArray();
@@ -83,8 +87,8 @@ namespace Framework
 
             __declspec( dllexport ) JSONArray &operator=( const JSONArray &arr );
 
-            __declspec( dllexport ) void addValue( JSONValue value );
-            __declspec( dllexport ) JSONValue getValue( int i ) const;
+            __declspec( dllexport ) void addValue( JSONValue *value );
+            __declspec( dllexport ) JSONValue *getValue( int i ) const;
             __declspec( dllexport ) int getLength() const;
 
             __declspec( dllexport ) Text toString() const override;
@@ -94,7 +98,7 @@ namespace Framework
         {
         private:
             Array< Text > *fields;
-            Array< JSONValue > *values;
+            RCArray< JSONValue > *values;
 
         public:
             __declspec( dllexport ) JSONObject();
@@ -104,12 +108,12 @@ namespace Framework
 
             __declspec( dllexport ) JSONObject &operator=( const JSONObject &obj );
 
-            __declspec( dllexport ) bool addValue( Text field, JSONValue value );
+            __declspec( dllexport ) bool addValue( Text field, JSONValue *value );
             __declspec( dllexport ) bool removeValue( Text field );
             __declspec( dllexport ) bool hasValue( Text field );
-            __declspec( dllexport ) JSONValue getValue( Text field );
+            __declspec( dllexport ) JSONValue *getValue( Text field );
             __declspec( dllexport ) Iterator< Text > getFields();
-            __declspec( dllexport ) Iterator< JSONValue > getValues();
+            __declspec( dllexport ) Iterator< JSONValue* > getValues();
 
             __declspec( dllexport ) Text toString() const override;
         };
@@ -118,7 +122,7 @@ namespace Framework
         {
             __declspec( dllexport ) int findObjectEndInArray( const char *str );
             __declspec( dllexport ) Text removeWhitespace( const char *str );
-            __declspec( dllexport ) JSONValue getValue( const char *str );
+            __declspec( dllexport ) JSONValue *getValue( const char *str );
             __declspec( dllexport ) int findFieldEndInObject( const char *str );
             __declspec( dllexport ) int findValueEndInObject( const char *str );
         };