48#include <Sawyer/Exception.h>
49#include <Sawyer/Parse.h>
51#include <boost/filesystem.hpp>
52#include <boost/lexical_cast.hpp>
77template<
typename T,
class Enable =
void>
79 static T Get(
const std::string &data) {
80 return boost::lexical_cast<T>(data);
83 static T Get(
const std::string &data,
const T &defaultValue) {
85 return boost::conversion::try_lexical_convert(data, result) ? result : defaultValue;
91struct StringConverter<T, typename std::enable_if<std::is_integral<T>::value>::type> {
92 static T Get(
const std::string &data) {
94 return boost::numeric_cast<T>(big);
97 static T Get(
const std::string &data,
const T &defaultValue) {
108 static std::string Get(
const std::string &data) {
112 static std::string Get(
const std::string &data,
const std::string &defaultValue) {
113 if (data.size() == 0) {
122 static bool Get(
const std::string &data) {
123 std::string tmpData = data;
124 std::transform(tmpData.begin(), tmpData.end(), tmpData.begin(), ::tolower);
125 if (tmpData ==
"true" || tmpData ==
"yes" || tmpData ==
"1") {
132 static bool Get(
const std::string &data,
const bool &defaultValue) {
133 if (data.size() == 0) {
233 std::unique_ptr<std::pair<const std::string&, Node&>>
operator->();
290 std::unique_ptr<std::pair<const std::string&, const Node&>>
operator->();
348 explicit Node(
const std::string &value);
349 explicit Node(
const char* value);
361 bool isMap()
const {
return IsMap(); }
368 explicit operator bool()
const {
391 T
as(
const T &defaultValue)
const {
399 size_t size()
const {
return Size(); }
433 bool exists(
const std::string &key)
const {
435 return (*
this)[key] ? true :
false;
443 void erase(
const size_t index) { Erase(index); }
444 void erase(
const std::string& key) { Erase(key); }
456 (*this) = boost::lexical_cast<std::string>(value);
478 bool IsSequence()
const;
480 bool IsScalar()
const;
483 Node& Insert(
const size_t index);
486 void Erase(
const size_t index);
487 void Erase(
const std::string &key);
499 T As(
const T &defaultValue)
const {
500 return impl::StringConverter<T>::Get(AsString(), defaultValue);
506 const std::string& AsString()
const;
514void Parse(Node &root,
const boost::filesystem::path&);
515void Parse(Node &root, std::istream&);
516void Parse(Node &root,
const std::string &data);
517void Parse(Node &root,
const char *data,
const size_t size);
541 const size_t scalarMaxLength = std::numeric_limits<size_t>::max(),
542 const bool sequenceMapNewline =
false,
543 const bool mapScalarNewline =
false);
Base class for Sawyer runtime errors.
Iterator over constant YAML nodes.
ConstIterator & operator++()
Increment operator.
ConstIterator operator++(int)
Increment operator.
bool operator!=(const ConstIterator &)
Check if this iterator is not equal to another iterator.
bool operator==(const ConstIterator &)
Check if this iterator is equal to another iterator.
~ConstIterator()
Destructor.
ConstIterator & operator=(const ConstIterator &)
Assignment operator.
ConstIterator operator--(int)
Decrement operator.
std::pair< const std::string &, const Node & > operator*()
Get node of iterator.
ConstIterator & operator--()
Decrement operator.
ConstIterator()
Default constructor.
ConstIterator(const ConstIterator &)
Copy constructor.
std::unique_ptr< std::pair< const std::string &, const Node & > > operator->()
Dereference.
Base class for YAML exceptions.
Exception(const std::string &message, const eType type)
Constructor.
const char * Message() const
Get message of exception.
eType Type() const
Get type of exception.
@ ParsingError
Invalid parsing data.
@ OperationError
User operation error.
@ InternalError
Internal error.
InternalException(const std::string &message)
Constructor.
Iterator over YAML nodes.
Iterator & operator--()
Decrement operator.
std::pair< const std::string &, Node & > operator*()
Get node of iterator.
Iterator operator--(int)
Decrement operator.
std::unique_ptr< std::pair< const std::string &, Node & > > operator->()
Dereference.
bool operator==(const Iterator &)
Check if this iterator is equal to another iterator.
bool operator!=(const Iterator &)
Check if this iterator is not equal to another iterator.
Iterator()
Default constructor.
Iterator & operator=(const Iterator &)
Assignment operator.
Iterator & operator++()
Increment operator.
Iterator operator++(int)
Increment operator.
Iterator(const Iterator &)
Copy constructor.
T as() const
Get node as given template type.
Node & operator[](const size_t index)
Get sequence/map item.
Node(const std::string &value)
Assignment constructors.
bool isMap() const
Functions for checking type of node.
Node & operator=(const char *value)
Assignment operators.
Node & insert(const size_t index)
Insert sequence item at given index.
bool exists(const std::string &key) const
Test whether a map item exists without inserting it.
Node & operator=(const std::string &value)
Assignment operators.
Iterator begin()
Get start iterator.
bool isScalar() const
Functions for checking type of node.
Node & pushBack()
Add new sequence index to back.
size_t size() const
Get size of node.
Node & operator[](const size_t index) const
Get sequence/map item.
bool operator!() const
Predicate to test whether node exists.
void clear()
Completely clear node.
eType type() const
Functions for checking type of node.
Node(const Node &node)
Copy constructor.
Iterator end()
Get end iterator.
T as(const T &defaultValue) const
Get node as given template type.
Node & operator=(const T &value)
Assignment operators.
ConstIterator begin() const
Get start iterator.
Node()
Default constructor.
bool isNone() const
Functions for checking type of node.
Node & pushFront()
Add new sequence index to front.
bool isSequence() const
Functions for checking type of node.
Node & operator[](const std::string &key)
Get sequence/map item.
void erase(const std::string &key)
Erase item.
Node & operator[](const std::string &key) const
Get sequence/map item.
Node(const char *value)
Assignment constructors.
eType
Enumeration of node types.
ConstIterator end() const
Get end iterator.
void erase(const size_t index)
Erase item.
Node & operator=(const Node &node)
Assignment operators.
OperationException(const std::string &message)
Constructor.
ParsingException(const std::string &message)
Constructor.
Serialization configuration structure, describing output behavior.
size_t SpaceIndentation
Number of spaces per indentation.
SerializeConfig(const size_t spaceIndentation=2, const size_t scalarMaxLength=std::numeric_limits< size_t >::max(), const bool sequenceMapNewline=false, const bool mapScalarNewline=false)
Constructor.
bool SequenceMapNewline
Put maps on a new line if parent node is a sequence.
bool MapScalarNewline
Put scalars on a new line if parent node is a map.
size_t ScalarMaxLength
Maximum length of scalars.