Skip to content

Commit

Permalink
compiler: generate code with move semantics, new cmd option is introd…
Browse files Browse the repository at this point in the history
…uced
  • Loading branch information
tysonite committed Apr 28, 2016
1 parent 4220f17 commit f038bfc
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
3 changes: 3 additions & 0 deletions compiler/src/main/java/org/tysonite/asn1/gen/Main.java
Expand Up @@ -26,6 +26,8 @@ public final class Main {
"ASN.1 compiler command line options");
public static final Option NO_CPP_CODE = new Option("ncpp", "no-cpp", false,
"Do not generate C++ code, only parser ASN.1");
public static final Option CPP11_CODE = new Option("cpp11", "cpp11", false,
"Generate C++ code with C++11 features");
public static final Option BIG_INTEGER = new Option("bi", "big-int", false,
"Generate code with variable length integers");

Expand All @@ -45,6 +47,7 @@ private void initOptions() {
options.addOption(METHOD_DER);
options.addOption(METHOD_XER);
options.addOption(NO_CPP_CODE);
options.addOption(CPP11_CODE);
options.addOption(BIG_INTEGER);
options.addOption(HELP);
}
Expand Down
Expand Up @@ -3,6 +3,7 @@
import org.tysonite.asn1.gen.ContentProvider;
import org.tysonite.asn1.gen.DoNothingASTVisitor;
import org.tysonite.asn1.gen.GeneratorContext;
import org.tysonite.asn1.gen.Main;
import org.tysonite.asn1.gen.utils.CodeBuilder;
import org.tysonite.asn1.gen.utils.GenerationUtils;
import org.tysonite.asn1.gen.utils.VisitorUtils;
Expand Down Expand Up @@ -42,6 +43,25 @@ public Object visit(ASTElementType node, Object data) {
append(" }");
builder.newLine();

// setter (move semantics)
if (context.getCommandLine().hasOption(Main.CPP11_CODE.getOpt())) {
builder.append(2, "void set_").
append(GenerationUtils.asCPPToken(node.jjtGetFirstToken().toString())).
append("(");
if (!VisitorUtils.visitChildsAndAccept(builder, node, new SimpleTypeName(context),
new DefinedCPPTypeName())) {
builder.append(VisitorUtils.queueGeneratedCodeForTypes(node, context));
}

builder.append("::ValueType&& v) { _").
append(GenerationUtils.asCPPToken(node.jjtGetFirstToken().toString())).
append(" = std::move(v);");
builder.append(" _id = ").
append(GenerationUtils.asCPPToken(node.jjtGetFirstToken().toString())).append("_ID;").
append(" }");
builder.newLine();
}

// getter (const)
builder.append(2, "const ");
if (!VisitorUtils.visitChildsAndAccept(builder, node, new SimpleTypeName(context),
Expand Down
Expand Up @@ -68,8 +68,13 @@ public Object visit(ASTElementType node, Object data) {

// write code for setting value
builder.append(2, "value.set_").
append(GenerationUtils.asCPPToken(node.jjtGetFirstToken().toString())).append("(v);").
newLine();
append(GenerationUtils.asCPPToken(node.jjtGetFirstToken().toString()));
if (context.getCommandLine().hasOption(Main.CPP11_CODE.getOpt())) {
builder.append("(std::move(v));");
} else {
builder.append("(v);");
}
builder.newLine();

builder.append(1, "}").newLine();
return data;
Expand Down
Expand Up @@ -3,6 +3,7 @@
import org.tysonite.asn1.gen.ContentProvider;
import org.tysonite.asn1.gen.DoNothingASTVisitor;
import org.tysonite.asn1.gen.GeneratorContext;
import org.tysonite.asn1.gen.Main;
import org.tysonite.asn1.gen.utils.CodeBuilder;
import org.tysonite.asn1.gen.utils.GenerationUtils;
import org.tysonite.asn1.gen.utils.VisitorUtils;
Expand Down Expand Up @@ -41,6 +42,25 @@ public Object visit(ASTElementType node, Object data) {
builder.append(" }");
builder.newLine();

// setter (move semantics)
if (context.getCommandLine().hasOption(Main.CPP11_CODE.getOpt())) {
builder.append(2, "void set_").
append(GenerationUtils.asCPPToken(node.jjtGetFirstToken().toString())).
append("(");
if (!VisitorUtils.visitChildsAndAccept(builder, node, new SimpleTypeName(context),
new DefinedCPPTypeName())
|| VisitorUtils.visitChildsAndAccept(builder, node, new IsNamedIntegerType())) {
builder.append(VisitorUtils.queueGeneratedCodeForTypes(node, context));
}

builder.append("::ValueType&& v) { _").
append(GenerationUtils.asCPPToken(node.jjtGetFirstToken().toString())).
append(" = std::move(v); ");
VisitorUtils.visitNodeAndAccept(builder, node, new SetAsPresent());
builder.append(" }");
builder.newLine();
}

// getter (const)
builder.append(2, "const ");
if (!VisitorUtils.visitChildsAndAccept(builder, node, new SimpleTypeName(context),
Expand Down

0 comments on commit f038bfc

Please sign in to comment.