Class array
Synopsis
#include <include/cpptoml.h>
class array : public base
Description
No description yet.
Mentioned in
- Getting Started / Arrays of Values
Inheritance
Ancestors: base
Methods
array_of | Obtains an array of value<T>s | |
at | ||
begin overload | ||
clear | Clear the array | |
clone | ||
end overload | ||
erase | Erase an element from the array | |
get overload | Obtains the array (vector) of base values. | |
get overload | Obtains the array (vector) of base values | |
get_array_of overload | Obtains a option<vector\<T>> | |
insert overload | Insert a value into the array | |
insert overload | Insert an array into the array | |
insert overload | Convenience function for inserting a simple element in the array | |
is_array | Determines if the TOML element is an array of "leaf" elements. | |
nested_array | Obtains an array of arrays | |
push_back overload | Add a value to the end of the array | |
push_back overload | Add an array to the end of the array | |
push_back overload | Convenience function for adding a simple element to the end of the array. | |
reserve | Reserve space for n values. |
Source
Lines 774-1019 in include/cpptoml.h.
class array : public base
{
public:
friend std::shared_ptr<array> make_array();
std::shared_ptr<base> clone() const override;
virtual bool is_array() const override
{
return true;
}
using size_type = std::size_t;
/**
* arrays can be iterated over
*/
using iterator = std::vector<std::shared_ptr<base>>::iterator;
/**
* arrays can be iterated over. Const version.
*/
using const_iterator = std::vector<std::shared_ptr<base>>::const_iterator;
iterator begin()
{
return values_.begin();
}
const_iterator begin() const
{
return values_.begin();
}
iterator end()
{
return values_.end();
}
const_iterator end() const
{
return values_.end();
}
/**
* Obtains the array (vector) of base values.
*/
std::vector<std::shared_ptr<base>>& get()
{
return values_;
}
/**
* Obtains the array (vector) of base values. Const version.
*/
const std::vector<std::shared_ptr<base>>& get() const
{
return values_;
}
std::shared_ptr<base> at(size_t idx) const
{
return values_.at(idx);
}
/**
* Obtains an array of value<T>s. Note that elements may be
* nullptr if they cannot be converted to a value<T>.
*/
template <class T>
std::vector<std::shared_ptr<value<T>>> array_of() const
{
std::vector<std::shared_ptr<value<T>>> result(values_.size());
std::transform(values_.begin(), values_.end(), result.begin(),
[&](std::shared_ptr<base> v) { return v->as<T>(); });
return result;
}
/**
* Obtains a option<vector<T>>. The option will be empty if the array
* contains values that are not of type T.
*/
template <class T>
inline typename array_of_trait<T>::return_type get_array_of() const
{
std::vector<T> result;
result.reserve(values_.size());
for (const auto& val : values_)
{
if (auto v = val->as<T>())
result.push_back(v->get());
else
return {};
}
return {std::move(result)};
}
/**
* Obtains an array of arrays. Note that elements may be nullptr
* if they cannot be converted to a array.
*/
std::vector<std::shared_ptr<array>> nested_array() const
{
std::vector<std::shared_ptr<array>> result(values_.size());
std::transform(values_.begin(), values_.end(), result.begin(),
[&](std::shared_ptr<base> v) -> std::shared_ptr<array> {
if (v->is_array())
return std::static_pointer_cast<array>(v);
return std::shared_ptr<array>{};
});
return result;
}
/**
* Add a value to the end of the array
*/
template <class T>
void push_back(const std::shared_ptr<value<T>>& val)
{
if (values_.empty() || values_[0]->as<T>())
{
values_.push_back(val);
}
else
{
throw array_exception{"Arrays must be homogenous."};
}
}
/**
* Add an array to the end of the array
*/
void push_back(const std::shared_ptr<array>& val)
{
if (values_.empty() || values_[0]->is_array())
{
values_.push_back(val);
}
else
{
throw array_exception{"Arrays must be homogenous."};
}
}
/**
* Convenience function for adding a simple element to the end
* of the array.
*/
template <class T>
void push_back(T&& val, typename value_traits<T>::type* = 0)
{
push_back(make_value(std::forward<T>(val)));
}
/**
* Insert a value into the array
*/
template <class T>
iterator insert(iterator position, const std::shared_ptr<value<T>>& value)
{
if (values_.empty() || values_[0]->as<T>())
{
return values_.insert(position, value);
}
else
{
throw array_exception{"Arrays must be homogenous."};
}
}
/**
* Insert an array into the array
*/
iterator insert(iterator position, const std::shared_ptr<array>& value)
{
if (values_.empty() || values_[0]->is_array())
{
return values_.insert(position, value);
}
else
{
throw array_exception{"Arrays must be homogenous."};
}
}
/**
* Convenience function for inserting a simple element in the array
*/
template <class T>
iterator insert(iterator position, T&& val,
typename value_traits<T>::type* = 0)
{
return insert(position, make_value(std::forward<T>(val)));
}
/**
* Erase an element from the array
*/
iterator erase(iterator position)
{
return values_.erase(position);
}
/**
* Clear the array
*/
void clear()
{
values_.clear();
}
/**
* Reserve space for n values.
*/
void reserve(size_type n)
{
values_.reserve(n);
}
private:
#if defined(CPPTOML_NO_RTTI)
array() : base(base_type::ARRAY)
{
// empty
}
#else
array() = default;
#endif
template <class InputIterator>
array(InputIterator begin, InputIterator end) : values_{begin, end}
{
// nothing
}
array(const array& obj) = delete;
array& operator=(const array& obj) = delete;
std::vector<std::shared_ptr<base>> values_;
};