Return references.

This commit is contained in:
JoelKatz
2013-05-17 01:40:55 -07:00
parent 5b68834e3b
commit d22c11be1c
2 changed files with 10 additions and 8 deletions

View File

@@ -639,38 +639,40 @@ std::vector<unsigned char> STObject::getFieldVL(SField::ref field) const
return cf->getValue(); return cf->getValue();
} }
static const STAmount defaultAmount;
const STAmount& STObject::getFieldAmount(SField::ref field) const const STAmount& STObject::getFieldAmount(SField::ref field) const
{ {
static STAmount empty;
const SerializedType* rf = peekAtPField(field); const SerializedType* rf = peekAtPField(field);
if (!rf) if (!rf)
throw std::runtime_error("Field not found"); throw std::runtime_error("Field not found");
SerializedTypeID id = rf->getSType(); SerializedTypeID id = rf->getSType();
if (id == STI_NOTPRESENT) if (id == STI_NOTPRESENT)
return defaultAmount; // optional field not present return empty; // optional field not present
const STAmount *cf = dynamic_cast<const STAmount *>(rf); const STAmount *cf = dynamic_cast<const STAmount *>(rf);
if (!cf) if (!cf)
throw std::runtime_error("Wrong field type"); throw std::runtime_error("Wrong field type");
return *cf; return *cf;
} }
STPathSet STObject::getFieldPathSet(SField::ref field) const const STPathSet& STObject::getFieldPathSet(SField::ref field) const
{ {
static STPathSet empty;
const SerializedType* rf = peekAtPField(field); const SerializedType* rf = peekAtPField(field);
if (!rf) throw std::runtime_error("Field not found"); if (!rf) throw std::runtime_error("Field not found");
SerializedTypeID id = rf->getSType(); SerializedTypeID id = rf->getSType();
if (id == STI_NOTPRESENT) return STPathSet(); // optional field not present if (id == STI_NOTPRESENT) return empty; // optional field not present
const STPathSet *cf = dynamic_cast<const STPathSet *>(rf); const STPathSet *cf = dynamic_cast<const STPathSet *>(rf);
if (!cf) throw std::runtime_error("Wrong field type"); if (!cf) throw std::runtime_error("Wrong field type");
return *cf; return *cf;
} }
STVector256 STObject::getFieldV256(SField::ref field) const const STVector256& STObject::getFieldV256(SField::ref field) const
{ {
static STVector256 empty;
const SerializedType* rf = peekAtPField(field); const SerializedType* rf = peekAtPField(field);
if (!rf) throw std::runtime_error("Field not found"); if (!rf) throw std::runtime_error("Field not found");
SerializedTypeID id = rf->getSType(); SerializedTypeID id = rf->getSType();
if (id == STI_NOTPRESENT) return STVector256(); // optional field not present if (id == STI_NOTPRESENT) return empty; // optional field not present
const STVector256 *cf = dynamic_cast<const STVector256 *>(rf); const STVector256 *cf = dynamic_cast<const STVector256 *>(rf);
if (!cf) throw std::runtime_error("Wrong field type"); if (!cf) throw std::runtime_error("Wrong field type");
return *cf; return *cf;

View File

@@ -131,8 +131,8 @@ public:
uint160 getFieldAccount160(SField::ref field) const; uint160 getFieldAccount160(SField::ref field) const;
std::vector<unsigned char> getFieldVL(SField::ref field) const; std::vector<unsigned char> getFieldVL(SField::ref field) const;
const STAmount& getFieldAmount(SField::ref field) const; const STAmount& getFieldAmount(SField::ref field) const;
STPathSet getFieldPathSet(SField::ref field) const; const STPathSet& getFieldPathSet(SField::ref field) const;
STVector256 getFieldV256(SField::ref field) const; const STVector256& getFieldV256(SField::ref field) const;
void setFieldU8(SField::ref field, unsigned char); void setFieldU8(SField::ref field, unsigned char);
void setFieldU16(SField::ref field, uint16); void setFieldU16(SField::ref field, uint16);