From fbb0baaca04d2350cdbee806ee4ba73cb2a4d0f1 Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Wed, 26 Sep 2012 00:28:58 -0700 Subject: [PATCH] Cleanups. --- src/FieldNames.cpp | 14 ++++++++++++++ src/FieldNames.h | 2 ++ src/SerializedObject.cpp | 11 +++++++++-- src/SerializedObject.h | 3 ++- src/SerializedTypes.h | 2 ++ src/Serializer.h | 2 ++ 6 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/FieldNames.cpp b/src/FieldNames.cpp index 14827bf7f..57f823de3 100644 --- a/src/FieldNames.cpp +++ b/src/FieldNames.cpp @@ -49,6 +49,20 @@ SField::ref SField::getField(int code); return *(new SField(code, static_cast(type), field, NULL)); } +int SField::compare(SField::ref f1, SField::ref f2) +{ // -1 = f1 comes before f2, 0 = illegal combination, 1 = f1 comes after f2 + if ((f1.fieldCode <= 0) || (f2.fieldCode <= 0)) + return 0; + + if (f1.fieldCode < f2.fieldCode) + return -1; + + if (f2.fieldCode < f1.fieldCode) + return 1; + + return 0; +} + SField::ref SField::getField(int type, int value) { return getField(FIELD_CODE(type, value)); diff --git a/src/FieldNames.h b/src/FieldNames.h index 443232044..cd035f133 100644 --- a/src/FieldNames.h +++ b/src/FieldNames.h @@ -69,6 +69,8 @@ public: bool operator==(const SField& f) const { return fieldCode == f.fieldCode; } bool operator!=(const SField& f) const { return fieldCode != f.fieldCode; } + + static int compare(SField::ref f1, SField::ref f2); }; extern SField sfInvalid, sfGeneric; diff --git a/src/SerializedObject.cpp b/src/SerializedObject.cpp index 5ec0c8ad4..c5713494d 100644 --- a/src/SerializedObject.cpp +++ b/src/SerializedObject.cpp @@ -192,9 +192,9 @@ std::string STObject::getFullText() const { std::string ret; bool first = true; - if (name != NULL) + if (fName->hasName()) { - ret = name; + ret = fName->getName(); ret += " = {"; } else ret = "{"; @@ -213,6 +213,13 @@ std::string STObject::getFullText() const void STObject::add(Serializer& s) const { + addFieldID(s); + addRaw(s); + s.addFieldID(STI_OBJECT, 1); +} + +void STObject::addRaw(Serializer& s) const +{ // FIXME: need to add in sorted order BOOST_FOREACH(const SerializedType& it, mData) it.add(s); } diff --git a/src/SerializedObject.h b/src/SerializedObject.h index 47e0d9639..3242802fe 100644 --- a/src/SerializedObject.h +++ b/src/SerializedObject.h @@ -56,7 +56,8 @@ public: virtual SerializedTypeID getSType() const { return STI_OBJECT; } virtual bool isEquivalent(const SerializedType& t) const; - void add(Serializer& s) const; + void add(Serializer& s) const; // with start/end of object + virtual void addRaw(Serializer& s) const; // just inner elements Serializer getSerializer() const { Serializer s; add(s); return s; } std::string getFullText() const; std::string getText() const; diff --git a/src/SerializedTypes.h b/src/SerializedTypes.h index d9bc627ab..64dae0d73 100644 --- a/src/SerializedTypes.h +++ b/src/SerializedTypes.h @@ -66,6 +66,8 @@ public: virtual bool isEquivalent(const SerializedType& t) const { assert(getSType() == STI_NOTPRESENT); return t.getSType() == STI_NOTPRESENT; } + void addFieldID(Serializer& s) const { s.addFieldID(fName->fieldType, fName->fieldValue); } + SerializedType& operator=(const SerializedType& t) { if (!fName->fieldCode) fName = t.fName; return *this; } bool operator==(const SerializedType& t) const diff --git a/src/Serializer.h b/src/Serializer.h index 30dfe1ceb..ec762e52c 100644 --- a/src/Serializer.h +++ b/src/Serializer.h @@ -9,6 +9,7 @@ #include "key.h" #include "uint256.h" +#include "FieldNames.h" typedef std::pair > TaggedListItem; @@ -69,6 +70,7 @@ public: bool getFieldID(int& type, int& name, int offset) const; int addFieldID(int type, int name); + int addFieldID(SerializedTypeID type, int name); // normal hash functions uint160 getRIPEMD160(int size=-1) const;