43 #include <boost/filesystem.hpp>
44 #include <boost/lexical_cast.hpp>
49 #include <rose_strtoull.h>
52 #include <type_traits>
68 template<
typename T,
class Enable =
void>
70 static T Get(
const std::string &data) {
71 return boost::lexical_cast<T>(data);
74 static T Get(
const std::string &data,
const T &defaultValue) {
76 return boost::conversion::try_lexical_convert(data, result) ? result : defaultValue;
82 struct StringConverter<T, typename std::enable_if<std::is_integral<T>::value>::type> {
83 static T Get(
const std::string &data) {
84 const char *s = data.c_str();
86 static_assert(
sizeof(T) <=
sizeof(uint64_t),
"sizeof(T) is too big for implementation");
87 if (strlen(s) != data.size() || data.empty() || isspace(data[0]))
88 throw std::runtime_error(
"string is not a recognized integral literal");
90 uint64_t big = rose_strtoull(s, &rest, 0);
92 throw std::runtime_error(
"string is not a recognized integral literal");
93 }
else if (ULLONG_MAX == big && ERANGE == errno) {
94 throw std::runtime_error(
"integral literal out of range");
96 return boost::numeric_cast<T>(big);
100 static T Get(
const std::string &data,
const T &defaultValue) {
111 static std::string Get(
const std::string &data) {
115 static std::string Get(
const std::string &data,
const std::string &defaultValue) {
116 if (data.size() == 0) {
125 static bool Get(
const std::string &data) {
126 std::string tmpData = data;
127 std::transform(tmpData.begin(), tmpData.end(), tmpData.begin(), ::tolower);
128 if (tmpData ==
"true" || tmpData ==
"yes" || tmpData ==
"1") {
135 static bool Get(
const std::string &data,
const bool &defaultValue) {
136 if (data.size() == 0) {
233 std::pair<const std::string&, Node&>
operator*();
236 std::unique_ptr<std::pair<const std::string&, Node&>>
operator->();
290 std::pair<const std::string&, const Node&>
operator*();
293 std::unique_ptr<std::pair<const std::string&, const Node&>>
operator->();
351 Node(
const std::string &value);
352 Node(
const char* value);
364 bool isMap()
const {
return IsMap(); }
371 explicit operator bool()
const {
394 T
as(
const T &defaultValue)
const {
402 size_t size()
const {
return Size(); }
440 void erase(
const size_t index) { Erase(index); }
441 void erase(
const std::string& key) { Erase(key); }
469 bool IsSequence()
const;
471 bool IsScalar()
const;
474 Node& Insert(
const size_t index);
477 void Erase(
const size_t index);
478 void Erase(
const std::string &key);
490 T As(
const T &defaultValue)
const {
491 return impl::StringConverter<T>::Get(AsString(), defaultValue);
497 const std::string& AsString()
const;
505 void Parse(Node &root,
const boost::filesystem::path&);
506 void Parse(Node &root, std::iostream&);
507 void Parse(Node &root,
const std::string &data);
508 void Parse(Node &root,
const char *data,
const size_t size);
511 void parse(Node &root,
const boost::filesystem::path&);
514 void parse(Node &root, std::iostream&);
519 void parse(Node &root,
const std::string &data);
520 void parse(Node &root,
const char *data);
533 const size_t scalarMaxLength = 64,
534 const bool sequenceMapNewline =
false,
535 const bool mapScalarNewline =
false);
558 void Serialize(
const Node &root, std::iostream &stream,
const SerializeConfig &config = {2, 64,
false,
false});
559 void Serialize(
const Node &root, std::string &
string,
const SerializeConfig &config = {2, 64,
false,
false});
bool isSequence() const
Functions for checking type of node.
ConstIterator & operator++()
Increment operator.
Node & operator[](const size_t index)
Get sequence/map item.
Iterator()
Default constructor.
void clear()
Completely clear node.
size_t ScalarMaxLength
Maximum length of scalars.
@ OperationError
User operation error.
std::unique_ptr< std::pair< const std::string &, Node & > > operator->()
Dereference.
Iterator & operator++()
Increment operator.
size_t size() const
Get size of node.
bool operator!=(const ConstIterator &)
Check if this iterator is not equal to another iterator.
OperationException(const std::string &message)
Constructor.
ParsingException(const std::string &message)
Constructor.
bool operator==(const ConstIterator &)
Check if this iterator is equal to another iterator.
Iterator end()
Get end iterator.
const char * Message() const
Get message of exception.
InternalException(const std::string &message)
Constructor.
Iterator over YAML nodes.
bool SequenceMapNewline
Put maps on a new line if parent node is a sequence.
@ InternalError
Internal error.
eType
Enumeration of node types.
ConstIterator & operator=(const ConstIterator &)
Assignment operator.
Iterator begin()
Get start iterator.
Iterator over constant YAML nodes.
void parse(Node &root, const boost::filesystem::path &)
Parse YAML from file into node.
bool MapScalarNewline
Put scalars on a new line if parent node is a map.
Node & pushFront()
Add new sequence index to back.
SerializeConfig(const size_t spaceIndentation=2, const size_t scalarMaxLength=64, const bool sequenceMapNewline=false, const bool mapScalarNewline=false)
Constructor.
bool isNone() const
Functions for checking type of node.
Node()
Default constructor.
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.
bool isScalar() const
Functions for checking type of node.
eType type() const
Functions for checking type of node.
eType Type() const
Get type of exception.
ConstIterator & operator--()
Decrement operator.
Node & pushBack()
Add new sequence index to front.
Serialization configuration structure, describing output behavior.
Exception(const std::string &message, const eType type)
Constructor.
Iterator & operator=(const Iterator &)
Assignment operator.
@ ParsingError
Invalid parsing data.
Main namespace for the ROSE library.
T as() const
Get node as given template type.
void erase(const std::string &key)
Erase item.
bool isMap() const
Functions for checking type of node.
std::pair< const std::string &, const Node & > operator*()
Get node of iterator.
ConstIterator()
Default constructor.
ConstIterator begin() const
Get start iterator.
Node & operator=(const Node &node)
Assignment operators.
std::unique_ptr< std::pair< const std::string &, const Node & > > operator->()
Dereference.
size_t SpaceIndentation
Number of spaces per indentation.
ConstIterator end() const
Get end iterator.
T as(const T &defaultValue) const
Get node as given template type.
std::pair< const std::string &, Node & > operator*()
Get node of iterator.
~ConstIterator()
Destructor.
Iterator & operator--()
Decrement operator.
void Serialize(const Node &root, const char *filename, const SerializeConfig &config={2, 64, false, false})
Serialize YAML.
Node & insert(const size_t index)
Insert sequence item at given index.
bool operator!() const
Predicate to test whether node exists.
Base class for YAML exceptions.
void erase(const size_t index)
Erase item.