Cleanup object templates:

* Avoid exposing class members - use boost::iterator_range instead
* Use std::make_unique instead of naked new
This commit is contained in:
Nik Bougalis
2015-06-02 21:46:49 -07:00
parent c2814308f1
commit 8c68eff460
3 changed files with 21 additions and 20 deletions

View File

@@ -21,7 +21,8 @@
#define RIPPLE_PROTOCOL_SOTEMPLATE_H_INCLUDED
#include <ripple/protocol/SField.h>
#include <memory>
#include <boost/range.hpp>
#include <beast/cxx14/memory.h> // <memory>
namespace ripple {
@@ -60,22 +61,26 @@ public:
class SOTemplate
{
public:
using value_type = std::unique_ptr <SOElement const>;
using list_type = std::vector <value_type>;
using list_type = std::vector <std::unique_ptr <SOElement const>>;
using iterator_range = boost::iterator_range<list_type::const_iterator>;
/** 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. */

View File

@@ -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<SOElement const> (r));
}
int SOTemplate::getIndex (SField const& f) const

View File

@@ -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;