Skip to content

Commit

Permalink
Generate parser an serializer functions for enums
Browse files Browse the repository at this point in the history
  • Loading branch information
martonmiklos committed Sep 14, 2020
1 parent 8d4e145 commit 970bec3
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
72 changes: 71 additions & 1 deletion kxml_compiler/creator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,17 @@ void Creator::createClass(const Schema::Element &element)
}
}

foreach (KODE::Enum e, description.enums()) {
for (const auto e : description.enums()) {
c.addEnum(e);

auto enumParserFunction = enumParserMethod(e);
enumParserFunction.setReturnType(e.name() + "::" + enumParserFunction.returnType());
enumParserFunction.setAccess(KODE::Function::Private);
c.addFunction(enumParserFunction);

auto enumSerializerFunction = enumSerializerMethod(e);
enumSerializerFunction.setAccess(KODE::Function::Private);
c.addFunction(enumSerializerFunction);
}

if (mUseQEnums)
Expand Down Expand Up @@ -596,6 +605,67 @@ bool Creator::useQEnums() const
return mUseQEnums;
}

KODE::Function Creator::enumParserMethod(const KODE::Enum enum_)
{
KODE::Function ret(KODE::Style::lowerFirst(enum_.name()) + "FromString", enum_.name());
ret.setStatic(true);

ret.addArgument("const QString & v");
ret.addArgument(KODE::Function::Argument("bool *ok", "nullptr"));

KODE::Code code;
code += "if (ok) *ok = true;";
code.newLine();

auto first = true;
for (const auto enumItem : enum_.enumValues()) {
if (first)
first = false;
else
code += "else "; // prefix of the else if (...) lines
code += "if ( v == \"" + enumItem + "\" )";
code.indent();
code += "return " + Namer::sanitize(enumItem) + ";";
code.unindent();
}
code += "else";
code.indent();
code += "if (ok) *ok = false;";
code.unindent();
code.newLine();
code += "return Invalid;";

ret.setBody(code);
return ret;
}

KODE::Function Creator::enumSerializerMethod(const KODE::Enum enum_)
{
KODE::Function ret(KODE::Style::lowerFirst(enum_.name()) + "ToString", "QString");
ret.setStatic(true);
ret.addArgument(QString("const %1 & v").arg(enum_.name()));

KODE::Code code;
code += "switch( v ) {";
code.indent();
for (const auto enumItem : enum_.enumValues())
code += QString("case %1: return \"%2\";").arg(Namer::sanitize(enumItem), enumItem);

code += "Invalid:";
code += "default:";
code.indent();
code += QString("qCritical() << \"Unable to serialize a(n) %1 enum because it has invalid "
"value\" << v;")
.arg(enum_.name());
code += "return QString();";
code.unindent();
code.unindent();
code += "}";
ret.setBody(code);
ret.setReturnType("QString");
return ret;
}

ParserCreator::ParserCreator(Creator *c) : mCreator(c) {}

ParserCreator::~ParserCreator() {}
Expand Down
3 changes: 3 additions & 0 deletions kxml_compiler/creator.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ class Creator
void setUseQEnums(bool useQEnums);
bool useQEnums() const;

static KODE::Function enumParserMethod(const KODE::Enum enum_);
static KODE::Function enumSerializerMethod(const KODE::Enum enum_);

protected:
void setExternalClassNames();

Expand Down
2 changes: 1 addition & 1 deletion libkode

0 comments on commit 970bec3

Please sign in to comment.