Miscellaneous cleanups:

* Limit HashPrefix construction and disallow assignment

* Give KnownFormats deleted copy members so that derived
  classes will give the right answers if queried with the
  std::is_copy_constructible/assignable traits.

* Replace SharedSingleton with a local static in
  LedgerFormats::getInstance() to be consistent with
  similar code in other places.  This also allows the
  LedgerFormats default constructor to be marked private
  so that the compiler enforces the design that
  LedgerFormats is a singleton type.

* Change return types of LedgerFormats::getInstance() and
  TxFormats::getInstance() from pointer to non-const to
  reference to const so as follow more established design
  guidelines for singletons.  This prevents pointers being
  mistaken for heap-allocated objects, and the const
  ensures the singleton isn't mutable.

* Change RippleAddress to inherit privately from
  CBase58Data instead of publicly.  This lets the compiler
  enforce that there are no unintended conversions from
  RippleAddress to CBase58Data.  This change allows us
  to remove a comment warning about unwanted conversions.
This commit is contained in:
Howard Hinnant
2014-09-29 12:48:37 -04:00
committed by Vinnie Falco
parent 8e91ce67c5
commit 616be1d76c
11 changed files with 86 additions and 22 deletions

View File

@@ -46,7 +46,7 @@ SerializedLedgerEntry::SerializedLedgerEntry (
void SerializedLedgerEntry::setSLEType () void SerializedLedgerEntry::setSLEType ()
{ {
mFormat = LedgerFormats::getInstance()->findByType ( mFormat = LedgerFormats::getInstance().findByType (
static_cast <LedgerEntryType> (getFieldU16 (sfLedgerEntryType))); static_cast <LedgerEntryType> (getFieldU16 (sfLedgerEntryType)));
if (mFormat == nullptr) if (mFormat == nullptr)
@@ -65,7 +65,7 @@ void SerializedLedgerEntry::setSLEType ()
SerializedLedgerEntry::SerializedLedgerEntry (LedgerEntryType type, uint256 const& index) : SerializedLedgerEntry::SerializedLedgerEntry (LedgerEntryType type, uint256 const& index) :
STObject (sfLedgerEntry), mIndex (index), mType (type), mMutable (true) STObject (sfLedgerEntry), mIndex (index), mType (type), mMutable (true)
{ {
mFormat = LedgerFormats::getInstance()->findByType (type); mFormat = LedgerFormats::getInstance().findByType (type);
if (mFormat == nullptr) if (mFormat == nullptr)
throw std::runtime_error ("invalid ledger entry type"); throw std::runtime_error ("invalid ledger entry type");

View File

@@ -29,7 +29,7 @@ SerializedTransaction::SerializedTransaction (TxType type)
, mSigGood (false) , mSigGood (false)
, mSigBad (false) , mSigBad (false)
{ {
mFormat = TxFormats::getInstance()->findByType (type); mFormat = TxFormats::getInstance().findByType (type);
if (mFormat == nullptr) if (mFormat == nullptr)
{ {
@@ -48,7 +48,7 @@ SerializedTransaction::SerializedTransaction (STObject const& object)
{ {
mType = static_cast <TxType> (getFieldU16 (sfTransactionType)); mType = static_cast <TxType> (getFieldU16 (sfTransactionType));
mFormat = TxFormats::getInstance()->findByType (mType); mFormat = TxFormats::getInstance().findByType (mType);
if (!mFormat) if (!mFormat)
{ {
@@ -76,7 +76,7 @@ SerializedTransaction::SerializedTransaction (SerializerIterator& sit) : STObjec
set (sit); set (sit);
mType = static_cast<TxType> (getFieldU16 (sfTransactionType)); mType = static_cast<TxType> (getFieldU16 (sfTransactionType));
mFormat = TxFormats::getInstance()->findByType (mType); mFormat = TxFormats::getInstance().findByType (mType);
if (!mFormat) if (!mFormat)
{ {

View File

@@ -42,7 +42,6 @@ class HashPrefix
private: private:
std::uint32_t m_prefix; std::uint32_t m_prefix;
public:
HashPrefix (char a, char b, char c) HashPrefix (char a, char b, char c)
: m_prefix (0) : m_prefix (0)
{ {
@@ -52,6 +51,10 @@ public:
m_prefix = m_prefix << 8; m_prefix = m_prefix << 8;
} }
public:
HashPrefix(HashPrefix const&) = delete;
HashPrefix& operator=(HashPrefix const&) = delete;
/** Returns the hash prefix associated with this object */ /** Returns the hash prefix associated with this object */
operator std::uint32_t () const operator std::uint32_t () const
{ {

View File

@@ -182,6 +182,9 @@ protected:
virtual void addCommonFields (Item& item) = 0; virtual void addCommonFields (Item& item) = 0;
private: private:
KnownFormats(KnownFormats const&) = delete;
KnownFormats& operator=(KnownFormats const&) = delete;
std::vector <std::unique_ptr <Item>> m_formats; std::vector <std::unique_ptr <Item>> m_formats;
NameMap m_names; NameMap m_names;
TypeMap m_types; TypeMap m_types;

View File

@@ -119,9 +119,11 @@ void LedgerFormats::addCommonFields (Item& item)
; ;
} }
LedgerFormats* LedgerFormats::getInstance () LedgerFormats const&
LedgerFormats::getInstance ()
{ {
return beast::SharedSingleton <LedgerFormats>::getInstance (); static LedgerFormats instance;
return instance;
} }
} // ripple } // ripple

View File

@@ -131,10 +131,11 @@ enum LedgerSpecificFlags
*/ */
class LedgerFormats : public KnownFormats <LedgerEntryType> class LedgerFormats : public KnownFormats <LedgerEntryType>
{ {
public: private:
LedgerFormats (); LedgerFormats ();
static LedgerFormats* getInstance (); public:
static LedgerFormats const& getInstance ();
private: private:
void addCommonFields (Item& item); void addCommonFields (Item& item);

View File

@@ -38,9 +38,8 @@ namespace ripple {
// Used to hold addresses and parse and produce human formats. // Used to hold addresses and parse and produce human formats.
// //
// XXX This needs to be reworked to store data in uint160 and uint256. // XXX This needs to be reworked to store data in uint160 and uint256.
// Conversion to CBase58Data should only happen as needed.
class RippleAddress : public CBase58Data class RippleAddress : private CBase58Data
{ {
private: private:
typedef enum typedef enum
@@ -229,10 +228,66 @@ public:
static RippleAddress createSeedRandom (); static RippleAddress createSeedRandom ();
static RippleAddress createSeedGeneric (std::string const& strText); static RippleAddress createSeedGeneric (std::string const& strText);
std::string ToString () const
{return static_cast<CBase58Data const&>(*this).ToString();}
template <class Hasher>
friend
void
hash_append(Hasher& hasher, RippleAddress const& value)
{
using beast::hash_append;
hash_append(hasher, static_cast<CBase58Data const&>(value));
}
friend
bool
operator==(RippleAddress const& lhs, RippleAddress const& rhs)
{
return static_cast<CBase58Data const&>(lhs) ==
static_cast<CBase58Data const&>(rhs);
}
friend
bool
operator <(RippleAddress const& lhs, RippleAddress const& rhs)
{
return static_cast<CBase58Data const&>(lhs) <
static_cast<CBase58Data const&>(rhs);
}
}; };
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
inline
bool
operator!=(RippleAddress const& lhs, RippleAddress const& rhs)
{
return !(lhs == rhs);
}
inline
bool
operator >(RippleAddress const& lhs, RippleAddress const& rhs)
{
return rhs < lhs;
}
inline
bool
operator<=(RippleAddress const& lhs, RippleAddress const& rhs)
{
return !(rhs < lhs);
}
inline
bool
operator>=(RippleAddress const& lhs, RippleAddress const& rhs)
{
return !(lhs < rhs);
}
/** RipplePublicKey */ /** RipplePublicKey */
template <> template <>
struct RipplePublicKeyTraits::assign <RippleAddress> struct RipplePublicKeyTraits::assign <RippleAddress>

View File

@@ -85,7 +85,7 @@ std::string STUInt16::getText () const
{ {
if (getFName () == sfLedgerEntryType) if (getFName () == sfLedgerEntryType)
{ {
auto item = LedgerFormats::getInstance ()->findByType ( auto item = LedgerFormats::getInstance ().findByType (
static_cast <LedgerEntryType> (value_)); static_cast <LedgerEntryType> (value_));
if (item != nullptr) if (item != nullptr)
@@ -95,7 +95,7 @@ std::string STUInt16::getText () const
if (getFName () == sfTransactionType) if (getFName () == sfTransactionType)
{ {
TxFormats::Item const* const item = TxFormats::Item const* const item =
TxFormats::getInstance()->findByType (static_cast <TxType> (value_)); TxFormats::getInstance().findByType (static_cast <TxType> (value_));
if (item != nullptr) if (item != nullptr)
return item->getName (); return item->getName ();
@@ -110,7 +110,7 @@ Json::Value STUInt16::getJson (int) const
if (getFName () == sfLedgerEntryType) if (getFName () == sfLedgerEntryType)
{ {
LedgerFormats::Item const* const item = LedgerFormats::Item const* const item =
LedgerFormats::getInstance ()->findByType (static_cast <LedgerEntryType> (value_)); LedgerFormats::getInstance ().findByType (static_cast <LedgerEntryType> (value_));
if (item != nullptr) if (item != nullptr)
return item->getName (); return item->getName ();
@@ -119,7 +119,7 @@ Json::Value STUInt16::getJson (int) const
if (getFName () == sfTransactionType) if (getFName () == sfTransactionType)
{ {
TxFormats::Item const* const item = TxFormats::Item const* const item =
TxFormats::getInstance()->findByType (static_cast <TxType> (value_)); TxFormats::getInstance().findByType (static_cast <TxType> (value_));
if (item != nullptr) if (item != nullptr)
return item->getName (); return item->getName ();

View File

@@ -204,7 +204,7 @@ bool STParsedJSON::parse (std::string const& json_name,
{ {
if (field == sfTransactionType) if (field == sfTransactionType)
{ {
TxType const txType (TxFormats::getInstance()-> TxType const txType (TxFormats::getInstance().
findTypeByName (strValue)); findTypeByName (strValue));
data.push_back (new STUInt16 (field, data.push_back (new STUInt16 (field,
@@ -215,7 +215,7 @@ bool STParsedJSON::parse (std::string const& json_name,
} }
else if (field == sfLedgerEntryType) else if (field == sfLedgerEntryType)
{ {
LedgerEntryType const type (LedgerFormats::getInstance()-> LedgerEntryType const type (LedgerFormats::getInstance().
findTypeByName (strValue)); findTypeByName (strValue));
data.push_back (new STUInt16 (field, data.push_back (new STUInt16 (field,

View File

@@ -102,11 +102,11 @@ void TxFormats::addCommonFields (Item& item)
; ;
} }
TxFormats* TxFormats::getInstance () TxFormats const&
TxFormats::getInstance ()
{ {
static TxFormats instance; static TxFormats instance;
//return SharedSingleton <TxFormats>::getInstance (); return instance;
return &instance;
} }
} // ripple } // ripple

View File

@@ -64,7 +64,7 @@ public:
*/ */
TxFormats (); TxFormats ();
static TxFormats* getInstance (); static TxFormats const& getInstance ();
}; };
} // ripple } // ripple