New serializer work.

Rework add/addRaw/addData per discussion with Arthur.
New STObject add/deserialize code.
STArray add/deserialize code.
Allow arrays of normal fields.
Other small fixes.
This commit is contained in:
JoelKatz
2012-09-27 15:43:29 -07:00
parent 5d0adc449b
commit b6653732b0
6 changed files with 132 additions and 56 deletions

View File

@@ -275,7 +275,7 @@ void STAmount::canonicalize()
assert((mValue != 0) || (mOffset != -100) ); assert((mValue != 0) || (mOffset != -100) );
} }
void STAmount::addData(Serializer& s) const void STAmount::add(Serializer& s) const
{ {
if (mIsNative) if (mIsNative)
{ {

View File

@@ -186,6 +186,14 @@ bool STObject::set(SerializerIterator& sit, int depth)
return false; return false;
} }
std::auto_ptr<SerializedType> STObject::deserialize(SerializerIterator& sit, SField::ref name)
{
STObject *o;
std::auto_ptr<SerializedType> object(o = new STObject(name));
o->set(sit, 1);
return object;
}
std::string STObject::getFullText() const std::string STObject::getFullText() const
{ {
std::string ret; std::string ret;
@@ -211,15 +219,28 @@ std::string STObject::getFullText() const
void STObject::add(Serializer& s) const void STObject::add(Serializer& s) const
{ {
addFieldID(s); std::map<int, const SerializedType*> fields;
addData(s);
s.addFieldID(STI_OBJECT, 1);
}
void STObject::addData(Serializer& s) const
{ // FIXME: need to add in sorted order
BOOST_FOREACH(const SerializedType& it, mData) BOOST_FOREACH(const SerializedType& it, mData)
it.add(s); { // pick out the fields and sort them
if (it.getSType() != STI_NOTPRESENT)
fields.insert(std::make_pair(it.getFName().fieldCode, &it));
}
typedef std::pair<const int, const SerializedType*> field_iterator;
BOOST_FOREACH(field_iterator& it, fields)
{ // insert them in sorted order
const SerializedType* field = it.second;
field->addFieldID(s);
field->add(s);
if (dynamic_cast<const STObject*>(field))
s.addFieldID(STI_OBJECT, 1);
else if (dynamic_cast<const STArray*>(field))
s.addFieldID(STI_ARRAY, 1);
}
} }
std::string STObject::getText() const std::string STObject::getText() const
@@ -653,6 +674,65 @@ Json::Value STVector256::getJson(int options) const
return ret; return ret;
} }
std::string STArray::getFullText() const
{
return "WRITEME";
}
std::string STArray::getText() const
{
return "WRITEME";
}
Json::Value STArray::getJson(int) const
{
return Json::Value("WRITEME");
}
void STArray::add(Serializer& s) const
{
BOOST_FOREACH(const SerializedType& object, value)
{
object.addFieldID(s);
object.add(s);
if (dynamic_cast<const STObject*>(&object))
s.addFieldID(STI_OBJECT, 1);
else if (dynamic_cast<const STArray*>(&object))
s.addFieldID(STI_ARRAY, 1);
}
}
bool STArray::isEquivalent(const SerializedType& t) const
{
const STArray* v = dynamic_cast<const STArray*>(&t);
if (!v)
return false;
return value == v->value;
}
STArray* STArray::construct(SerializerIterator& sit, SField::ref field)
{
vector value;
while (!sit.empty())
{
int type, field;
sit.getFieldID(type, field);
if ((type == STI_ARRAY) && (field == 1))
break;
SField::ref fn = SField::getField(type, field);
if (fn.isInvalid())
throw std::runtime_error("Unknown field");
value.push_back(*STObject::makeDeserializedObject(fn.fieldType, fn, sit, 1));
}
return new STArray(field, value);
}
#if 0 #if 0
static SOElement testSOElements[2][16] = static SOElement testSOElements[2][16] =

View File

@@ -28,7 +28,6 @@ protected:
std::vector<SOElement::ptr> mType; std::vector<SOElement::ptr> mType;
STObject* duplicate() const { return new STObject(*this); } STObject* duplicate() const { return new STObject(*this); }
static STObject* construct(SerializerIterator&, SField::ref);
public: public:
STObject() { ; } STObject() { ; }
@@ -43,8 +42,7 @@ public:
virtual ~STObject() { ; } virtual ~STObject() { ; }
static std::auto_ptr<SerializedType> deserialize(SerializerIterator& sit, SField::ref name) static std::auto_ptr<SerializedType> deserialize(SerializerIterator& sit, SField::ref name);
{ return std::auto_ptr<SerializedType>(construct(sit, name)); }
void setType(SOElement::ptrList); void setType(SOElement::ptrList);
bool isValidForType(); bool isValidForType();
@@ -56,8 +54,7 @@ public:
virtual SerializedTypeID getSType() const { return STI_OBJECT; } virtual SerializedTypeID getSType() const { return STI_OBJECT; }
virtual bool isEquivalent(const SerializedType& t) const; virtual bool isEquivalent(const SerializedType& t) const;
void add(Serializer& s) const; // with start/end of object virtual void add(Serializer& s) const; // just inner elements
virtual void addData(Serializer& s) const; // just inner elements
Serializer getSerializer() const { Serializer s; add(s); return s; } Serializer getSerializer() const { Serializer s; add(s); return s; }
std::string getFullText() const; std::string getFullText() const;
std::string getText() const; std::string getText() const;
@@ -140,12 +137,12 @@ public:
class STArray : public SerializedType class STArray : public SerializedType
{ {
public: public:
typedef std::vector<STObject> vector; typedef std::vector<SerializedType> vector;
typedef std::vector<STObject>::iterator iterator; typedef std::vector<SerializedType>::iterator iterator;
typedef std::vector<STObject>::const_iterator const_iterator; typedef std::vector<SerializedType>::const_iterator const_iterator;
typedef std::vector<STObject>::reverse_iterator reverse_iterator; typedef std::vector<SerializedType>::reverse_iterator reverse_iterator;
typedef std::vector<STObject>::const_reverse_iterator const_reverse_iterator; typedef std::vector<SerializedType>::const_reverse_iterator const_reverse_iterator;
typedef std::vector<STObject>::size_type size_type; typedef std::vector<SerializedType>::size_type size_type;
protected: protected:
@@ -164,36 +161,36 @@ public:
static std::auto_ptr<SerializedType> deserialize(SerializerIterator& sit, SField::ref name) static std::auto_ptr<SerializedType> deserialize(SerializerIterator& sit, SField::ref name)
{ return std::auto_ptr<SerializedType>(construct(sit, name)); } { return std::auto_ptr<SerializedType>(construct(sit, name)); }
const vector& getValue() const { return value; } const vector& getValue() const { return value; }
vector& getValue() { return value; } vector& getValue() { return value; }
// vector-like functions // vector-like functions
void push_back(const STObject& object) { value.push_back(object); } void push_back(const SerializedType& object) { value.push_back(object); }
STObject& operator[](int j) { return value[j]; } SerializedType& operator[](int j) { return value[j]; }
const STObject& operator[](int j) const { return value[j]; } const SerializedType& operator[](int j) const { return value[j]; }
iterator begin() { return value.begin(); } iterator begin() { return value.begin(); }
const_iterator begin() const { return value.begin(); } const_iterator begin() const { return value.begin(); }
iterator end() { return value.end(); } iterator end() { return value.end(); }
const_iterator end() const { return value.end(); } const_iterator end() const { return value.end(); }
size_type size() const { return value.size(); } size_type size() const { return value.size(); }
reverse_iterator rbegin() { return value.rbegin(); } reverse_iterator rbegin() { return value.rbegin(); }
const_reverse_iterator rbegin() const { return value.rbegin(); } const_reverse_iterator rbegin() const { return value.rbegin(); }
reverse_iterator rend() { return value.rend(); } reverse_iterator rend() { return value.rend(); }
const_reverse_iterator rend() const { return value.rend(); } const_reverse_iterator rend() const { return value.rend(); }
iterator erase(iterator pos) { return value.erase(pos); } iterator erase(iterator pos) { return value.erase(pos); }
void pop_back() { value.pop_back(); } void pop_back() { value.pop_back(); }
bool empty() const { return value.empty(); } bool empty() const { return value.empty(); }
void clear() { value.clear(); } void clear() { value.clear(); }
virtual std::string getFullText() const; virtual std::string getFullText() const;
virtual std::string getText() const; virtual std::string getText() const;
virtual Json::Value getJson(int) const; virtual Json::Value getJson(int) const;
virtual void add(Serializer& s) const; virtual void add(Serializer& s) const;
bool operator==(const STArray &s) { return value == s.value; } bool operator==(const STArray &s) { return value == s.value; }
bool operator!=(const STArray &s) { return value != s.value; } bool operator!=(const STArray &s) { return value != s.value; }
virtual SerializedTypeID getSType() const { return STI_ARRAY; } virtual SerializedTypeID getSType() const { return STI_ARRAY; }
virtual bool isEquivalent(const SerializedType& t) const; virtual bool isEquivalent(const SerializedType& t) const;
}; };

View File

@@ -202,7 +202,7 @@ STVector256* STVector256::construct(SerializerIterator& u, SField::ref name)
return new STVector256(name, value); return new STVector256(name, value);
} }
void STVector256::addData(Serializer& s) const void STVector256::add(Serializer& s) const
{ {
s.addVL(mValue.empty() ? NULL : mValue[0].begin(), mValue.size() * (256 / 8)); s.addVL(mValue.empty() ? NULL : mValue[0].begin(), mValue.size() * (256 / 8));
} }
@@ -427,7 +427,7 @@ std::string STPathSet::getText() const
} }
#endif #endif
void STPathSet::addData(Serializer& s) const void STPathSet::add(Serializer& s) const
{ {
bool bFirst = true; bool bFirst = true;

View File

@@ -61,8 +61,7 @@ public:
virtual Json::Value getJson(int) const virtual Json::Value getJson(int) const
{ return getText(); } { return getText(); }
virtual void add(Serializer& s) const { addFieldID(s); addData(s); } virtual void add(Serializer& s) const { ; }
virtual void addData(Serializer& s) const { ; }
virtual bool isEquivalent(const SerializedType& t) const virtual bool isEquivalent(const SerializedType& t) const
{ assert(getSType() == STI_NOTPRESENT); return t.getSType() == STI_NOTPRESENT; } { assert(getSType() == STI_NOTPRESENT); return t.getSType() == STI_NOTPRESENT; }
@@ -98,7 +97,7 @@ public:
SerializedTypeID getSType() const { return STI_UINT8; } SerializedTypeID getSType() const { return STI_UINT8; }
std::string getText() const; std::string getText() const;
void addData(Serializer& s) const { s.add8(value); } void add(Serializer& s) const { s.add8(value); }
unsigned char getValue() const { return value; } unsigned char getValue() const { return value; }
void setValue(unsigned char v) { value = v; } void setValue(unsigned char v) { value = v; }
@@ -124,7 +123,7 @@ public:
SerializedTypeID getSType() const { return STI_UINT16; } SerializedTypeID getSType() const { return STI_UINT16; }
std::string getText() const; std::string getText() const;
void addData(Serializer& s) const { s.add16(value); } void add(Serializer& s) const { s.add16(value); }
uint16 getValue() const { return value; } uint16 getValue() const { return value; }
void setValue(uint16 v) { value=v; } void setValue(uint16 v) { value=v; }
@@ -150,7 +149,7 @@ public:
SerializedTypeID getSType() const { return STI_UINT32; } SerializedTypeID getSType() const { return STI_UINT32; }
std::string getText() const; std::string getText() const;
void addData(Serializer& s) const { s.add32(value); } void add(Serializer& s) const { s.add32(value); }
uint32 getValue() const { return value; } uint32 getValue() const { return value; }
void setValue(uint32 v) { value=v; } void setValue(uint32 v) { value=v; }
@@ -176,7 +175,7 @@ public:
SerializedTypeID getSType() const { return STI_UINT64; } SerializedTypeID getSType() const { return STI_UINT64; }
std::string getText() const; std::string getText() const;
void addData(Serializer& s) const { s.add64(value); } void add(Serializer& s) const { s.add64(value); }
uint64 getValue() const { return value; } uint64 getValue() const { return value; }
void setValue(uint64 v) { value=v; } void setValue(uint64 v) { value=v; }
@@ -268,7 +267,7 @@ public:
std::string getText() const; std::string getText() const;
std::string getRaw() const; std::string getRaw() const;
std::string getFullText() const; std::string getFullText() const;
void addData(Serializer& s) const; void add(Serializer& s) const;
int getExponent() const { return mOffset; } int getExponent() const { return mOffset; }
uint64 getMantissa() const { return mValue; } uint64 getMantissa() const { return mValue; }
@@ -392,7 +391,7 @@ public:
SerializedTypeID getSType() const { return STI_HASH128; } SerializedTypeID getSType() const { return STI_HASH128; }
virtual std::string getText() const; virtual std::string getText() const;
void addData(Serializer& s) const { s.add128(value); } void add(Serializer& s) const { s.add128(value); }
const uint128& getValue() const { return value; } const uint128& getValue() const { return value; }
void setValue(const uint128& v) { value=v; } void setValue(const uint128& v) { value=v; }
@@ -420,7 +419,7 @@ public:
SerializedTypeID getSType() const { return STI_HASH160; } SerializedTypeID getSType() const { return STI_HASH160; }
virtual std::string getText() const; virtual std::string getText() const;
void addData(Serializer& s) const { s.add160(value); } void add(Serializer& s) const { s.add160(value); }
const uint160& getValue() const { return value; } const uint160& getValue() const { return value; }
void setValue(const uint160& v) { value=v; } void setValue(const uint160& v) { value=v; }
@@ -448,7 +447,7 @@ public:
SerializedTypeID getSType() const { return STI_HASH256; } SerializedTypeID getSType() const { return STI_HASH256; }
std::string getText() const; std::string getText() const;
void addData(Serializer& s) const { s.add256(value); } void add(Serializer& s) const { s.add256(value); }
const uint256& getValue() const { return value; } const uint256& getValue() const { return value; }
void setValue(const uint256& v) { value=v; } void setValue(const uint256& v) { value=v; }
@@ -477,7 +476,7 @@ public:
virtual SerializedTypeID getSType() const { return STI_VL; } virtual SerializedTypeID getSType() const { return STI_VL; }
virtual std::string getText() const; virtual std::string getText() const;
void addData(Serializer& s) const { s.addVL(value); } void add(Serializer& s) const { s.addVL(value); }
const std::vector<unsigned char>& peekValue() const { return value; } const std::vector<unsigned char>& peekValue() const { return value; }
std::vector<unsigned char>& peekValue() { return value; } std::vector<unsigned char>& peekValue() { return value; }
@@ -642,7 +641,7 @@ public:
{ return std::auto_ptr<SerializedType>(construct(sit, name)); } { return std::auto_ptr<SerializedType>(construct(sit, name)); }
// std::string getText() const; // std::string getText() const;
void addData(Serializer& s) const; void add(Serializer& s) const;
virtual Json::Value getJson(int) const; virtual Json::Value getJson(int) const;
SerializedTypeID getSType() const { return STI_PATHSET; } SerializedTypeID getSType() const { return STI_PATHSET; }
@@ -711,7 +710,7 @@ public:
STVector256(const std::vector<uint256>& vector) : mValue(vector) { ; } STVector256(const std::vector<uint256>& vector) : mValue(vector) { ; }
SerializedTypeID getSType() const { return STI_VECTOR256; } SerializedTypeID getSType() const { return STI_VECTOR256; }
void addData(Serializer& s) const; void add(Serializer& s) const;
static std::auto_ptr<SerializedType> deserialize(SerializerIterator& sit, SField::ref name) static std::auto_ptr<SerializedType> deserialize(SerializerIterator& sit, SField::ref name)
{ return std::auto_ptr<SerializedType>(construct(sit, name)); } { return std::auto_ptr<SerializedType>(construct(sit, name)); }

View File

@@ -70,7 +70,7 @@ public:
bool getFieldID(int& type, int& name, int offset) const; bool getFieldID(int& type, int& name, int offset) const;
int addFieldID(int type, int name); int addFieldID(int type, int name);
int addFieldID(SerializedTypeID type, int name); int addFieldID(SerializedTypeID type, int name) { return addFieldID(static_cast<int>(type), name); }
// normal hash functions // normal hash functions
uint160 getRIPEMD160(int size=-1) const; uint160 getRIPEMD160(int size=-1) const;