From b6653732b0f84c120a45fff01932d5c4460a6de1 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Thu, 27 Sep 2012 15:43:29 -0700 Subject: [PATCH] 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. --- src/Amount.cpp | 2 +- src/SerializedObject.cpp | 94 +++++++++++++++++++++++++++++++++++++--- src/SerializedObject.h | 61 +++++++++++++------------- src/SerializedTypes.cpp | 4 +- src/SerializedTypes.h | 25 +++++------ src/Serializer.h | 2 +- 6 files changed, 132 insertions(+), 56 deletions(-) diff --git a/src/Amount.cpp b/src/Amount.cpp index 8bd2d96a02..bfcad54d37 100644 --- a/src/Amount.cpp +++ b/src/Amount.cpp @@ -275,7 +275,7 @@ void STAmount::canonicalize() assert((mValue != 0) || (mOffset != -100) ); } -void STAmount::addData(Serializer& s) const +void STAmount::add(Serializer& s) const { if (mIsNative) { diff --git a/src/SerializedObject.cpp b/src/SerializedObject.cpp index 18f20388b7..25219648d3 100644 --- a/src/SerializedObject.cpp +++ b/src/SerializedObject.cpp @@ -186,6 +186,14 @@ bool STObject::set(SerializerIterator& sit, int depth) return false; } +std::auto_ptr STObject::deserialize(SerializerIterator& sit, SField::ref name) +{ + STObject *o; + std::auto_ptr object(o = new STObject(name)); + o->set(sit, 1); + return object; +} + std::string STObject::getFullText() const { std::string ret; @@ -211,15 +219,28 @@ std::string STObject::getFullText() const void STObject::add(Serializer& s) const { - addFieldID(s); - addData(s); - s.addFieldID(STI_OBJECT, 1); -} + std::map fields; -void STObject::addData(Serializer& s) const -{ // FIXME: need to add in sorted order 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 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(field)) + s.addFieldID(STI_OBJECT, 1); + else if (dynamic_cast(field)) + s.addFieldID(STI_ARRAY, 1); + } } std::string STObject::getText() const @@ -653,6 +674,65 @@ Json::Value STVector256::getJson(int options) const 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(&object)) + s.addFieldID(STI_OBJECT, 1); + else if (dynamic_cast(&object)) + s.addFieldID(STI_ARRAY, 1); + } +} + +bool STArray::isEquivalent(const SerializedType& t) const +{ + const STArray* v = dynamic_cast(&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 static SOElement testSOElements[2][16] = diff --git a/src/SerializedObject.h b/src/SerializedObject.h index f037f0446d..8c696b6b7f 100644 --- a/src/SerializedObject.h +++ b/src/SerializedObject.h @@ -28,7 +28,6 @@ protected: std::vector mType; STObject* duplicate() const { return new STObject(*this); } - static STObject* construct(SerializerIterator&, SField::ref); public: STObject() { ; } @@ -43,8 +42,7 @@ public: virtual ~STObject() { ; } - static std::auto_ptr deserialize(SerializerIterator& sit, SField::ref name) - { return std::auto_ptr(construct(sit, name)); } + static std::auto_ptr deserialize(SerializerIterator& sit, SField::ref name); void setType(SOElement::ptrList); bool isValidForType(); @@ -56,8 +54,7 @@ public: virtual SerializedTypeID getSType() const { return STI_OBJECT; } virtual bool isEquivalent(const SerializedType& t) const; - void add(Serializer& s) const; // with start/end of object - virtual void addData(Serializer& s) const; // just inner elements + virtual void add(Serializer& s) const; // just inner elements Serializer getSerializer() const { Serializer s; add(s); return s; } std::string getFullText() const; std::string getText() const; @@ -140,12 +137,12 @@ public: class STArray : public SerializedType { public: - typedef std::vector vector; - typedef std::vector::iterator iterator; - typedef std::vector::const_iterator const_iterator; - typedef std::vector::reverse_iterator reverse_iterator; - typedef std::vector::const_reverse_iterator const_reverse_iterator; - typedef std::vector::size_type size_type; + typedef std::vector vector; + typedef std::vector::iterator iterator; + typedef std::vector::const_iterator const_iterator; + typedef std::vector::reverse_iterator reverse_iterator; + typedef std::vector::const_reverse_iterator const_reverse_iterator; + typedef std::vector::size_type size_type; protected: @@ -164,36 +161,36 @@ public: static std::auto_ptr deserialize(SerializerIterator& sit, SField::ref name) { return std::auto_ptr(construct(sit, name)); } - const vector& getValue() const { return value; } - vector& getValue() { return value; } + const vector& getValue() const { return value; } + vector& getValue() { return value; } // vector-like functions - void push_back(const STObject& object) { value.push_back(object); } - STObject& operator[](int j) { return value[j]; } - const STObject& operator[](int j) const { return value[j]; } - iterator begin() { return value.begin(); } - const_iterator begin() const { return value.begin(); } - iterator end() { return value.end(); } - const_iterator end() const { return value.end(); } - size_type size() const { return value.size(); } - reverse_iterator rbegin() { return value.rbegin(); } - const_reverse_iterator rbegin() const { return value.rbegin(); } - reverse_iterator rend() { return value.rend(); } - const_reverse_iterator rend() const { return value.rend(); } - iterator erase(iterator pos) { return value.erase(pos); } - void pop_back() { value.pop_back(); } - bool empty() const { return value.empty(); } - void clear() { value.clear(); } + void push_back(const SerializedType& object) { value.push_back(object); } + SerializedType& operator[](int j) { return value[j]; } + const SerializedType& operator[](int j) const { return value[j]; } + iterator begin() { return value.begin(); } + const_iterator begin() const { return value.begin(); } + iterator end() { return value.end(); } + const_iterator end() const { return value.end(); } + size_type size() const { return value.size(); } + reverse_iterator rbegin() { return value.rbegin(); } + const_reverse_iterator rbegin() const { return value.rbegin(); } + reverse_iterator rend() { return value.rend(); } + const_reverse_iterator rend() const { return value.rend(); } + iterator erase(iterator pos) { return value.erase(pos); } + void pop_back() { value.pop_back(); } + bool empty() const { return value.empty(); } + void clear() { value.clear(); } virtual std::string getFullText() const; virtual std::string getText() const; virtual Json::Value getJson(int) 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; }; diff --git a/src/SerializedTypes.cpp b/src/SerializedTypes.cpp index 4e7ed6ec56..6eeb0c9120 100644 --- a/src/SerializedTypes.cpp +++ b/src/SerializedTypes.cpp @@ -202,7 +202,7 @@ STVector256* STVector256::construct(SerializerIterator& u, SField::ref name) 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)); } @@ -427,7 +427,7 @@ std::string STPathSet::getText() const } #endif -void STPathSet::addData(Serializer& s) const +void STPathSet::add(Serializer& s) const { bool bFirst = true; diff --git a/src/SerializedTypes.h b/src/SerializedTypes.h index e30a2f783c..5748f960ec 100644 --- a/src/SerializedTypes.h +++ b/src/SerializedTypes.h @@ -61,8 +61,7 @@ public: virtual Json::Value getJson(int) const { return getText(); } - virtual void add(Serializer& s) const { addFieldID(s); addData(s); } - virtual void addData(Serializer& s) const { ; } + virtual void add(Serializer& s) const { ; } virtual bool isEquivalent(const SerializedType& t) const { assert(getSType() == STI_NOTPRESENT); return t.getSType() == STI_NOTPRESENT; } @@ -98,7 +97,7 @@ public: SerializedTypeID getSType() const { return STI_UINT8; } 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; } void setValue(unsigned char v) { value = v; } @@ -124,7 +123,7 @@ public: SerializedTypeID getSType() const { return STI_UINT16; } 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; } void setValue(uint16 v) { value=v; } @@ -150,7 +149,7 @@ public: SerializedTypeID getSType() const { return STI_UINT32; } 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; } void setValue(uint32 v) { value=v; } @@ -176,7 +175,7 @@ public: SerializedTypeID getSType() const { return STI_UINT64; } 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; } void setValue(uint64 v) { value=v; } @@ -268,7 +267,7 @@ public: std::string getText() const; std::string getRaw() const; std::string getFullText() const; - void addData(Serializer& s) const; + void add(Serializer& s) const; int getExponent() const { return mOffset; } uint64 getMantissa() const { return mValue; } @@ -392,7 +391,7 @@ public: SerializedTypeID getSType() const { return STI_HASH128; } 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; } void setValue(const uint128& v) { value=v; } @@ -420,7 +419,7 @@ public: SerializedTypeID getSType() const { return STI_HASH160; } 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; } void setValue(const uint160& v) { value=v; } @@ -448,7 +447,7 @@ public: SerializedTypeID getSType() const { return STI_HASH256; } 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; } void setValue(const uint256& v) { value=v; } @@ -477,7 +476,7 @@ public: virtual SerializedTypeID getSType() const { return STI_VL; } virtual std::string getText() const; - void addData(Serializer& s) const { s.addVL(value); } + void add(Serializer& s) const { s.addVL(value); } const std::vector& peekValue() const { return value; } std::vector& peekValue() { return value; } @@ -642,7 +641,7 @@ public: { return std::auto_ptr(construct(sit, name)); } // std::string getText() const; - void addData(Serializer& s) const; + void add(Serializer& s) const; virtual Json::Value getJson(int) const; SerializedTypeID getSType() const { return STI_PATHSET; } @@ -711,7 +710,7 @@ public: STVector256(const std::vector& vector) : mValue(vector) { ; } SerializedTypeID getSType() const { return STI_VECTOR256; } - void addData(Serializer& s) const; + void add(Serializer& s) const; static std::auto_ptr deserialize(SerializerIterator& sit, SField::ref name) { return std::auto_ptr(construct(sit, name)); } diff --git a/src/Serializer.h b/src/Serializer.h index ec762e52c7..8666efe4c3 100644 --- a/src/Serializer.h +++ b/src/Serializer.h @@ -70,7 +70,7 @@ public: bool getFieldID(int& type, int& name, int offset) const; int addFieldID(int type, int name); - int addFieldID(SerializedTypeID type, int name); + int addFieldID(SerializedTypeID type, int name) { return addFieldID(static_cast(type), name); } // normal hash functions uint160 getRIPEMD160(int size=-1) const;