Skip to content

Commit

Permalink
eckit::geometry instance()-based factories (4/7)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmaciel committed Aug 18, 2023
1 parent 053537f commit f61a864
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 156 deletions.
117 changes: 5 additions & 112 deletions src/eckit/geometry/Grid.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include "eckit/exception/Exceptions.h"
#include "eckit/geometry/GridConfig.h"
#include "eckit/geometry/util/regex.h"
#include "eckit/log/JSON.h"
#include "eckit/log/Log.h"
#include "eckit/thread/AutoLock.h"
#include "eckit/thread/Mutex.h"
Expand Down Expand Up @@ -100,13 +99,11 @@ static pthread_once_t __once;
static Mutex* __mutex = nullptr;
static std::map<std::string, GridFactoryUID*>* __grid_uids = nullptr;
static std::map<std::string, GridFactoryName*>* __grid_names = nullptr;
static std::map<std::string, GridFactoryType*>* __grid_types = nullptr;

static void __init() {
__mutex = new Mutex;
__grid_uids = new std::remove_reference<decltype(*__grid_uids)>::type;
__grid_names = new std::remove_reference<decltype(*__grid_names)>::type;
__grid_types = new std::remove_reference<decltype(*__grid_types)>::type;
}


Expand All @@ -124,8 +121,8 @@ const Grid* GridFactory::build(const Configuration& config) {
return GridFactoryName::build(name);
}

if (config.has("type")) {
return GridFactoryType::build(config);
if (std::string type; config.get("type", type)) {
return GridFactoryType::instance().get(type).create(config);
}

list(Log::error() << "Grid: cannot build grid, choices are: ");
Expand All @@ -145,22 +142,7 @@ void GridFactory::list(std::ostream& out) {
GridFactoryName::list(out << "name: ");
out << std::endl;

GridFactoryType::list(out << "type: ");
out << std::endl;
}


void GridFactory::json(JSON& j) {
pthread_once(&__once, __init);
AutoLock<Mutex> lock(*__mutex);

GridConfig::instance();

j.startObject();
GridFactoryUID::json(j << "uid");
GridFactoryName::json(j << "name");
GridFactoryType::json(j << "type");
j.endObject();
out << GridFactoryType::instance() << std::endl;
}


Expand Down Expand Up @@ -202,23 +184,7 @@ void GridFactoryUID::list(std::ostream& out) {
pthread_once(&__once, __init);
AutoLock<Mutex> lock(*__mutex);

JSON j(out);
json(j);
}


void GridFactoryUID::json(JSON& j) {
pthread_once(&__once, __init);
AutoLock<Mutex> lock(*__mutex);

j.startObject();
for (const auto& p : *__grid_uids) {
j << p.first;

std::unique_ptr<Configuration> config(p.second->config());
j << *config;
}
j.endObject();
out << "..." << std::endl;
}


Expand Down Expand Up @@ -281,23 +247,7 @@ void GridFactoryName::list(std::ostream& out) {
pthread_once(&__once, __init);
AutoLock<Mutex> lock(*__mutex);

JSON j(out);
json(j);
}


void GridFactoryName::json(JSON& j) {
pthread_once(&__once, __init);
AutoLock<Mutex> lock(*__mutex);

j.startObject();
for (const auto& p : *__grid_names) {
j << p.first;

std::unique_ptr<Configuration> config(p.second->config());
j << *config;
}
j.endObject();
out << "..." << std::endl;
}


Expand All @@ -323,61 +273,4 @@ GridFactoryName::~GridFactoryName() {
}


GridFactoryType::GridFactoryType(const std::string& type) :
type_(type) {
pthread_once(&__once, __init);
AutoLock<Mutex> lock(*__mutex);

if (__grid_types->find(type) == __grid_types->end()) {
(*__grid_types)[type] = this;
return;
}

throw SeriousBug("Grid: duplicate type '" + type + "'");
}


GridFactoryType::~GridFactoryType() {
AutoLock<Mutex> lock(*__mutex);
__grid_types->erase(type_);
}


const Grid* GridFactoryType::build(const Configuration& config) {
pthread_once(&__once, __init);
AutoLock<Mutex> lock(*__mutex);

std::string type;
if (config.get("type", type)) {
if (auto j = __grid_types->find(type); j != __grid_types->end()) {
return j->second->make(config);
}
}

list(Log::error() << "Grid: unknown type '" << type << "', choices are: ");
throw SeriousBug("Grid: unknown type '" + type + "'");
}


void GridFactoryType::list(std::ostream& out) {
pthread_once(&__once, __init);
AutoLock<Mutex> lock(*__mutex);

JSON j(out);
json(j);
}


void GridFactoryType::json(JSON& j) {
pthread_once(&__once, __init);
AutoLock<Mutex> lock(*__mutex);

j.startList();
for (const auto& p : *__grid_types) {
j << p.first;
}
j.endList();
}


} // namespace eckit::geometry
53 changes: 13 additions & 40 deletions src/eckit/geometry/Grid.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,8 @@
#include "eckit/geometry/Point.h"
#include "eckit/geometry/Renumber.h"
#include "eckit/geometry/area/BoundingBox.h"


namespace eckit {
class JSON;
}
#include "eckit/memory/Builder.h"
#include "eckit/memory/Factory.h"


namespace eckit::geometry {
Expand All @@ -41,6 +38,9 @@ class Grid {
public:
// -- Types

using builder_t = BuilderT1<Grid>;
using ARG1 = const Configuration&;

struct Iterator final : std::unique_ptr<geometry::Iterator> {
explicit Iterator(geometry::Iterator* it) :
unique_ptr(it) { ASSERT(unique_ptr::operator bool()); }
Expand Down Expand Up @@ -124,7 +124,8 @@ class Grid {
// None

// -- Class methods
// None

static std::string className() { return "grid"; }

protected:
// -- Constructors
Expand Down Expand Up @@ -167,11 +168,16 @@ class Grid {
};


using GridFactoryType = Factory<Grid>;

template <typename T>
using GridRegisterType = ConcreteBuilderT1<Grid, T>;


struct GridFactory {
// This is 'const' as Grid should always be immutable
static const Grid* build(const Configuration&);
static void list(std::ostream&);
static void json(JSON&);
};


Expand All @@ -185,7 +191,6 @@ struct GridFactoryUID {
static const Grid* build(const std::string&);

static void list(std::ostream&);
static void json(JSON&);
static void insert(const std::string& name, MappedConfiguration*);

protected:
Expand All @@ -209,7 +214,6 @@ struct GridFactoryName {
static const Grid* build(const std::string& name);

static void list(std::ostream&);
static void json(JSON&);
static void insert(const std::string& name, MappedConfiguration*);

protected:
Expand All @@ -225,29 +229,6 @@ struct GridFactoryName {
};


struct GridFactoryType {
GridFactoryType(const GridFactoryType&) = delete;
GridFactoryType(GridFactoryType&&) = delete;
GridFactoryType& operator=(const GridFactoryType&) = delete;
GridFactoryType& operator=(GridFactoryType&&) = delete;

// This is 'const' as Grid should always be immutable
static const Grid* build(const Configuration&);

static void list(std::ostream&);
static void json(JSON&);

protected:
explicit GridFactoryType(const std::string& type);
virtual ~GridFactoryType();

private:
virtual const Grid* make(const Configuration&) = 0;

const std::string type_;
};


template <class T>
struct GridRegisterUID final : GridFactoryUID {
explicit GridRegisterUID(const std::string& uid) :
Expand All @@ -264,12 +245,4 @@ struct GridRegisterName final : GridFactoryName {
};


template <class T>
struct GridRegisterType final : GridFactoryType {
explicit GridRegisterType(const std::string& type) :
GridFactoryType(type) {}
const Grid* make(const Configuration& config) override { return new T(config); }
};


} // namespace eckit::geometry
5 changes: 1 addition & 4 deletions src/tools/eckit-grid-list.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@


#include "eckit/geometry/Grid.h"
#include "eckit/log/JSON.h"
#include "eckit/log/Log.h"
#include "eckit/option/EckitTool.h"

Expand All @@ -23,9 +22,7 @@ struct EckitGridList final : EckitTool {
EckitTool(argc, argv) {}

void execute(const option::CmdArgs&) override {
JSON j(Log::info());
geometry::GridFactory::json(j);
Log::info() << std::endl;
Log::info() << geometry::GridFactoryType::instance() << std::endl;
}

void usage(const std::string& tool) const override {
Expand Down

0 comments on commit f61a864

Please sign in to comment.