added rvalue overload of array::flatten

also:
- made array::flatten return a reference to the array
- minor documentation fixes
This commit is contained in:
Mark Gillard 2020-06-28 15:26:18 +03:00
parent b8438b3258
commit 0b334fd7d2
8 changed files with 69 additions and 42 deletions

View File

@ -121,7 +121,7 @@
///////////////////////////////////////////////////////////////////////
///
/// \section mainpage-adding-lib Adding toml++ to your project
/// It's header-only library so really all you have to do is clone
/// It's a header-only library so really all you have to do is clone
/// [the repository](https://github.com/marzer/tomlplusplus/) from GitHub and set your include paths.
/// There's some minor configuration you can do to customize some basic library functionality, but it's totally
/// optional.
@ -176,6 +176,11 @@
/// toml::parse_file().
/// \endparblock
///
/// \see
/// - toml::parse_file()
/// - toml::table
/// - toml::parse_error
///
///////////////////////////////////
///
/// \subsection mainpage-example-parsing-strings Parsing strings and iostreams
@ -231,9 +236,14 @@
/// ... twice
/// \eout
///
/// \see
/// - toml::parse_file()
/// - toml::table
/// - toml::parse_error
///
///////////////////////////////////
///
/// \subsection mainpage-example-parsing-without-exceptions Parsing without using exceptions
/// \subsection mainpage-example-parsing-without-exceptions Handling errors without exceptions
/// Can't (or won't) use exceptions? That's fine too. You can disable exceptions in your compiler flags and/or
/// explicitly disable the library's use of them by setting the option \ref TOML_EXCEPTIONS to `0`. In either case,
/// the parsing functions return a toml::parse_result instead of a toml::table:
@ -264,8 +274,8 @@
/// \subsection mainpage-example-custom-error-formatting Custom error formatting
/// The examples above use an overloaded `operator<<` with ostreams to print basic error messages, and look like this:
/// \out
/// Encountered unexpected character while parsing boolean; expected 'true', saw 'trU'
/// (error occurred at line 1, column 13)
/// Error while parsing key: expected bare key starting character or string delimiter, saw '?'
/// (error occurred at line 2, column 5)
/// \eout
///
/// In order to keep the library as small as possible I haven't bent over backwards to support things like custom
@ -501,13 +511,12 @@
///////////////////////////////////////////////////////////////////////
///
/// \section mainpage-contact Contacting the author
/// For bug reports and feature requests please consider using the
/// [GitHub issues](https://github.com/marzer/tomlplusplus/issues) system. For anything else though you're welcome
/// to reach out via other means. In order of likely response speed:
/// For bug reports and feature requests please use the [GitHub issues](https://github.com/marzer/tomlplusplus/issues)
/// system. For anything else you're welcome to reach out via other means. In order of likely response speed:
/// - Twitter: [marzer8789](https://twitter.com/marzer8789)
/// - Email: [mark.gillard@outlook.com.au](mailto:mark.gillard@outlook.com.au)
/// - Facebook: [marzer](https://www.facebook.com/marzer)
/// - LinkedIn: [marzer](https://www.linkedin.com/in/marzer/)
/// - LinkedIn: [marzer](https://www.linkedin.com/in/marzer/)
///
//# {{

View File

@ -316,8 +316,6 @@ namespace toml
/// \tparam V One of the TOML node or value types (or a type promotable to one).
/// \param val The value used to initialize node 0.
/// \param vals The values used to initialize nodes 1...N.
///
/// \returns A TOML_NODISCARD_CTOR.
template <typename U, typename... V>
TOML_NODISCARD_CTOR
explicit array(U&& val, V&&... vals)
@ -589,7 +587,7 @@ namespace toml
/// \ecpp
///
/// \out
/// [1, "drill" 2]
/// [1, "drill", 2]
/// \eout
///
/// \tparam U One of the TOML node or value types.
@ -933,7 +931,15 @@ namespace toml
/// \eout
///
/// \remarks Arrays inside child tables are not flattened.
void flatten();
///
/// A reference to the array.
array& flatten() &;
/// \brief Flattens this array, recursively hoisting the contents of child arrays up into itself (rvalue overload).
array&& flatten()&&
{
return static_cast<toml::array&&>(static_cast<toml::array&>(*this).flatten());
}
template <typename Char>
friend std::basic_ostream<Char>& operator << (std::basic_ostream<Char>&, const array&);

View File

@ -177,10 +177,10 @@ namespace toml
}
TOML_EXTERNAL_LINKAGE
void array::flatten()
array& array::flatten() &
{
if (values.empty())
return;
return *this;
bool requires_flattening = false;
size_t size_after_flattening = values.size();
@ -201,7 +201,7 @@ namespace toml
}
if (!requires_flattening)
return;
return *this;
values.reserve(size_after_flattening);
@ -221,5 +221,7 @@ namespace toml
preinsertion_resize(i + 1_sz, leaf_count - 1_sz);
flatten_child(std::move(*arr), i); //increments i
}
return *this;
}
}

View File

@ -156,18 +156,24 @@ namespace toml
///
/// if (auto val = tbl.get("int_val"sv)->value<int64_t>())
/// std::cout << "'int_val' as int64_t: "sv << *val << std::endl;
///
/// if (auto val = tbl.get("int_val"sv)->value<double>())
/// std::cout << "'int_val' as double: "sv << *val << std::endl;
///
/// if (auto val = tbl.get("float_val"sv)->value<int64_t>())
/// std::cout << "'float_val' as int64_t: "sv << *val << std::endl;
///
/// if (auto val = tbl.get("float_val"sv)->value<double>())
/// std::cout << "'float_val' as double: "sv << *val << std::endl;
///
/// if (auto val = tbl.get("string_val"sv)->value<std::string>())
/// std::cout << "'string_val' as std::string: "sv << *val << std::endl;
///
/// if (auto val = tbl.get("string_val"sv)->value<std::string_view>())
/// std::cout << "'string_val' as std::string_view: "sv << *val << std::endl;
///
/// if (auto val = tbl.get("string_val"sv)->value<int64_t>())
/// std::cout << "this line won't be printed."sv << std::endl;
/// std::cout << "this line won't be printed because string_val wasn't an int."sv << std::endl;
/// \ecpp
///
/// \out
@ -187,12 +193,12 @@ namespace toml
/// \brief Gets the raw value contained by this node, or a default.
///
/// \tparam T Default value type. Must be (or be promotable to) one of the TOML value types.
/// \tparam T Default value type. Must be (or be promotable to) one of the TOML value types.
/// \param default_value The default value to return if the node wasn't a value, wasn't the
/// correct type, or no conversion was possible.
///
/// \returns The node's underlying value, or the default if the node wasn't a value, wasn't the
/// correct type, or no conversion was possible.
/// correct type, or no conversion was possible.
///
/// \see node::value()
template <typename T>
@ -202,15 +208,15 @@ namespace toml
///
/// \details \cpp
///
/// auto int_value = node->as<int64_t>();
/// auto tbl = node->as<toml::table>();
/// toml::value<int64_t>* int_value = node->as<int64_t>();
/// toml::table* tbl = node->as<toml::table>();
/// if (int_value)
/// std::cout << "Node is a value<int64_t>" << std::endl;
/// else if (tbl)
/// std::cout << "Node is a table" << std::endl;
///
/// // fully-qualified value node types also work:
/// auto int_value2 = node->as<toml::value<int64_t>>();
/// // fully-qualified value node types also work (useful for template code):
/// toml::value<int64_t>* int_value2 = node->as<toml::value<int64_t>>();
/// if (int_value2)
/// std::cout << "Node is a value<int64_t>" << std::endl;
///
@ -471,12 +477,14 @@ namespace toml
return do_visit(*this, std::forward<Func>(visitor));
}
/// \brief Invokes a visitor on the node based on the node's concrete type (rvalue overload).
template <typename Func>
decltype(auto) visit(Func&& visitor) && noexcept(visit_is_nothrow<Func&&, node&&>)
{
return do_visit(std::move(*this), std::forward<Func>(visitor));
}
/// \brief Invokes a visitor on the node based on the node's concrete type (const lvalue overload).
template <typename Func>
decltype(auto) visit(Func&& visitor) const& noexcept(visit_is_nothrow<Func&&, const node&>)
{
@ -490,14 +498,12 @@ namespace toml
/// will fire when invalid accesses are attempted: \cpp
///
/// auto tbl = toml::parse(R"(
///
/// min = 32
/// max = 45
///
/// min = 32
/// max = 45
/// )"sv);
///
/// auto& min_ref = tbl.get("min")->ref<int64_t>(); // this is OK
/// auto& max_ref = tbl.get("max")->ref<double>(); // hits assertion because the type is wrong
/// int64_t& min_ref = tbl.get("min")->ref<int64_t>(); // matching type
/// double& max_ref = tbl.get("max")->ref<double>(); // mismatched type, hits assert()
///
/// \ecpp
///
@ -510,12 +516,14 @@ namespace toml
return do_ref<T>(*this);
}
/// \brief Gets a raw reference to a value node's underlying data (rvalue overload).
template <typename T>
[[nodiscard]] impl::unwrapped<T>&& ref() && noexcept
{
return do_ref<T>(std::move(*this));
}
/// \brief Gets a raw reference to a value node's underlying data (const lvalue overload).
template <typename T>
[[nodiscard]] const impl::unwrapped<T>& ref() const& noexcept
{

View File

@ -233,15 +233,13 @@ namespace toml
/// actual type. In debug builds an assertion will fire when invalid accesses are attempted: \cpp
///
/// auto tbl = toml::parse(R"(
///
/// min = 32
/// max = 45
///
/// min = 32
/// max = 45
/// )"sv);
///
/// auto& min_ref = tbl["min"].ref<int64_t>(); // this is OK
/// auto& max_ref = tbl["max"].ref<double>(); // hits assertion because the type is wrong
/// auto& min_ref = tbl["foo"].ref<int64_t>(); // hits assertion because "foo" didn't exist
/// int64_t& min_ref = tbl["min"].ref<int64_t>(); // matching type
/// double& max_ref = tbl["max"].ref<double>(); // mismatched type, hits assert()
/// int64_t& foo_ref = tbl["foo"].ref<int64_t>(); // nonexistent key, hits assert()
///
/// \ecpp
///

View File

@ -549,7 +549,6 @@ namespace toml::impl
// handle 'line ending slashes' in multi-line mode
if constexpr (MultiLine)
{
//consume_leading_whitespace
if (is_line_break(*cp) || is_whitespace(*cp))
{
consume_leading_whitespace();

View File

@ -368,7 +368,7 @@ namespace toml
/// auto tbl = toml::parse("bar = 42"sv);
///
/// std::cout << "The value for 'bar' was found on "sv
/// << tbl.get("bar")->source()
/// << tbl.get("bar")->source().begin()
/// << std::endl;
///
/// \ecpp

View File

@ -2862,7 +2862,11 @@ namespace toml
return container_equality(lhs, rhs);
}
TOML_ASYMMETRICAL_EQUALITY_OPS(const array&, const std::vector<T>&, template <typename T>)
void flatten();
array& flatten() &;
array&& flatten()&&
{
return static_cast<toml::array&&>(static_cast<toml::array&>(*this).flatten());
}
template <typename Char>
friend std::basic_ostream<Char>& operator << (std::basic_ostream<Char>&, const array&);
@ -6271,10 +6275,10 @@ namespace toml
}
TOML_EXTERNAL_LINKAGE
void array::flatten()
array& array::flatten() &
{
if (values.empty())
return;
return *this;
bool requires_flattening = false;
size_t size_after_flattening = values.size();
@ -6295,7 +6299,7 @@ namespace toml
}
if (!requires_flattening)
return;
return *this;
values.reserve(size_after_flattening);
@ -6315,6 +6319,8 @@ namespace toml
preinsertion_resize(i + 1_sz, leaf_count - 1_sz);
flatten_child(std::move(*arr), i); //increments i
}
return *this;
}
}
@ -7280,7 +7286,6 @@ namespace toml::impl
// handle 'line ending slashes' in multi-line mode
if constexpr (MultiLine)
{
//consume_leading_whitespace
if (is_line_break(*cp) || is_whitespace(*cp))
{
consume_leading_whitespace();