Skip to content

Commit

Permalink
createInteger()
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Smith committed Apr 3, 2024
1 parent b617ab7 commit 59a06c8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
3 changes: 3 additions & 0 deletions six/modules/c++/six/include/six/XmlLite.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ struct SIX_SIX_API XmlLite final
xml::lite::Element& createInt(const xml::lite::QName& name, int32_t p, xml::lite::Element& parent) const;
xml::lite::Element& createLong(const xml::lite::QName& name, const std::string& p, xml::lite::Element& parent) const;
xml::lite::Element& createLong(const xml::lite::QName& name, int64_t p, xml::lite::Element& parent) const;
xml::lite::Element& createInteger(const xml::lite::QName& name, const std::string& p, xml::lite::Element& parent) const;
xml::lite::Element& createInteger(const xml::lite::QName& name, const XsInteger& p, xml::lite::Element& parent) const;

xml::lite::Element& createDouble(const xml::lite::QName&, double p, xml::lite::Element& parent) const;
xml::lite::Element& createDouble(const xml::lite::QName&, const std::optional<double>& p, xml::lite::Element& parent) const;
Expand Down Expand Up @@ -310,6 +312,7 @@ struct SIX_SIX_API XmlLite final
private:
xml::lite::Element& createInt_(const std::string& name, int32_t p, xml::lite::Element& parent) const;
xml::lite::Element& createLong_(const std::string& name, int64_t p, xml::lite::Element& parent) const;
xml::lite::Element& createInteger_(const std::string& name, const XsInteger& p, xml::lite::Element& parent) const;
xml::lite::Element& createString_(const std::string& name, const std::string& p, xml::lite::Element& parent) const;
xml::lite::QName makeQName(const std::string& name) const;

Expand Down
2 changes: 2 additions & 0 deletions six/modules/c++/six/include/six/XsInteger.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <stdexcept>
#include <limits>
#include <type_traits>
#include <tuple>

#include <gsl/gsl.h>
#include <config/compiler_extensions.h>
Expand Down Expand Up @@ -114,6 +115,7 @@ namespace details
// Be sure we can round-trip the value; to_int64() does validation.
const auto str_value = std::to_string(v);
const auto i64 = to_int64(str_value); // validation
std::ignore = i64; // compiler warning
assert(v == i64);
assert(str_value == std::to_string(i64));
return str_value;
Expand Down
21 changes: 21 additions & 0 deletions six/modules/c++/six/source/XmlLite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ inline std::string toString_(const xml::lite::QName& name, const std::u8string&
{
return toString(name, str::to_native(v), parent);
}
template<>
inline std::string toString_(const xml::lite::QName& name, const XsInteger& v, const xml::lite::Element& parent)
{
return toString(name, to_string(v), parent);
}

template<typename T, typename ToString>
static xml::lite::Element& createValue(const xml::lite::QName& name,
Expand Down Expand Up @@ -278,6 +283,22 @@ xml::lite::Element& XmlLite::createLong_(const std::string& name, int64_t p, xml
return createLong(makeQName(name), p, parent);
}

// https://www.oreilly.com/library/view/xml-schema/0596002521/re80.html
xml::lite::Element& XmlLite::createInteger(const xml::lite::QName& name, const XsInteger& p, xml::lite::Element& parent) const
{
return createValue(name, p, parent, "xs:integer"); // unbounded, not xs:int
}
xml::lite::Element& XmlLite::createInteger(const xml::lite::QName& name, const std::string& p, xml::lite::Element& parent) const
{
auto& elem = newElement(name, p, parent);
addClassAttributes(elem, "xs:integer"); // unbounded, not xs:int
return elem;
}
xml::lite::Element& XmlLite::createInteger_(const std::string& name, const XsInteger& p, xml::lite::Element& parent) const
{
return createInteger(makeQName(name), p, parent);
}

xml::lite::Element& XmlLite::createDouble(const xml::lite::QName& name, double p, xml::lite::Element& parent) const
{
p = value(p); // be sure this is initialized; throws if not
Expand Down

0 comments on commit 59a06c8

Please sign in to comment.