A more sensible implementaiton SOTemplate::getIndex.

This commit is contained in:
JoelKatz
2013-03-02 22:16:52 -08:00
parent c6b2b5b819
commit 402f296c36
4 changed files with 20 additions and 6 deletions

View File

@@ -12,6 +12,7 @@
// These must stay at the top of this file // These must stay at the top of this file
std::map<int, SField::ptr> SField::codeToField; std::map<int, SField::ptr> SField::codeToField;
boost::mutex SField::mapMutex; boost::mutex SField::mapMutex;
int SField::num = 0;
SField sfInvalid(-1), sfGeneric(0); SField sfInvalid(-1), sfGeneric(0);
SField sfLedgerEntry(STI_LEDGERENTRY, 1, "LedgerEntry"); SField sfLedgerEntry(STI_LEDGERENTRY, 1, "LedgerEntry");

View File

@@ -51,6 +51,7 @@ public:
protected: protected:
static std::map<int, ptr> codeToField; static std::map<int, ptr> codeToField;
static boost::mutex mapMutex; static boost::mutex mapMutex;
static int num;
SField(SerializedTypeID id, int val); SField(SerializedTypeID id, int val);
@@ -61,6 +62,7 @@ public:
const int fieldValue; // Code number for protocol const int fieldValue; // Code number for protocol
std::string fieldName; std::string fieldName;
int fieldMeta; int fieldMeta;
int fieldNum;
bool signingField; bool signingField;
SField(int fc, SerializedTypeID tid, int fv, const char* fn) : SField(int fc, SerializedTypeID tid, int fv, const char* fn) :
@@ -68,6 +70,7 @@ public:
{ {
boost::mutex::scoped_lock sl(mapMutex); boost::mutex::scoped_lock sl(mapMutex);
codeToField[fieldCode] = this; codeToField[fieldCode] = this;
fieldNum = ++num;
} }
SField(SerializedTypeID tid, int fv, const char *fn) : SField(SerializedTypeID tid, int fv, const char *fn) :
@@ -76,9 +79,14 @@ public:
{ {
boost::mutex::scoped_lock sl(mapMutex); boost::mutex::scoped_lock sl(mapMutex);
codeToField[fieldCode] = this; codeToField[fieldCode] = this;
fieldNum = ++num;
} }
SField(int fc) : fieldCode(fc), fieldType(STI_UNKNOWN), fieldValue(0), fieldMeta(sMD_Never) { ; } SField(int fc) : fieldCode(fc), fieldType(STI_UNKNOWN), fieldValue(0), fieldMeta(sMD_Never)
{
boost::mutex::scoped_lock sl(mapMutex);
fieldNum = ++num;
}
~SField(); ~SField();
@@ -97,6 +105,8 @@ public:
bool isBinary() const { return fieldValue < 256; } bool isBinary() const { return fieldValue < 256; }
bool isDiscardable() const { return fieldValue > 256; } bool isDiscardable() const { return fieldValue > 256; }
int getCode() const { return fieldCode; } int getCode() const { return fieldCode; }
int getNum() const { return fieldNum; }
static int getNumFields() { return num; }
bool isSigningField() const { return signingField; } bool isSigningField() const { return signingField; }
void notSigningField() { signingField = false; } void notSigningField() { signingField = false; }

View File

@@ -132,15 +132,18 @@ std::auto_ptr<SerializedType> STObject::makeDeserializedObject(SerializedTypeID
void SOTemplate::push_back(const SOElement &r) void SOTemplate::push_back(const SOElement &r)
{ {
assert(mMap.find(r.e_field.getCode()) == mMap.end()); if (mIndex.empty())
mMap[r.e_field.getCode()] = mTypes.size(); mIndex.resize(SField::getNumFields() + 1, -1);
assert(r.e_field.getNum() < mIndex.size());
assert(getIndex(r.e_field) == -1);
mIndex[r.e_field.getNum()] = mTypes.size();
mTypes.push_back(new SOElement(r)); mTypes.push_back(new SOElement(r));
} }
int SOTemplate::getIndex(SField::ref f) const int SOTemplate::getIndex(SField::ref f) const
{ {
std::map<int, int>::const_iterator it = mMap.find(f.getCode()); assert(f.getNum() < mIndex.size());
return (it == mMap.end()) ? -1 : it->second; return mIndex[f.getNum()];
} }
void STObject::set(const SOTemplate& type) void STObject::set(const SOTemplate& type)

View File

@@ -28,7 +28,7 @@ class SOTemplate
{ {
protected: protected:
std::vector<const SOElement*> mTypes; std::vector<const SOElement*> mTypes;
std::map<int, int> mMap; // field code -> index std::vector<int> mIndex; // field num -> index
public: public:
SOTemplate() { ; } SOTemplate() { ; }