NeBuild dev
Loading...
Searching...
No Matches
array Class Reference

A TOML array. More...

#include <array.hpp>

Inheritance diagram for array:

Public Types

using value_type = node
 
using size_type = size_t
 
using difference_type = ptrdiff_t
 
using reference = node &
 
using const_reference = const node &
 

Public Member Functions

TOML_NODISCARD_CTOR TOML_EXPORTED_MEMBER_FUNCTION array () noexcept
 Default constructor.
 
TOML_EXPORTED_MEMBER_FUNCTION ~array () noexcept
 
TOML_NODISCARD_CTOR TOML_EXPORTED_MEMBER_FUNCTION array (const array &)
 Copy constructor.
 
TOML_NODISCARD_CTOR TOML_EXPORTED_MEMBER_FUNCTION array (array &&other) noexcept
 Move constructor.
 
 TOML_CONSTRAINED_TEMPLATE ((sizeof...(ElemTypes) > 0||!std::is_same_v< impl::remove_cvref< ElemType >, array >), typename ElemType, typename... ElemTypes) TOML_NODISCARD_CTOR explicit array(ElemType &&val
 Constructs an array with one or more initial elements.
 
TOML_EXPORTED_MEMBER_FUNCTION arrayoperator= (array &&rhs) noexcept
 Move-assignment operator.
 
Type checks
TOML_CONST_INLINE_GETTER node_type type () const noexcept final
 Returns #toml::node_type::array.
 
TOML_PURE_GETTER TOML_EXPORTED_MEMBER_FUNCTION bool is_homogeneous (node_type ntype) const noexcept final
 
TOML_PURE_GETTER TOML_EXPORTED_MEMBER_FUNCTION bool is_homogeneous (node_type ntype, node *&first_nonmatch) noexcept final
 
TOML_PURE_GETTER TOML_EXPORTED_MEMBER_FUNCTION bool is_homogeneous (node_type ntype, const node *&first_nonmatch) const noexcept final
 
TOML_CONST_INLINE_GETTER bool is_table () const noexcept final
 Returns false.
 
TOML_CONST_INLINE_GETTER bool is_array () const noexcept final
 Returns true.
 
TOML_PURE_GETTER bool is_array_of_tables () const noexcept final
 Returns true if the array contains only tables.
 
TOML_CONST_INLINE_GETTER bool is_value () const noexcept final
 Returns false.
 
TOML_CONST_INLINE_GETTER bool is_string () const noexcept final
 Returns false.
 
TOML_CONST_INLINE_GETTER bool is_integer () const noexcept final
 Returns false.
 
TOML_CONST_INLINE_GETTER bool is_floating_point () const noexcept final
 Returns false.
 
TOML_CONST_INLINE_GETTER bool is_number () const noexcept final
 Returns false.
 
TOML_CONST_INLINE_GETTER bool is_boolean () const noexcept final
 Returns false.
 
TOML_CONST_INLINE_GETTER bool is_date () const noexcept final
 Returns false.
 
TOML_CONST_INLINE_GETTER bool is_time () const noexcept final
 Returns false.
 
TOML_CONST_INLINE_GETTER bool is_date_time () const noexcept final
 Returns false.
 
Type casts
TOML_CONST_INLINE_GETTER tableas_table () noexcept final
 Returns nullptr.
 
TOML_CONST_INLINE_GETTER arrayas_array () noexcept final
 Returns a pointer to the array.
 
TOML_CONST_INLINE_GETTER toml::value< std::string > * as_string () noexcept final
 Returns nullptr.
 
TOML_CONST_INLINE_GETTER toml::value< int64_t > * as_integer () noexcept final
 Returns nullptr.
 
TOML_CONST_INLINE_GETTER toml::value< double > * as_floating_point () noexcept final
 Returns nullptr.
 
TOML_CONST_INLINE_GETTER toml::value< bool > * as_boolean () noexcept final
 Returns nullptr.
 
TOML_CONST_INLINE_GETTER toml::value< date > * as_date () noexcept final
 Returns nullptr.
 
TOML_CONST_INLINE_GETTER toml::value< time > * as_time () noexcept final
 Returns nullptr.
 
TOML_CONST_INLINE_GETTER toml::value< date_time > * as_date_time () noexcept final
 Returns nullptr.
 
TOML_CONST_INLINE_GETTER const tableas_table () const noexcept final
 Returns nullptr.
 
TOML_CONST_INLINE_GETTER const arrayas_array () const noexcept final
 Returns a const-qualified pointer to the array.
 
TOML_CONST_INLINE_GETTER const toml::value< std::string > * as_string () const noexcept final
 Returns nullptr.
 
TOML_CONST_INLINE_GETTER const toml::value< int64_t > * as_integer () const noexcept final
 Returns nullptr.
 
TOML_CONST_INLINE_GETTER const toml::value< double > * as_floating_point () const noexcept final
 Returns nullptr.
 
TOML_CONST_INLINE_GETTER const toml::value< bool > * as_boolean () const noexcept final
 Returns nullptr.
 
TOML_CONST_INLINE_GETTER const toml::value< date > * as_date () const noexcept final
 Returns nullptr.
 
TOML_CONST_INLINE_GETTER const toml::value< time > * as_time () const noexcept final
 Returns nullptr.
 
TOML_CONST_INLINE_GETTER const toml::value< date_time > * as_date_time () const noexcept final
 Returns nullptr.
 
Value retrieval
TOML_PURE_INLINE_GETTER node * get (size_t index) noexcept
 Gets a pointer to the element at a specific index.
 
TOML_PURE_INLINE_GETTER const node * get (size_t index) const noexcept
 Gets a pointer to the element at a specific index (const overload).
 
template<typename ElemType >
TOML_NODISCARD impl::wrap_node< ElemType > * get_as (size_t index) noexcept
 Gets a pointer to the element at a specific index if it is a particular type.
 
template<typename ElemType >
TOML_NODISCARD const impl::wrap_node< ElemType > * get_as (size_t index) const noexcept
 Gets a pointer to the element at a specific index if it is a particular type (const overload).
 
TOML_NODISCARD node & operator[] (size_t index) noexcept
 Gets a reference to the element at a specific index.
 
TOML_NODISCARD const node & operator[] (size_t index) const noexcept
 Gets a reference to the element at a specific index.
 
TOML_NODISCARD TOML_EXPORTED_MEMBER_FUNCTION node & at (size_t index)
 Gets a reference to the element at a specific index, throwing std::out_of_range if none existed.
 
TOML_NODISCARD const node & at (size_t index) const
 Gets a reference to the element at a specific index, throwing std::out_of_range if none existed.
 
TOML_NODISCARD node & front () noexcept
 Returns a reference to the first element in the array.
 
TOML_NODISCARD const node & front () const noexcept
 Returns a reference to the first element in the array.
 
TOML_NODISCARD node & back () noexcept
 Returns a reference to the last element in the array.
 
TOML_NODISCARD const node & back () const noexcept
 Returns a reference to the last element in the array.
 
Size and Capacity
TOML_NODISCARD bool empty () const noexcept
 Returns true if the array is empty.
 
TOML_NODISCARD size_t size () const noexcept
 Returns the number of elements in the array.
 
TOML_NODISCARD size_t max_size () const noexcept
 Returns the maximum number of elements that can be stored in an array on the current platform.
 
TOML_NODISCARD size_t capacity () const noexcept
 Returns the current max number of elements that may be held in the array's internal storage.
 
TOML_EXPORTED_MEMBER_FUNCTION void reserve (size_t new_capacity)
 Reserves internal storage capacity up to a pre-determined number of elements.
 
TOML_EXPORTED_MEMBER_FUNCTION void shrink_to_fit ()
 Requests the removal of any unused internal storage capacity.
 
TOML_EXPORTED_MEMBER_FUNCTION void truncate (size_t new_size)
 Shrinks the array to the given size.
 
template<typename ElemType >
void resize (size_t new_size, ElemType &&default_init_val, value_flags default_init_flags=preserve_source_value_flags)
 Resizes the array.
 
Erasure
TOML_EXPORTED_MEMBER_FUNCTION iterator erase (const_iterator pos) noexcept
 Removes the specified element from the array.
 
TOML_EXPORTED_MEMBER_FUNCTION iterator erase (const_iterator first, const_iterator last) noexcept
 Removes the elements in the range [first, last) from the array.
 
TOML_EXPORTED_MEMBER_FUNCTION arrayflatten () &
 Flattens this array, recursively hoisting the contents of child arrays up into itself.
 
array && flatten () &&
 Flattens this array, recursively hoisting the contents of child arrays up into itself (rvalue overload).
 
TOML_EXPORTED_MEMBER_FUNCTION arrayprune (bool recursive=true) &noexcept
 Removes empty child arrays and tables.
 
array && prune (bool recursive=true) &&noexcept
 Removes empty child arrays and tables (rvalue overload).
 
TOML_EXPORTED_MEMBER_FUNCTION void pop_back () noexcept
 Removes the last element from the array.
 
TOML_EXPORTED_MEMBER_FUNCTION void clear () noexcept
 Removes all elements from the array.
 
Insertion and Emplacement
template<typename ElemType >
iterator insert (const_iterator pos, ElemType &&val, value_flags flags=preserve_source_value_flags)
 Inserts a new element at a specific position in the array.
 
template<typename ElemType >
iterator insert (const_iterator pos, size_t count, ElemType &&val, value_flags flags=preserve_source_value_flags)
 Repeatedly inserts a new element starting at a specific position in the array.
 
template<typename Iter >
iterator insert (const_iterator pos, Iter first, Iter last, value_flags flags=preserve_source_value_flags)
 Inserts a range of elements into the array at a specific position.
 
template<typename ElemType >
iterator insert (const_iterator pos, std::initializer_list< ElemType > ilist, value_flags flags=preserve_source_value_flags)
 Inserts a range of elements into the array at a specific position.
 
template<typename ElemType = void, typename... Args>
iterator emplace (const_iterator pos, Args &&... args)
 Emplaces a new element at a specific position in the array.
 
template<typename ElemType >
iterator replace (const_iterator pos, ElemType &&val, value_flags flags=preserve_source_value_flags)
 Replaces the element at a specific position in the array with a different value.
 
template<typename ElemType >
void push_back (ElemType &&val, value_flags flags=preserve_source_value_flags)
 Appends a new element to the end of the array.
 
template<typename ElemType = void, typename... Args>
decltype(auto) emplace_back (Args &&... args)
 Emplaces a new element at the end of the array.
 

Public Attributes

ElemTypes && vals: array{ std::false_type{}
 

Friends

std::ostream & operator<< (std::ostream &lhs, const array &rhs)
 Prints the array out to a stream as formatted TOML.
 

Iteration

using iterator = array_iterator
 A RandomAccessIterator for iterating over elements in a toml::array.
 
using const_iterator = const_array_iterator
 A RandomAccessIterator for iterating over const elements in a toml::array.
 
TOML_NODISCARD iterator begin () noexcept
 Returns an iterator to the first element.
 
TOML_NODISCARD const_iterator begin () const noexcept
 Returns an iterator to the first element.
 
TOML_NODISCARD const_iterator cbegin () const noexcept
 Returns an iterator to the first element.
 
TOML_NODISCARD iterator end () noexcept
 Returns an iterator to one-past-the-last element.
 
TOML_NODISCARD const_iterator end () const noexcept
 Returns an iterator to one-past-the-last element.
 
TOML_NODISCARD const_iterator cend () const noexcept
 Returns an iterator to one-past-the-last element.
 
template<typename Func >
arrayfor_each (Func &&visitor) &noexcept(for_each_is_nothrow< Func &&, array & >::value)
 Invokes a visitor on each element in the array.
 
template<typename Func >
array && for_each (Func &&visitor) &&noexcept(for_each_is_nothrow< Func &&, array && >::value)
 Invokes a visitor on each element in the array (rvalue overload).
 
template<typename Func >
const arrayfor_each (Func &&visitor) const &noexcept(for_each_is_nothrow< Func &&, const array & >::value)
 Invokes a visitor on each element in the array (const lvalue overload).
 
template<typename Func >
const array && for_each (Func &&visitor) const &&noexcept(for_each_is_nothrow< Func &&, const array && >::value)
 Invokes a visitor on each element in the array (const rvalue overload).
 

Equality

 TOML_ASYMMETRICAL_EQUALITY_OPS (const array &, const std::initializer_list< T > &, template< typename T >)
 
 TOML_ASYMMETRICAL_EQUALITY_OPS (const array &, const std::vector< T > &, template< typename T >)
 
TOML_NODISCARD friend bool operator== (const array &lhs, const array &rhs) noexcept
 Equality operator.
 
TOML_NODISCARD friend bool operator!= (const array &lhs, const array &rhs) noexcept
 Inequality operator.
 
template<typename T >
TOML_NODISCARD friend bool operator== (const array &lhs, const std::initializer_list< T > &rhs) noexcept
 Initializer list equality operator.
 
template<typename T >
TOML_NODISCARD friend bool operator== (const array &lhs, const std::vector< T > &rhs) noexcept
 Vector equality operator.
 

Detailed Description

A TOML array.

\detail The interface of this type is modeled after std::vector, with some additional considerations made for the heterogeneous nature of a TOML array.

\godbolt{sjK4da}

\cpp

toml::table tbl = toml::parse(R"( arr = [1, 2, 3, 4, 'five'] )"sv);

// get the element as an array toml::array& arr = *tbl.get_as<toml::array>("arr"); std::cout << arr << "\n";

// increment each element with visit() for (auto&& elem : arr) { elem.visit([](auto&& el) noexcept { if constexpr (toml::is_number<decltype(el)>) (*el)++; else if constexpr (toml::is_string<decltype(el)>) el = "six"sv; }); } std::cout << arr << "\n";

// add and remove elements arr.push_back(7); arr.push_back(8.0f); arr.push_back("nine"sv); arr.erase(arr.cbegin()); std::cout << arr << "\n";

// emplace elements arr.emplace_back("ten"); arr.emplace_back<toml::array>(11, 12.0); std::cout << arr << "\n"; \ecpp

\out [ 1, 2, 3, 4, 'five' ] [ 2, 3, 4, 5, 'six' ] [ 3, 4, 5, 'six', 7, 8.0, 'nine' ] [ 3, 4, 5, 'six', 7, 8.0, 'nine', 'ten', [ 11, 12.0 ] ] \eout

Member Typedef Documentation

◆ const_iterator

A RandomAccessIterator for iterating over const elements in a toml::array.

◆ const_reference

using array::const_reference = const node&

◆ difference_type

using array::difference_type = ptrdiff_t

◆ iterator

using array::iterator = array_iterator

A RandomAccessIterator for iterating over elements in a toml::array.

◆ reference

using array::reference = node&

◆ size_type

using array::size_type = size_t

◆ value_type

using array::value_type = node

Constructor & Destructor Documentation

◆ array() [1/3]

Default constructor.

◆ ~array()

TOML_EXPORTED_MEMBER_FUNCTION array::~array ( )
noexcept

◆ array() [2/3]

Copy constructor.

◆ array() [3/3]

TOML_NODISCARD_CTOR TOML_EXPORTED_MEMBER_FUNCTION array::array ( array &&  other)
noexcept

Move constructor.

Member Function Documentation

◆ as_array() [1/2]

TOML_CONST_INLINE_GETTER const array * array::as_array ( ) const
inlinefinalnoexcept

Returns a const-qualified pointer to the array.

◆ as_array() [2/2]

TOML_CONST_INLINE_GETTER array * array::as_array ( )
inlinefinalnoexcept

Returns a pointer to the array.

◆ as_boolean() [1/2]

TOML_CONST_INLINE_GETTER const toml::value< bool > * array::as_boolean ( ) const
inlinefinalnoexcept

Returns nullptr.

◆ as_boolean() [2/2]

TOML_CONST_INLINE_GETTER toml::value< bool > * array::as_boolean ( )
inlinefinalnoexcept

Returns nullptr.

◆ as_date() [1/2]

TOML_CONST_INLINE_GETTER const toml::value< date > * array::as_date ( ) const
inlinefinalnoexcept

Returns nullptr.

◆ as_date() [2/2]

TOML_CONST_INLINE_GETTER toml::value< date > * array::as_date ( )
inlinefinalnoexcept

Returns nullptr.

◆ as_date_time() [1/2]

TOML_CONST_INLINE_GETTER const toml::value< date_time > * array::as_date_time ( ) const
inlinefinalnoexcept

Returns nullptr.

◆ as_date_time() [2/2]

TOML_CONST_INLINE_GETTER toml::value< date_time > * array::as_date_time ( )
inlinefinalnoexcept

Returns nullptr.

◆ as_floating_point() [1/2]

TOML_CONST_INLINE_GETTER const toml::value< double > * array::as_floating_point ( ) const
inlinefinalnoexcept

Returns nullptr.

◆ as_floating_point() [2/2]

TOML_CONST_INLINE_GETTER toml::value< double > * array::as_floating_point ( )
inlinefinalnoexcept

Returns nullptr.

◆ as_integer() [1/2]

TOML_CONST_INLINE_GETTER const toml::value< int64_t > * array::as_integer ( ) const
inlinefinalnoexcept

Returns nullptr.

◆ as_integer() [2/2]

TOML_CONST_INLINE_GETTER toml::value< int64_t > * array::as_integer ( )
inlinefinalnoexcept

Returns nullptr.

◆ as_string() [1/2]

TOML_CONST_INLINE_GETTER const toml::value< std::string > * array::as_string ( ) const
inlinefinalnoexcept

Returns nullptr.

◆ as_string() [2/2]

TOML_CONST_INLINE_GETTER toml::value< std::string > * array::as_string ( )
inlinefinalnoexcept

Returns nullptr.

◆ as_table() [1/2]

TOML_CONST_INLINE_GETTER const table * array::as_table ( ) const
inlinefinalnoexcept

Returns nullptr.

◆ as_table() [2/2]

TOML_CONST_INLINE_GETTER table * array::as_table ( )
inlinefinalnoexcept

Returns nullptr.

◆ as_time() [1/2]

TOML_CONST_INLINE_GETTER const toml::value< time > * array::as_time ( ) const
inlinefinalnoexcept

Returns nullptr.

◆ as_time() [2/2]

TOML_CONST_INLINE_GETTER toml::value< time > * array::as_time ( )
inlinefinalnoexcept

Returns nullptr.

◆ at() [1/2]

TOML_NODISCARD TOML_EXPORTED_MEMBER_FUNCTION node & array::at ( size_t  index)

Gets a reference to the element at a specific index, throwing std::out_of_range if none existed.

◆ at() [2/2]

TOML_NODISCARD const node & array::at ( size_t  index) const
inline

Gets a reference to the element at a specific index, throwing std::out_of_range if none existed.

◆ back() [1/2]

TOML_NODISCARD const node & array::back ( ) const
inlinenoexcept

Returns a reference to the last element in the array.

◆ back() [2/2]

TOML_NODISCARD node & array::back ( )
inlinenoexcept

Returns a reference to the last element in the array.

◆ begin() [1/2]

TOML_NODISCARD const_iterator array::begin ( ) const
inlinenoexcept

Returns an iterator to the first element.

◆ begin() [2/2]

TOML_NODISCARD iterator array::begin ( )
inlinenoexcept

Returns an iterator to the first element.

◆ capacity()

TOML_NODISCARD size_t array::capacity ( ) const
inlinenoexcept

Returns the current max number of elements that may be held in the array's internal storage.

◆ cbegin()

TOML_NODISCARD const_iterator array::cbegin ( ) const
inlinenoexcept

Returns an iterator to the first element.

◆ cend()

TOML_NODISCARD const_iterator array::cend ( ) const
inlinenoexcept

Returns an iterator to one-past-the-last element.

◆ clear()

TOML_EXPORTED_MEMBER_FUNCTION void array::clear ( )
noexcept

Removes all elements from the array.

◆ emplace()

template<typename ElemType = void, typename... Args>
iterator array::emplace ( const_iterator  pos,
Args &&...  args 
)
inline

Emplaces a new element at a specific position in the array.

\detail \cpp auto arr = toml::array{ 1, 2 };

//add a string using std::string's substring constructor arr.emplace<std::string>(arr.cbegin() + 1, "this is not a drill"sv, 14, 5); std::cout << arr << "\n"; \ecpp

\out [ 1, 'drill', 2 ] \eout

Template Parameters
ElemTypetoml::table, toml::array, or any native TOML value type.
ArgsValue constructor argument types.
Parameters
posThe insertion position.
argsArguments to forward to the value's constructor.
Returns
An iterator to the inserted element.
Remarks
There is no difference between insert() and emplace() for trivial value types (floats, ints, bools).

◆ emplace_back()

template<typename ElemType = void, typename... Args>
decltype(auto) array::emplace_back ( Args &&...  args)
inline

Emplaces a new element at the end of the array.

\detail \cpp auto arr = toml::array{ 1, 2 }; arr.emplace_back<toml::array>(3, "four"sv); std::cout << arr << "\n"; \ecpp

\out [ 1, 2, [ 3, 'four' ] ] \eout

Template Parameters
ElemTypetoml::table, toml::array, or a native TOML value type
ArgsElement constructor argument types.
Parameters
argsArguments to forward to the elements's constructor.
Returns
A reference to the newly-constructed element.
Remarks
There is no difference between push_back() and emplace_back() For trivial value types (floats, ints, bools).

◆ empty()

TOML_NODISCARD bool array::empty ( ) const
inlinenoexcept

Returns true if the array is empty.

◆ end() [1/2]

TOML_NODISCARD const_iterator array::end ( ) const
inlinenoexcept

Returns an iterator to one-past-the-last element.

◆ end() [2/2]

TOML_NODISCARD iterator array::end ( )
inlinenoexcept

Returns an iterator to one-past-the-last element.

◆ erase() [1/2]

TOML_EXPORTED_MEMBER_FUNCTION iterator array::erase ( const_iterator  first,
const_iterator  last 
)
noexcept

Removes the elements in the range [first, last) from the array.

\detail \cpp auto arr = toml::array{ 1, "bad", "karma" 2 }; std::cout << arr << "\n";

arr.erase(arr.cbegin() + 1, arr.cbegin() + 3); std::cout << arr << "\n"; \ecpp

\out [ 1, 'bad', 'karma', 3 ] [ 1, 3 ] \eout

Parameters
firstIterator to the first element being erased.
lastIterator to the one-past-the-last element being erased.
Returns
Iterator to the first element immediately following the last removed element.

◆ erase() [2/2]

TOML_EXPORTED_MEMBER_FUNCTION iterator array::erase ( const_iterator  pos)
noexcept

Removes the specified element from the array.

\detail \cpp auto arr = toml::array{ 1, 2, 3 }; std::cout << arr << "\n";

arr.erase(arr.cbegin() + 1); std::cout << arr << "\n"; \ecpp

\out [ 1, 2, 3 ] [ 1, 3 ] \eout

Parameters
posIterator to the element being erased.
Returns
Iterator to the first element immediately following the removed element.

◆ flatten() [1/2]

TOML_EXPORTED_MEMBER_FUNCTION array & array::flatten ( ) &

Flattens this array, recursively hoisting the contents of child arrays up into itself.

\detail \cpp

auto arr = toml::array{ 1, 2, toml::array{ 3, 4, toml::array{ 5 } }, 6, toml::array{} }; std::cout << arr << "\n";

arr.flatten(); std::cout << arr << "\n"; \ecpp

\out [ 1, 2, [ 3, 4, [ 5 ] ], 6, [] ] [ 1, 2, 3, 4, 5, 6 ] \eout

Remarks
Arrays inside child tables are not flattened.
Returns
A reference to the array.

◆ flatten() [2/2]

array && array::flatten ( ) &&
inline

Flattens this array, recursively hoisting the contents of child arrays up into itself (rvalue overload).

◆ for_each() [1/4]

template<typename Func >
array && array::for_each ( Func &&  visitor) &&
inlinenoexcept

Invokes a visitor on each element in the array (rvalue overload).

◆ for_each() [2/4]

template<typename Func >
array & array::for_each ( Func &&  visitor) &
inlinenoexcept

Invokes a visitor on each element in the array.

Template Parameters
FuncA callable type invocable with one of the following signatures:
  • func(elem, index)
  • func(elem)
  • func(index, elem)
Where:
  • elem will recieve the element as it's concrete type with cvref-qualifications matching the array
  • index will recieve a size_t indicating the element's index
Visitors returning bool (or something convertible to bool) will cause iteration to stop if they return false.
Parameters
visitorThe visitor object.
Returns
A reference to the array.

\cpp toml::array arr{ 0, 1, 2, 3.0, "four", "five", 6 };

// select only the integers using a strongly-typed visitor arr.for_each([](toml::value<int64_t>& elem) { std::cout << elem << ", "; }); std::cout << "\n";

// select all the numeric values using a generic visitor + is_number<> metafunction arr.for_each([](auto&& elem) { if constexpr (toml::is_number<decltype(elem)>) std::cout << elem << ", "; }); std::cout << "\n";

// select all the numeric values until we encounter something non-numeric arr.for_each([](auto&& elem) { if constexpr (toml::is_number<decltype(elem)>) { std::cout << elem << ", "; return true; // "keep going" } else return false; // "stop!"

}); std::cout << "\n";

\ecpp \out 0, 1, 2, 6, 0, 1, 2, 3.0, 6, 0, 1, 2, 3.0, \eout

See also
node::visit()

◆ for_each() [3/4]

template<typename Func >
const array && array::for_each ( Func &&  visitor) const &&
inlinenoexcept

Invokes a visitor on each element in the array (const rvalue overload).

◆ for_each() [4/4]

template<typename Func >
const array & array::for_each ( Func &&  visitor) const &
inlinenoexcept

Invokes a visitor on each element in the array (const lvalue overload).

◆ front() [1/2]

TOML_NODISCARD const node & array::front ( ) const
inlinenoexcept

Returns a reference to the first element in the array.

◆ front() [2/2]

TOML_NODISCARD node & array::front ( )
inlinenoexcept

Returns a reference to the first element in the array.

◆ get() [1/2]

TOML_PURE_INLINE_GETTER const node * array::get ( size_t  index) const
inlinenoexcept

Gets a pointer to the element at a specific index (const overload).

Parameters
indexThe element's index.
Returns
A pointer to the element at the specified index if one existed, or nullptr.

◆ get() [2/2]

TOML_PURE_INLINE_GETTER node * array::get ( size_t  index)
inlinenoexcept

Gets a pointer to the element at a specific index.

\detail \cpp auto arr = toml::array{ 99, "bottles of beer on the wall" }; std::cout << "element [0] exists: "sv << !!arr.get(0) << "\n"; std::cout << "element [1] exists: "sv << !!arr.get(1) << "\n"; std::cout << "element [2] exists: "sv << !!arr.get(2) << "\n"; if (toml::node* val = arr.get(0)) std::cout << "element [0] is an "sv << val->type() << "\n"; \ecpp

\out element [0] exists: true element [1] exists: true element [2] exists: false element [0] is an integer \eout

Parameters
indexThe element's index.
Returns
A pointer to the element at the specified index if one existed, or nullptr.

◆ get_as() [1/2]

template<typename ElemType >
TOML_NODISCARD const impl::wrap_node< ElemType > * array::get_as ( size_t  index) const
inlinenoexcept

Gets a pointer to the element at a specific index if it is a particular type (const overload).

Template Parameters
ElemTypetoml::table, toml::array, or a native TOML value type
Parameters
indexThe element's index.
Returns
A pointer to the selected element if it existed and was of the specified type, or nullptr.

◆ get_as() [2/2]

template<typename ElemType >
TOML_NODISCARD impl::wrap_node< ElemType > * array::get_as ( size_t  index)
inlinenoexcept

Gets a pointer to the element at a specific index if it is a particular type.

\detail \cpp auto arr = toml::array{ 42, "is the meaning of life, apparently."sv }; if (toml::value<int64_t>* val = arr.get_as<int64_t>(0)) std::cout << "element [0] is an integer with value "sv << *val << "\n"; \ecpp

\out element [0] is an integer with value 42 \eout

Template Parameters
ElemTypetoml::table, toml::array, or a native TOML value type
Parameters
indexThe element's index.
Returns
A pointer to the selected element if it existed and was of the specified type, or nullptr.

◆ insert() [1/4]

template<typename ElemType >
iterator array::insert ( const_iterator  pos,
ElemType &&  val,
value_flags  flags = preserve_source_value_flags 
)
inline

Inserts a new element at a specific position in the array.

\detail \cpp auto arr = toml::array{ 1, 3 }; arr.insert(arr.cbegin() + 1, "two"); arr.insert(arr.cend(), toml::array{ 4, 5 }); std::cout << arr << "\n"; \ecpp

\out [ 1, 'two', 3, [ 4, 5 ] ] \eout

Template Parameters
ElemTypetoml::node, toml::node_view, toml::table, toml::array, or a native TOML value type (or a type promotable to one).
Parameters
posThe insertion position.
valThe node or value being inserted.
flagsValue flags to apply to new values.
Returns
\conditional_return{Valid input} An iterator to the newly-inserted element. \conditional_return{Input is a null toml::node_view} end()
Attention
The return value will always be end() if the input value was a null toml::node_view, because no insertion can take place. This is the only circumstance in which this can occur.

◆ insert() [2/4]

template<typename Iter >
iterator array::insert ( const_iterator  pos,
Iter  first,
Iter  last,
value_flags  flags = preserve_source_value_flags 
)
inline

Inserts a range of elements into the array at a specific position.

Template Parameters
IterAn iterator type. Must satisfy ForwardIterator.
Parameters
posThe insertion position.
firstIterator to the first node or value being inserted.
lastIterator to the one-past-the-last node or value being inserted.
flagsValue flags to apply to new values.
Returns
\conditional_return{Valid input} An iterator to the first newly-inserted element. \conditional_return{first >= last} A copy of pos \conditional_return{All objects in the range were null toml::node_views} A copy of pos

◆ insert() [3/4]

template<typename ElemType >
iterator array::insert ( const_iterator  pos,
size_t  count,
ElemType &&  val,
value_flags  flags = preserve_source_value_flags 
)
inline

Repeatedly inserts a new element starting at a specific position in the array.

\detail \cpp auto arr = toml::array{ "with an evil twinkle in its eye the goose said", "and immediately we knew peace was never an option." }; arr.insert(arr.cbegin() + 1, 3, "honk"); std::cout << arr << "\n"; \ecpp

\out [ 'with an evil twinkle in its eye the goose said', 'honk', 'honk', 'honk', 'and immediately we knew peace was never an option.' ] \eout

Template Parameters
ElemTypetoml::node, toml::node_view, toml::table, toml::array, or a native TOML value type (or a type promotable to one).
Parameters
posThe insertion position.
countThe number of times the node or value should be inserted.
valThe node or value being inserted.
flagsValue flags to apply to new values.
Returns
\conditional_return{Valid input} An iterator to the newly-inserted element. \conditional_return{count == 0} A copy of pos \conditional_return{Input is a null toml::node_view} end()
Attention
The return value will always be end() if the input value was a null toml::node_view, because no insertion can take place. This is the only circumstance in which this can occur.

◆ insert() [4/4]

template<typename ElemType >
iterator array::insert ( const_iterator  pos,
std::initializer_list< ElemType >  ilist,
value_flags  flags = preserve_source_value_flags 
)
inline

Inserts a range of elements into the array at a specific position.

Template Parameters
ElemTypetoml::node_view, toml::table, toml::array, or a native TOML value type (or a type promotable to one).
Parameters
posThe insertion position.
ilistAn initializer list containing the values to be inserted.
flagsValue flags to apply to new values.
Returns
\conditional_return{Valid input} An iterator to the first newly-inserted element. \conditional_return{Input list is empty} A copy of pos \conditional_return{All objects in the list were null toml::node_views} A copy of pos

◆ is_array()

TOML_CONST_INLINE_GETTER bool array::is_array ( ) const
inlinefinalnoexcept

Returns true.

◆ is_array_of_tables()

TOML_PURE_GETTER bool array::is_array_of_tables ( ) const
inlinefinalnoexcept

Returns true if the array contains only tables.

◆ is_boolean()

TOML_CONST_INLINE_GETTER bool array::is_boolean ( ) const
inlinefinalnoexcept

Returns false.

◆ is_date()

TOML_CONST_INLINE_GETTER bool array::is_date ( ) const
inlinefinalnoexcept

Returns false.

◆ is_date_time()

TOML_CONST_INLINE_GETTER bool array::is_date_time ( ) const
inlinefinalnoexcept

Returns false.

◆ is_floating_point()

TOML_CONST_INLINE_GETTER bool array::is_floating_point ( ) const
inlinefinalnoexcept

Returns false.

◆ is_homogeneous() [1/3]

TOML_PURE_GETTER TOML_EXPORTED_MEMBER_FUNCTION bool array::is_homogeneous ( node_type  ntype) const
finalnoexcept

◆ is_homogeneous() [2/3]

TOML_PURE_GETTER TOML_EXPORTED_MEMBER_FUNCTION bool array::is_homogeneous ( node_type  ntype,
const node *&  first_nonmatch 
) const
finalnoexcept

◆ is_homogeneous() [3/3]

TOML_PURE_GETTER TOML_EXPORTED_MEMBER_FUNCTION bool array::is_homogeneous ( node_type  ntype,
node *&  first_nonmatch 
)
finalnoexcept

◆ is_integer()

TOML_CONST_INLINE_GETTER bool array::is_integer ( ) const
inlinefinalnoexcept

Returns false.

◆ is_number()

TOML_CONST_INLINE_GETTER bool array::is_number ( ) const
inlinefinalnoexcept

Returns false.

◆ is_string()

TOML_CONST_INLINE_GETTER bool array::is_string ( ) const
inlinefinalnoexcept

Returns false.

◆ is_table()

TOML_CONST_INLINE_GETTER bool array::is_table ( ) const
inlinefinalnoexcept

Returns false.

◆ is_time()

TOML_CONST_INLINE_GETTER bool array::is_time ( ) const
inlinefinalnoexcept

Returns false.

◆ is_value()

TOML_CONST_INLINE_GETTER bool array::is_value ( ) const
inlinefinalnoexcept

Returns false.

◆ max_size()

TOML_NODISCARD size_t array::max_size ( ) const
inlinenoexcept

Returns the maximum number of elements that can be stored in an array on the current platform.

◆ operator=()

TOML_EXPORTED_MEMBER_FUNCTION array & array::operator= ( array &&  rhs)
noexcept

Move-assignment operator.

◆ operator[]() [1/2]

TOML_NODISCARD const node & array::operator[] ( size_t  index) const
inlinenoexcept

Gets a reference to the element at a specific index.

◆ operator[]() [2/2]

TOML_NODISCARD node & array::operator[] ( size_t  index)
inlinenoexcept

Gets a reference to the element at a specific index.

◆ pop_back()

TOML_EXPORTED_MEMBER_FUNCTION void array::pop_back ( )
noexcept

Removes the last element from the array.

◆ prune() [1/2]

array && array::prune ( bool  recursive = true) &&
inlinenoexcept

Removes empty child arrays and tables (rvalue overload).

Parameters
recursiveShould child arrays and tables themselves be pruned?
Returns
An rvalue reference to the array.

◆ prune() [2/2]

TOML_EXPORTED_MEMBER_FUNCTION array & array::prune ( bool  recursive = true) &
noexcept

Removes empty child arrays and tables.

\detail \cpp

auto arr = toml::array{ 1, 2, toml::array{ }, toml::array{ 3, toml::array{ } }, 4 }; std::cout << arr << "\n";

arr.prune(true); std::cout << arr << "\n"; \ecpp

\out [ 1, 2, [], [ 3, [] ], 4 ] [ 1, 2, [ 3 ], 4 ] \eout

Parameters
recursiveShould child arrays and tables themselves be pruned?
Returns
A reference to the array.

◆ push_back()

template<typename ElemType >
void array::push_back ( ElemType &&  val,
value_flags  flags = preserve_source_value_flags 
)
inline

Appends a new element to the end of the array.

\detail \cpp auto arr = toml::array{ 1, 2 }; arr.push_back(3); arr.push_back(4.0); arr.push_back(toml::array{ 5, "six"sv }); std::cout << arr << "\n"; \ecpp

\out [ 1, 2, 3, 4.0, [ 5, 'six' ] ] \eout

Template Parameters
ElemTypetoml::node, toml::node_view, toml::table, toml::array, or a native TOML value type
Parameters
valThe node or value being added.
flagsValue flags to apply to new values.
Attention
No insertion takes place if the input value is a null toml::node_view. This is the only circumstance in which this can occur.

◆ replace()

template<typename ElemType >
iterator array::replace ( const_iterator  pos,
ElemType &&  val,
value_flags  flags = preserve_source_value_flags 
)
inline

Replaces the element at a specific position in the array with a different value.

\detail \cpp auto arr = toml::array{ 1, 2, 3 }; std::cout << arr << "\n"; arr.replace(arr.cbegin() + 1, "two"); std::cout << arr << "\n"; \ecpp

\out [ 1, 2, 3 ] [ 1, 'two', 3 ] \eout

Template Parameters
ElemTypetoml::node, toml::node_view, toml::table, toml::array, or a native TOML value type (or a type promotable to one).
Parameters
posThe insertion position.
valThe node or value being inserted.
flagsValue flags to apply to new values.
Returns
\conditional_return{Valid input} An iterator to the replaced element. \conditional_return{Input is a null toml::node_view} end()
Attention
The return value will always be end() if the input value was a null toml::node_view, because no replacement can take place. This is the only circumstance in which this can occur.

◆ reserve()

TOML_EXPORTED_MEMBER_FUNCTION void array::reserve ( size_t  new_capacity)

Reserves internal storage capacity up to a pre-determined number of elements.

◆ resize()

template<typename ElemType >
void array::resize ( size_t  new_size,
ElemType &&  default_init_val,
value_flags  default_init_flags = preserve_source_value_flags 
)
inline

Resizes the array.

\detail \godbolt{W5zqx3}

\cpp auto arr = toml::array{ 1, 2, 3 }; std::cout << arr << "\n";

arr.resize(6, 42); std::cout << arr << "\n";

arr.resize(2, 0); std::cout << arr << "\n"; \ecpp

\out [ 1, 2, 3 ] [ 1, 2, 3, 42, 42, 42 ] [ 1, 2 ] \eout

Template Parameters
ElemTypetoml::node, toml::table, toml::array, or a native TOML value type (or a type promotable to one).
Parameters
new_sizeThe number of elements the array will have after resizing.
default_init_valThe node or value used to initialize new elements if the array needs to grow.
default_init_flagsValue flags to apply to new values created if new elements are created by growing.

◆ shrink_to_fit()

TOML_EXPORTED_MEMBER_FUNCTION void array::shrink_to_fit ( )

Requests the removal of any unused internal storage capacity.

◆ size()

TOML_NODISCARD size_t array::size ( ) const
inlinenoexcept

Returns the number of elements in the array.

◆ TOML_ASYMMETRICAL_EQUALITY_OPS() [1/2]

array::TOML_ASYMMETRICAL_EQUALITY_OPS ( const array ,
const std::initializer_list< T > &  ,
template< typename T >   
)

◆ TOML_ASYMMETRICAL_EQUALITY_OPS() [2/2]

array::TOML_ASYMMETRICAL_EQUALITY_OPS ( const array ,
const std::vector< T > &  ,
template< typename T >   
)

◆ TOML_CONSTRAINED_TEMPLATE()

array::TOML_CONSTRAINED_TEMPLATE ( (sizeof...(ElemTypes) > 0||!std::is_same_v< impl::remove_cvref< ElemType >, array >)  ,
typename ElemType  ,
typename...  ElemTypes 
) &&

Constructs an array with one or more initial elements.

\detail \cpp auto arr = toml::array{ 1, 2.0, "three"sv, toml::array{ 4, 5 } }; std::cout << arr << "\n"; \ecpp

\out [ 1, 2.0, 'three', [ 4, 5 ] ] \eout

Remarks

If you need to construct an array with one child array element, the array's move constructor will take precedence and perform a move-construction instead. You can use toml::inserter to suppress this behaviour: \cpp // desired result: [ [ 42 ] ] auto bad = toml::array{ toml::array{ 42 } } auto good = toml::array{ toml::inserter{ toml::array{ 42 } } } std::cout << "bad: " << bad << "\n"; std::cout << "good:" << good << "\n"; \ecpp

\out bad: [ 42 ] good: [ [ 42 ] ] \eout

Template Parameters
ElemTypeOne of the TOML node or value types (or a type promotable to one).
ElemTypesOne of the TOML node or value types (or a type promotable to one).
Parameters
valThe node or value used to initialize element 0.
valsThe nodes or values used to initialize elements 1...N.

◆ truncate()

TOML_EXPORTED_MEMBER_FUNCTION void array::truncate ( size_t  new_size)

Shrinks the array to the given size.

\detail \godbolt{rxEzK5}

\cpp auto arr = toml::array{ 1, 2, 3 }; std::cout << arr << "\n";

arr.truncate(5); // no-op std::cout << arr << "\n";

arr.truncate(1); std::cout << arr << "\n"; \ecpp

\out [ 1, 2, 3 ] [ 1, 2, 3 ] [ 1] \eout

Remarks
Does nothing if the requested size is larger than or equal to the current size.

◆ type()

TOML_CONST_INLINE_GETTER node_type array::type ( ) const
inlinefinalnoexcept

Returns #toml::node_type::array.

Friends And Related Symbol Documentation

◆ operator!=

TOML_NODISCARD friend bool operator!= ( const array lhs,
const array rhs 
)
friend

Inequality operator.

Parameters
lhsThe LHS array.
rhsThe RHS array.
Returns
True if the arrays did not contain the same elements.

◆ operator<<

std::ostream & operator<< ( std::ostream &  lhs,
const array rhs 
)
friend

Prints the array out to a stream as formatted TOML.

\availability This operator is only available when TOML_ENABLE_FORMATTERS is enabled.

◆ operator== [1/3]

TOML_NODISCARD friend bool operator== ( const array lhs,
const array rhs 
)
friend

Equality operator.

Parameters
lhsThe LHS array.
rhsThe RHS array.
Returns
True if the arrays contained the same elements.

◆ operator== [2/3]

template<typename T >
TOML_NODISCARD friend bool operator== ( const array lhs,
const std::initializer_list< T > &  rhs 
)
friend

Initializer list equality operator.

◆ operator== [3/3]

template<typename T >
TOML_NODISCARD friend bool operator== ( const array lhs,
const std::vector< T > &  rhs 
)
friend

Vector equality operator.

Member Data Documentation

◆ vals

ElemTypes&& array::vals

The documentation for this class was generated from the following file: