| 1 |
/* |
| 2 |
* (C) Copyright 2013 ECMWF. |
| 3 |
* |
| 4 |
* This software is licensed under the terms of the Apache Licence Version 2.0 |
| 5 |
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
| 6 |
* In applying this licence, ECMWF does not waive the privileges and immunities |
| 7 |
* granted to it by virtue of its status as an intergovernmental organisation |
| 8 |
* nor does it submit to any jurisdiction. |
| 9 |
*/ |
| 10 |
|
| 11 |
// file deepcode ignore CppMemoryLeak: static pointers for global registry are OK and will be cleaned up at end |
| 12 |
|
| 13 |
#include "atlas/field/FieldCreator.h" |
| 14 |
|
| 15 |
#include <map> |
| 16 |
#include <sstream> |
| 17 |
#include <string> |
| 18 |
|
| 19 |
#include "eckit/thread/AutoLock.h" |
| 20 |
#include "eckit/thread/Mutex.h" |
| 21 |
|
| 22 |
#include "atlas/field/Field.h" |
| 23 |
#include "atlas/field/FieldCreatorArraySpec.h" |
| 24 |
#include "atlas/field/FieldCreatorIFS.h" |
| 25 |
#include "atlas/grid/Grid.h" |
| 26 |
#include "atlas/runtime/Exception.h" |
| 27 |
#include "atlas/runtime/Log.h" |
| 28 |
|
| 29 |
namespace { |
| 30 |
static eckit::Mutex* local_mutex = nullptr; |
| 31 |
static std::map<std::string, atlas::field::FieldCreatorFactory*>* m = nullptr; |
| 32 |
static pthread_once_t once = PTHREAD_ONCE_INIT; |
| 33 |
|
| 34 |
static void init() { |
| 35 |
local_mutex = new eckit::Mutex(); |
| 36 |
m = new std::map<std::string, atlas::field::FieldCreatorFactory*>(); |
| 37 |
} |
| 38 |
} // namespace |
| 39 |
|
| 40 |
namespace atlas { |
| 41 |
namespace field { |
| 42 |
|
| 43 |
namespace { |
| 44 |
|
| 45 |
template <typename T> |
| 46 |
void load_builder(const std::string& name) { |
| 47 |
FieldCreatorBuilder<T> tmp(name); |
| 48 |
} |
| 49 |
struct force_link { |
| 50 |
force_link() { |
| 51 |
load_builder<FieldCreatorIFS>("tmp_IFS"); |
| 52 |
load_builder<FieldCreatorArraySpec>("tmp_ArraySpec"); |
| 53 |
} |
| 54 |
}; |
| 55 |
|
| 56 |
} // namespace |
| 57 |
|
| 58 |
// ------------------------------------------------------------------ |
| 59 |
|
| 60 |
FieldCreator::FieldCreator() = default; |
| 61 |
|
| 62 |
FieldCreator::~FieldCreator() = default; |
| 63 |
|
| 64 |
FieldCreatorFactory::FieldCreatorFactory(const std::string& name): name_(name) { |
| 65 |
pthread_once(&once, init); |
| 66 |
|
| 67 |
eckit::AutoLock<eckit::Mutex> lock(local_mutex); |
| 68 |
|
| 69 |
if (m->find(name) != m->end()) { |
| 70 |
throw_Exception("FieldCreatorFactory [" + name + "] already registered\n\nBacktrace:\n" + backtrace(), Here()); |
| 71 |
} |
| 72 |
(*m)[name] = this; |
| 73 |
} |
| 74 |
|
| 75 |
FieldCreatorFactory::~FieldCreatorFactory() { |
| 76 |
eckit::AutoLock<eckit::Mutex> lock(local_mutex); |
| 77 |
m->erase(name_); |
| 78 |
} |
| 79 |
|
| 80 |
void FieldCreatorFactory::list(std::ostream& out) { |
| 81 |
pthread_once(&once, init); |
| 82 |
|
| 83 |
eckit::AutoLock<eckit::Mutex> lock(local_mutex); |
| 84 |
|
| 85 |
static force_link static_linking; |
| 86 |
|
| 87 |
const char* sep = ""; |
| 88 |
for (std::map<std::string, FieldCreatorFactory*>::const_iterator j = m->begin(); j != m->end(); ++j) { |
| 89 |
out << sep << (*j).first; |
| 90 |
sep = ", "; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
FieldCreator* FieldCreatorFactory::build(const std::string& name) { |
| 95 |
pthread_once(&once, init); |
| 96 |
|
| 97 |
eckit::AutoLock<eckit::Mutex> lock(local_mutex); |
| 98 |
|
| 99 |
static force_link static_linking; |
| 100 |
|
| 101 |
std::map<std::string, FieldCreatorFactory*>::const_iterator j = m->find(name); |
| 102 |
|
| 103 |
if (j == m->end()) { |
| 104 |
Log::error() << "No FieldCreatorFactory for [" << name << "]" << '\n'; |
| 105 |
Log::error() << "FieldCreatorFactories are:" << '\n'; |
| 106 |
for (j = m->begin(); j != m->end(); ++j) { |
| 107 |
Log::error() << " " << (*j).first << '\n'; |
| 108 |
} |
| 109 |
throw_Exception(std::string("No FieldCreatorFactory called ") + name); |
| 110 |
} |
| 111 |
|
| 112 |
return (*j).second->make(); |
| 113 |
} |
| 114 |
|
| 115 |
FieldCreator* FieldCreatorFactory::build(const std::string& name, const eckit::Parametrisation& param) { |
| 116 |
pthread_once(&once, init); |
| 117 |
|
| 118 |
eckit::AutoLock<eckit::Mutex> lock(local_mutex); |
| 119 |
|
| 120 |
static force_link static_linking; |
| 121 |
|
| 122 |
std::map<std::string, FieldCreatorFactory*>::const_iterator j = m->find(name); |
| 123 |
|
| 124 |
if (j == m->end()) { |
| 125 |
Log::error() << "No FieldCreatorFactory for [" << name << "]" << '\n'; |
| 126 |
Log::error() << "FieldCreatorFactories are:" << '\n'; |
| 127 |
for (j = m->begin(); j != m->end(); ++j) { |
| 128 |
Log::error() << " " << (*j).first << '\n'; |
| 129 |
} |
| 130 |
throw_Exception(std::string("No FieldCreatorFactory called ") + name); |
| 131 |
} |
| 132 |
|
| 133 |
return (*j).second->make(param); |
| 134 |
} |
| 135 |
|
| 136 |
} // namespace field |
| 137 |
} // namespace atlas |