Simplify generated code for getOption() and setOption() (#6462)
authorGereon Kremer <gereon.kremer@cs.rwth-aachen.de>
Thu, 29 Apr 2021 16:20:36 +0000 (18:20 +0200)
committerGitHub <noreply@github.com>
Thu, 29 Apr 2021 16:20:36 +0000 (16:20 +0000)
This PR simplifies the generated code for Options::getOption() and Options::setOption(). It now uses less string streams, less temporary vectors and the new options[...] syntax (instead of options::...().

src/options/mkoptions.py

index 13df7b8bdf77a083ac103acb92b0f8c98f10bfaa..e2c13849004bbfd74c6aa2f638e172817f40ecae 100644 (file)
@@ -746,15 +746,17 @@ def codegen_all_modules(modules, dst_dir, tpl_options, tpl_options_holder):
                         'if ({}) {{'.format(cond))
                     if option.type == 'bool':
                         getoption_handlers.append(
-                            'return options::{}() ? "true" : "false";'.format(
-                                option.name))
+                            'return (*this)[options::{}] ? "true" : "false";'.format(option.name))
+                    elif option.type == 'std::string':
+                        getoption_handlers.append(
+                            'return (*this)[options::{}];'.format(option.name))
+                    elif is_numeric_cpp_type(option.type):
+                        getoption_handlers.append(
+                            'return std::to_string((*this)[options::{}]);'.format(option.name))
                     else:
                         getoption_handlers.append('std::stringstream ss;')
-                        if is_numeric_cpp_type(option.type):
-                            getoption_handlers.append(
-                                'ss << std::fixed << std::setprecision(8);')
-                        getoption_handlers.append('ss << options::{}();'.format(
-                            option.name))
+                        getoption_handlers.append(
+                            'ss << (*this)[options::{}];'.format(option.name))
                         getoption_handlers.append('return ss.str();')
                     getoption_handlers.append('}')
 
@@ -788,21 +790,14 @@ def codegen_all_modules(modules, dst_dir, tpl_options, tpl_options_holder):
                     options_smt.append('"{}",'.format(optname))
 
                     if option.type == 'bool':
-                        s = '{ std::vector<std::string> v; '
-                        s += 'v.push_back("{}"); '.format(optname)
-                        s += 'v.push_back(std::string('
-                        s += 'd_holder->{}'.format(option.name)
-                        s += ' ? "true" : "false")); '
-                        s += 'opts.push_back(v); }'
+                        s = 'opts.push_back({{"{}", d_holder->{} ? "true" : "false"}});'.format(
+                            optname, option.name)
+                    elif is_numeric_cpp_type(option.type):
+                        s = 'opts.push_back({{"{}", std::to_string(d_holder->{})}});'.format(
+                            optname, option.name)
                     else:
-                        s = '{ std::stringstream ss; '
-                        if is_numeric_cpp_type(option.type):
-                            s += 'ss << std::fixed << std::setprecision(8); '
-                        s += 'ss << d_holder->{}; '.format(option.name)
-                        s += 'std::vector<std::string> v; '
-                        s += 'v.push_back("{}"); '.format(optname)
-                        s += 'v.push_back(ss.str()); '
-                        s += 'opts.push_back(v); }'
+                        s = '{{ std::stringstream ss; ss << d_holder->{}; opts.push_back({{"{}", ss.str()}}); }}'.format(
+                            option.name, optname)
                     options_getoptions.append(s)