Skip to content

Commit

Permalink
Merge branch 'hoensr-xml-no-size-attributes' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
AzothAmmo committed May 5, 2017
2 parents ad90557 + 35a36af commit 950aca4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ cereal is licensed under the [BSD license](http://opensource.org/licenses/BSD-3-
## cereal build status

* develop : [![Build Status](https://travis-ci.org/USCiLab/cereal.png?branch=develop)](https://travis-ci.org/USCiLab/cereal)
[![Build status](https://ci.appveyor.com/api/projects/status/91aou6smj36or0vb/branch/develop?svg=true)](https://ci.appveyor.com/project/AzothAmmo/cereal/branch/develop)

---

Expand Down
48 changes: 40 additions & 8 deletions include/cereal/archives/xml.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,31 +101,56 @@ namespace cereal
//! @{

//! A class containing various advanced options for the XML archive
/*! Options can either be directly passed to the constructor, or chained using the
modifier functions for an interface analogous to named parameters */
class Options
{
public:
//! Default options
static Options Default(){ return Options(); }

//! Default options with no indentation
static Options NoIndent(){ return Options( std::numeric_limits<double>::max_digits10, false ); }

//! Specify specific options for the XMLOutputArchive
/*! @param precision The precision used for floating point numbers
@param indent Whether to indent each line of XML
@param outputType Whether to output the type of each serialized object as an attribute */
@param outputType Whether to output the type of each serialized object as an attribute
@param sizeAttributes Whether dynamically sized containers output the size=dynamic attribute */
explicit Options( int precision = std::numeric_limits<double>::max_digits10,
bool indent = true,
bool outputType = false ) :
bool outputType = false,
bool sizeAttributes = true ) :
itsPrecision( precision ),
itsIndent( indent ),
itsOutputType( outputType ) { }
itsOutputType( outputType ),
itsSizeAttributes( sizeAttributes )
{ }

/*! @name Option Modifiers
An interface for setting option settings analogous to named parameters.
@code{cpp}
cereal::XMLOutputArchive ar( myStream,
cereal::XMLOutputArchive::Options()
.indent(true)
.sizeAttributes(false) );
@endcode
*/
//! @{

//! Whether to indent each line of XML
Options & indent( bool enable ){ itsIndent = enable; return *this; }
//! Whether to output the type of each serialized object as an attribute
Options & outputType( bool enable ){ itsOutputType = enable; return *this; }
//! Whether dynamically sized containers (e.g. vector) output the size=dynamic attribute
Options & sizeAttributes( bool enable ){ itsSizeAttributes = enable; return *this; }

//! @}

private:
friend class XMLOutputArchive;
int itsPrecision;
bool itsIndent;
bool itsOutputType;
bool itsSizeAttributes;
};

//! Construct, outputting to the provided stream upon destruction
Expand All @@ -137,7 +162,8 @@ namespace cereal
OutputArchive<XMLOutputArchive>(this),
itsStream(stream),
itsOutputType( options.itsOutputType ),
itsIndent( options.itsIndent )
itsIndent( options.itsIndent ),
itsSizeAttributes(options.itsSizeAttributes)
{
// rapidxml will delete all allocations when xml_document is cleared
auto node = itsXML.allocate_node( rapidxml::node_declaration );
Expand Down Expand Up @@ -289,6 +315,8 @@ namespace cereal
itsNodes.top().node->append_attribute( itsXML.allocate_attribute( namePtr, valuePtr ) );
}

bool hasSizeAttributes() const { return itsSizeAttributes; }

protected:
//! A struct that contains metadata about a node
struct NodeInfo
Expand Down Expand Up @@ -330,6 +358,7 @@ namespace cereal
std::ostringstream itsOS; //!< Used to format strings internally
bool itsOutputType; //!< Controls whether type information is printed
bool itsIndent; //!< Controls whether indenting is used
bool itsSizeAttributes; //!< Controls whether lists have a size attribute
}; // XMLOutputArchive

// ######################################################################
Expand Down Expand Up @@ -764,7 +793,10 @@ namespace cereal
template <class T> inline
void prologue( XMLOutputArchive & ar, SizeTag<T> const & )
{
ar.appendAttribute( "size", "dynamic" );
if (ar.hasSizeAttributes())
{
ar.appendAttribute("size", "dynamic");
}
}

template <class T> inline
Expand Down

0 comments on commit 950aca4

Please sign in to comment.