Debian Codegraph @

src/atlas/field/MultiField.h

raw
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 #include <vector>
18
19 #include "atlas/array/Array.h"
20 #include "atlas/field/Field.h"
21 #include "atlas/field/FieldSet.h"
22 #include "atlas/util/Config.h"
23 #include "atlas/util/Factory.h"
24 #include "atlas/util/Metadata.h"
25 #include "atlas/util/Object.h"
26 #include "atlas/util/ObjectHandle.h"
27
28 namespace eckit {
29 class Parametrisation;
30 }
31
32 namespace atlas {
33 namespace field {
34 class MultiFieldImpl;
35 }
36 }
37
38 namespace atlas {
39 namespace field {
40
41 /**
42 * \brief MultiField class that owns a collection of fields that are co-allocated
43 *
44 * Fields can only be described by parametrisation during the construction.
45 * Once setup, no additional fields can be added.
46 *
47 * Fields have to all be of same memory layout and data type
48 */
49
50 class MultiField : public util::ObjectHandle<MultiFieldImpl> {
51 public: // methods
52 //-- Constructors
53 using Handle::Handle;
54
55 MultiField(const eckit::Configuration&);
56 MultiField(const array::DataType datatype, const array::ArrayShape& shape,
57 const std::vector<std::string>& var_names);
58
59 //-- Accessors
60
61 const Field& field(const std::string& name) const;
62 Field& field(const std::string& name);
63 bool has(const std::string& name) const;
64 std::vector<std::string> field_names() const;
65
66 const Field& field(const idx_t idx) const;
67 Field& field(const idx_t idx);
68 idx_t size() const;
69
70 const Field& operator[](const idx_t idx) const;
71 Field& operator[](const idx_t idx);
72
73 const Field& operator[](const std::string& name) const;
74 Field& operator[](const std::string& name);
75
76 const util::Metadata& metadata() const;
77 util::Metadata& metadata();
78
79 // -- Modifiers
80
81 /// @brief Implicit conversion to Array
82 operator const array::Array&() const;
83 operator array::Array&();
84
85 operator const FieldSet&() const;
86 operator FieldSet&();
87
88 /// @brief Access contained Array
89 const array::Array& array() const;
90 array::Array& array();
91
92 private:
93 template<typename datatype>
94 void create(const std::vector<int> shape, const std::vector<std::string> var_names);
95 };
96
97 /**
98 * \brief MultiFieldArrayRegistry
99 */
100
101 class MultiFieldArrayRegistry : public field::FieldObserver {
102 private:
103 MultiFieldArrayRegistry() {}
104
105 public:
106 static MultiFieldArrayRegistry& instance() {
107 static MultiFieldArrayRegistry inst;
108 return inst;
109 }
110 void onFieldDestruction(FieldImpl& field) override {
111 std::lock_guard<std::mutex> guard(lock_);
112 map_.erase(&field);
113 }
114
115 ~MultiFieldArrayRegistry() override = default;
116
117 void add(Field& field, std::shared_ptr<array::Array> array) {
118 std::lock_guard<std::mutex> guard(lock_);
119 map_.emplace(field.get(), array);
120 field->attachObserver(*this);
121 }
122
123 public:
124 std::mutex lock_;
125 std::map<FieldImpl*,std::shared_ptr<array::Array>> map_;
126
127 };
128
129 // ------------------------------------------------------------------------------------
130
131 } // namespace field
132 } // namespace atlas