Skip to content

Commit

Permalink
Merge pull request #2431 from lf-lang/cpp_fqn_fix
Browse files Browse the repository at this point in the history
API for getting fully-qualified name in Cpp target
  • Loading branch information
lhstrh authored Oct 31, 2024
2 parents daaea1b + d8c12f9 commit d65750a
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
2 changes: 2 additions & 0 deletions core/src/main/resources/lib/cpp/lfutil.hh
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public:
reactor::Duration get_elapsed_logical_time() const { return reactor->get_elapsed_logical_time(); }
reactor::Duration get_elapsed_physical_time() const { return reactor->get_elapsed_physical_time(); }
reactor::Environment* environment() const { return reactor->environment(); }
const std::string& fqn() const { return reactor->fqn(); }
const std::string& name() const { return reactor->name(); }
void request_stop() const { return environment()->sync_shutdown(); }
};

Expand Down
77 changes: 77 additions & 0 deletions test/Cpp/src/FqnTest.lf
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
target Cpp {
workers: 1
}

public preamble {=
inline bool name_validation (const std::string& fqn, const std::string& name) {
if (fqn.empty() || name.empty()) {
return false;
}
size_t last_dot = fqn.find_last_of('.');
return (last_dot == std::string::npos) ? (name == fqn) : (name == fqn.substr(last_dot + 1));
}
=}

reactor A(bank_index: size_t = 0) {
reaction(startup) {=
std::cout << "fqn:" << fqn() << " name:" << name() << " Started\n";
assert (name_validation(fqn(), name()));
=}

reaction(shutdown) {=
std::cout << "fqn:" << fqn() << " name:" << name() << " Terminated\n";
=}
}

reactor B(bank_index: size_t = 0) {
a = new[3] A()

reaction(startup) {=
std::cout << "fqn:" << fqn() << " name:" << name() << " Started\n";
assert (name_validation(fqn(), name()));
=}

reaction(shutdown) {=
std::cout << "fqn:" << fqn() << " name:" << name() << " Terminated\n";
=}
}

reactor C(bank_index: size_t = 0) {
b = new[2] B()

reaction(startup) {=
std::cout << "fqn:" << fqn() << " name:" << name() << " Started\n";
assert (name_validation(fqn(), name()));
=}

reaction(shutdown) {=
std::cout << "fqn:" << fqn() << " name:" << name() << " Terminated\n";
=}
}

reactor D(bank_index: size_t = 0) {
a = new[2] A()

reaction(startup) {=
std::cout << "fqn:" << fqn() << " name:" << name() << " Started\n";
assert (name_validation(fqn(), name()));
=}

reaction(shutdown) {=
std::cout << "fqn:" << fqn() << " name:" << name() << " Terminated\n";
=}
}

main reactor {
c = new[2] C()
d = new[3] D()

reaction(startup) {=
std::cout << "fqn:" << fqn() << " name:" << name() << " Started\n";
assert (name_validation(fqn(), name()));
=}

reaction(shutdown) {=
std::cout << "fqn:" << fqn() << " name:" << name() << " Terminated\n";
=}
}

0 comments on commit d65750a

Please sign in to comment.