From 8eef31bba1a2d72c6888b5dcfc54e1396d454afb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20M=C3=BCller?= Date: Thu, 14 Dec 2017 19:08:22 +0100 Subject: [PATCH] Add `on_container_end()` function to code generator to allow injection of children --- include/cppast/code_generator.hpp | 15 +++++++++++++++ src/code_generator.cpp | 7 +++++++ 2 files changed, 22 insertions(+) diff --git a/include/cppast/code_generator.hpp b/include/cppast/code_generator.hpp index b2925e5..a6e95f5 100644 --- a/include/cppast/code_generator.hpp +++ b/include/cppast/code_generator.hpp @@ -230,6 +230,12 @@ namespace cppast return gen_; } + /// \effects Calls `on_container_end()`. + void container_end() const noexcept + { + gen_->on_container_end(*this, *e_); + } + /// \effects Call `do_indent()` followed by `do_write_newline()` (if `print_newline` is `true`). void indent(bool print_newline = true) const noexcept { @@ -399,6 +405,15 @@ namespace cppast (void)e; } + /// \effects Will be invoked after all children of a container have been generated. + /// It can be used to inject additional children. + /// The base class version has no effect. + virtual void on_container_end(const output& out, const cpp_entity& e) + { + (void)out; + (void)e; + } + /// \effects Will be invoked when the indentation level should be increased by one. /// The level change must be applied on the next call to `do_write_newline()`. virtual void do_indent() = 0; diff --git a/src/code_generator.cpp b/src/code_generator.cpp index 1546ea6..41cb7f4 100644 --- a/src/code_generator.cpp +++ b/src/code_generator.cpp @@ -88,6 +88,8 @@ namespace if (!need_sep) // file empty, write newl output << newl; + else + output.container_end(); } return static_cast(output); } @@ -173,6 +175,7 @@ namespace output.indent(); write_container(output, ns, newl, cur_access); + output.container_end(); output.unindent(); output << punctuation("}") << newl; @@ -285,6 +288,8 @@ namespace if (need_sep) output << newl; + output.container_end(); + output.unindent(); output << punctuation("};") << newl; } @@ -426,6 +431,8 @@ namespace } } + output.container_end(); + output.unindent(); output << punctuation("};") << newl; }