From ee27342a247d4d722245d6f5587ed7485999470f Mon Sep 17 00:00:00 2001 From: Ray Greenwell Date: Fri, 6 Nov 2009 01:27:03 +0000 Subject: [PATCH] Proposed change to readObject(). The purpose is to - Ensure type checking for "fail-fast" detection of streaming errors. - But simplify it so that callers don't have to cast themselves and potentially screw up by using "String(obj)" or "Array(obj)", or miss out on the fail-fastyness by using "obj as SomeType". - But, at the same time, allow for the sveltest code possible. // first one is a bit shorter! _name = ins.readObject(Name); _name = Name(ins.readObject()); // first one is a bit shorter! _state = ins.readObject(String); _state = ins.readObject() as String; So, to sum up: the code is the shortest and doesn't require special-casing if you are reading a String, Number, int, uint, Boolean, or Array. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5984 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- src/as/com/threerings/io/ObjectInputStream.as | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/as/com/threerings/io/ObjectInputStream.as b/src/as/com/threerings/io/ObjectInputStream.as index 209b5692e..5d21db8e5 100644 --- a/src/as/com/threerings/io/ObjectInputStream.as +++ b/src/as/com/threerings/io/ObjectInputStream.as @@ -62,13 +62,16 @@ public class ObjectInputStream } /** - * Attempts to read the next object from the stream. If a type is passed and the read object's - * type doesn't match, a TypeError is thrown. - * @param type optional type to check the read object against + * Reads the next Object (or null) from the stream and returns it as * so that you may assign + * to any variable without casting. It is strongly recommended that you pass a 'checkType' + * parameter so that the type of the Object is verified to be what you believe it to be, + * thus helping you detect streaming errors as quickly as possible. + * + * @param checkType optional type to check the read object against + * @return the Object read, or null. * @throws TypeError if the object is read successfully but is the wrong type - * @return null if the object could not be read (or is actually null) */ - public function readObject (type :Class = null) :Object + public function readObject (checkType :Class = null) :* //throws IOError { var DEBUG_ID :String = "[" + (++_debugObjectCounter) + "] "; @@ -121,9 +124,9 @@ public class ObjectInputStream //log.debug("Reading object..."); readBareObjectImpl(target, cmap.streamer); if (DEBUG) log.debug(DEBUG_ID + "Read object: " + target); - if (type != null && !(target is type)) { + if (checkType != null && !(target is checkType)) { throw new TypeError( - "Cannot convert " + ClassUtil.getClass(target) + " to " + type); + "Cannot convert " + ClassUtil.getClass(target) + " to " + checkType); } return target;