From 380b36be3352cd9a5c93dbd67db25346166a8547 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 11 Jun 2012 04:05:22 +0000 Subject: All languages now support Null except for python and php, python is proving slightly trickier. --- c++-qt/src/null.cpp | 31 ++++++++++++++++++++++++++ c++-qt/src/null.h | 28 ++++++++++++++++++++++++ c++-qt/src/object.cpp | 5 +++++ c++-qt/src/object.h | 3 ++- c++-qt/src/types.h | 1 + java/com/xagasoft/gats/GatsNull.java | 40 ++++++++++++++++++++++++++++++++++ java/com/xagasoft/gats/GatsObject.java | 5 +++++ python/gats.py | 6 ++++- src/null.cpp | 33 ++++++++++++++++++++++++++++ src/null.h | 24 ++++++++++++++++++++ src/object.cpp | 14 ++++++++++++ src/object.h | 3 ++- src/types.h | 1 + src/unit/basic.unit | 21 ++++++++++++++++++ 14 files changed, 212 insertions(+), 3 deletions(-) create mode 100644 c++-qt/src/null.cpp create mode 100644 c++-qt/src/null.h create mode 100644 java/com/xagasoft/gats/GatsNull.java create mode 100644 src/null.cpp create mode 100644 src/null.h diff --git a/c++-qt/src/null.cpp b/c++-qt/src/null.cpp new file mode 100644 index 0000000..f259887 --- /dev/null +++ b/c++-qt/src/null.cpp @@ -0,0 +1,31 @@ +#include "gats-qt/null.h" + +#include + +Gats::Null::Null() +{ +} + +Gats::Null::~Null() +{ +} + +Gats::Object *Gats::Null::clone() const +{ + return new Gats::Null(); +} + +void Gats::Null::write( QIODevice &rOut ) const +{ + rOut.write("n", 1 ); +} + +void Gats::Null::read( QIODevice &rIn, char cType ) +{ +} +/* +Bu::Formatter &operator<<( Bu::Formatter &f, const Gats::Null &b ) +{ + return f << "(bool) " << b.getValue(); +} +*/ diff --git a/c++-qt/src/null.h b/c++-qt/src/null.h new file mode 100644 index 0000000..354de12 --- /dev/null +++ b/c++-qt/src/null.h @@ -0,0 +1,28 @@ +#ifndef GATS_NULL_H +#define GATS_NULL_H + +#include "gats-qt/object.h" + +class QIODevice; + +namespace Gats +{ + class Null : public Gats::Object + { + Q_OBJECT; + public: + Null(); + virtual ~Null(); + + virtual Object *clone() const; + + virtual Type getType() const { return typeNull; } + + virtual void write( QIODevice &rOut ) const; + virtual void read( QIODevice &rIn, char cType ); + }; +}; + +//Bu::Formatter &operator<<( Bu::Formatter &f, const Gats::Null &b ); + +#endif diff --git a/c++-qt/src/object.cpp b/c++-qt/src/object.cpp index 3955ced..4290e17 100644 --- a/c++-qt/src/object.cpp +++ b/c++-qt/src/object.cpp @@ -6,6 +6,7 @@ #include "gats-qt/string.h" #include "gats-qt/list.h" #include "gats-qt/dictionary.h" +#include "gats-qt/null.h" #include @@ -52,6 +53,10 @@ Gats::Object *Gats::Object::read( QIODevice &rIn ) pObj = new Gats::Float(); break; + case 'n': + pObj = new Gats::Null(); + break; + case 'e': return NULL; diff --git a/c++-qt/src/object.h b/c++-qt/src/object.h index 10501e4..008ebef 100644 --- a/c++-qt/src/object.h +++ b/c++-qt/src/object.h @@ -15,7 +15,8 @@ namespace Gats typeString, typeInteger, typeFloat, - typeBoolean + typeBoolean, + typeNull }; /** diff --git a/c++-qt/src/types.h b/c++-qt/src/types.h index 1264a9d..bd1c8ae 100644 --- a/c++-qt/src/types.h +++ b/c++-qt/src/types.h @@ -5,3 +5,4 @@ #include "gats-qt/integer.h" #include "gats-qt/list.h" #include "gats-qt/string.h" +#include "gats-qt/null.h" diff --git a/java/com/xagasoft/gats/GatsNull.java b/java/com/xagasoft/gats/GatsNull.java new file mode 100644 index 0000000..7919433 --- /dev/null +++ b/java/com/xagasoft/gats/GatsNull.java @@ -0,0 +1,40 @@ +package com.xagasoft.gats; + +import java.io.InputStream; +import java.io.OutputStream; + +/** + * Represents a null value. This is really is the simplest type, GatsBoolean is + * a damn liar. This doesn't have any value, it's just null. + */ +public class GatsNull extends GatsObject +{ + private boolean bValue = false; + + /** + * Construct a new GatsNull, the default value is false. + */ + public GatsNull() + { + } + + public int getType() + { + return GatsObject.NULL; + } + + public String toString() + { + return "(null)"; + } + + void read( InputStream is, char cType ) throws java.io.IOException + { + } + + void write( OutputStream os ) throws java.io.IOException + { + os.write( (int)'n' ); + } +}; + diff --git a/java/com/xagasoft/gats/GatsObject.java b/java/com/xagasoft/gats/GatsObject.java index 47602b3..e26ab7b 100644 --- a/java/com/xagasoft/gats/GatsObject.java +++ b/java/com/xagasoft/gats/GatsObject.java @@ -18,6 +18,7 @@ public abstract class GatsObject public final static int LIST = 4; public final static int DICTIONARY = 5; public final static int BOOLEAN = 6; + public final static int NULL = 7; /** * Gets the type of the current object, type can be one of INTEGER, FLOAT, @@ -74,6 +75,10 @@ public abstract class GatsObject goRet = new GatsFloat(); break; + case 'n': + goRet = new GatsNull(); + break; + case 'e': return null; diff --git a/python/gats.py b/python/gats.py index 40139a2..22463ab 100644 --- a/python/gats.py +++ b/python/gats.py @@ -146,6 +146,8 @@ def _readObj( sIn ): return 0.0 else: raise Exception('Invalid exceptional float subtype found.') + elif t == 'n': + return None elif t == 'e': # End marker return None else: @@ -153,7 +155,9 @@ def _readObj( sIn ): return 'not implemented yet'; def _writeObj( obj, sOut ): - if isinstance( obj, bool ): + if obj is None: + sOut.write('n') + elif isinstance( obj, bool ): if obj == True: sOut.write('1') else: diff --git a/src/null.cpp b/src/null.cpp new file mode 100644 index 0000000..13a61ed --- /dev/null +++ b/src/null.cpp @@ -0,0 +1,33 @@ +#include "gats/null.h" + +#include +#include + +Gats::Null::Null() +{ +} + +Gats::Null::~Null() +{ +} + +Gats::Object *Gats::Null::clone() const +{ + return new Gats::Null(); +} + +void Gats::Null::write( Bu::Stream &rOut ) const +{ + rOut.write("n", 1 ); +} + +void Gats::Null::read( Bu::Stream &rIn, char cType ) +{ + // Nothing to do... +} + +Bu::Formatter &operator<<( Bu::Formatter &f, const Gats::Null &b ) +{ + return f << "(null)"; +} + diff --git a/src/null.h b/src/null.h new file mode 100644 index 0000000..afa2d0a --- /dev/null +++ b/src/null.h @@ -0,0 +1,24 @@ +#ifndef GATS_NULL_H +#define GATS_NULL_H + +#include "gats/object.h" + +namespace Gats +{ + class Null : public Gats::Object + { + public: + Null(); + virtual ~Null(); + + virtual Type getType() const { return typeNull; } + virtual Object *clone() const; + + virtual void write( Bu::Stream &rOut ) const; + virtual void read( Bu::Stream &rIn, char cType ); + }; +}; + +Bu::Formatter &operator<<( Bu::Formatter &f, const Gats::Null &b ); + +#endif diff --git a/src/object.cpp b/src/object.cpp index 9662b6a..15d7cb5 100644 --- a/src/object.cpp +++ b/src/object.cpp @@ -6,6 +6,7 @@ #include "gats/string.h" #include "gats/list.h" #include "gats/dictionary.h" +#include "gats/null.h" #include @@ -56,6 +57,10 @@ Gats::Object *Gats::Object::read( Bu::Stream &rIn ) pObj = new Gats::Float(); break; + case 'n': + pObj = new Gats::Null(); + break; + case 'e': return NULL; @@ -250,6 +255,10 @@ Gats::Object *Gats::Object::strToGats( Bu::String::const_iterator &i ) { return new Gats::Boolean( false ); } + else if( st == "null" ) + { + return new Gats::Null(); + } } } } @@ -288,6 +297,9 @@ Bu::Formatter &operator<<( Bu::Formatter &f, const Gats::Object &obj ) case Gats::typeBoolean: return f << dynamic_cast(obj); + case Gats::typeNull: + return f << dynamic_cast(obj); + default: return f << "***ERROR: Bad Gats type***"; } @@ -303,6 +315,7 @@ Bu::Formatter &operator<<( Bu::Formatter &f, const Gats::Type &t ) case Gats::typeInteger: return f << "integer"; case Gats::typeFloat: return f << "float"; case Gats::typeBoolean: return f << "boolean"; + case Gats::typeNull: return f << "null"; } return f << "***unknown***"; @@ -318,6 +331,7 @@ const char *Gats::typeToStr( Gats::Type t ) case Gats::typeInteger: return "integer"; case Gats::typeFloat: return "float"; case Gats::typeBoolean: return "boolean"; + case Gats::typeNull: return "null"; } return "***unknown***"; diff --git a/src/object.h b/src/object.h index b2b1b92..2724189 100644 --- a/src/object.h +++ b/src/object.h @@ -18,7 +18,8 @@ namespace Gats typeString, typeInteger, typeFloat, - typeBoolean + typeBoolean, + typeNull }; /** diff --git a/src/types.h b/src/types.h index e6b23dc..81240e8 100644 --- a/src/types.h +++ b/src/types.h @@ -5,3 +5,4 @@ #include "gats/integer.h" #include "gats/list.h" #include "gats/string.h" +#include "gats/null.h" diff --git a/src/unit/basic.unit b/src/unit/basic.unit index 3ab8dc2..2c2c31a 100644 --- a/src/unit/basic.unit +++ b/src/unit/basic.unit @@ -12,6 +12,8 @@ #include "gats/list.h" #include "gats/boolean.h" #include "gats/string.h" +#include "gats/null.h" +#include "gats/gatsstream.h" #include "bu/membuf.h" #include "bu/list.h" @@ -167,4 +169,23 @@ suite Basic delete pDict; } } + + test null + { + MemBuf mb; + { + Gats::Null n; + Gats::GatsStream gs( mb ); + gs.writeObject( &n ); + } + + mb.setPos( 0 ); + + { + Gats::GatsStream gs( mb ); + Gats::Object *pObj = gs.readObject(); + unitTest( pObj->getType() == Gats::typeNull ); + delete pObj; + } + } } -- cgit v1.2.3