From 35c5c16b9277ebe0f1fc11cd71373630cb65c75c Mon Sep 17 00:00:00 2001 From: Luc Grosheintz Date: Wed, 7 Aug 2024 16:16:04 +0200 Subject: [PATCH] Refactor `print_namespace_{start,stop}.` (#1380) --- src/codegen/codegen_coreneuron_cpp_visitor.cpp | 13 +++---------- src/codegen/codegen_coreneuron_cpp_visitor.hpp | 11 +---------- src/codegen/codegen_cpp_visitor.cpp | 14 ++++++++++++++ src/codegen/codegen_cpp_visitor.hpp | 11 +++++++++-- src/codegen/codegen_neuron_cpp_visitor.cpp | 10 ++-------- src/codegen/codegen_neuron_cpp_visitor.hpp | 11 +---------- 6 files changed, 30 insertions(+), 40 deletions(-) diff --git a/src/codegen/codegen_coreneuron_cpp_visitor.cpp b/src/codegen/codegen_coreneuron_cpp_visitor.cpp index 01bd95ade..6e72deb0e 100644 --- a/src/codegen/codegen_coreneuron_cpp_visitor.cpp +++ b/src/codegen/codegen_coreneuron_cpp_visitor.cpp @@ -349,7 +349,7 @@ void CodegenCoreneuronCppVisitor::print_top_verbatim_blocks() { print_namespace_stop(); printer->add_newline(2); - printer->add_line("using namespace coreneuron;"); + print_using_namespace(); printing_top_verbatim_blocks = true; @@ -742,17 +742,10 @@ void CodegenCoreneuronCppVisitor::print_memb_list_getter() { } -void CodegenCoreneuronCppVisitor::print_namespace_start() { - printer->add_newline(2); - printer->push_block("namespace coreneuron"); -} - - -void CodegenCoreneuronCppVisitor::print_namespace_stop() { - printer->pop_block(); +std::string CodegenCoreneuronCppVisitor::namespace_name() { + return "coreneuron"; } - /** * \details There are three types of thread variables currently considered: * - top local thread variables diff --git a/src/codegen/codegen_coreneuron_cpp_visitor.hpp b/src/codegen/codegen_coreneuron_cpp_visitor.hpp index cfc47e114..b23052d97 100644 --- a/src/codegen/codegen_coreneuron_cpp_visitor.hpp +++ b/src/codegen/codegen_coreneuron_cpp_visitor.hpp @@ -487,16 +487,7 @@ class CodegenCoreneuronCppVisitor: public CodegenCppVisitor { void print_memb_list_getter(); - /** - * Prints the start of the \c coreneuron namespace - */ - void print_namespace_start() override; - - - /** - * Prints the end of the \c coreneuron namespace - */ - void print_namespace_stop() override; + virtual std::string namespace_name() override; /** diff --git a/src/codegen/codegen_cpp_visitor.cpp b/src/codegen/codegen_cpp_visitor.cpp index 0da8604d8..106f15fb7 100644 --- a/src/codegen/codegen_cpp_visitor.cpp +++ b/src/codegen/codegen_cpp_visitor.cpp @@ -569,6 +569,20 @@ void CodegenCppVisitor::print_mechanism_info() { printer->add_line("};"); } +void CodegenCppVisitor::print_using_namespace() { + printer->fmt_line("using namespace {};", namespace_name()); +} + +void CodegenCppVisitor::print_namespace_start() { + printer->add_newline(2); + printer->fmt_push_block("namespace {}", namespace_name()); +} + + +void CodegenCppVisitor::print_namespace_stop() { + printer->pop_block(); +} + /****************************************************************************************/ /* Printing routines for code generation */ diff --git a/src/codegen/codegen_cpp_visitor.hpp b/src/codegen/codegen_cpp_visitor.hpp index 8bae8ea04..7b6bed7ca 100644 --- a/src/codegen/codegen_cpp_visitor.hpp +++ b/src/codegen/codegen_cpp_visitor.hpp @@ -1072,18 +1072,25 @@ class CodegenCppVisitor: public visitor::ConstAstVisitor { /* Code-specific printing routines for code generations */ /****************************************************************************************/ + /** Name of "our" namespace. + */ + virtual std::string namespace_name() = 0; /** * Prints the start of the simulator namespace */ - virtual void print_namespace_start() = 0; + void print_namespace_start(); /** * Prints the end of the simulator namespace */ - virtual void print_namespace_stop() = 0; + void print_namespace_stop(); + /** + * Prints f"using namespace {namespace_name()}". + */ + void print_using_namespace(); /****************************************************************************************/ /* Routines for returning variable name */ diff --git a/src/codegen/codegen_neuron_cpp_visitor.cpp b/src/codegen/codegen_neuron_cpp_visitor.cpp index 301202811..c31f30626 100644 --- a/src/codegen/codegen_neuron_cpp_visitor.cpp +++ b/src/codegen/codegen_neuron_cpp_visitor.cpp @@ -511,17 +511,11 @@ std::string CodegenNeuronCppVisitor::py_function_signature( /* Code-specific printing routines for code generation */ /****************************************************************************************/ - -void CodegenNeuronCppVisitor::print_namespace_start() { - printer->add_newline(2); - printer->push_block("namespace neuron"); +std::string CodegenNeuronCppVisitor::namespace_name() { + return "neuron"; } -void CodegenNeuronCppVisitor::print_namespace_stop() { - printer->pop_block(); -} - void CodegenNeuronCppVisitor::append_conc_write_statements( std::vector& statements, const Ion& ion, diff --git a/src/codegen/codegen_neuron_cpp_visitor.hpp b/src/codegen/codegen_neuron_cpp_visitor.hpp index 62621c494..2404d68f4 100644 --- a/src/codegen/codegen_neuron_cpp_visitor.hpp +++ b/src/codegen/codegen_neuron_cpp_visitor.hpp @@ -343,16 +343,7 @@ class CodegenNeuronCppVisitor: public CodegenCppVisitor { /****************************************************************************************/ - /** - * Prints the start of the \c neuron namespace - */ - void print_namespace_start() override; - - - /** - * Prints the end of the \c neuron namespace - */ - void print_namespace_stop() override; + std::string namespace_name() override; /****************************************************************************************/