Some helper functions.

This commit is contained in:
JoelKatz
2012-03-25 19:54:57 -07:00
parent 38cecf2d8f
commit 42a7f57614
2 changed files with 101 additions and 2 deletions

View File

@@ -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<const STUInt8 *>(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<const STUInt16 *>(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<const STUInt32 *>(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<const STUInt64 *>(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<const STHash160 *>(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<const STHash256 *>(rf);
if(!cf) throw std::runtime_error("Wrong field type");
return cf->getValue();
}
std::vector<unsigned char> 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<unsigned char>(); // optional field not present
const STVariableLength *cf=dynamic_cast<const STVariableLength *>(rf);
if(!cf) throw std::runtime_error("Wrong field type");
return cf->getValue();
}
std::vector<TaggedListItem> 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<TaggedListItem>(); // optional field not present
const STTaggedList *cf=dynamic_cast<const STTaggedList *>(rf);
if(!cf) throw std::runtime_error("Wrong field type");
return cf->getValue();
}

View File

@@ -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<unsigned char> getValueFieldVL(SOE_Field field) const;
std::vector<TaggedListItem> getValueFieldTL(SOE_Field field) const;
bool isFieldPresent(SOE_Field field) const;
void makeFieldPresent(SOE_Field field);
void makeFieldAbsent(SOE_Field field);