|
| 1 | +/* |
| 2 | + * Copyright 2009- 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 | +#ifndef ecflow_node_DefsTreeVisitor_HPP |
| 12 | +#define ecflow_node_DefsTreeVisitor_HPP |
| 13 | + |
| 14 | +#include <string> |
| 15 | +#include <vector> |
| 16 | + |
| 17 | +#include <ecflow/node/Alias.hpp> |
| 18 | +#include <ecflow/node/Defs.hpp> |
| 19 | +#include <ecflow/node/Family.hpp> |
| 20 | +#include <ecflow/node/Node.hpp> |
| 21 | +#include <ecflow/node/Suite.hpp> |
| 22 | +#include <ecflow/node/Task.hpp> |
| 23 | + |
| 24 | +namespace ecf { |
| 25 | + |
| 26 | +struct NodeNotFound : public std::runtime_error |
| 27 | +{ |
| 28 | + using std::runtime_error::runtime_error; |
| 29 | +}; |
| 30 | + |
| 31 | +template <typename V> |
| 32 | +struct DefsTreeVisitor |
| 33 | +{ |
| 34 | + using path_t = std::string; |
| 35 | + using nodes_t = std::vector<const Node*>; |
| 36 | + |
| 37 | + DefsTreeVisitor(const defs_ptr& defs, V& v) : defs_{*defs}, v_{v} {} |
| 38 | + |
| 39 | + DefsTreeVisitor(const Defs& defs, V& v) : defs_{defs}, v_{v} {} |
| 40 | + |
| 41 | + void visit_at(const path_t& path) { |
| 42 | + nodes_t nodes = nodes_at(path); |
| 43 | + for (auto node : nodes) { |
| 44 | + visit(*node); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | +private: |
| 49 | + const Defs& defs_; |
| 50 | + V& v_; |
| 51 | + |
| 52 | +private: |
| 53 | + nodes_t nodes_at(const path_t& path) const { |
| 54 | + nodes_t nodes; |
| 55 | + if (path == "/") { |
| 56 | + std::vector<suite_ptr> suites = defs_.suiteVec(); |
| 57 | + std::transform(std::begin(suites), std::end(suites), std::back_inserter(nodes), [](const suite_ptr& ptr) { |
| 58 | + return static_cast<Node*>(ptr.get()); |
| 59 | + }); |
| 60 | + } |
| 61 | + else { |
| 62 | + nodes = nodes_t{get_node(path).get()}; |
| 63 | + } |
| 64 | + return nodes; |
| 65 | + } |
| 66 | + |
| 67 | + node_ptr get_node(const std::string& path) const { |
| 68 | + if (node_ptr node = defs_.findAbsNode(path); node) { |
| 69 | + return node; |
| 70 | + } |
| 71 | + |
| 72 | + throw NodeNotFound(std::string{"Unable to find node: "} + path); |
| 73 | + } |
| 74 | + |
| 75 | + void visit(const Node& node) { |
| 76 | + |
| 77 | + if (auto found = dynamic_cast<const Suite*>(&node); found) { |
| 78 | + // Visit suite itself |
| 79 | + v_.begin_visit(*found); |
| 80 | + // Visit suite children |
| 81 | + for (auto&& entry : found->nodeVec()) { |
| 82 | + visit(*entry.get()); |
| 83 | + } |
| 84 | + v_.end_visit(*found); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + if (auto found = dynamic_cast<const Family*>(&node); found) { |
| 89 | + // Visit family itself |
| 90 | + v_.begin_visit(*found); |
| 91 | + // Visit family children |
| 92 | + for (auto&& entry : found->nodeVec()) { |
| 93 | + visit(*entry.get()); |
| 94 | + } |
| 95 | + v_.end_visit(*found); |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + if (auto found = dynamic_cast<const Task*>(&node); found) { |
| 100 | + // Visit task itself |
| 101 | + v_.begin_visit(*found); |
| 102 | + // Visit task children (i.e. aliases) |
| 103 | + std::vector<alias_ptr> aliases; |
| 104 | + found->get_all_aliases(aliases); |
| 105 | + for (const auto& entry : aliases) { |
| 106 | + visit(*entry.get()); |
| 107 | + } |
| 108 | + v_.end_visit(*found); |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + if (auto found = dynamic_cast<const Alias*>(&node); found) { |
| 113 | + // Visit alias itself |
| 114 | + v_.begin_visit(*found); |
| 115 | + v_.end_visit(*found); |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + // Important! |
| 120 | + // If this point is reached, then some kind of Node isn't being properly handled. |
| 121 | + panic(); |
| 122 | + } |
| 123 | + |
| 124 | + static void panic() { |
| 125 | + assert(false); |
| 126 | + std::terminate(); |
| 127 | + } |
| 128 | +}; |
| 129 | + |
| 130 | +} // namespace ecf |
| 131 | + |
| 132 | +#endif /* ecflow_node_DefsTreeVisitor_HPP */ |
0 commit comments