Skip to content

Commit

Permalink
Improve GridBuilder and atlas-grids output
Browse files Browse the repository at this point in the history
Adding GridBuilder::registerNamedGrid, which allows to add grids in a plugin programatically
  • Loading branch information
wdeconinck committed Oct 20, 2023
1 parent b8a595a commit e38becb
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
16 changes: 10 additions & 6 deletions src/apps/atlas-grids.cc
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,12 @@ int AtlasGrids::execute(const Args& args) {
Log::info() << "usage: atlas-grids <grid> [OPTION]... [--help]\n" << std::endl;
Log::info() << "\n";
Log::info() << "Available grid types:" << std::endl;
for (auto b : grid::GridBuilder::typeRegistry()) {
Log::info() << " -- " << b.second->type() << '\n';
std::set<std::string> grid_types;
for (const auto& [key, builder] : grid::GridBuilder::typeRegistry()) {
grid_types.insert(builder->type());
}
for (const auto& b : grid_types) {
Log::info() << " -- " << b << '\n';
}
Log::info() << "\n";
Log::info() << "Available named grids:" << std::endl;
Expand All @@ -159,13 +163,13 @@ int AtlasGrids::execute(const Args& args) {
}
}

for (auto b : grid::GridBuilder::nameRegistry()) {
for (const auto& [key, builder] : grid::GridBuilder::typeRegistry()) {
int c = 0;
for (const auto& name : b.second->names()) {
for (const auto& name : builder->names()) {
if (c == 0) {
Log::info() << " -- " << std::left << std::setw(maxlen + 8) << name;
if (!b.second->type().empty()) {
Log::info() << "type: " << b.second->type();
if (!builder->type().empty()) {
Log::info() << "type: " << builder->type();
}
Log::info() << std::endl;
}
Expand Down
12 changes: 6 additions & 6 deletions src/atlas/grid/detail/grid/Grid.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,8 @@ const Grid* Grid::create(const std::string& name) {
}

const Grid* Grid::create(const std::string& name, const Grid::Config& config) {
const GridBuilder::Registry& registry = GridBuilder::nameRegistry();
for (GridBuilder::Registry::const_iterator it = registry.begin(); it != registry.end(); ++it) {
const Grid* grid = it->second->create(name, config);
for (const auto& [key, builder]: GridBuilder::nameRegistry()) {
const Grid* grid = builder->create(name, config);
if (grid) {
return grid;
}
Expand All @@ -83,11 +82,12 @@ const Grid* Grid::create(const std::string& name, const Grid::Config& config) {
std::ostringstream log;
log << "Could not construct Grid from the name \"" << name << "\"\n";
log << "Accepted names are: \n";
for (GridBuilder::Registry::const_iterator it = registry.begin(); it != registry.end(); ++it) {
log << " - " << *it->second << "\n";
for (const auto& [key, grid_builder]: GridBuilder::typeRegistry()) {
for( auto& grid_name: grid_builder->names()) {
log << " - " << grid_name << "\n";
}
}
throw_Exception(log.str());
// return GridBuilder::createNamed(name);
}

const Grid* Grid::create(const Grid& grid, const Domain& domain) {
Expand Down
10 changes: 10 additions & 0 deletions src/atlas/grid/detail/grid/GridBuilder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,16 @@ GridBuilder::~GridBuilder() {
}
}

void GridBuilder::registerNamedGrid(const std::string& name) {
pthread_once(&once, init);
eckit::AutoLock<eckit::Mutex> lock(local_mutex);
std::string regex = "^"+name+"$";
ATLAS_ASSERT(named_grids->find(regex) == named_grids->end());
(*named_grids)[regex] = this;
names_.emplace_back(regex);
pretty_names_.emplace_back(name);
}

const Grid::Implementation* GridBuilder::create(const Grid::Config& config) const {
//eckit::Factory<Grid::Implementation>& fact = eckit::Factory<Grid::Implementation>::instance();

Expand Down
4 changes: 3 additions & 1 deletion src/atlas/grid/detail/grid/GridBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ class GridBuilder {

virtual const Grid::Implementation* create(const std::string&, const Grid::Config& = Grid::Config()) const = 0;

const std::string& type() const;
virtual const std::string& type() const;
const std::vector<std::string>& regexes() const;
const std::vector<std::string>& names() const;

void registerNamedGrid(const std::string& name);

protected:
bool match(const std::string& string, std::vector<std::string>& matches, int& id) const;

Expand Down

0 comments on commit e38becb

Please sign in to comment.