Files
xahaud/src/ripple/protocol/SOTemplate.h
Scott Schurr afcc4ff296 Reduce likelihood of malformed SOTemplate:
Formerly an SOTemplate was default constructed and its elements
added using push_back().  This left open the possibility of a
malformed SOTemplate if adding one of the elements caused a throw.

With this commit the SOTemplate requires an initializer_list of
its elements at construction.  Elements may not be added after
construction.  With this approach either the SOTemplate is fully
constructed with all of its elements or the constructor throws,
which prevents an invalid SOTemplate from even existing.

This change requires all SOTemplate construction to be adjusted
at the call site.  Those changes are also in this commit.

The SOE_Flags enum is also renamed to SOEStyle, which harmonizes
the name with other uses in the code base.  SOEStyle elements
are renamed (slightly) to have an "soe" prefix rather than "SOE_".
This heads toward reserving identifiers with all upper case for
macros.  The new style also aligns with other prominent enums in
the code base like the collection of TER identifiers.

SOElement is adjusted so it can be stored directly in an STL
container, rather than requiring storage in a unique_ptr.
Correspondingly, unique_ptr usage is removed from both
SOTemplate and KnownFormats.
2019-04-26 11:17:45 -07:00

134 lines
3.9 KiB
C++

//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012, 2013 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#ifndef RIPPLE_PROTOCOL_SOTEMPLATE_H_INCLUDED
#define RIPPLE_PROTOCOL_SOTEMPLATE_H_INCLUDED
#include <ripple/basics/contract.h>
#include <ripple/protocol/SField.h>
#include <functional>
#include <initializer_list>
#include <memory>
namespace ripple {
/** Kind of element in each entry of an SOTemplate. */
enum SOEStyle
{
soeINVALID = -1,
soeREQUIRED = 0, // required
soeOPTIONAL = 1, // optional, may be present with default value
soeDEFAULT = 2, // optional, if present, must not have default value
};
//------------------------------------------------------------------------------
/** An element in a SOTemplate. */
class SOElement
{
// Use std::reference_wrapper so SOElement can be stored in a std::vector.
std::reference_wrapper<SField const> sField_;
SOEStyle style_;
public:
SOElement (SField const& fieldName, SOEStyle style)
: sField_ (fieldName)
, style_ (style)
{
if (! sField_.get().isUseful())
Throw<std::runtime_error> ("SField in SOElement must be useful.");
}
SField const& sField () const
{
return sField_.get();
}
SOEStyle style () const
{
return style_;
}
};
//------------------------------------------------------------------------------
/** Defines the fields and their attributes within a STObject.
Each subclass of SerializedObject will provide its own template
describing the available fields and their metadata attributes.
*/
class SOTemplate
{
public:
// Copying vectors is expensive. Make this a move-only type until
// there is motivation to change that.
SOTemplate(SOTemplate&& other) = default;
SOTemplate& operator=(SOTemplate&& other) = default;
/** Create a template populated with all fields.
After creating the template fields cannot be
added, modified, or removed.
*/
SOTemplate (std::initializer_list<SOElement> uniqueFields,
std::initializer_list<SOElement> commonFields = {});
/* Provide for the enumeration of fields */
std::vector<SOElement>::const_iterator begin() const
{
return elements_.cbegin();
}
std::vector<SOElement>::const_iterator cbegin() const
{
return begin();
}
std::vector<SOElement>::const_iterator end() const
{
return elements_.cend();
}
std::vector<SOElement>::const_iterator cend() const
{
return end();
}
/** The number of entries in this template */
std::size_t size () const
{
return elements_.size ();
}
/** Retrieve the position of a named field. */
int getIndex (SField const&) const;
SOEStyle
style(SField const& sf) const
{
return elements_[indices_[sf.getNum()]].style();
}
private:
std::vector<SOElement> elements_;
std::vector<int> indices_; // field num -> index
};
} // ripple
#endif