Debian Codegraph @

src/atlas/field/FieldCreator.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 #ifndef atlas_field_FieldCreator_h
15 #define atlas_field_FieldCreator_h
16
17 #include <string>
18
19 #include "atlas/util/Object.h"
20
21 #include "atlas/field/Field.h"
22
23 namespace eckit {
24 class Parametrisation;
25 }
26
27 //------------------------------------------------------------------------------------------------------
28
29 namespace atlas {
30 namespace field {
31
32 //------------------------------------------------------------------------------------------------------
33
34 /*!
35 * \brief Base class for creating new fields based on Parametrisation
36 *
37 * \details
38 * Example to create field[100][3] of default type double:
39 * \code{.cpp}
40 * FieldImpl* field = Field::create(
41 * Config
42 * ("creator","ArraySpec") // ArraySpec FieldCreator
43 * ("shape",array::make_shape(100,3)) // Rank 2 field with indexing [100][3]
44 * );
45 * \endcode
46 */
47 class FieldCreator : public util::Object {
48 public:
49 FieldCreator();
50
51 virtual ~FieldCreator();
52
53 virtual FieldImpl* createField(const eckit::Parametrisation&) const = 0;
54 };
55
56 //------------------------------------------------------------------------------------------------------
57
58 class FieldCreatorFactory {
59 public:
60 /*!
61 * \brief build FieldCreator with factory key, and default options
62 * \return FieldCreator
63 */
64 static FieldCreator* build(const std::string&);
65
66 /*!
67 * \brief build FieldCreator with options specified in parametrisation
68 * \return mesh generator
69 */
70 static FieldCreator* build(const std::string&, const eckit::Parametrisation&);
71
72 /*!
73 * \brief list all registered field creators
74 */
75 static void list(std::ostream&);
76
77 private:
78 std::string name_;
79 virtual FieldCreator* make() = 0;
80 virtual FieldCreator* make(const eckit::Parametrisation&) = 0;
81
82 protected:
83 FieldCreatorFactory(const std::string&);
84 virtual ~FieldCreatorFactory();
85 };
86
87 template <class T>
88 class FieldCreatorBuilder : public FieldCreatorFactory {
89 virtual FieldCreator* make() { return new T(); }
90 virtual FieldCreator* make(const eckit::Parametrisation& param) { return new T(param); }
91
92 public:
93 FieldCreatorBuilder(const std::string& name): FieldCreatorFactory(name) {}
94 };
95
96 //------------------------------------------------------------------------------------------------------
97
98 } // namespace field
99 } // namespace atlas
100
101 #endif