remove static order dependency

This commit is contained in:
graham sanderson 2021-02-01 10:04:34 -06:00 committed by Graham Sanderson
parent 8a4e21bd8e
commit 2d5789eca8
3 changed files with 10 additions and 7 deletions

View File

@ -18,7 +18,7 @@ void usage() {
std::cerr << "\n"; std::cerr << "\n";
std::cerr << "options:\n"; std::cerr << "options:\n";
std::cerr << " -o <output_format> select output_format (default '" << DEFAULT_OUTPUT_FORMAT << "'); available options are:\n"; std::cerr << " -o <output_format> select output_format (default '" << DEFAULT_OUTPUT_FORMAT << "'); available options are:\n";
for(const auto& f : output_format::output_formats) { for(const auto& f : output_format::all()) {
std::cerr << " " << f->name << std::endl; std::cerr << " " << f->name << std::endl;
std::cerr << " " << f->get_description() << std::endl; std::cerr << " " << f->get_description() << std::endl;
} }
@ -79,11 +79,11 @@ int main(int argc, char *argv[]) {
} }
std::shared_ptr<output_format> oformat; std::shared_ptr<output_format> oformat;
if (!res) { if (!res) {
const auto& e = std::find_if(output_format::output_formats.begin(), output_format::output_formats.end(), const auto& e = std::find_if(output_format::all().begin(), output_format::all().end(),
[&](const std::shared_ptr<output_format> &f) { [&](const std::shared_ptr<output_format> &f) {
return f->name == format; return f->name == format;
}); });
if (e == output_format::output_formats.end()) { if (e == output_format::all().end()) {
std::cerr << "error: unknown output format '" << format << "'" << std::endl; std::cerr << "error: unknown output format '" << format << "'" << std::endl;
res = 1; res = 1;
} else { } else {

View File

@ -77,13 +77,12 @@ struct compiled_source {
}; };
struct output_format { struct output_format {
static std::vector<std::shared_ptr<output_format>> output_formats;
static std::string default_name; static std::string default_name;
std::string name; std::string name;
static void add(output_format *lang) { static void add(output_format *lang) {
output_formats.push_back(std::shared_ptr<output_format>(lang)); all().push_back(std::shared_ptr<output_format>(lang));
} }
virtual int output(std::string destination, std::vector<std::string> output_options, virtual int output(std::string destination, std::vector<std::string> output_options,
@ -93,6 +92,11 @@ struct output_format {
FILE *open_single_output(std::string destination); FILE *open_single_output(std::string destination);
virtual ~output_format() = default; virtual ~output_format() = default;
static std::vector<std::shared_ptr<output_format>>& all() {
static std::vector<std::shared_ptr<output_format>> output_formats;
return output_formats;
}
protected: protected:
output_format(std::string name) : name(std::move(name)) {} output_format(std::string name) : name(std::move(name)) {}
}; };

View File

@ -15,7 +15,6 @@
using syntax_error = yy::parser::syntax_error; using syntax_error = yy::parser::syntax_error;
std::vector<std::shared_ptr<output_format>> output_format::output_formats;
std::string output_format::default_name = "c-sdk"; std::string output_format::default_name = "c-sdk";
pio_assembler::pio_assembler() { pio_assembler::pio_assembler() {
@ -324,7 +323,7 @@ std::vector<compiled_source::symbol> pio_assembler::public_symbols(program &prog
int pio_assembler::write_output() { int pio_assembler::write_output() {
std::set<std::string> known_output_formats; std::set<std::string> known_output_formats;
std::transform(output_format::output_formats.begin(), output_format::output_formats.end(), std::transform(output_format::all().begin(), output_format::all().end(),
std::inserter(known_output_formats, known_output_formats.begin()), std::inserter(known_output_formats, known_output_formats.begin()),
[&](std::shared_ptr<output_format> &f) { [&](std::shared_ptr<output_format> &f) {
return f->name; return f->name;