From 42a7f57614fb983ae59cccb62733694c7a49a54a Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Sun, 25 Mar 2012 19:54:57 -0700 Subject: [PATCH] Some helper functions. --- src/SerializedObject.cpp | 90 +++++++++++++++++++++++++++++++++++++++- src/SerializedObject.h | 13 +++++- 2 files changed, 101 insertions(+), 2 deletions(-) diff --git a/src/SerializedObject.cpp b/src/SerializedObject.cpp index fafbabdea..ac00f503f 100644 --- a/src/SerializedObject.cpp +++ b/src/SerializedObject.cpp @@ -191,7 +191,7 @@ SerializedType& STObject::getField(SOE_Field field) return getIndex(index); } -const SerializedType* STObject::peekAtPField(SOE_Field field) +const SerializedType* STObject::peekAtPField(SOE_Field field) const { int index=getFieldIndex(field); if(index==-1) return NULL; @@ -255,3 +255,91 @@ void STObject::makeFieldAbsent(SOE_Field field) mData.replace(index, new STObject(mType[index]->e_name)); clearFlag(mType[index]->e_flags); } + +unsigned char STObject::getValueFieldU8(SOE_Field field) const +{ + const SerializedType* rf=peekAtPField(field); + if(!rf) throw std::runtime_error("Field not found"); + SerializedTypeID id=rf->getType(); + if(id==STI_OBJECT) return 0; // optional field not present + const STUInt8 *cf=dynamic_cast(rf); + if(!cf) throw std::runtime_error("Wrong field type"); + return cf->getValue(); +} + +uint16 STObject::getValueFieldU16(SOE_Field field) const +{ + const SerializedType* rf=peekAtPField(field); + if(!rf) throw std::runtime_error("Field not found"); + SerializedTypeID id=rf->getType(); + if(id==STI_OBJECT) return 0; // optional field not present + const STUInt16 *cf=dynamic_cast(rf); + if(!cf) throw std::runtime_error("Wrong field type"); + return cf->getValue(); +} + +uint32 STObject::getValueFieldU32(SOE_Field field) const +{ + const SerializedType* rf=peekAtPField(field); + if(!rf) throw std::runtime_error("Field not found"); + SerializedTypeID id=rf->getType(); + if(id==STI_OBJECT) return 0; // optional field not present + const STUInt32 *cf=dynamic_cast(rf); + if(!cf) throw std::runtime_error("Wrong field type"); + return cf->getValue(); +} + +uint64 STObject::getValueFieldU64(SOE_Field field) const +{ + const SerializedType* rf=peekAtPField(field); + if(!rf) throw std::runtime_error("Field not found"); + SerializedTypeID id=rf->getType(); + if(id==STI_OBJECT) return 0; // optional field not present + const STUInt64 *cf=dynamic_cast(rf); + if(!cf) throw std::runtime_error("Wrong field type"); + return cf->getValue(); +} + +uint160 STObject::getValueFieldH160(SOE_Field field) const +{ + const SerializedType* rf=peekAtPField(field); + if(!rf) throw std::runtime_error("Field not found"); + SerializedTypeID id=rf->getType(); + if(id==STI_OBJECT) return uint160(); // optional field not present + const STHash160 *cf=dynamic_cast(rf); + if(!cf) throw std::runtime_error("Wrong field type"); + return cf->getValue(); +} + +uint256 STObject::getValueFieldH256(SOE_Field field) const +{ + const SerializedType* rf=peekAtPField(field); + if(!rf) throw std::runtime_error("Field not found"); + SerializedTypeID id=rf->getType(); + if(id==STI_OBJECT) return uint256(); // optional field not present + const STHash256 *cf=dynamic_cast(rf); + if(!cf) throw std::runtime_error("Wrong field type"); + return cf->getValue(); +} + +std::vector STObject::getValueFieldVL(SOE_Field field) const +{ + const SerializedType* rf=peekAtPField(field); + if(!rf) throw std::runtime_error("Field not found"); + SerializedTypeID id=rf->getType(); + if(id==STI_OBJECT) return std::vector(); // optional field not present + const STVariableLength *cf=dynamic_cast(rf); + if(!cf) throw std::runtime_error("Wrong field type"); + return cf->getValue(); +} + +std::vector STObject::getValueFieldTL(SOE_Field field) const +{ + const SerializedType* rf=peekAtPField(field); + if(!rf) throw std::runtime_error("Field not found"); + SerializedTypeID id=rf->getType(); + if(id==STI_OBJECT) return std::vector(); // optional field not present + const STTaggedList *cf=dynamic_cast(rf); + if(!cf) throw std::runtime_error("Wrong field type"); + return cf->getValue(); +} diff --git a/src/SerializedObject.h b/src/SerializedObject.h index d7e81b6f3..ff631acc6 100644 --- a/src/SerializedObject.h +++ b/src/SerializedObject.h @@ -78,10 +78,21 @@ public: const SerializedType& peekAtField(SOE_Field field) const; SerializedType& getField(SOE_Field field); - const SerializedType* peekAtPField(SOE_Field field); + const SerializedType* peekAtPField(SOE_Field field) const; SerializedType* getPField(SOE_Field field); const SOElement* getFieldType(SOE_Field field) const; + // these throw if the field type doesn't match, or return default values if the + // field is optional but not present + unsigned char getValueFieldU8(SOE_Field field) const; + uint16 getValueFieldU16(SOE_Field field) const; + uint32 getValueFieldU32(SOE_Field field) const; + uint64 getValueFieldU64(SOE_Field field) const; + uint160 getValueFieldH160(SOE_Field field) const; + uint256 getValueFieldH256(SOE_Field field) const; + std::vector getValueFieldVL(SOE_Field field) const; + std::vector getValueFieldTL(SOE_Field field) const; + bool isFieldPresent(SOE_Field field) const; void makeFieldPresent(SOE_Field field); void makeFieldAbsent(SOE_Field field);