| 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/State.h" |
| 14 |
|
| 15 |
#include <iomanip> |
| 16 |
#include <map> |
| 17 |
#include <memory> |
| 18 |
#include <string> |
| 19 |
|
| 20 |
#include "eckit/thread/AutoLock.h" |
| 21 |
#include "eckit/thread/Mutex.h" |
| 22 |
|
| 23 |
#include "atlas/field/Field.h" |
| 24 |
#include "atlas/grid/Grid.h" |
| 25 |
#include "atlas/mesh/Mesh.h" |
| 26 |
#include "atlas/runtime/Exception.h" |
| 27 |
#include "atlas/runtime/Log.h" |
| 28 |
|
| 29 |
namespace atlas { |
| 30 |
namespace field { |
| 31 |
|
| 32 |
namespace { |
| 33 |
|
| 34 |
static eckit::Mutex* local_mutex = nullptr; |
| 35 |
static std::map<std::string, StateGeneratorFactory*>* m = nullptr; |
| 36 |
static pthread_once_t once = PTHREAD_ONCE_INIT; |
| 37 |
|
| 38 |
static void init() { |
| 39 |
local_mutex = new eckit::Mutex(); |
| 40 |
m = new std::map<std::string, StateGeneratorFactory*>(); |
| 41 |
} |
| 42 |
|
| 43 |
template <typename T> |
| 44 |
void load_builder() { |
| 45 |
StateGeneratorBuilder<T>("tmp"); |
| 46 |
} |
| 47 |
|
| 48 |
void force_link() { |
| 49 |
static struct Link { Link() = default; } link; |
| 50 |
[](const Link&) {}(link); // disable unused warnings |
| 51 |
} |
| 52 |
|
| 53 |
} // namespace |
| 54 |
|
| 55 |
void State::initialize(const std::string& generator, const eckit::Parametrisation& params) { |
| 56 |
std::unique_ptr<StateGenerator> state_generator(StateGeneratorFactory::build(generator, params)); |
| 57 |
state_generator->generate(*this, params); |
| 58 |
} |
| 59 |
|
| 60 |
//------------------------------------------------------------------------------------------------------ |
| 61 |
|
| 62 |
State::State() = default; |
| 63 |
|
| 64 |
State::State(const std::string& generator, const eckit::Parametrisation& params) { |
| 65 |
initialize(generator, params); |
| 66 |
} |
| 67 |
|
| 68 |
const util::Metadata& State::metadata() const { |
| 69 |
return metadata_; |
| 70 |
} |
| 71 |
|
| 72 |
util::Metadata& State::metadata() { |
| 73 |
return metadata_; |
| 74 |
} |
| 75 |
|
| 76 |
Field State::add(Field field) { |
| 77 |
ATLAS_ASSERT(field); |
| 78 |
|
| 79 |
if (field.name().empty()) { |
| 80 |
std::stringstream new_name; |
| 81 |
new_name << "field_" << std::setw(5) << std::setfill('0') << fields_.size(); |
| 82 |
ATLAS_ASSERT(!has(new_name.str())); |
| 83 |
field.rename(new_name.str()); |
| 84 |
} |
| 85 |
|
| 86 |
if (has(field.name())) { |
| 87 |
std::stringstream msg; |
| 88 |
msg << "Trying to add field '" << field.name() << "' to State, but State already has a field with this name."; |
| 89 |
throw_AssertionFailed(msg.str(), Here()); |
| 90 |
} |
| 91 |
fields_[field.name()] = field; |
| 92 |
return field; |
| 93 |
} |
| 94 |
|
| 95 |
const Field& State::field(const std::string& name) const { |
| 96 |
if (!has(name)) { |
| 97 |
std::ostringstream msg; |
| 98 |
msg << "Trying to access field `" << name << "' in State, but no field with this name is present in State."; |
| 99 |
throw_AssertionFailed(msg.str(), Here()); |
| 100 |
} |
| 101 |
return fields_.find(name)->second; |
| 102 |
} |
| 103 |
|
| 104 |
Field& State::field(const std::string& name) { |
| 105 |
return const_cast<Field&>(static_cast<const State*>(this)->field(name)); |
| 106 |
} |
| 107 |
|
| 108 |
const Field& State::field(const idx_t idx) const { |
| 109 |
if (idx >= size()) { |
| 110 |
std::ostringstream msg; |
| 111 |
msg << "Trying to access field in State with index " << idx << ", but there exist only " << size() |
| 112 |
<< " fields in State."; |
| 113 |
throw_AssertionFailed(msg.str(), Here()); |
| 114 |
} |
| 115 |
FieldMap::const_iterator it = fields_.begin(); |
| 116 |
for (idx_t i = 0; i < idx; ++i) { |
| 117 |
++it; |
| 118 |
} |
| 119 |
return it->second; |
| 120 |
} |
| 121 |
|
| 122 |
Field& State::field(const idx_t idx) { |
| 123 |
return const_cast<Field&>(static_cast<const State*>(this)->field(idx)); |
| 124 |
} |
| 125 |
|
| 126 |
std::vector<std::string> State::field_names() const { |
| 127 |
std::vector<std::string> ret; |
| 128 |
if (fields_.size()) { |
| 129 |
ret.reserve(fields_.size()); |
| 130 |
} |
| 131 |
|
| 132 |
for (FieldMap::const_iterator it = fields_.begin(); it != fields_.end(); ++it) { |
| 133 |
ret.push_back(it->first); |
| 134 |
} |
| 135 |
return ret; |
| 136 |
} |
| 137 |
|
| 138 |
void State::remove(const std::string& name) { |
| 139 |
if (fields_.find(name) == fields_.end()) { |
| 140 |
std::stringstream msg; |
| 141 |
msg << "Trying to remove field '" << name << "' from State, but it is not present in State."; |
| 142 |
throw_AssertionFailed(msg.str(), Here()); |
| 143 |
} |
| 144 |
fields_.erase(name); |
| 145 |
} |
| 146 |
|
| 147 |
//----------------------------------------------------------------------------- |
| 148 |
|
| 149 |
StateGenerator::StateGenerator(const eckit::Parametrisation&) {} |
| 150 |
|
| 151 |
StateGenerator::~StateGenerator() = default; |
| 152 |
|
| 153 |
StateGenerator* StateGeneratorFactory::build(const std::string& name, const eckit::Parametrisation& param) { |
| 154 |
pthread_once(&once, init); |
| 155 |
|
| 156 |
eckit::AutoLock<eckit::Mutex> lock(local_mutex); |
| 157 |
|
| 158 |
force_link(); |
| 159 |
|
| 160 |
std::map<std::string, StateGeneratorFactory*>::const_iterator j = m->find(name); |
| 161 |
|
| 162 |
Log::debug() << "Looking for StateGeneratorFactory [" << name << "]" << std::endl; |
| 163 |
|
| 164 |
if (j == m->end()) { |
| 165 |
Log::error() << "No StateGeneratorFactory for [" << name << "]" << std::endl; |
| 166 |
Log::error() << "StateFactories are:" << std::endl; |
| 167 |
for (j = m->begin(); j != m->end(); ++j) { |
| 168 |
Log::error() << " " << (*j).first << std::endl; |
| 169 |
} |
| 170 |
throw_Exception(std::string("No StateGeneratorFactory called ") + name, Here()); |
| 171 |
} |
| 172 |
|
| 173 |
return (*j).second->make(param); |
| 174 |
} |
| 175 |
|
| 176 |
void StateGeneratorFactory::list(std::ostream& out) { |
| 177 |
pthread_once(&once, init); |
| 178 |
|
| 179 |
eckit::AutoLock<eckit::Mutex> lock(local_mutex); |
| 180 |
|
| 181 |
force_link(); |
| 182 |
|
| 183 |
const char* sep = ""; |
| 184 |
for (std::map<std::string, StateGeneratorFactory*>::const_iterator j = m->begin(); j != m->end(); ++j) { |
| 185 |
out << sep << (*j).first; |
| 186 |
sep = ", "; |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
bool StateGeneratorFactory::has(const std::string& name) { |
| 191 |
pthread_once(&once, init); |
| 192 |
|
| 193 |
eckit::AutoLock<eckit::Mutex> lock(local_mutex); |
| 194 |
|
| 195 |
force_link(); |
| 196 |
|
| 197 |
return (m->find(name) != m->end()); |
| 198 |
} |
| 199 |
|
| 200 |
StateGeneratorFactory::StateGeneratorFactory(const std::string& name): name_(name) { |
| 201 |
pthread_once(&once, init); |
| 202 |
|
| 203 |
eckit::AutoLock<eckit::Mutex> lock(local_mutex); |
| 204 |
|
| 205 |
ATLAS_ASSERT(m->find(name) == m->end()); |
| 206 |
(*m)[name] = this; |
| 207 |
} |
| 208 |
|
| 209 |
StateGeneratorFactory::~StateGeneratorFactory() { |
| 210 |
eckit::AutoLock<eckit::Mutex> lock(local_mutex); |
| 211 |
m->erase(name_); |
| 212 |
} |
| 213 |
|
| 214 |
//----------------------------------------------------------------------------- |
| 215 |
// C wrapper interfaces to C++ routines |
| 216 |
|
| 217 |
extern "C" { |
| 218 |
|
| 219 |
State* atlas__State__new() { |
| 220 |
return new State; |
| 221 |
} |
| 222 |
|
| 223 |
void atlas__State__initialize(State* This, const char* generator, const eckit::Parametrisation* params) { |
| 224 |
ATLAS_ASSERT(This != nullptr, "Reason: Use of uninitialised atlas_State"); |
| 225 |
This->initialize(std::string(generator), *params); |
| 226 |
} |
| 227 |
|
| 228 |
void atlas__State__delete(State* This) { |
| 229 |
ATLAS_ASSERT(This != nullptr, "Reason: Use of uninitialised atlas_State"); |
| 230 |
delete This; |
| 231 |
} |
| 232 |
|
| 233 |
void atlas__State__add(State* This, FieldImpl* field) { |
| 234 |
ATLAS_ASSERT(This != nullptr, "Reason: Use of uninitialised atlas_State"); |
| 235 |
This->add(field); |
| 236 |
} |
| 237 |
|
| 238 |
void atlas__State__remove(State* This, const char* name) { |
| 239 |
ATLAS_ASSERT(This != nullptr, "Reason: Use of uninitialised atlas_State"); |
| 240 |
This->remove(name); |
| 241 |
} |
| 242 |
|
| 243 |
int atlas__State__has(State* This, const char* name) { |
| 244 |
ATLAS_ASSERT(This != nullptr, "Reason: Use of uninitialised atlas_State"); |
| 245 |
return This->has(name); |
| 246 |
} |
| 247 |
|
| 248 |
FieldImpl* atlas__State__field_by_name(State* This, const char* name) { |
| 249 |
ATLAS_ASSERT(This != nullptr, "Reason: Use of uninitialised atlas_State"); |
| 250 |
return This->field(std::string(name)).get(); |
| 251 |
} |
| 252 |
|
| 253 |
FieldImpl* atlas__State__field_by_index(State* This, idx_t index) { |
| 254 |
ATLAS_ASSERT(This != nullptr, "Reason: Use of uninitialised atlas_State"); |
| 255 |
return This->field(index).get(); |
| 256 |
} |
| 257 |
|
| 258 |
idx_t atlas__State__size(const State* This) { |
| 259 |
ATLAS_ASSERT(This != nullptr, "Reason: Use of uninitialised atlas_State"); |
| 260 |
return This->size(); |
| 261 |
} |
| 262 |
|
| 263 |
util::Metadata* atlas__State__metadata(State* This) { |
| 264 |
ATLAS_ASSERT(This != nullptr, "Reason: Use of uninitialised atlas_State"); |
| 265 |
return &This->metadata(); |
| 266 |
} |
| 267 |
} |
| 268 |
//----------------------------------------------------------------------------- |
| 269 |
|
| 270 |
} // namespace field |
| 271 |
} // namespace atlas |