Get rid of some ugliness in the use of ptr_vectors. I hope I didn't break anything.

This commit is contained in:
JoelKatz
2012-05-15 16:07:42 -07:00
parent c9571a2692
commit cb4fcfddac
5 changed files with 120 additions and 76 deletions

View File

@@ -17,6 +17,8 @@ protected:
STObject mObject;
LedgerEntryFormat* mFormat;
SerializedLedgerEntry* duplicate() const { return new SerializedLedgerEntry(*this); }
public:
SerializedLedgerEntry(const Serializer& s, const uint256& index);
SerializedLedgerEntry(SerializerIterator& sit, const uint256& index);
@@ -24,7 +26,6 @@ public:
int getLength() const { return mVersion.getLength() + mObject.getLength(); }
SerializedTypeID getSType() const { return STI_LEDGERENTRY; }
SerializedLedgerEntry* duplicate() const { return new SerializedLedgerEntry(*this); }
std::string getFullText() const;
std::string getText() const;
Json::Value getJson(int options) const;

View File

@@ -5,77 +5,88 @@
#include "../json/writer.h"
SerializedType* STObject::makeDefaultObject(SerializedTypeID id, const char *name)
std::auto_ptr<SerializedType> STObject::makeDefaultObject(SerializedTypeID id, const char *name)
{
switch(id)
{
case STI_NOTPRESENT:
return std::auto_ptr<SerializedType>(new SerializedType(name));
case STI_UINT16:
return new STUInt16(name);
return std::auto_ptr<SerializedType>(new STUInt16(name));
case STI_UINT32:
return new STUInt32(name);
return std::auto_ptr<SerializedType>(new STUInt32(name));
case STI_UINT64:
return new STUInt64(name);
return std::auto_ptr<SerializedType>(new STUInt64(name));
case STI_AMOUNT:
return new STAmount(name);
return std::auto_ptr<SerializedType>(new STAmount(name));
case STI_HASH128:
return std::auto_ptr<SerializedType>(new STHash128(name));
case STI_HASH160:
return new STHash160(name);
return std::auto_ptr<SerializedType>(new STHash160(name));
case STI_HASH256:
return new STHash256(name);
return std::auto_ptr<SerializedType>(new STHash256(name));
case STI_VL:
return new STVariableLength(name);
return std::auto_ptr<SerializedType>(new STVariableLength(name));
case STI_TL:
return new STTaggedList(name);
return std::auto_ptr<SerializedType>(new STTaggedList(name));
case STI_ACCOUNT:
return new STAccount(name);
return std::auto_ptr<SerializedType>(new STAccount(name));
default:
assert(false);
return NULL;
throw std::runtime_error("Unknown object type");
}
}
SerializedType* STObject::makeDeserializedObject(SerializedTypeID id, const char *name, SerializerIterator& sit)
std::auto_ptr<SerializedType> STObject::makeDeserializedObject(SerializedTypeID id, const char *name,
SerializerIterator& sit)
{
switch(id)
{
case STI_NOTPRESENT:
return SerializedType::deserialize(name);
case STI_UINT16:
return STUInt16::construct(sit, name);
return STUInt16::deserialize(sit, name);
case STI_UINT32:
return STUInt32::construct(sit, name);
return STUInt32::deserialize(sit, name);
case STI_UINT64:
return STUInt64::construct(sit, name);
return STUInt64::deserialize(sit, name);
case STI_AMOUNT:
return STAmount::construct(sit, name);
return STAmount::deserialize(sit, name);
case STI_HASH128:
return STHash128::deserialize(sit, name);
case STI_HASH160:
return STHash160::construct(sit, name);
return STHash160::deserialize(sit, name);
case STI_HASH256:
return STHash256::construct(sit, name);
return STHash256::deserialize(sit, name);
case STI_VL:
return STVariableLength::construct(sit, name);
return STVariableLength::deserialize(sit, name);
case STI_TL:
return STTaggedList::construct(sit, name);
return STTaggedList::deserialize(sit, name);
case STI_ACCOUNT:
return STAccount::construct(sit, name);
return STAccount::deserialize(sit, name);
default:
assert(false);
return NULL;
throw std::runtime_error("Unknown object type");
}
}
@@ -86,13 +97,9 @@ STObject::STObject(SOElement* elem, const char *name) : SerializedType(name), mF
if (elem->e_type == SOE_FLAGS) mFlagIdx = mType.size();
mType.push_back(elem);
if (elem->e_type == SOE_IFFLAG)
giveObject(new SerializedType(elem->e_name));
giveObject(makeDefaultObject(STI_NOTPRESENT, elem->e_name));
else
{
SerializedType* t = makeDefaultObject(elem->e_id, elem->e_name);
if (!t) throw std::runtime_error("invalid transaction element");
giveObject(t);
}
giveObject(makeDefaultObject(elem->e_id, elem->e_name));
++elem;
}
}
@@ -110,7 +117,7 @@ STObject::STObject(SOElement* elem, SerializerIterator& sit, const char *name) :
if ((flags&elem->e_flags) == 0)
{
done = true;
giveObject(new SerializedType(elem->e_name));
giveObject(makeDefaultObject(elem->e_id, elem->e_name));
}
}
else if (elem->e_type == SOE_IFNFLAG)
@@ -119,7 +126,7 @@ STObject::STObject(SOElement* elem, SerializerIterator& sit, const char *name) :
if ((flags&elem->e_flags) != 0)
{
done = true;
giveObject(new SerializedType(elem->e_name));
giveObject(makeDefaultObject(STI_NOTPRESENT, elem->e_name));
}
}
else if (elem->e_type == SOE_FLAGS)
@@ -130,11 +137,7 @@ STObject::STObject(SOElement* elem, SerializerIterator& sit, const char *name) :
done = true;
}
if (!done)
{
SerializedType* t = makeDeserializedObject(elem->e_id, elem->e_name, sit);
if (!t) throw std::runtime_error("invalid transaction element");
giveObject(t);
}
giveObject(makeDeserializedObject(elem->e_id, elem->e_name, sit));
elem++;
}
}
@@ -279,23 +282,22 @@ uint32 STObject::getFlags(void) const
SerializedType* STObject::makeFieldPresent(SOE_Field field)
{
SerializedType* ret = NULL;
int index = getFieldIndex(field);
if (index == -1) throw std::runtime_error("Field not found");
if ((mType[index]->e_type != SOE_IFFLAG) && (mType[index]->e_type != SOE_IFNFLAG))
throw std::runtime_error("field is not optional");
ret = getPIndex(index);
if (ret->getSType() != STI_NOTPRESENT) return ret;
ret = makeDefaultObject(mType[index]->e_id, mType[index]->e_name);
mData.replace(index, ret);
SerializedType* f = getPIndex(index);
if (f->getSType() != STI_NOTPRESENT) return f;
mData.replace(index, makeDefaultObject(mType[index]->e_id, mType[index]->e_name));
f = getPIndex(index);
if (mType[index]->e_type == SOE_IFFLAG)
setFlag(mType[index]->e_flags);
else if (mType[index]->e_type == SOE_IFNFLAG)
clearFlag(mType[index]->e_flags);
return ret;
return f;
}
void STObject::makeFieldAbsent(SOE_Field field)

View File

@@ -54,8 +54,10 @@ protected:
boost::ptr_vector<SerializedType> mData;
std::vector<SOElement*> mType;
static SerializedType* makeDefaultObject(SerializedTypeID id, const char *name);
static SerializedType* makeDeserializedObject(SerializedTypeID id, const char *name, SerializerIterator&);
static std::auto_ptr<SerializedType> makeDefaultObject(SerializedTypeID id, const char *name);
static std::auto_ptr<SerializedType> makeDeserializedObject(SerializedTypeID id, const char *name,
SerializerIterator&);
STObject* duplicate() const { return new STObject(*this); }
public:
STObject(const char *n = NULL) : SerializedType(n), mFlagIdx(-1) { ; }
@@ -65,7 +67,6 @@ public:
int getLength() const;
SerializedTypeID getSType() const { return STI_OBJECT; }
STObject* duplicate() const { return new STObject(*this); }
virtual bool isEquivalent(const SerializedType& t) const;
void add(Serializer& s) const;
@@ -74,8 +75,9 @@ public:
std::string getText() const;
virtual Json::Value getJson(int options) const;
int addObject(const SerializedType& t) { mData.push_back(t.duplicate()); return mData.size()-1; }
int giveObject(SerializedType* t) { mData.push_back(t); return mData.size()-1; }
int addObject(const SerializedType& t) { mData.push_back(t.clone()); return mData.size() - 1; }
int giveObject(std::auto_ptr<SerializedType> t) { mData.push_back(t); return mData.size() - 1; }
int giveObject(SerializedType* t) { mData.push_back(t); return mData.size() - 1; }
const boost::ptr_vector<SerializedType>& peekData() const { return mData; }
boost::ptr_vector<SerializedType>& peekData() { return mData; }

View File

@@ -23,6 +23,8 @@ protected:
STObject mMiddleTxn, mInnerTxn;
TransactionFormat* mFormat;
SerializedTransaction* duplicate() const { return new SerializedTransaction(*this); }
public:
SerializedTransaction(SerializerIterator& sit, int length); // -1=all remaining, 0=get from sit
SerializedTransaction(TransactionType type);
@@ -30,7 +32,6 @@ public:
// STObject functions
int getLength() const;
SerializedTypeID getSType() const { return STI_TRANSACTION; }
SerializedTransaction* duplicate() const { return new SerializedTransaction(*this); }
std::string getFullText() const;
std::string getText() const;
void add(Serializer& s) const { getTransaction(s, true); }

View File

@@ -26,6 +26,8 @@ class SerializedType
protected:
const char *name;
virtual SerializedType* duplicate() const { return new SerializedType(name); }
public:
SerializedType() : name(NULL) { ; }
@@ -33,12 +35,15 @@ public:
SerializedType(const SerializedType& n) : name(n.name) { ; }
virtual ~SerializedType() { ; }
static std::auto_ptr<SerializedType> deserialize(const char *name)
{ return std::auto_ptr<SerializedType>(new SerializedType(name)); }
void setName(const char *n) { name=n; }
const char *getName() const { return name; }
virtual int getLength() const { return 0; }
virtual SerializedTypeID getSType() const { return STI_NOTPRESENT; }
virtual SerializedType* duplicate() const { return new SerializedType(name); }
std::auto_ptr<SerializedType> clone() const { return std::auto_ptr<SerializedType>(duplicate()); }
virtual std::string getFullText() const;
virtual std::string getText() const // just the value
@@ -49,12 +54,12 @@ public:
virtual bool isEquivalent(const SerializedType& t) const { return true; }
bool operator==(const SerializedType& t) const
{ return (getSType()==t.getSType()) && isEquivalent(t); }
{ return (getSType() == t.getSType()) && isEquivalent(t); }
bool operator!=(const SerializedType& t) const
{ return (getSType()!=t.getSType()) || !isEquivalent(t); }
{ return (getSType() != t.getSType()) || !isEquivalent(t); }
};
inline SerializedType* new_clone(const SerializedType& s) { return s.duplicate(); }
inline SerializedType* new_clone(const SerializedType& s) { return s.clone().release(); }
inline void delete_clone(const SerializedType* s) { boost::checked_delete(s); }
class STUInt8 : public SerializedType
@@ -62,15 +67,18 @@ class STUInt8 : public SerializedType
protected:
unsigned char value;
STUInt8* duplicate() const { return new STUInt8(name, value); }
static STUInt8* construct(SerializerIterator&, const char *name = NULL);
public:
STUInt8(unsigned char v=0) : value(v) { ; }
STUInt8(const char *n, unsigned char v=0) : SerializedType(n), value(v) { ; }
static STUInt8* construct(SerializerIterator&, const char *name = NULL);
static std::auto_ptr<SerializedType> deserialize(SerializerIterator& sit, const char *name)
{ return std::auto_ptr<SerializedType>(construct(sit, name)); }
int getLength() const { return 1; }
SerializedTypeID getSType() const { return STI_UINT8; }
STUInt8* duplicate() const { return new STUInt8(name, value); }
std::string getText() const;
void add(Serializer& s) const { s.add8(value); }
@@ -87,15 +95,18 @@ class STUInt16 : public SerializedType
protected:
uint16 value;
STUInt16* duplicate() const { return new STUInt16(name, value); }
static STUInt16* construct(SerializerIterator&, const char *name = NULL);
public:
STUInt16(uint16 v=0) : value(v) { ; }
STUInt16(const char *n, uint16 v=0) : SerializedType(n), value(v) { ; }
static STUInt16* construct(SerializerIterator&, const char *name = NULL);
static std::auto_ptr<SerializedType> deserialize(SerializerIterator& sit, const char *name)
{ return std::auto_ptr<SerializedType>(construct(sit, name)); }
int getLength() const { return 2; }
SerializedTypeID getSType() const { return STI_UINT16; }
STUInt16* duplicate() const { return new STUInt16(name, value); }
std::string getText() const;
void add(Serializer& s) const { s.add16(value); }
@@ -112,15 +123,18 @@ class STUInt32 : public SerializedType
protected:
uint32 value;
STUInt32* duplicate() const { return new STUInt32(name, value); }
static STUInt32* construct(SerializerIterator&, const char *name = NULL);
public:
STUInt32(uint32 v=0) : value(v) { ; }
STUInt32(const char *n, uint32 v=0) : SerializedType(n), value(v) { ; }
static STUInt32* construct(SerializerIterator&, const char *name = NULL);
static std::auto_ptr<SerializedType> deserialize(SerializerIterator& sit, const char *name)
{ return std::auto_ptr<SerializedType>(construct(sit, name)); }
int getLength() const { return 4; }
SerializedTypeID getSType() const { return STI_UINT32; }
STUInt32* duplicate() const { return new STUInt32(name, value); }
std::string getText() const;
void add(Serializer& s) const { s.add32(value); }
@@ -137,15 +151,18 @@ class STUInt64 : public SerializedType
protected:
uint64 value;
STUInt64* duplicate() const { return new STUInt64(name, value); }
static STUInt64* construct(SerializerIterator&, const char *name = NULL);
public:
STUInt64(uint64 v=0) : value(v) { ; }
STUInt64(const char *n, uint64 v=0) : SerializedType(n), value(v) { ; }
static STUInt64* construct(SerializerIterator&, const char *name = NULL);
static std::auto_ptr<SerializedType> deserialize(SerializerIterator& sit, const char *name)
{ return std::auto_ptr<SerializedType>(construct(sit, name)); }
int getLength() const { return 8; }
SerializedTypeID getSType() const { return STI_UINT64; }
STUInt64* duplicate() const { return new STUInt64(name, value); }
std::string getText() const;
void add(Serializer& s) const { s.add64(value); }
@@ -175,6 +192,8 @@ protected:
uint64 value;
void canonicalize();
STAmount* duplicate() const { return new STAmount(name, offset, value); }
static STAmount* construct(SerializerIterator&, const char *name = NULL);
static const int cMinOffset=-96, cMaxOffset=80;
static const uint64 cMinValue=1000000000000000ull, cMaxValue=9999999999999999ull;
@@ -184,11 +203,11 @@ public:
{ canonicalize(); } // (1,0)=$1 (1,-2)=$.01 (100,0)=(10000,-2)=$.01
STAmount(const char *n, uint64 v = 0, int off = 0) : SerializedType(n), offset(off), value(v)
{ canonicalize(); }
static STAmount* construct(SerializerIterator&, const char *name = NULL);
static std::auto_ptr<SerializedType> deserialize(SerializerIterator& sit, const char *name)
{ return std::auto_ptr<SerializedType>(construct(sit, name)); }
int getLength() const { return 8; }
SerializedTypeID getSType() const { return STI_AMOUNT; }
STAmount* duplicate() const { return new STAmount(name, offset, value); }
std::string getText() const;
std::string getRaw() const;
void add(Serializer& s) const;
@@ -245,17 +264,20 @@ class STHash128 : public SerializedType
protected:
uint128 value;
STHash128* duplicate() const { return new STHash128(name, value); }
static STHash128* construct(SerializerIterator&, const char *name = NULL);
public:
STHash128(const uint128& v) : value(v) { ; }
STHash128(const char *n, const uint128& v) : SerializedType(n), value(v) { ; }
STHash128(const char *n) : SerializedType(n) { ; }
STHash128() { ; }
static STHash128* construct(SerializerIterator&, const char *name = NULL);
static std::auto_ptr<SerializedType> deserialize(SerializerIterator& sit, const char *name)
{ return std::auto_ptr<SerializedType>(construct(sit, name)); }
int getLength() const { return 20; }
SerializedTypeID getSType() const { return STI_HASH128; }
STHash128* duplicate() const { return new STHash128(name, value); }
virtual std::string getText() const;
void add(Serializer& s) const { s.add128(value); }
@@ -272,17 +294,20 @@ class STHash160 : public SerializedType
protected:
uint160 value;
STHash160* duplicate() const { return new STHash160(name, value); }
static STHash160* construct(SerializerIterator&, const char *name = NULL);
public:
STHash160(const uint160& v) : value(v) { ; }
STHash160(const char *n, const uint160& v) : SerializedType(n), value(v) { ; }
STHash160(const char *n) : SerializedType(n) { ; }
STHash160() { ; }
static STHash160* construct(SerializerIterator&, const char *name = NULL);
static std::auto_ptr<SerializedType> deserialize(SerializerIterator& sit, const char *name)
{ return std::auto_ptr<SerializedType>(construct(sit, name)); }
int getLength() const { return 20; }
SerializedTypeID getSType() const { return STI_HASH160; }
STHash160* duplicate() const { return new STHash160(name, value); }
virtual std::string getText() const;
void add(Serializer& s) const { s.add160(value); }
@@ -299,17 +324,20 @@ class STHash256 : public SerializedType
protected:
uint256 value;
STHash256* duplicate() const { return new STHash256(name, value); }
static STHash256* construct(SerializerIterator&, const char *name = NULL);
public:
STHash256(const uint256& v) : value(v) { ; }
STHash256(const char *n, const uint256& v) : SerializedType(n), value(v) { ; }
STHash256(const char *n) : SerializedType(n) { ; }
STHash256() { ; }
static STHash256* construct(SerializerIterator&, const char *name = NULL);
static std::auto_ptr<SerializedType> deserialize(SerializerIterator& sit, const char *name)
{ return std::auto_ptr<SerializedType>(construct(sit, name)); }
int getLength() const { return 32; }
SerializedTypeID getSType() const { return STI_HASH256; }
STHash256* duplicate() const { return new STHash256(name, value); }
std::string getText() const;
void add(Serializer& s) const { s.add256(value); }
@@ -326,6 +354,9 @@ class STVariableLength : public SerializedType
protected:
std::vector<unsigned char> value;
virtual STVariableLength* duplicate() const { return new STVariableLength(name, value); }
static STVariableLength* construct(SerializerIterator&, const char *name = NULL);
public:
STVariableLength(const std::vector<unsigned char>& v) : value(v) { ; }
@@ -333,11 +364,11 @@ public:
STVariableLength(const char *n) : SerializedType(n) { ; }
STVariableLength(SerializerIterator&, const char *name = NULL);
STVariableLength() { ; }
static STVariableLength* construct(SerializerIterator&, const char *name = NULL);
static std::auto_ptr<SerializedType> deserialize(SerializerIterator& sit, const char *name)
{ return std::auto_ptr<SerializedType>(construct(sit, name)); }
int getLength() const;
virtual SerializedTypeID getSType() const { return STI_VL; }
virtual STVariableLength* duplicate() const { return new STVariableLength(name, value); }
virtual std::string getText() const;
void add(Serializer& s) const { s.addVL(value); }
@@ -353,16 +384,20 @@ public:
class STAccount : public STVariableLength
{
protected:
virtual STAccount* duplicate() const { return new STAccount(name, value); }
static STAccount* construct(SerializerIterator&, const char *name = NULL);
public:
STAccount(const std::vector<unsigned char>& v) : STVariableLength(v) { ; }
STAccount(const char *n, const std::vector<unsigned char>& v) : STVariableLength(n, v) { ; }
STAccount(const char *n) : STVariableLength(n) { ; }
STAccount() { ; }
static STAccount* construct(SerializerIterator&, const char *name = NULL);
static std::auto_ptr<SerializedType> deserialize(SerializerIterator& sit, const char *name)
{ return std::auto_ptr<SerializedType>(construct(sit, name)); }
SerializedTypeID getSType() const { return STI_ACCOUNT; }
virtual STAccount* duplicate() const { return new STAccount(name, value); }
std::string getText() const;
NewcoinAddress getValueNCA() const;
@@ -378,17 +413,20 @@ class STTaggedList : public SerializedType
protected:
std::vector<TaggedListItem> value;
STTaggedList* duplicate() const { return new STTaggedList(name, value); }
static STTaggedList* construct(SerializerIterator&, const char *name = NULL);
public:
STTaggedList() { ; }
STTaggedList(const char *n) : SerializedType(n) { ; }
STTaggedList(const std::vector<TaggedListItem>& v) : value(v) { ; }
STTaggedList(const char *n, const std::vector<TaggedListItem>& v) : SerializedType(n), value(v) { ; }
static STTaggedList* construct(SerializerIterator&, const char *name = NULL);
static std::auto_ptr<SerializedType> deserialize(SerializerIterator& sit, const char *name)
{ return std::auto_ptr<SerializedType>(construct(sit, name)); }
int getLength() const;
SerializedTypeID getSType() const { return STI_TL; }
STTaggedList* duplicate() const { return new STTaggedList(name, value); }
std::string getText() const;
void add(Serializer& s) const { if(s.addTaggedList(value)<0) throw(0); }