atlas
FieldSet.h
Go to the documentation of this file.
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 
15 
16 #pragma once
17 
18 #include <cstdlib>
19 #include <iterator>
20 #include <map>
21 #include <string>
22 #include <type_traits>
23 #include <vector>
24 
25 #include "atlas/field/Field.h"
26 #include "atlas/library/config.h"
27 #include "atlas/runtime/Exception.h"
28 #include "atlas/util/Object.h"
29 #include "atlas/util/ObjectHandle.h"
30 
31 namespace atlas {
32 
33 class FieldSet;
34 
35 #ifndef DOXYGEN_SHOULD_SKIP_THIS
36 
37 namespace field {
38 
42 class FieldSetImpl : public util::Object {
43 public: // types
44  using iterator = std::vector<Field>::iterator;
45  using const_iterator = std::vector<Field>::const_iterator;
46 
47  template <typename T>
48  static constexpr bool is_index() {
49  return std::is_integral<T>::value or std::is_enum<T>::value;
50  }
51 
52  template <bool pred>
53  using enable_if_t = typename std::enable_if<pred, int>::type;
54 
55  template <typename T>
56  using enable_if_index_t = enable_if_t<is_index<T>()>;
57 
58 
59 public: // methods
61  FieldSetImpl( const std::string& name = "untitled" );
62 
63  idx_t size() const { return static_cast<idx_t>( fields_.size() ); }
64  bool empty() const { return !fields_.size(); }
65 
66  void clear();
67 
68  const std::string& name() const { return name_; }
69  std::string& name() { return name_; }
70 
71  template <typename Index, enable_if_index_t<Index> = 0>
72  const Field& operator[]( Index i ) const {
73  return field( i );
74  }
75 
76  template <typename Index, enable_if_index_t<Index> = 0>
77  Field& operator[]( Index i ) {
78  return field( i );
79  }
80 
81  const Field& operator[]( const std::string& name ) const { return field( name ); }
82  Field& operator[]( const std::string& name ) { return field( name ); }
83 
84  template <typename Index, enable_if_index_t<Index> = 0>
85  const Field& field( Index i ) const {
86  if ( i >= size() )
87  throw_OutOfRange( "fieldset", i, size(), Here() );
88  return fields_[i];
89  }
90 
91  template <typename Index, enable_if_index_t<Index> = 0>
92  Field& field( Index i ) {
93  if ( i >= size() )
94  throw_OutOfRange( "fieldset", i, size(), Here() );
95  return fields_[i];
96  }
97 
98  std::vector<std::string> field_names() const;
99 
100  Field add( const Field& );
101 
102  bool has_field( const std::string& name ) const;
103 
104  Field& field( const std::string& name ) const;
105 
106  iterator begin() { return fields_.begin(); }
107  iterator end() { return fields_.end(); }
108  const_iterator begin() const { return fields_.begin(); }
109  const_iterator end() const { return fields_.end(); }
110  const_iterator cbegin() const { return fields_.begin(); }
111  const_iterator cend() const { return fields_.end(); }
112 
113  void haloExchange( bool on_device = false ) const;
114  void adjointHaloExchange( bool on_device = false ) const;
115  void set_dirty( bool = true ) const;
116 
117 protected: // data
118  std::vector<Field> fields_;
119  std::string name_;
120  std::map<std::string, idx_t> index_;
121 };
122 
123 class FieldImpl;
124 
125 // C wrapper interfaces to C++ routines
126 extern "C" {
127 FieldSetImpl* atlas__FieldSet__new( char* name );
128 void atlas__FieldSet__delete( FieldSetImpl* This );
129 void atlas__FieldSet__add_field( FieldSetImpl* This, FieldImpl* field );
130 int atlas__FieldSet__has_field( const FieldSetImpl* This, char* name );
131 idx_t atlas__FieldSet__size( const FieldSetImpl* This );
132 FieldImpl* atlas__FieldSet__field_by_name( FieldSetImpl* This, char* name );
133 FieldImpl* atlas__FieldSet__field_by_idx( FieldSetImpl* This, idx_t idx );
134 void atlas__FieldSet__set_dirty( FieldSetImpl* This, int value );
135 void atlas__FieldSet__halo_exchange( FieldSetImpl* This, int on_device );
136 }
137 
138 } // namespace field
139 #endif
140 
141 //---------------------------------------------------------------------------------------------------------------------
142 
146 class FieldSet : DOXYGEN_HIDE( public util::ObjectHandle<field::FieldSetImpl> ) {
147 public: // types
148  using iterator = Implementation::iterator;
149  using const_iterator = Implementation::const_iterator;
150 
151  template <typename T>
152  using enable_if_index_t = Implementation::enable_if_index_t<T>;
153 
154 public: // methods
155  using Handle::Handle;
156  FieldSet();
157  FieldSet( const std::string& name );
158  FieldSet( const Field& );
159 
160  idx_t size() const { return get()->size(); }
161  bool empty() const { return get()->empty(); }
162 
163  void clear() { get()->clear(); }
164 
165  const std::string& name() const { return get()->name(); }
166  std::string& name() { return get()->name(); }
167 
168  template <typename Index, enable_if_index_t<Index> = 0>
169  const Field& operator[]( Index i ) const {
170  return get()->operator[]( i );
171  }
172 
173  template <typename Index, enable_if_index_t<Index> = 0>
174  Field& operator[]( Index i ) {
175  return get()->operator[]( i );
176  }
177 
178  const Field& operator[]( const std::string& name ) const { return get()->operator[]( name ); }
179  Field& operator[]( const std::string& name ) { return get()->operator[]( name ); }
180 
181  const Field& operator[]( const char* name ) const { return get()->operator[]( name ); }
182  Field& operator[]( const char* name ) { return get()->operator[]( name ); }
183 
184  template <typename Index, enable_if_index_t<Index> = 0>
185  const Field& field( Index i ) const {
186  return get()->field( i );
187  }
188 
189  template <typename Index, enable_if_index_t<Index> = 0>
190  Field& field( Index i ) {
191  return get()->field( i );
192  }
193 
194  std::vector<std::string> field_names() const { return get()->field_names(); }
195 
196  Field add( const Field& field ) { return get()->add( field ); }
197 
198  bool has_field( const std::string& name ) const { return get()->has_field( name ); }
199 
200  Field& field( const std::string& name ) const { return get()->field( name ); }
201 
202  iterator begin() { return get()->begin(); }
203  iterator end() { return get()->end(); }
204  const_iterator begin() const { return get()->begin(); }
205  const_iterator end() const { return get()->end(); }
206  const_iterator cbegin() const { return get()->begin(); }
207  const_iterator cend() const { return get()->end(); }
208 
209  void haloExchange( bool on_device = false ) const { get()->haloExchange( on_device ); }
210  void set_dirty( bool = true ) const;
211 };
212 
213 } // namespace atlas
A Field contains an Array, Metadata, and a reference to a FunctionSpace.
Definition: Field.h:59
std::map< std::string, idx_t > index_
name-to-index map, to refer fields by name
Definition: FieldSet.h:120
FieldSetImpl(const std::string &name="untitled")
Constructs an empty FieldSet.
Definition: FieldSet.cc:23
Definition: Object.h:18
Represents a set of fields, where order is preserved.
Definition: FieldSet.h:42
std::vector< Field > fields_
field storage
Definition: FieldSet.h:118
Represents a set of fields, where order is preserved.
Definition: FieldSet.h:146
Contains all atlas classes and methods.
Definition: atlas-grids.cc:33
Definition: FieldImpl.h:40
long idx_t
Integer type for indices in connectivity tables.
Definition: config.h:42
std::string name_
internal name
Definition: FieldSet.h:119