|
| 1 | +/* |
| 2 | + * (C) Copyright 1996- 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 nor |
| 8 | + * does it submit to any jurisdiction. |
| 9 | + */ |
| 10 | + |
| 11 | +#include "metkit/mars/MarsExpansion.h" |
| 12 | + |
| 13 | +#include "metkit/mars/MarsLanguage.h" |
| 14 | + |
| 15 | + |
| 16 | +namespace metkit::mars { |
| 17 | + |
| 18 | +//---------------------------------------------------------------------------------------------------------------------- |
| 19 | + |
| 20 | +FlattenCallback::~FlattenCallback() = default; |
| 21 | + |
| 22 | +ExpandCallback::~ExpandCallback() = default; |
| 23 | + |
| 24 | +//---------------------------------------------------------------------------------------------------------------------- |
| 25 | + |
| 26 | +MarsExpansion::MarsExpansion(bool inherit, bool strict) : inherit_(inherit), strict_(strict) {} |
| 27 | + |
| 28 | +MarsExpansion::~MarsExpansion() { |
| 29 | + for (auto& language : languages_) { |
| 30 | + delete language.second; |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +void MarsExpansion::reset() { |
| 35 | + for (auto& language : languages_) { |
| 36 | + language.second->reset(); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | + |
| 41 | +MarsLanguage& MarsExpansion::language(const MarsExpandContext& ctx, const std::string& verb) { |
| 42 | + auto v = MarsLanguage::expandVerb(ctx, verb); |
| 43 | + |
| 44 | + if (auto j = languages_.find(v); j != languages_.end()) { |
| 45 | + return *(*j).second; |
| 46 | + } |
| 47 | + |
| 48 | + auto j = languages_.emplace(v, new MarsLanguage(v)).first; |
| 49 | + return *(*j).second; |
| 50 | +} |
| 51 | + |
| 52 | + |
| 53 | +std::vector<MarsRequest> MarsExpansion::expand(const std::vector<MarsParsedRequest>& requests) { |
| 54 | + std::vector<MarsRequest> result; |
| 55 | + result.reserve(requests.size()); |
| 56 | + |
| 57 | + // Implement inheritence |
| 58 | + for (const auto& request : requests) { |
| 59 | + auto& lang = language(request, request.verb()); |
| 60 | + result.emplace_back(lang.expand(request, request, inherit_, strict_)); |
| 61 | + } |
| 62 | + |
| 63 | + return result; |
| 64 | +} |
| 65 | + |
| 66 | +MarsRequest MarsExpansion::expand(const MarsRequest& request) { |
| 67 | + DummyContext ctx; |
| 68 | + auto& lang = language(ctx, request.verb()); |
| 69 | + return lang.expand(ctx, request, inherit_, strict_); |
| 70 | +} |
| 71 | + |
| 72 | + |
| 73 | +void MarsExpansion::expand(const MarsExpandContext& ctx, const MarsRequest& request, ExpandCallback& callback) { |
| 74 | + MarsRequest r = language(ctx, request.verb()).expand(ctx, request, inherit_, strict_); |
| 75 | + callback(ctx, r); |
| 76 | +} |
| 77 | + |
| 78 | + |
| 79 | +void MarsExpansion::flatten(const MarsExpandContext& ctx, const MarsRequest& request, FlattenCallback& callback) { |
| 80 | + language(ctx, request.verb()).flatten(ctx, request, callback); |
| 81 | +} |
| 82 | + |
| 83 | +//---------------------------------------------------------------------------------------------------------------------- |
| 84 | + |
| 85 | +} // namespace metkit::mars |
0 commit comments