Skip to content

Commit

Permalink
Merge pull request #467 from icyphy/cpp-generic-parameter
Browse files Browse the repository at this point in the history
Cpp: Support generic parameters
  • Loading branch information
cmnrd authored Aug 20, 2021
2 parents 8938a19 + 115029c commit 51161b2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ class CppParameterGenerator(private val reactor: Reactor) {
/** Get a C++ type that is a const reference to the parameter type */
val Parameter.constRefType: String
get() =
"std::add_lvalue_reference<std::add_const<$targetType>::type>::type"
"typename std::add_lvalue_reference<typename std::add_const<$targetType>::type>::type"
}

/** Generate all parameter declarations */
fun generateDeclarations() =
reactor.parameters.joinToString("\n", "// parameters\n", "\n") {
"std::add_const<${it.targetType}>::type ${it.name};"
"typename std::add_const<${it.targetType}>::type ${it.name};"
}

/** Generate all constructor initializers for parameters */
Expand Down
20 changes: 20 additions & 0 deletions test/Cpp/src/target/GenericParameterAndState.lf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
target Cpp;

reactor Foo<T> (bar:T(0)) {
state baz:T(bar)

reaction (startup) {=
if (bar != 42) {
std::cerr << "ERROR: Expected baz=42 but got baz=" << bar << '\n';
exit(1);
}
if (baz != 42) {
std::cerr << "ERROR: Expected baz=42 but got baz=" << baz << '\n';
exit(1);
}
=}
}

main reactor {
foo = new Foo<int>(bar=42)
}

0 comments on commit 51161b2

Please sign in to comment.