Skip to content

Commit

Permalink
use new xml.lite APIs to simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Smith committed Dec 10, 2021
1 parent 01f811e commit 5d641a4
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 20 deletions.
2 changes: 2 additions & 0 deletions six/modules/c++/six/include/six/XmlLite.h
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ struct XmlLite final
*/
static xml::lite::Element& require(xml::lite::Element* element, const std::string& name);

static void setAttribute_(xml::lite::Element& e, const std::string& name, const std::string& v, const std::string& uri);

private:
xml::lite::Element* createInt_(const std::string& name, const std::string& uri, int p, xml::lite::Element* parent) const;
xml::lite::Element* createInt_(const std::string& name, const std::string& uri, const std::string& p, xml::lite::Element* parent) const;
Expand Down
79 changes: 59 additions & 20 deletions six/modules/c++/six/source/XmlLite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,15 @@ xml::lite::Element& XmlLite::newElement(const std::string& name, const std::stri
return *newElement(name, uri, "", &parent);
}

static void addClassAttributes_(xml::lite::Element& elem, const std::string& type, const std::string& uri)
{
XmlLite::setAttribute_(elem, "class", type, uri);
}
void XmlLite::addClassAttributes(xml::lite::Element& elem, const std::string& type) const
{
if (mAddClassAttributes)
{
setAttribute_(&elem, "class", type, getDefaultURI());
addClassAttributes_(elem, type, getDefaultURI());
}
}

Expand Down Expand Up @@ -143,22 +147,54 @@ static std::string toString(const std::string& name, T p, const xml::lite::Eleme
throw except::Exception(Ctxt(message));
}
}
template<typename T>
inline std::string toString_(const std::string& name, const T& v, const xml::lite::Element& parent)
{
return toString(name, v, &parent);
}

template<typename T>
static xml::lite::Element& createValue(const std::string& name, const std::string& uri,
const T& v, xml::lite::Element& parent,
bool addClassAttributes, const std::string& type, const std::string& attributeUri)
{
const auto toString = [&](const T& v) { return toString_(name, v, parent); };
auto& elem = xml::lite::createElement(name, uri, v, parent, toString);
if (addClassAttributes)
{
addClassAttributes_(elem, type, attributeUri);
}

return elem;
}

template<typename T>
static xml::lite::Element* createOptionalValue(const std::string& name, const std::string& uri,
const std::optional<T>& v, xml::lite::Element& parent,
bool addClassAttributes, const std::string& type, const std::string& attributeUri)
{
if (v.has_value())
{
return &createValue(name, uri, v.value(), parent, addClassAttributes, type, attributeUri);
}
return nullptr;
}


xml::lite::Element* XmlLite::createInt_(const std::string& name, const std::string& uri,
int p, xml::lite::Element* parent) const
{
const auto elementValue = toString(name, p, parent);
return createInt(name, uri, elementValue, parent);
assert(parent != nullptr);
return &createValue(name, uri, p, *parent, mAddClassAttributes, "xs:int", getDefaultURI());
}

xml::lite::Element* XmlLite::createInt_(const std::string& name, const std::string& uri,
const std::string& p, xml::lite::Element* parent) const
const std::string& p, xml::lite::Element* parent) const
{
assert(parent != nullptr);
xml::lite::Element* const elem = newElement(name, uri, p, parent);
addClassAttributes(*elem, "xs:int");
return elem;
}

xml::lite::Element* XmlLite::createInt_(const std::string& name, int p, xml::lite::Element* parent) const
{
return createInt(name, mDefaultURI, p, parent);
Expand All @@ -167,23 +203,20 @@ xml::lite::Element* XmlLite::createInt_(const std::string& name, int p, xml::lit
xml::lite::Element* XmlLite::createDouble(const std::string& name,
const std::string& uri, double p, xml::lite::Element* parent) const
{
p = value(p); // be sure this is initialized; throws if not

const auto elementValue = toString(name, p, parent);
xml::lite::Element* elem = newElement(name, uri, elementValue, parent);
addClassAttributes(*elem, "xs:double");
assert(parent != nullptr);

return elem;
p = value(p); // be sure this is initialized; throws if not
return &createValue(name, uri, p, *parent, mAddClassAttributes, "xs::double", getDefaultURI());
}
xml::lite::Element& XmlLite::createDouble(const std::string& name, double p,
xml::lite::Element& parent) const
{
return * createDouble(name, mDefaultURI, p, &parent);
return *createDouble(name, mDefaultURI, p, &parent);
}
xml::lite::Element* XmlLite::createDouble(const std::string& name, const std::optional<double>& p,
xml::lite::Element* parent) const
{
return & createDouble(name, p.value(), *parent);
return createDouble(name, mDefaultURI, p.value(), parent);
}

xml::lite::Element* XmlLite::createOptionalDouble(const std::string& name,
Expand All @@ -194,17 +227,18 @@ xml::lite::Element* XmlLite::createOptionalDouble(const std::string& name,
xml::lite::Element* XmlLite::createOptionalDouble(const std::string& name,
const std::string& uri, const std::optional<double>& p, xml::lite::Element* parent) const
{
return p.has_value() ? createDouble(name, uri, *p, parent) : nullptr;
assert(parent != nullptr);
return createOptionalValue(name, uri, p, *parent, mAddClassAttributes, "xs::double", getDefaultURI());
}
xml::lite::Element* XmlLite::createOptionalDouble(const std::string& name, const double& p,
xml::lite::Element* parent) const
{
return Init::isDefined(p) ? createDouble(name, p, parent) : nullptr;
return createOptionalDouble(name, mDefaultURI, p, parent);
}
xml::lite::Element* XmlLite::createOptionalDouble(const std::string& name, const std::optional<double>& p,
xml::lite::Element* parent) const
{
return p.has_value() ? createOptionalDouble(name, *p, parent) : nullptr;
return createOptionalDouble(name, mDefaultURI, p, parent);
}

xml::lite::Element* XmlLite::createBooleanType(const std::string& name,
Expand Down Expand Up @@ -284,15 +318,20 @@ xml::lite::Element& XmlLite::require(xml::lite::Element* element, const std::str
return *element;
}

void XmlLite::setAttribute_(xml::lite::Element* e, const std::string& name,
void XmlLite::setAttribute_(xml::lite::Element& e, const std::string& name,
const std::string& v, const std::string& uri)
{
assert(e != nullptr);
xml::lite::AttributeNode node;
node.setUri(uri);
node.setQName(name);
node.setValue(v);
e->getAttributes().add(node);
e.getAttributes().add(node);
}
void XmlLite::setAttribute_(xml::lite::Element* e, const std::string& name,
const std::string& v, const std::string& uri)
{
assert(e != nullptr);
setAttribute_(*e, name, v, uri);
}

template<typename TGetValue>
Expand Down

0 comments on commit 5d641a4

Please sign in to comment.