blob: 3e950664d143075f1d72b819cb9bc125a1566079 [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átd6662962018-03-22 17:41:33 +010015
Václav Kubernátb96eef72018-05-04 19:10:22 +020016namespace schema {
17 struct container {
18 };
19 struct list {
20 std::set<std::string> m_keys;
21 };
22}
Václav Kubernátd6662962018-03-22 17:41:33 +010023
Václav Kubernátb96eef72018-05-04 19:10:22 +020024
25using NodeType = boost::variant<schema::container, schema::list>;
26
Václav Kubernát624a8872018-03-02 17:28:47 +010027
28class InvalidNodeException : public std::invalid_argument {
29public:
30 using std::invalid_argument::invalid_argument;
31 ~InvalidNodeException() override;
Václav Kubernát624a8872018-03-02 17:28:47 +010032};
33
34/*! \class CTree
35 * \brief The class representing the tree, that the user traverses.
36 *
37 * This class holds the current position in the tree and handles changing the position,
38 * including checking what nodes are available.
39 * */
40class CTree {
41public:
Václav Kubernátd6662962018-03-22 17:41:33 +010042 CTree();
Václav Kubernátb96eef72018-05-04 19:10:22 +020043 bool nodeExists(const std::string& location, const std::string& name) const;
Václav Kubernátd6662962018-03-22 17:41:33 +010044
Václav Kubernátb96eef72018-05-04 19:10:22 +020045 bool isContainer(const std::string& location, const std::string& name) const;
46 void addContainer(const std::string& location, const std::string& name);
47 const std::set<std::string>& listKeys(const std::string& location, const std::string& name) const;
48 bool listHasKey(const std::string& location, const std::string& name, const std::string& key) const;
49 bool isList(const std::string& location, const std::string& name) const;
50 void addList(const std::string& location, const std::string& name, const std::set<std::string>& keys);
51 void changeNode(const std::string& name);
Václav Kubernát624a8872018-03-02 17:28:47 +010052 std::string currentNode() const;
53
54private:
Václav Kubernátb96eef72018-05-04 19:10:22 +020055 const std::unordered_map<std::string, NodeType>& children(const std::string& name) const;
Václav Kubernát624a8872018-03-02 17:28:47 +010056
Václav Kubernátb96eef72018-05-04 19:10:22 +020057 std::unordered_map<std::string, std::unordered_map<std::string, NodeType>> m_nodes;
Václav Kubernát624a8872018-03-02 17:28:47 +010058 std::string m_curDir;
59};