Skip to content

Commit

Permalink
Merge branch 'new_options' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
AzothAmmo committed Mar 14, 2014
2 parents 1364e5f + 5a303d3 commit c8b6e24
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 21 deletions.
49 changes: 44 additions & 5 deletions include/cereal/archives/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,55 @@ namespace cereal
Common use cases for directly interacting with an JSONOutputArchive */
//! @{

//! A class containing various advanced options for the JSON archive
class Options
{
public:
//! Default options
static Options Default(){ return {}; }

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

//! The character to use for indenting
enum class IndentChar : char
{
space = ' ',
tab = '\t',
newline = '\n',
carriage_return = '\r'
};

//! Specify specific options for the JSONOutputArchive
/*! @param precision The precision used for floating point numbers
@param indentChar The type of character to indent with
@param indentLength The number of indentChar to use for indentation
(0 corresponds to no indentation) */
explicit Options( int precision = std::numeric_limits<double>::max_digits10,
IndentChar indentChar = IndentChar::space,
unsigned int indentLength = 4 ) :
itsPrecision( precision ),
itsIndentChar( static_cast<char>(indentChar) ),
itsIndentLength( indentLength ) { }

private:
friend class JSONOutputArchive;
int itsPrecision;
char itsIndentChar;
unsigned int itsIndentLength;
};

//! Construct, outputting to the provided stream
/*! @param stream The stream to output to. Can be a stringstream, a file stream, or
even cout!
@param precision The precision for floating point output */
JSONOutputArchive(std::ostream & stream, int precision = std::numeric_limits<double>::max_digits10) :
/*! @param stream The stream to output to.
@param options The JSON specific options to use. See the Options struct
for the values of default parameters */
JSONOutputArchive(std::ostream & stream, Options const & options = Options::Default() ) :
OutputArchive<JSONOutputArchive>(this),
itsWriteStream(stream),
itsWriter(itsWriteStream, precision),
itsWriter(itsWriteStream, options.itsPrecision),
itsNextName(nullptr)
{
itsWriter.SetIndent( options.itsIndentChar, options.itsIndentLength );
itsNameCounter.push(0);
itsNodeStack.push(NodeType::StartObject);
}
Expand Down
10 changes: 5 additions & 5 deletions include/cereal/archives/portable_binary.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ namespace cereal
across machines, is portable over different architectures.
When using a binary archive and a file stream, you must use the
std::ios::binary format flag to avoid having your data altered
std::ios::binary format flag to avoid having your data altered
inadvertently.
\warning This archive has not been thoroughly tested across different architectures.
Expand All @@ -84,9 +84,9 @@ namespace cereal
PortableBinaryOutputArchive(std::ostream & stream) :
OutputArchive<PortableBinaryOutputArchive, AllowEmptyClassElision>(this),
itsStream(stream)
{
this->operator()( portable_binary_detail::is_little_endian() );
}
{
this->operator()( portable_binary_detail::is_little_endian() );
}

//! Writes size bytes of data to the output stream
void saveBinary( const void * data, std::size_t size )
Expand Down Expand Up @@ -119,7 +119,7 @@ namespace cereal
the responsibility of the user.
When using a binary archive and a file stream, you must use the
std::ios::binary format flag to avoid having your data altered
std::ios::binary format flag to avoid having your data altered
inadvertently.
\warning This archive has not been thoroughly tested across different architectures.
Expand Down
50 changes: 40 additions & 10 deletions include/cereal/archives/xml.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,44 @@ namespace cereal
Common use cases for directly interacting with an XMLOutputArchive */
//! @{

//! A class containing various advanced options for the XML archive
class Options
{
public:
//! Default options
static Options Default(){ return {}; }

//! 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 */
explicit Options( int precision = std::numeric_limits<double>::max_digits10,
bool indent = true,
bool outputType = false ) :
itsPrecision( precision ),
itsIndent( indent ),
itsOutputType( outputType ) { }

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

//! Construct, outputting to the provided stream upon destruction
/*! @param stream The stream to output to. Can be a stringstream, a file stream, or
even cout! Note that since this archive builds a tree in memory,
it will not output to the stream until its destructor is called.
@param precision The precision for floating point output.
@param outputType Controls whether type information will be printed in attributes */
XMLOutputArchive(std::ostream & stream, size_t precision = std::numeric_limits<double>::max_digits10, bool outputType = false ) :
/*! @param stream The stream to output to. Note that XML is only guaranteed to flush
its output to the stream upon destruction.
@param options The XML specific options to use. See the Options struct
for the values of default parameters */
XMLOutputArchive( std::ostream & stream, Options const & options = Options::Default() ) :
OutputArchive<XMLOutputArchive>(this),
itsStream(stream),
itsOutputType( outputType )
itsOutputType( options.itsOutputType ),
itsIndent( options.itsIndent )
{
// rapidxml will delete all allocations when xml_document is cleared
auto node = itsXML.allocate_node( rapidxml::node_declaration );
Expand All @@ -118,15 +146,16 @@ namespace cereal

// set attributes on the streams
itsStream << std::boolalpha;
itsStream.precision( precision );
itsStream.precision( options.itsPrecision );
itsOS << std::boolalpha;
itsOS.precision( precision );
itsOS.precision( options.itsPrecision );
}

//! Destructor, flushes the XML
~XMLOutputArchive()
{
itsStream << itsXML;
const int flags = itsIndent ? 0x0 : rapidxml::print_no_indenting;
rapidxml::print( itsStream, itsXML, flags );
itsXML.clear();
}

Expand Down Expand Up @@ -280,6 +309,7 @@ namespace cereal
std::stack<NodeInfo> itsNodes; //!< A stack of nodes added to the document
std::ostringstream itsOS; //!< Used to format strings internally
bool itsOutputType; //!< Controls whether type information is printed
bool itsIndent; //!< Controls whether indenting is used
}; // XMLOutputArchive

// ######################################################################
Expand Down
3 changes: 2 additions & 1 deletion sandbox/sandbox_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,8 @@ int main()
std::stringstream oos;
{
cereal::JSONOutputArchive ar(oos);
cereal::JSONOutputArchive ar2(std::cout);
cereal::JSONOutputArchive ar2(std::cout,
cereal::JSONOutputArchive::Options(2, cereal::JSONOutputArchive::Options::IndentChar::tab, 4) );

ar( cereal::make_nvp( "1", 1 ),
cereal::make_nvp( "2", 2 ),
Expand Down

0 comments on commit c8b6e24

Please sign in to comment.