From 8c68eff460532df2c3a75ffb7a08ab915084847a Mon Sep 17 00:00:00 2001 From: Nik Bougalis Date: Tue, 2 Jun 2015 21:46:49 -0700 Subject: [PATCH] Cleanup object templates: * Avoid exposing class members - use boost::iterator_range instead * Use std::make_unique instead of naked new --- src/ripple/protocol/SOTemplate.h | 23 ++++++++++++++--------- src/ripple/protocol/impl/SOTemplate.cpp | 6 +----- src/ripple/protocol/impl/STObject.cpp | 12 ++++++------ 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/ripple/protocol/SOTemplate.h b/src/ripple/protocol/SOTemplate.h index 6d0abf166..ca35bedd9 100644 --- a/src/ripple/protocol/SOTemplate.h +++ b/src/ripple/protocol/SOTemplate.h @@ -21,7 +21,8 @@ #define RIPPLE_PROTOCOL_SOTEMPLATE_H_INCLUDED #include -#include +#include +#include // namespace ripple { @@ -60,22 +61,26 @@ public: class SOTemplate { public: - using value_type = std::unique_ptr ; - using list_type = std::vector ; + using list_type = std::vector >; + using iterator_range = boost::iterator_range; /** Create an empty template. After creating the template, call @ref push_back with the desired fields. @see push_back */ - SOTemplate (); + SOTemplate () = default; - // VFALCO NOTE Why do we even bother with the 'private' keyword if - // this function is present? - // - list_type const& peek () const + /* Provide for the enumeration of fields */ + iterator_range all () const { - return mTypes; + return boost::make_iterator_range(mTypes); + } + + /** The number of entries in this template */ + std::size_t size () const + { + return mTypes.size (); } /** Add an element to the template. */ diff --git a/src/ripple/protocol/impl/SOTemplate.cpp b/src/ripple/protocol/impl/SOTemplate.cpp index c10f7adf0..0edafda17 100644 --- a/src/ripple/protocol/impl/SOTemplate.cpp +++ b/src/ripple/protocol/impl/SOTemplate.cpp @@ -22,10 +22,6 @@ namespace ripple { -SOTemplate::SOTemplate () -{ -} - void SOTemplate::push_back (SOElement const& r) { // Ensure there is the enough space in the index mapping @@ -52,7 +48,7 @@ void SOTemplate::push_back (SOElement const& r) // Append the new element. // - mTypes.push_back (value_type (new SOElement (r))); + mTypes.push_back (std::make_unique (r)); } int SOTemplate::getIndex (SField const& f) const diff --git a/src/ripple/protocol/impl/STObject.cpp b/src/ripple/protocol/impl/STObject.cpp index 635c070a2..37be3e9fb 100644 --- a/src/ripple/protocol/impl/STObject.cpp +++ b/src/ripple/protocol/impl/STObject.cpp @@ -68,7 +68,7 @@ STObject::STObject (SOTemplate const& type, SerialIter & sit, SField const& name) : STBase (name) { - v_.reserve(type.peek().size()); + v_.reserve(type.size()); set (sit); setType (type); } @@ -92,10 +92,10 @@ STObject::operator= (STObject&& other) void STObject::set (const SOTemplate& type) { v_.clear(); - v_.reserve(type.peek().size()); + v_.reserve(type.size()); mType = &type; - for (auto const& elem : type.peek()) + for (auto const& elem : type.all()) { if (elem->flags != SOE_REQUIRED) v_.emplace_back(detail::nonPresentObject, elem->e_field); @@ -109,8 +109,8 @@ bool STObject::setType (const SOTemplate& type) bool valid = true; mType = &type; decltype(v_) v; - v.reserve(type.peek().size()); - for (auto const& e : type.peek()) + v.reserve(type.size()); + for (auto const& e : type.all()) { auto const iter = std::find_if( v_.begin(), v_.end(), [&](detail::STVar const& b) @@ -173,7 +173,7 @@ STObject::setTypeFromSField (SField const& sField) bool STObject::isValidForType () { auto it = v_.begin(); - for (SOTemplate::value_type const& elem : mType->peek()) + for (auto const& elem : mType->all()) { if (it == v_.end()) return false;