| 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 |
/// @author Willem Deconinck |
| 12 |
/// @date June 2015 |
| 13 |
|
| 14 |
#pragma once |
| 15 |
|
| 16 |
#include <map> |
| 17 |
|
| 18 |
#include "atlas/field/Field.h" |
| 19 |
#include "atlas/util/Config.h" |
| 20 |
#include "atlas/util/Metadata.h" |
| 21 |
#include "atlas/util/Object.h" |
| 22 |
|
| 23 |
namespace eckit { |
| 24 |
class Parametrisation; |
| 25 |
} |
| 26 |
|
| 27 |
namespace atlas { |
| 28 |
namespace field { |
| 29 |
|
| 30 |
/** |
| 31 |
* \brief State class that owns a collection of fields |
| 32 |
*/ |
| 33 |
class State : public util::Object { |
| 34 |
public: // methods |
| 35 |
//-- Constructors |
| 36 |
State(); |
| 37 |
|
| 38 |
State(const std::string& generator, const eckit::Parametrisation& = util::Config()); |
| 39 |
|
| 40 |
//-- Accessors |
| 41 |
|
| 42 |
const Field& field(const std::string& name) const; |
| 43 |
Field& field(const std::string& name); |
| 44 |
bool has(const std::string& name) const { return (fields_.find(name) != fields_.end()); } |
| 45 |
std::vector<std::string> field_names() const; |
| 46 |
|
| 47 |
const Field& field(const idx_t idx) const; |
| 48 |
Field& field(const idx_t idx); |
| 49 |
idx_t size() const { return static_cast<idx_t>(fields_.size()); } |
| 50 |
|
| 51 |
const Field& operator[](const idx_t idx) const { return field(idx); } |
| 52 |
Field& operator[](const idx_t idx) { return field(idx); } |
| 53 |
|
| 54 |
const Field& operator[](const std::string& name) const { return field(name); } |
| 55 |
Field& operator[](const std::string& name) { return field(name); } |
| 56 |
|
| 57 |
const util::Metadata& metadata() const; |
| 58 |
util::Metadata& metadata(); |
| 59 |
|
| 60 |
// -- Modifiers |
| 61 |
|
| 62 |
void initialize(const std::string& generator, const eckit::Parametrisation& = util::Config()); |
| 63 |
|
| 64 |
Field add(Field); |
| 65 |
|
| 66 |
void remove(const std::string& name); |
| 67 |
|
| 68 |
private: |
| 69 |
typedef std::map<std::string, Field> FieldMap; |
| 70 |
|
| 71 |
private: |
| 72 |
FieldMap fields_; |
| 73 |
util::Metadata metadata_; |
| 74 |
}; |
| 75 |
|
| 76 |
//------------------------------------------------------------------------------------------------------ |
| 77 |
|
| 78 |
class StateGenerator : public util::Object { |
| 79 |
public: |
| 80 |
StateGenerator(const eckit::Parametrisation& = util::Config()); |
| 81 |
|
| 82 |
virtual ~StateGenerator(); |
| 83 |
|
| 84 |
virtual void generate(State&, const eckit::Parametrisation& = util::Config()) const = 0; |
| 85 |
}; |
| 86 |
|
| 87 |
//------------------------------------------------------------------------------------------------------ |
| 88 |
|
| 89 |
class StateGeneratorFactory { |
| 90 |
public: |
| 91 |
/*! |
| 92 |
* \brief build StateCreator with options specified in parametrisation |
| 93 |
* \return mesh generator |
| 94 |
*/ |
| 95 |
static StateGenerator* build(const std::string& state_generator, const eckit::Parametrisation& = util::Config()); |
| 96 |
|
| 97 |
/*! |
| 98 |
* \brief list all registered field creators |
| 99 |
*/ |
| 100 |
static void list(std::ostream&); |
| 101 |
static bool has(const std::string& name); |
| 102 |
|
| 103 |
private: |
| 104 |
virtual StateGenerator* make(const eckit::Parametrisation& = util::Config()) = 0; |
| 105 |
|
| 106 |
std::string name_; |
| 107 |
|
| 108 |
protected: |
| 109 |
StateGeneratorFactory(const std::string&); |
| 110 |
virtual ~StateGeneratorFactory(); |
| 111 |
}; |
| 112 |
|
| 113 |
template <class T> |
| 114 |
class StateGeneratorBuilder : public StateGeneratorFactory { |
| 115 |
virtual StateGenerator* make(const eckit::Parametrisation& param = util::Config()) { return new T(param); } |
| 116 |
|
| 117 |
public: |
| 118 |
StateGeneratorBuilder(const std::string& name): StateGeneratorFactory(name) {} |
| 119 |
}; |
| 120 |
|
| 121 |
// ------------------------------------------------------------------------------------ |
| 122 |
|
| 123 |
// C wrapper interfaces to C++ routines |
| 124 |
extern "C" { |
| 125 |
State* atlas__State__new(); |
| 126 |
void atlas__State__initialize(State* This, const char* generator, const eckit::Parametrisation* params); |
| 127 |
void atlas__State__delete(State* This); |
| 128 |
void atlas__State__add(State* This, FieldImpl* field); |
| 129 |
void atlas__State__remove(State* This, const char* name); |
| 130 |
int atlas__State__has(State* This, const char* name); |
| 131 |
FieldImpl* atlas__State__field_by_name(State* This, const char* name); |
| 132 |
FieldImpl* atlas__State__field_by_index(State* This, idx_t index); |
| 133 |
idx_t atlas__State__size(const State* This); |
| 134 |
util::Metadata* atlas__State__metadata(State* This); |
| 135 |
} |
| 136 |
|
| 137 |
} // namespace field |
| 138 |
} // namespace atlas |