blob: 8b42371b7cf77569ef6c71e1484b68a1204c3e22 [file] [log] [blame]
Václav Kubernát624a8872018-03-02 17:28:47 +01001/*
2 * Copyright (C) 2018 CESNET, https://photonics.cesnet.cz/
Václav Kubernátd6662962018-03-22 17:41:33 +01003 * Copyright (C) 2018 FIT CVUT, https://fit.cvut.cz/
Václav Kubernát624a8872018-03-02 17:28:47 +01004 *
5 * Written by Václav Kubernát <kubervac@fit.cvut.cz>
6 *
7*/
8
9#pragma once
10
Václav Kubernátb96eef72018-05-04 19:10:22 +020011#include <boost/variant.hpp>
12#include <set>
Václav Kubernát624a8872018-03-02 17:28:47 +010013#include <stdexcept>
14#include <unordered_map>
Václav Kubernátff2c9f62018-05-16 20:26:31 +020015#include "ast.hpp"
Václav Kubernátd6662962018-03-22 17:41:33 +010016
Václav Kubernátb96eef72018-05-04 19:10:22 +020017namespace schema {
18 struct container {
19 };
20 struct list {
21 std::set<std::string> m_keys;
22 };
23}
Václav Kubernátd6662962018-03-22 17:41:33 +010024
Václav Kubernát814fa412018-05-25 19:47:18 +020025struct 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átb96eef72018-05-04 19:10:22 +020036
37using NodeType = boost::variant<schema::container, schema::list>;
38
Václav Kubernát624a8872018-03-02 17:28:47 +010039
40class InvalidNodeException : public std::invalid_argument {
41public:
42 using std::invalid_argument::invalid_argument;
43 ~InvalidNodeException() override;
Václav Kubernát624a8872018-03-02 17:28:47 +010044};
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 * */
52class CTree {
53public:
Václav Kubernátd6662962018-03-22 17:41:33 +010054 CTree();
Václav Kubernátb96eef72018-05-04 19:10:22 +020055 bool nodeExists(const std::string& location, const std::string& name) const;
Václav Kubernátd6662962018-03-22 17:41:33 +010056
Václav Kubernát814fa412018-05-25 19:47:18 +020057 bool isContainer(const path_& location, const std::string& name) const;
Václav Kubernátb96eef72018-05-04 19:10:22 +020058 void addContainer(const std::string& location, const std::string& name);
Václav Kubernát814fa412018-05-25 19:47:18 +020059 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átb96eef72018-05-04 19:10:22 +020062 void addList(const std::string& location, const std::string& name, const std::set<std::string>& keys);
Václav Kubernát624a8872018-03-02 17:28:47 +010063
64private:
Václav Kubernátb96eef72018-05-04 19:10:22 +020065 const std::unordered_map<std::string, NodeType>& children(const std::string& name) const;
Václav Kubernát624a8872018-03-02 17:28:47 +010066
Václav Kubernátb96eef72018-05-04 19:10:22 +020067 std::unordered_map<std::string, std::unordered_map<std::string, NodeType>> m_nodes;
Václav Kubernát624a8872018-03-02 17:28:47 +010068};