Skip to content

Commit

Permalink
[DLG] Added try_get_child_value to Xml_element
Browse files Browse the repository at this point in the history
  • Loading branch information
diegolodares committed Oct 17, 2023
1 parent 2929e8d commit 12d622c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lion/foundation/type_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,12 @@ template<class Class, typename SingleArg,
constexpr auto is_not_copy_ctor() { return false; }

#ifdef _MSC_VER
// In VS2017-2019 the "is_not_copy_ctor()" constexpr function does not
// work, we'll use a macro until the compiler catches up!
#define WINDOWS_IS_NOT_COPY_CTOR(CLASSNAME) \
template<typename... Args, \
typename std::enable_if_t<(sizeof...(Args) > 1u) || \
!std::is_same_v<std::decay_t<std::tuple_element_t<0u, std::tuple<Args..., void> > >, CLASSNAME> >* = nullptr>
// In VS2017-2019 the "is_not_copy_ctor()" constexpr function does not
// work, we'll use a macro until the compiler catches up!
#define WINDOWS_IS_NOT_COPY_CTOR(CLASSNAME) \
template<typename... Args, \
typename std::enable_if_t<(sizeof...(Args) > 1u) || \
!std::is_same_v<std::decay_t<std::tuple_element_t<0u, std::tuple<Args..., void> > >, CLASSNAME> >* = nullptr>
#endif


Expand Down
36 changes: 36 additions & 0 deletions lion/io/Xml_element.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ class Xml_element
//! @param[in] simply pass bool() to overload this function
bool get_value(bool&&) { return to_bool(get_value()); }

//! Get value as an std::array<double, N>
//! @param[in] simply pass and std::array<double, N>{} to overload this function
template<std::size_t N>
std::array<double, N> get_value(std::array<double, N>&&)
{
const auto vec = get_value(std::vector<double>{});
if (vec.size() != N) {
throw std::runtime_error("Xml_element::get_value(std::array<double, N>): incorrect size.");
}
std::array<double, N> arr{};
std::copy_n(vec.cbegin(), N, arr.begin());
return arr;
}

//! Set the value from string
//! @param[in] val: new string value
Xml_element& set_value(const std::string& val) { _e->SetText(val.c_str()); return *this;}
Expand All @@ -79,6 +93,28 @@ class Xml_element
//! See if a child exists
bool has_child(const std::string& name) const;

//! Try to get the value of a child, or return a default one
template<typename ValueType>
ValueType try_get_child_value(const std::string &childname, ValueType default_value,
bool warn_if_returning_default_value = true) const
{
if (has_child(childname)) {
return get_child(childname).get_value(ValueType{});
}
else {
if (warn_if_returning_default_value) {
std::cerr << "Xml_element::try_get_child_value: warning, xml element \""
<< get_name()
<< "\" does not contain child \""
<< childname << "\", returning a default value of \""
<< default_value
<< "\"."
<< std::endl;
}
return default_value;
}
}

//! See if parent exists
bool has_parent() const { return (_e->Parent()->ToElement() ? true : false); }

Expand Down

0 comments on commit 12d622c

Please sign in to comment.