Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 CESNET, https://photonics.cesnet.cz/ |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 3 | * Copyright (C) 2018 FIT CVUT, https://fit.cvut.cz/ |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 4 | * |
| 5 | * Written by Václav Kubernát <kubervac@fit.cvut.cz> |
| 6 | * |
| 7 | */ |
| 8 | |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 9 | #include <iostream> |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 10 | #include "CTree.hpp" |
Václav Kubernát | 94938b7 | 2018-05-04 15:12:24 +0200 | [diff] [blame] | 11 | #include "utils.hpp" |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 12 | |
| 13 | InvalidNodeException::~InvalidNodeException() = default; |
| 14 | |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 15 | |
Václav Kubernát | 814fa41 | 2018-05-25 19:47:18 +0200 | [diff] [blame^] | 16 | std::string pathToString(const path_& path) |
| 17 | { |
| 18 | std::string res; |
| 19 | for (const auto it : path.m_nodes) |
| 20 | res = joinPaths(res, boost::apply_visitor(nodeToString(), it)); |
| 21 | return res; |
| 22 | } |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 23 | |
| 24 | CTree::CTree() |
| 25 | { |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 26 | m_nodes.emplace("", std::unordered_map<std::string, NodeType>()); |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 27 | } |
| 28 | |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 29 | const std::unordered_map<std::string, NodeType>& CTree::children(const std::string& name) const |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 30 | { |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 31 | return m_nodes.at(name); |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 32 | } |
| 33 | |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 34 | bool CTree::nodeExists(const std::string& location, const std::string& name) const |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 35 | { |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 36 | if (name.empty()) |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 37 | return true; |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 38 | const auto& childrenRef = children(location); |
| 39 | |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 40 | return childrenRef.find(name) != childrenRef.end(); |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 41 | } |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 42 | |
Václav Kubernát | 814fa41 | 2018-05-25 19:47:18 +0200 | [diff] [blame^] | 43 | bool CTree::isContainer(const path_& location, const std::string& name) const |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 44 | { |
Václav Kubernát | 814fa41 | 2018-05-25 19:47:18 +0200 | [diff] [blame^] | 45 | std::string locationString = pathToString(location); |
| 46 | if (!nodeExists(locationString, name)) |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 47 | return false; |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 48 | |
Václav Kubernát | 814fa41 | 2018-05-25 19:47:18 +0200 | [diff] [blame^] | 49 | return children(locationString).at(name).type() == typeid(schema::container); |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | void CTree::addContainer(const std::string& location, const std::string& name) |
| 53 | { |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 54 | m_nodes.at(location).emplace(name, schema::container{}); |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 55 | |
| 56 | //create a new set of children for the new node |
| 57 | std::string key = joinPaths(location, name); |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 58 | m_nodes.emplace(key, std::unordered_map<std::string, NodeType>()); |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | |
Václav Kubernát | 814fa41 | 2018-05-25 19:47:18 +0200 | [diff] [blame^] | 62 | bool CTree::listHasKey(const path_& location, const std::string& name, const std::string& key) const |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 63 | { |
Václav Kubernát | 814fa41 | 2018-05-25 19:47:18 +0200 | [diff] [blame^] | 64 | std::string locationString = pathToString(location); |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 65 | assert(isList(location, name)); |
| 66 | |
Václav Kubernát | 814fa41 | 2018-05-25 19:47:18 +0200 | [diff] [blame^] | 67 | const auto& child = children(locationString).at(name); |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 68 | const auto& list = boost::get<schema::list>(child); |
| 69 | return list.m_keys.find(key) != list.m_keys.end(); |
| 70 | } |
| 71 | |
Václav Kubernát | 814fa41 | 2018-05-25 19:47:18 +0200 | [diff] [blame^] | 72 | const std::set<std::string>& CTree::listKeys(const path_& location, const std::string& name) const |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 73 | { |
Václav Kubernát | 814fa41 | 2018-05-25 19:47:18 +0200 | [diff] [blame^] | 74 | std::string locationString = pathToString(location); |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 75 | assert(isList(location, name)); |
| 76 | |
Václav Kubernát | 814fa41 | 2018-05-25 19:47:18 +0200 | [diff] [blame^] | 77 | const auto& child = children(locationString).at(name); |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 78 | const auto& list = boost::get<schema::list>(child); |
| 79 | return list.m_keys; |
| 80 | } |
| 81 | |
Václav Kubernát | 814fa41 | 2018-05-25 19:47:18 +0200 | [diff] [blame^] | 82 | bool CTree::isList(const path_& location, const std::string& name) const |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 83 | { |
Václav Kubernát | 814fa41 | 2018-05-25 19:47:18 +0200 | [diff] [blame^] | 84 | std::string locationString = pathToString(location); |
| 85 | if (!nodeExists(locationString, name)) |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 86 | return false; |
Václav Kubernát | 814fa41 | 2018-05-25 19:47:18 +0200 | [diff] [blame^] | 87 | const auto& child = children(locationString).at(name); |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 88 | if (child.type() != typeid(schema::list)) |
| 89 | return false; |
| 90 | |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | void CTree::addList(const std::string& location, const std::string& name, const std::set<std::string>& keys) |
| 95 | { |
| 96 | m_nodes.at(location).emplace(name, schema::list{keys}); |
| 97 | |
| 98 | m_nodes.emplace(name, std::unordered_map<std::string, NodeType>()); |
| 99 | } |
| 100 | |