2020#include " eckit/geo/Spec.h"
2121#include " eckit/geo/spec/Layered.h"
2222#include " eckit/geo/util/mutex.h"
23+ #include " eckit/parser/YAMLParser.h"
2324
2425
2526namespace eckit ::geo {
@@ -36,26 +37,34 @@ class lock_type {
3637};
3738
3839
39- std::map<std::string, size_t > ORDERING_INDEX ;
40- std::map<size_t , std::string> ORDERING_NAME ;
40+ std::map<std::string, size_t > ORDER_INDEX ;
41+ std::map<size_t , std::string> ORDER_NAME ;
4142
4243
4344} // namespace
4445
4546
46- std::string Order::className () {
47- static const std::string ordering = " ordering" ;
48- return ordering;
47+ const Spec& Order::spec () const {
48+ if (!spec_) {
49+ spec_ = std::make_shared<spec::Custom>();
50+ ASSERT (spec_);
51+
52+ auto & custom = *spec_;
53+ fill_spec (custom);
54+ }
55+
56+ return *spec_;
4957}
5058
5159
52- Order::value_type Order::make_ordering_from_spec (const Spec&) {
53- NOTIMP;
60+ std::string Order::className () {
61+ static const std::string order = " order" ;
62+ return order;
63+ }
64+
5465
55- // int key = (spec.get_bool("scan_i_plus", true) ? 0 : 1) + (spec.get_bool("scan_j_plus", false) ? 1 << 1 : 0) +
56- // (spec.get_bool("scan_ij", true) ? 0 : 1 << 2) + (spec.get_bool("scan_alternating", false) ? 1 << 3 :
57- // 0);
58- // return static_cast<value_type>(key);
66+ Order::value_type Order::make_order_from_spec (const Spec& spec) {
67+ return std::unique_ptr<const Order>(OrderFactory::build (spec))->order ();
5968}
6069
6170
@@ -66,69 +75,59 @@ Reordering Order::no_reorder(size_t size) {
6675}
6776
6877
69- void Order::fill_spec (spec::Custom& custom) const {
70- if (order () != order_default ()) {
71- custom.set (" type" , type ());
72- custom.set (" order" , order ());
73- }
74- }
75-
76-
7778void Order::register_ordering (const std::string& name) {
7879 lock_type lock;
7980
80- auto index = ORDERING_INDEX.size ();
81-
82- ASSERT (ORDERING_INDEX.emplace (name, index).second );
83- ASSERT (ORDERING_NAME.emplace (index, name).second );
81+ auto index = ORDER_INDEX.size ();
82+ ASSERT (ORDER_INDEX.try_emplace (name, index).second == ORDER_NAME.try_emplace (index, name).second );
8483}
8584
8685
87- const Order* ReorderFactory ::make_from_string (const std::string&) {
88- // FIXME
89- NOTIMP ;
86+ const Order* OrderFactory ::make_from_string (const std::string& str ) {
87+ std::unique_ptr<Spec> spec ( spec::Custom::make_from_value ( YAMLParser::decodeString (str)));
88+ return instance (). make_from_spec_ (*spec) ;
9089}
9190
9291
93- ReorderFactory& ReorderFactory ::instance () {
94- static ReorderFactory obj;
92+ OrderFactory& OrderFactory ::instance () {
93+ static OrderFactory obj;
9594 return obj;
9695}
9796
9897
99- const Order* ReorderFactory ::make_from_spec_ (const Spec& spec) const {
98+ const Order* OrderFactory ::make_from_spec_ (const Spec& spec) const {
10099 lock_type lock;
101100
102101 std::unique_ptr<Spec> cfg (make_spec_ (spec));
103102
104103 if (std::string type; cfg->get (" type" , type)) {
105- return ReorderFactoryType ::instance ().get (type).create (*cfg);
104+ return OrderFactoryType ::instance ().get (type).create (*cfg);
106105 }
107106
108- list (Log::error () << " Reorder : cannot build ordering without 'type', choices are: " );
109- throw exception::SpecError (" Reorder : cannot build ordering without 'type'" , Here ());
107+ list (Log::error () << " Order : cannot build order without 'type', choices are: " );
108+ throw exception::SpecError (" Order : cannot build order without 'type'" , Here ());
110109}
111110
112111
113- Spec* ReorderFactory ::make_spec_ (const Spec& spec) const {
112+ Spec* OrderFactory ::make_spec_ (const Spec& spec) const {
114113 lock_type lock;
115114
116115 auto * cfg = new spec::Layered (spec);
117116 ASSERT (cfg != nullptr );
118117
119118
120- // hardcoded, interpreted options (contributing to orderingspec )
119+ // hardcoded, interpreted options (contributing to orderspec )
121120
122- // cfg->push_back(new spec::Custom{{"type", ""}});
121+ cfg->push_back (new spec::Custom{{" type" , " scan " }});
123122
124123 return cfg;
125124}
126125
127126
128- std::ostream& ReorderFactory ::list_ (std::ostream& out) const {
127+ std::ostream& OrderFactory ::list_ (std::ostream& out) const {
129128 lock_type lock;
130129
131- out << ReorderFactoryType ::instance () << std::endl;
130+ out << OrderFactoryType ::instance () << std::endl;
132131
133132 return out;
134133}
0 commit comments