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 | |
| 9 | #pragma once |
| 10 | |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 11 | #include <boost/variant.hpp> |
| 12 | #include <set> |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 13 | #include <stdexcept> |
| 14 | #include <unordered_map> |
Václav Kubernát | ff2c9f6 | 2018-05-16 20:26:31 +0200 | [diff] [blame] | 15 | #include "ast.hpp" |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 16 | |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 17 | namespace schema { |
| 18 | struct container { |
| 19 | }; |
| 20 | struct list { |
| 21 | std::set<std::string> m_keys; |
| 22 | }; |
| 23 | } |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 24 | |
Václav Kubernát | 814fa41 | 2018-05-25 19:47:18 +0200 | [diff] [blame^] | 25 | struct nodeToString : public boost::static_visitor<std::string> { |
| 26 | std::string operator()(const nodeup_&) const |
| 27 | { |
| 28 | return ".."; |
| 29 | } |
| 30 | template <class T> |
| 31 | std::string operator()(const T& node) const |
| 32 | { |
| 33 | return node.m_name; |
| 34 | } |
| 35 | }; |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 36 | |
| 37 | using NodeType = boost::variant<schema::container, schema::list>; |
| 38 | |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 39 | |
| 40 | class InvalidNodeException : public std::invalid_argument { |
| 41 | public: |
| 42 | using std::invalid_argument::invalid_argument; |
| 43 | ~InvalidNodeException() override; |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | /*! \class CTree |
| 47 | * \brief The class representing the tree, that the user traverses. |
| 48 | * |
| 49 | * This class holds the current position in the tree and handles changing the position, |
| 50 | * including checking what nodes are available. |
| 51 | * */ |
| 52 | class CTree { |
| 53 | public: |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 54 | CTree(); |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 55 | bool nodeExists(const std::string& location, const std::string& name) const; |
Václav Kubernát | d666296 | 2018-03-22 17:41:33 +0100 | [diff] [blame] | 56 | |
Václav Kubernát | 814fa41 | 2018-05-25 19:47:18 +0200 | [diff] [blame^] | 57 | bool isContainer(const path_& location, const std::string& name) const; |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 58 | void addContainer(const std::string& location, const std::string& name); |
Václav Kubernát | 814fa41 | 2018-05-25 19:47:18 +0200 | [diff] [blame^] | 59 | const std::set<std::string>& listKeys(const path_& location, const std::string& name) const; |
| 60 | bool listHasKey(const path_& location, const std::string& name, const std::string& key) const; |
| 61 | bool isList(const path_& location, const std::string& name) const; |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 62 | void addList(const std::string& location, const std::string& name, const std::set<std::string>& keys); |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 63 | |
| 64 | private: |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 65 | const std::unordered_map<std::string, NodeType>& children(const std::string& name) const; |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 66 | |
Václav Kubernát | b96eef7 | 2018-05-04 19:10:22 +0200 | [diff] [blame] | 67 | std::unordered_map<std::string, std::unordered_map<std::string, NodeType>> m_nodes; |
Václav Kubernát | 624a887 | 2018-03-02 17:28:47 +0100 | [diff] [blame] | 68 | }; |