Skip to content

Commit

Permalink
minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
marzer committed Nov 7, 2021
1 parent 76e681d commit 9783a94
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 260 deletions.
174 changes: 81 additions & 93 deletions include/toml++/impl/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,84 @@ TOML_NAMESPACE_START
/// \name Array operations
/// @{

/// \brief 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
///
/// \param index The element's index.
///
/// \returns A pointer to the element at the specified index if one existed, or nullptr.
TOML_PURE_INLINE_GETTER
node* get(size_t index) noexcept
{
return index < elems_.size() ? elems_[index].get() : nullptr;
}

/// \brief Gets a pointer to the element at a specific index (const overload).
///
/// \param index The element's index.
///
/// \returns A pointer to the element at the specified index if one existed, or nullptr.
TOML_PURE_INLINE_GETTER
const node* get(size_t index) const noexcept
{
return const_cast<array&>(*this).get(index);
}

/// \brief 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
///
/// \tparam ElemType toml::table, toml::array, or a native TOML value type
/// \param index The element's index.
///
/// \returns A pointer to the selected element if it existed and was of the specified type, or nullptr.
template <typename ElemType>
TOML_NODISCARD
impl::wrap_node<ElemType>* get_as(size_t index) noexcept
{
if (auto val = get(index))
return val->template as<ElemType>();
return nullptr;
}

/// \brief Gets a pointer to the element at a specific index if it is a particular type (const overload).
///
/// \tparam ElemType toml::table, toml::array, or a native TOML value type
/// \param index The element's index.
///
/// \returns A pointer to the selected element if it existed and was of the specified type, or nullptr.
template <typename ElemType>
TOML_NODISCARD
const impl::wrap_node<ElemType>* get_as(size_t index) const noexcept
{
return const_cast<array&>(*this).template get_as<ElemType>(index);
}

/// \brief Gets a reference to the element at a specific index.
TOML_NODISCARD
node& operator[](size_t index) noexcept
Expand All @@ -646,28 +724,18 @@ TOML_NAMESPACE_START
return *elems_[index];
}

#if TOML_COMPILER_EXCEPTIONS

/// \brief Gets a reference to the element at a specific index, throwing `std::out_of_range` if none existed.
///
/// \availability This function is only available if you compile with exceptions enabled.
TOML_NODISCARD
node& at(size_t index)
{
return *elems_.at(index);
}
TOML_API
node& at(size_t index);

/// \brief Gets a reference to the element at a specific index, throwing `std::out_of_range` if none existed.
///
/// \availability This function is only available if you compile with exceptions enabled.
TOML_NODISCARD
const node& at(size_t index) const
{
return *elems_.at(index);
return const_cast<array&>(*this).at(index);
}

#endif // TOML_COMPILER_EXCEPTIONS

/// \brief Returns a reference to the first element in the array.
TOML_NODISCARD
node& front() noexcept
Expand Down Expand Up @@ -1228,86 +1296,6 @@ TOML_NAMESPACE_START
elems_.pop_back();
}

/// \brief Gets 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
///
/// \param index The element's index.
///
/// \returns A pointer to the element at the specified index if one existed, or nullptr.
TOML_NODISCARD
node* get(size_t index) noexcept
{
return index < elems_.size() ? elems_[index].get() : nullptr;
}

/// \brief Gets the element at a specific index (const overload).
///
/// \param index The element's index.
///
/// \returns A pointer to the element at the specified index if one existed, or nullptr.
TOML_NODISCARD
const node* get(size_t index) const noexcept
{
return index < elems_.size() ? elems_[index].get() : nullptr;
}

/// \brief Gets 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
///
/// \tparam ElemType toml::table, toml::array, or a native TOML value type
/// \param index The element's index.
///
/// \returns A pointer to the selected element if it existed and was of the specified type, or nullptr.
template <typename ElemType>
TOML_NODISCARD
impl::wrap_node<ElemType>* get_as(size_t index) noexcept
{
if (auto val = get(index))
return val->as<ElemType>();
return nullptr;
}

/// \brief Gets the element at a specific index if it is a particular type (const overload).
///
/// \tparam ElemType toml::table, toml::array, or a native TOML value type
/// \param index The element's index.
///
/// \returns A pointer to the selected element if it existed and was of the specified type, or nullptr.
template <typename ElemType>
TOML_NODISCARD
const impl::wrap_node<ElemType>* get_as(size_t index) const noexcept
{
if (auto val = get(index))
return val->as<ElemType>();
return nullptr;
}

/// \brief Flattens this array, recursively hoisting the contents of child arrays up into itself.
///
/// \detail \cpp
Expand Down
64 changes: 36 additions & 28 deletions include/toml++/impl/array.inl
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,6 @@
#include "array.h"
#include "header_start.h"

TOML_ANON_NAMESPACE_START
{
template <typename T, typename U>
TOML_INTERNAL_LINKAGE
bool array_is_homogeneous(T & elements, node_type ntype, U & first_nonmatch) noexcept
{
if (elements.empty())
{
first_nonmatch = {};
return false;
}
if (ntype == node_type::none)
ntype = elements[0]->type();
for (const auto& val : elements)
{
if (val->type() != ntype)
{
first_nonmatch = val.get();
return false;
}
}
return true;
}
}
TOML_ANON_NAMESPACE_END;

TOML_NAMESPACE_START
{
TOML_EXTERNAL_LINKAGE
Expand Down Expand Up @@ -125,13 +99,47 @@ TOML_NAMESPACE_START
TOML_EXTERNAL_LINKAGE
bool array::is_homogeneous(node_type ntype, node * &first_nonmatch) noexcept
{
return TOML_ANON_NAMESPACE::array_is_homogeneous(elems_, ntype, first_nonmatch);
if (elems_.empty())
{
first_nonmatch = {};
return false;
}
if (ntype == node_type::none)
ntype = elems_[0]->type();
for (const auto& val : elems_)
{
if (val->type() != ntype)
{
first_nonmatch = val.get();
return false;
}
}
return true;
}

TOML_EXTERNAL_LINKAGE
bool array::is_homogeneous(node_type ntype, const node*& first_nonmatch) const noexcept
{
return TOML_ANON_NAMESPACE::array_is_homogeneous(elems_, ntype, first_nonmatch);
node* fnm = nullptr;
const auto result = const_cast<array&>(*this).is_homogeneous(ntype, fnm);
first_nonmatch = fnm;
return result;
}

TOML_EXTERNAL_LINKAGE
node& array::at(size_t index)
{
#if TOML_COMPILER_EXCEPTIONS

return *elems_.at(index);

#else

auto n = get(index);
TOML_ASSERT(n && "element index not found in array!");
return *n;

#endif
}

TOML_EXTERNAL_LINKAGE
Expand Down
23 changes: 10 additions & 13 deletions include/toml++/impl/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -672,8 +672,10 @@ TOML_NAMESPACE_START
///
/// \returns A pointer to the node at the specified key, or nullptr.
TOML_NODISCARD
TOML_API
node* get(std::wstring_view key);
node* get(std::wstring_view key)
{
return get(impl::narrow(key));
}

/// \brief Gets the node at a specific key (const overload).
///
Expand Down Expand Up @@ -765,18 +767,12 @@ TOML_NAMESPACE_START

#endif // TOML_ENABLE_WINDOWS_COMPAT

#if TOML_COMPILER_EXCEPTIONS

/// \brief Gets a reference to the element at a specific key, throwing `std::out_of_range` if none existed.
///
/// \availability This function is only available if you compile with exceptions enabled.
TOML_NODISCARD
TOML_API
node& at(std::string_view key);

/// \brief Gets a reference to the element at a specific key, throwing `std::out_of_range` if none existed.
///
/// \availability This function is only available if you compile with exceptions enabled.
TOML_NODISCARD
const node& at(std::string_view key) const
{
Expand All @@ -787,22 +783,23 @@ TOML_NAMESPACE_START

/// \brief Gets a reference to the element at a specific key, throwing `std::out_of_range` if none existed.
///
/// \availability This function is only available if you compile with exceptions and #TOML_ENABLE_WINDOWS_COMPAT enabled.
/// \availability This overload is only available when #TOML_ENABLE_WINDOWS_COMPAT is enabled.
TOML_NODISCARD
TOML_API
node& at(std::wstring_view key);
node& at(std::wstring_view key)
{
return at(impl::narrow(key));
}

/// \brief Gets a reference to the element at a specific key, throwing `std::out_of_range` if none existed.
///
/// \availability This function is only available if you compile with exceptions and #TOML_ENABLE_WINDOWS_COMPAT enabled.
/// \availability This overload is only available when #TOML_ENABLE_WINDOWS_COMPAT is enabled.
TOML_NODISCARD
const node& at(std::wstring_view key) const
{
return const_cast<table&>(*this).at(key);
}

#endif // TOML_ENABLE_WINDOWS_COMPAT
#endif // TOML_COMPILER_EXCEPTIONS

/// @}

Expand Down
29 changes: 8 additions & 21 deletions include/toml++/impl/table.inl
Original file line number Diff line number Diff line change
Expand Up @@ -141,42 +141,29 @@ TOML_NAMESPACE_START
return nullptr;
}

#if TOML_ENABLE_WINDOWS_COMPAT

TOML_EXTERNAL_LINKAGE
node* table::get(std::wstring_view key)
node& table::at(std::string_view key)
{
return get(impl::narrow(key));
}

#endif
auto n = get(key);

#if TOML_COMPILER_EXCEPTIONS

TOML_EXTERNAL_LINKAGE
node& table::at(std::string_view key)
{
auto n = get(key);
if (!n)
{
auto err = "key '"s;
err.append(key);
err.append("' not found in table"sv);
throw std::out_of_range{ err };
}
return *n;
}

#if TOML_ENABLE_WINDOWS_COMPAT
#else

TOML_EXTERNAL_LINKAGE
node& table::at(std::wstring_view key)
{
return at(impl::narrow(key));
}
TOML_ASSERT(n && "key not found in table!");

#endif // TOML_ENABLE_WINDOWS_COMPAT
#endif // TOML_COMPILER_EXCEPTIONS
#endif

return *n;
}

TOML_EXTERNAL_LINKAGE
bool table::equal(const table& lhs, const table& rhs) noexcept
Expand Down
Loading

0 comments on commit 9783a94

Please sign in to comment.