Refactor TxFormats and LedgerEntryFormat to use a common template.

This commit is contained in:
Vinnie Falco
2013-07-06 19:31:34 -07:00
parent 1e43c10dee
commit b426d62075
16 changed files with 300 additions and 296 deletions

View File

@@ -0,0 +1,171 @@
//------------------------------------------------------------------------------
/*
Copyright (c) 2011-2013, OpenCoin, Inc.
*/
//==============================================================================
#ifndef RIPPLE_KNOWNFORMATS_H_INCLUDED
#define RIPPLE_KNOWNFORMATS_H_INCLUDED
/** Manages a list of known formats.
Each format has a name, an associated KeyType (typically an enumeration),
and a predefined @ref SOElement.
@tparam KeyType The type of key identifying the format.
*/
template <class KeyType>
class KnownFormats
{
public:
/** A known format.
*/
class Item
{
public:
Item (char const* name, KeyType type)
: m_name (name)
, m_type (type)
{
}
Item& operator<< (SOElement const& el)
{
elements.push_back (el);
return *this;
}
/** Retrieve the name of the format.
*/
std::string const& getName () const noexcept
{
return m_name;
}
/** Retrieve the transaction type this format represents.
*/
KeyType getType () const noexcept
{
return m_type;
}
public:
// VFALCO TODO make an accessor for this
SOTemplate elements;
private:
std::string const m_name;
KeyType const m_type;
};
public:
/** Create the known formats object.
Derived classes will load the object will all the known formats.
*/
KnownFormats ()
{
}
/** Destroy the known formats object.
The defined formats are deleted.
*/
~KnownFormats ()
{
}
/** Retrieve the type for a format specified by name.
If the format name is unknown, an exception is thrown.
@param name The name of the type.
@return The type.
*/
KeyType findTypeByName (std::string const name) const
{
Item const* const result = findByName (name);
if (result != nullptr)
{
return result->getType ();
}
else
{
throw std::runtime_error ("Unknown format name");
}
}
/** Retrieve a format based on its type.
*/
// VFALCO TODO Can we just return the SOElement& ?
Item const* findByType (KeyType type) const noexcept
{
Item* result = nullptr;
TypeMap::const_iterator const iter = m_types.find (type);
if (iter != m_types.end ())
{
result = iter->second;
}
return result;
}
protected:
/** Retrieve a format based on its name.
*/
Item const* findByName (std::string const& name) const noexcept
{
Item* result = nullptr;
NameMap::const_iterator const iter = m_names.find (name);
if (iter != m_names.end ())
{
result = iter->second;
}
return result;
}
/** Add a new format.
The new format has the set of common fields already added.
@param name The name of this format.
@param type The type of this format.
@return The created format.
*/
Item& add (char const* name, KeyType type)
{
Item& item = *m_formats.add (new Item (name, type));
addCommonFields (item);
m_types [item.getType ()] = &item;
m_names [item.getName ()] = &item;
return item;
}
/** Adds common fields.
This is called for every new item.
*/
virtual void addCommonFields (Item& item) = 0;
private:
// VFALCO TODO use String instead of std::string
typedef std::map <std::string, Item*> NameMap;
typedef std::map <KeyType, Item*> TypeMap;
OwnedArray <Item> m_formats;
NameMap m_names;
TypeMap m_types;
};
#endif

View File

@@ -4,22 +4,10 @@
*/
//==============================================================================
std::map <int, LedgerEntryFormat*> LedgerEntryFormat::byType;
std::map <std::string, LedgerEntryFormat*> LedgerEntryFormat::byName;
#define LEF_BASE \
<< SOElement(sfLedgerIndex, SOE_OPTIONAL) \
<< SOElement(sfLedgerEntryType, SOE_REQUIRED) \
<< SOElement(sfFlags, SOE_REQUIRED)
#define DECLARE_LEF(name, type) lef = new LedgerEntryFormat(#name, type); (*lef) LEF_BASE
void LEFInit ()
LedgerFormats::LedgerFormats ()
: SharedSingleton <LedgerFormats> (SingletonLifetime::persistAfterCreation)
{
LedgerEntryFormat* lef;
DECLARE_LEF (AccountRoot, ltACCOUNT_ROOT)
add ("AccountRoot", ltACCOUNT_ROOT)
<< SOElement (sfAccount, SOE_REQUIRED)
<< SOElement (sfSequence, SOE_REQUIRED)
<< SOElement (sfBalance, SOE_REQUIRED)
@@ -35,7 +23,7 @@ void LEFInit ()
<< SOElement (sfDomain, SOE_OPTIONAL)
;
DECLARE_LEF (Contract, ltCONTRACT)
add ("Contract", ltCONTRACT)
<< SOElement (sfAccount, SOE_REQUIRED)
<< SOElement (sfBalance, SOE_REQUIRED)
<< SOElement (sfPreviousTxnID, SOE_REQUIRED)
@@ -50,7 +38,7 @@ void LEFInit ()
<< SOElement (sfExpireCode, SOE_OPTIONAL)
;
DECLARE_LEF (DirectoryNode, ltDIR_NODE)
add ("DirectoryNode", ltDIR_NODE)
<< SOElement (sfOwner, SOE_OPTIONAL) // for owner directories
<< SOElement (sfTakerPaysCurrency, SOE_OPTIONAL) // for order book directories
<< SOElement (sfTakerPaysIssuer, SOE_OPTIONAL) // for order book directories
@@ -63,16 +51,16 @@ void LEFInit ()
<< SOElement (sfIndexPrevious, SOE_OPTIONAL)
;
DECLARE_LEF (GeneratorMap, ltGENERATOR_MAP)
add ("GeneratorMap", ltGENERATOR_MAP)
<< SOElement (sfGenerator, SOE_REQUIRED)
;
DECLARE_LEF (Nickname, ltNICKNAME)
add ("Nickname", ltNICKNAME)
<< SOElement (sfAccount, SOE_REQUIRED)
<< SOElement (sfMinimumOffer, SOE_OPTIONAL)
;
DECLARE_LEF (Offer, ltOFFER)
add ("Offer", ltOFFER)
<< SOElement (sfAccount, SOE_REQUIRED)
<< SOElement (sfSequence, SOE_REQUIRED)
<< SOElement (sfTakerPays, SOE_REQUIRED)
@@ -85,7 +73,7 @@ void LEFInit ()
<< SOElement (sfExpiration, SOE_OPTIONAL)
;
DECLARE_LEF (RippleState, ltRIPPLE_STATE)
add ("RippleState", ltRIPPLE_STATE)
<< SOElement (sfBalance, SOE_REQUIRED)
<< SOElement (sfLowLimit, SOE_REQUIRED)
<< SOElement (sfHighLimit, SOE_REQUIRED)
@@ -99,17 +87,17 @@ void LEFInit ()
<< SOElement (sfHighQualityOut, SOE_OPTIONAL)
;
DECLARE_LEF (LedgerHashes, ltLEDGER_HASHES)
add ("LedgerHashes", ltLEDGER_HASHES)
<< SOElement (sfFirstLedgerSequence, SOE_OPTIONAL) // Remove if we do a ledger restart
<< SOElement (sfLastLedgerSequence, SOE_OPTIONAL)
<< SOElement (sfHashes, SOE_REQUIRED)
;
DECLARE_LEF (EnabledFeatures, ltFEATURES)
add ("EnabledFeatures", ltFEATURES)
<< SOElement (sfFeatures, SOE_REQUIRED)
;
DECLARE_LEF (FeeSettings, ltFEE_SETTINGS)
add ("FeeSettings", ltFEE_SETTINGS)
<< SOElement (sfBaseFee, SOE_REQUIRED)
<< SOElement (sfReferenceFeeUnits, SOE_REQUIRED)
<< SOElement (sfReserveBase, SOE_REQUIRED)
@@ -117,34 +105,16 @@ void LEFInit ()
;
}
LedgerEntryFormat* LedgerEntryFormat::getLgrFormat (LedgerEntryType t)
LedgerFormats* LedgerFormats::createInstance ()
{
std::map<int, LedgerEntryFormat*>::iterator it = byType.find (static_cast<int> (t));
if (it == byType.end ())
return NULL;
return it->second;
return new LedgerFormats;
}
LedgerEntryFormat* LedgerEntryFormat::getLgrFormat (int t)
void LedgerFormats::addCommonFields (Item& item)
{
std::map<int, LedgerEntryFormat*>::iterator it = byType.find ((t));
if (it == byType.end ())
return NULL;
return it->second;
item
<< SOElement(sfLedgerIndex, SOE_OPTIONAL)
<< SOElement(sfLedgerEntryType, SOE_REQUIRED)
<< SOElement(sfFlags, SOE_REQUIRED)
;
}
LedgerEntryFormat* LedgerEntryFormat::getLgrFormat (const std::string& t)
{
std::map<std::string, LedgerEntryFormat*>::iterator it = byName.find ((t));
if (it == byName.end ())
return NULL;
return it->second;
}
// vim:ts=4

View File

@@ -97,33 +97,23 @@ enum LedgerSpecificFlags
lsfHighAuth = 0x00080000,
};
// VFALCO TODO See if we can merge LedgerEntryFormat with TxFormats
//
class LedgerEntryFormat
//------------------------------------------------------------------------------
/** Holds the list of known ledger entry formats.
*/
class LedgerFormats
: public KnownFormats <LedgerEntryType>
, public SharedSingleton <LedgerFormats>
{
private:
LedgerFormats ();
public:
std::string t_name;
LedgerEntryType t_type;
SOTemplate elements;
static LedgerFormats* createInstance ();
static std::map<int, LedgerEntryFormat*> byType;
static std::map<std::string, LedgerEntryFormat*> byName;
LedgerEntryFormat (const char* name, LedgerEntryType type) : t_name (name), t_type (type)
{
byName[name] = this;
byType[type] = this;
}
LedgerEntryFormat& operator<< (const SOElement& el)
{
elements.push_back (el);
return *this;
}
static LedgerEntryFormat* getLgrFormat (LedgerEntryType t);
static LedgerEntryFormat* getLgrFormat (const std::string& t);
static LedgerEntryFormat* getLgrFormat (int t);
private:
void addCommonFields (Item& item);
};
#endif
// vim:ts=4

View File

@@ -1288,7 +1288,7 @@ UPTR_T<STObject> STObject::parseJson (const Json::Value& object, SField::ref inN
if (field == sfTransactionType)
{
// Retrieve type from name. Throws if not found.
TxType const txType = TxFormats::getInstance().findTypeByName (strValue);
TxType const txType = TxFormats::getInstance()->findTypeByName (strValue);
data.push_back (new STUInt16 (field, static_cast<uint16> (txType)));
@@ -1297,12 +1297,9 @@ UPTR_T<STObject> STObject::parseJson (const Json::Value& object, SField::ref inN
}
else if (field == sfLedgerEntryType)
{
LedgerEntryFormat* f = LedgerEntryFormat::getLgrFormat (strValue);
LedgerEntryType const type = LedgerFormats::getInstance()->findTypeByName (strValue);
if (!f)
throw std::runtime_error ("Unknown ledger entry type");
data.push_back (new STUInt16 (field, static_cast<uint16> (f->t_type)));
data.push_back (new STUInt16 (field, static_cast<uint16> (type)));
if (*name == sfGeneric)
name = &sfLedgerEntry;

View File

@@ -131,18 +131,20 @@ std::string STUInt16::getText () const
{
if (getFName () == sfLedgerEntryType)
{
LedgerEntryFormat* f = LedgerEntryFormat::getLgrFormat (value);
LedgerFormats::Item const* const item =
LedgerFormats::getInstance ()->findByType (static_cast <LedgerEntryType> (value));
if (f != NULL)
return f->t_name;
if (item != nullptr)
return item->getName ();
}
if (getFName () == sfTransactionType)
{
TxFormats::Item const* f = TxFormats::getInstance().findByType (static_cast <TxType> (value));
TxFormats::Item const* const item =
TxFormats::getInstance()->findByType (static_cast <TxType> (value));
if (f != NULL)
return f->getName ();
if (item != nullptr)
return item->getName ();
}
return boost::lexical_cast<std::string> (value);
@@ -152,18 +154,20 @@ Json::Value STUInt16::getJson (int) const
{
if (getFName () == sfLedgerEntryType)
{
LedgerEntryFormat* f = LedgerEntryFormat::getLgrFormat (value);
LedgerFormats::Item const* const item =
LedgerFormats::getInstance ()->findByType (static_cast <LedgerEntryType> (value));
if (f != NULL)
return f->t_name;
if (item != nullptr)
return item->getName ();
}
if (getFName () == sfTransactionType)
{
TxFormats::Item const* f = TxFormats::getInstance().findByType (static_cast <TxType> (value));
TxFormats::Item const* const item =
TxFormats::getInstance()->findByType (static_cast <TxType> (value));
if (f != NULL)
return f->getName ();
if (item != nullptr)
return item->getName ();
}
return value;

View File

@@ -4,48 +4,8 @@
*/
//==============================================================================
TxFormats::Item::Item (char const* name, TxType type)
: m_name (name)
, m_type (type)
{
}
TxFormats::Item& TxFormats::Item::operator<< (SOElement const& el)
{
elements.push_back (el);
return *this;
}
std::string const& TxFormats::Item::getName () const noexcept
{
return m_name;
}
TxType TxFormats::Item::getType () const noexcept
{
return m_type;
}
//------------------------------------------------------------------------------
void TxFormats::addCommonFields (Item& item)
{
item
<< SOElement(sfTransactionType, SOE_REQUIRED)
<< SOElement(sfFlags, SOE_OPTIONAL)
<< SOElement(sfSourceTag, SOE_OPTIONAL)
<< SOElement(sfAccount, SOE_REQUIRED)
<< SOElement(sfSequence, SOE_REQUIRED)
<< SOElement(sfPreviousTxnID, SOE_OPTIONAL)
<< SOElement(sfFee, SOE_REQUIRED)
<< SOElement(sfOperationLimit, SOE_OPTIONAL)
<< SOElement(sfSigningPubKey, SOE_REQUIRED)
<< SOElement(sfTxnSignature, SOE_OPTIONAL)
;
}
TxFormats::TxFormats ()
: SharedSingleton <TxFormats> (SingletonLifetime::persistAfterCreation)
{
add ("AccountSet", ttACCOUNT_SET)
<< SOElement (sfEmailHash, SOE_OPTIONAL)
@@ -115,63 +75,23 @@ TxFormats::TxFormats ()
;
}
TxFormats const& TxFormats::getInstance ()
void TxFormats::addCommonFields (Item& item)
{
static TxFormats instance;
return instance;
item
<< SOElement(sfTransactionType, SOE_REQUIRED)
<< SOElement(sfFlags, SOE_OPTIONAL)
<< SOElement(sfSourceTag, SOE_OPTIONAL)
<< SOElement(sfAccount, SOE_REQUIRED)
<< SOElement(sfSequence, SOE_REQUIRED)
<< SOElement(sfPreviousTxnID, SOE_OPTIONAL)
<< SOElement(sfFee, SOE_REQUIRED)
<< SOElement(sfOperationLimit, SOE_OPTIONAL)
<< SOElement(sfSigningPubKey, SOE_REQUIRED)
<< SOElement(sfTxnSignature, SOE_OPTIONAL)
;
}
TxType TxFormats::findTypeByName (std::string const name) const
TxFormats* TxFormats::createInstance ()
{
Item const* const result = findByName (name);
if (result != nullptr)
{
return result->getType ();
}
else
{
throw std::runtime_error ("Unknown format name");
}
}
TxFormats::Item const* TxFormats::findByType (TxType type) const noexcept
{
Item* result = nullptr;
TypeMap::const_iterator const iter = m_types.find (type);
if (iter != m_types.end ())
{
result = iter->second;
}
return result;
}
TxFormats::Item const* TxFormats::findByName (std::string const& name) const noexcept
{
Item* result = nullptr;
NameMap::const_iterator const iter = m_names.find (name);
if (iter != m_names.end ())
{
result = iter->second;
}
return result;
}
TxFormats::Item& TxFormats::add (char const* name, TxType type)
{
Item& item = *m_formats.add (new Item (name, type));
addCommonFields (item);
m_types [item.getType ()] = &item;
m_names [item.getName ()] = &item;
return item;
return new TxFormats;
}

View File

@@ -38,34 +38,9 @@ enum TxType
/** Manages the list of known transaction formats.
*/
class TxFormats
: public KnownFormats <TxType>
, public SharedSingleton <TxFormats>
{
public:
/** A transaction format.
*/
class Item
{
public:
Item (char const* name, TxType type);
Item& operator<< (SOElement const& el);
/** Retrieve the name of the format.
*/
std::string const& getName () const noexcept;
/** Retrieve the transaction type this format represents.
*/
TxType getType () const noexcept;
public:
// VFALCO TODO make an accessor for this
SOTemplate elements;
private:
std::string const m_name;
TxType const m_type;
};
private:
/** Create the object.
@@ -73,50 +48,10 @@ private:
*/
TxFormats ();
static void addCommonFields (Item& item);
void addCommonFields (Item& item);
public:
/** Retrieve the global instance.
*/
static TxFormats const& getInstance ();
/** Retrieve the type for a transaction format specified by name.
If the format name is unknown, an exception is thrown.
@param name The name of the transaction type.
@return The transaction type.
*/
TxType findTypeByName (std::string const name) const;
/** Retrieve a format based on its transaction type.
*/
Item const* findByType (TxType type) const noexcept;
/** Retrieve a format based on its name.
*/
Item const* findByName (std::string const& name) const noexcept;
protected:
/** Add a new format.
The new format has the set of common fields already added.
@param name The name of this format.
@param type The transaction type of this format.
@return The created format.
*/
Item& add (char const* name, TxType type);
private:
// VFALCO TODO use String instead of std::string
typedef std::map <std::string, Item*> NameMap;
typedef std::map <TxType, Item*> TypeMap;
OwnedArray <Item> m_formats;
NameMap m_names;
TypeMap m_types;
static TxFormats* createInstance ();
};
#endif