Split up ripple_SerializedObject.h

This commit is contained in:
Vinnie Falco
2013-06-07 14:39:35 -07:00
parent ef232f4359
commit 6d8ee90a8d
12 changed files with 237 additions and 139 deletions

View File

@@ -23,14 +23,7 @@ enum SerializedTypeID
STI_VALIDATION = 10003,
};
enum SOE_Flags
{
SOE_INVALID = -1,
SOE_REQUIRED = 0, // required
SOE_OPTIONAL = 1, // optional, may be present with default value
SOE_DEFAULT = 2, // optional, if present, must not have default value
};
// VFALCO: TODO, rename this to NamedField
class SField
{
public:

View File

@@ -117,22 +117,6 @@ UPTR_T<SerializedType> STObject::makeDeserializedObject(SerializedTypeID id, SFi
}
}
void SOTemplate::push_back(const SOElement &r)
{
if (mIndex.empty())
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));
}
int SOTemplate::getIndex(SField::ref f) const
{
assert(f.getNum() < mIndex.size());
return mIndex[f.getNum()];
}
void STObject::set(const SOTemplate& type)
{
mData.clear();

View File

@@ -4,41 +4,17 @@
DEFINE_INSTANCE(SerializedObject);
DEFINE_INSTANCE(SerializedArray);
// Serializable object/array types
class SOElement
{ // An element in the description of a serialized object
public:
SField::ref e_field;
const SOE_Flags flags;
SOElement(SField::ref fi, SOE_Flags fl) : e_field(fi), flags(fl) { ; }
};
class SOTemplate
{
public:
SOTemplate() { ; }
const std::vector<const SOElement*>& peek() const { return mTypes; }
void push_back(const SOElement& r);
int getIndex(SField::ref) const;
private:
std::vector<const SOElement*> mTypes;
std::vector<int> mIndex; // field num -> index
};
class STObject : public SerializedType, private IS_INSTANCE(SerializedObject)
{
public:
STObject() : mType(NULL) { ; }
STObject(SField::ref name) : SerializedType(name), mType(NULL) { ; }
explicit STObject(SField::ref name) : SerializedType(name), mType(NULL) { ; }
STObject(const SOTemplate& type, SField::ref name) : SerializedType(name)
STObject (const SOTemplate& type, SField::ref name) : SerializedType(name)
{ set(type); }
STObject(const SOTemplate& type, SerializerIterator& sit, SField::ref name) : SerializedType(name)
STObject (const SOTemplate& type, SerializerIterator& sit, SField::ref name) : SerializedType(name)
{ set(sit); setType(type); }
UPTR_T<STObject> oClone() const { return UPTR_T<STObject>(new STObject(*this)); }
@@ -184,7 +160,7 @@ namespace boost
template<> struct range_const_iterator<STObject> { typedef STObject::const_iterator type; };
}
//------------------------------------------------------------------------------
class STArray : public SerializedType, private IS_INSTANCE(SerializedArray)
{
@@ -197,12 +173,12 @@ public:
typedef boost::ptr_vector<STObject>::size_type size_type;
public:
STArray() { ; }
STArray(int n) { value.reserve(n); }
STArray(SField::ref f) : SerializedType(f) { ; }
STArray () { ; }
explicit STArray(int n) { value.reserve(n); }
explicit STArray(SField::ref f) : SerializedType(f) { ; }
STArray(SField::ref f, int n) : SerializedType(f) { value.reserve(n); }
STArray(SField::ref f, const vector& v) : SerializedType(f), value(v) { ; }
STArray(vector& v) : value(v) { ; }
explicit STArray(vector& v) : value(v) { ; }
static UPTR_T<SerializedType> deserialize(SerializerIterator& sit, SField::ref name)
{ return UPTR_T<SerializedType>(construct(sit, name)); }
@@ -210,7 +186,9 @@ public:
const vector& getValue() const { return value; }
vector& getValue() { return value; }
// vector-like functions
// VFALCO: NOTE as long as we're married to boost why not use boost::iterator_facade?
//
// vector-like functions
void push_back(const STObject& object) { value.push_back(object.oClone().release()); }
STObject& operator[](int j) { return value[j]; }
const STObject& operator[](int j) const { return value[j]; }

View File

@@ -0,0 +1,42 @@
SOTemplate::SOTemplate ()
{
}
void SOTemplate::push_back (SOElement const& r)
{
// Ensure there is the enough space in the index mapping
// table for all possible fields.
//
if (mIndex.empty())
{
// Unmapped indices will be set to -1
//
mIndex.resize (SField::getNumFields() + 1, -1);
}
// Make sure the field's index is in range
//
assert (r.e_field.getNum() < mIndex.size());
// Make sure that this field hasn't already been assigned
//
assert(getIndex(r.e_field) == -1);
// Add the field to the index mapping table
//
mIndex [r.e_field.getNum ()] = mTypes.size();
// Append the new element.
//
mTypes.push_back (new SOElement (r));
}
int SOTemplate::getIndex (SField::ref f) const
{
// The mapping table should be large enough for any possible field
//
assert (f.getNum() < mIndex.size());
return mIndex[f.getNum()];
}

View File

@@ -0,0 +1,71 @@
#ifndef RIPPLE_SERIALIZEDOBJECTTEMPLATE_H
#define RIPPLE_SERIALIZEDOBJECTTEMPLATE_H
//------------------------------------------------------------------------------
/** Flags for elements in a SerializedObjectTemplate.
*/
// VFALCO: NOTE, these don't look like bit-flags...
enum SOE_Flags
{
SOE_INVALID = -1,
SOE_REQUIRED = 0, // required
SOE_OPTIONAL = 1, // optional, may be present with default value
SOE_DEFAULT = 2, // optional, if present, must not have default value
};
//------------------------------------------------------------------------------
/** An element in a SerializedObjectTemplate.
*/
class SOElement
{
public:
SField::ref const e_field;
SOE_Flags const flags;
SOElement (SField::ref fieldName, SOE_Flags flags)
: e_field (fieldName)
, flags (flags)
{
}
};
//------------------------------------------------------------------------------
/** Defines the fields and their attributes within a SerializedObject.
Each subclass of SerializedObject will provide its own template
describing the available fields and their metadata attributes.
*/
class SOTemplate
{
public:
/** Create an empty template.
After creating the template, call @ref push_back with the
desired fields.
@see push_back
*/
SOTemplate ();
std::vector <SOElement const*> const& peek() const
{
return mTypes;
}
/** Add an element to the template.
*/
void push_back (SOElement const& r);
/** Retrieve the position of a named field.
*/
int getIndex (SField::ref) const;
private:
std::vector<SOElement const*> mTypes;
std::vector <int> mIndex; // field num -> index
};
#endif

View File

@@ -82,6 +82,7 @@
#include "protocol/ripple_RippleAddress.cpp"
#include "protocol/ripple_SerializedTypes.cpp"
#include "protocol/ripple_Serializer.cpp"
#include "protocol/ripple_SerializedObjectTemplate.cpp"
#include "protocol/ripple_SerializedObject.cpp"
#include "protocol/ripple_TER.cpp"
#include "protocol/ripple_TransactionFormat.cpp"

View File

@@ -72,6 +72,7 @@
#include "protocol/ripple_Serializer.h" // needs CKey
#include "protocol/ripple_TER.h"
#include "protocol/ripple_SerializedTypes.h" // needs Serializer, TER
#include "protocol/ripple_SerializedObjectTemplate.h"
#include "protocol/ripple_SerializedObject.h"
#include "protocol/ripple_LedgerFormat.h" // needs SOTemplate from SerializedObject
#include "protocol/ripple_TransactionFormat.h"