blob: 6840697b8b58d57f7755fc95521214c126b54ec9 [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
Václav Kubernátb96eef72018-05-04 19:10:22 +02009#include <iostream>
Václav Kubernát624a8872018-03-02 17:28:47 +010010#include "CTree.hpp"
Václav Kubernát94938b72018-05-04 15:12:24 +020011#include "utils.hpp"
Václav Kubernát624a8872018-03-02 17:28:47 +010012
13InvalidNodeException::~InvalidNodeException() = default;
14
Václav Kubernát624a8872018-03-02 17:28:47 +010015
Václav Kubernát814fa412018-05-25 19:47:18 +020016std::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átd6662962018-03-22 17:41:33 +010023
24CTree::CTree()
25{
Václav Kubernátb96eef72018-05-04 19:10:22 +020026 m_nodes.emplace("", std::unordered_map<std::string, NodeType>());
Václav Kubernátd6662962018-03-22 17:41:33 +010027}
28
Václav Kubernátb96eef72018-05-04 19:10:22 +020029const std::unordered_map<std::string, NodeType>& CTree::children(const std::string& name) const
Václav Kubernát624a8872018-03-02 17:28:47 +010030{
Václav Kubernátb96eef72018-05-04 19:10:22 +020031 return m_nodes.at(name);
Václav Kubernát624a8872018-03-02 17:28:47 +010032}
33
Václav Kubernátb96eef72018-05-04 19:10:22 +020034bool CTree::nodeExists(const std::string& location, const std::string& name) const
Václav Kubernát624a8872018-03-02 17:28:47 +010035{
Václav Kubernátb96eef72018-05-04 19:10:22 +020036 if (name.empty())
Václav Kubernát624a8872018-03-02 17:28:47 +010037 return true;
Václav Kubernátd6662962018-03-22 17:41:33 +010038 const auto& childrenRef = children(location);
39
Václav Kubernátb96eef72018-05-04 19:10:22 +020040 return childrenRef.find(name) != childrenRef.end();
Václav Kubernát624a8872018-03-02 17:28:47 +010041}
Václav Kubernátd6662962018-03-22 17:41:33 +010042
Václav Kubernát814fa412018-05-25 19:47:18 +020043bool CTree::isContainer(const path_& location, const std::string& name) const
Václav Kubernátd6662962018-03-22 17:41:33 +010044{
Václav Kubernát814fa412018-05-25 19:47:18 +020045 std::string locationString = pathToString(location);
46 if (!nodeExists(locationString, name))
Václav Kubernátd6662962018-03-22 17:41:33 +010047 return false;
Václav Kubernátb96eef72018-05-04 19:10:22 +020048
Václav Kubernát814fa412018-05-25 19:47:18 +020049 return children(locationString).at(name).type() == typeid(schema::container);
Václav Kubernátd6662962018-03-22 17:41:33 +010050}
51
52void CTree::addContainer(const std::string& location, const std::string& name)
53{
Václav Kubernátb96eef72018-05-04 19:10:22 +020054 m_nodes.at(location).emplace(name, schema::container{});
Václav Kubernátd6662962018-03-22 17:41:33 +010055
56 //create a new set of children for the new node
57 std::string key = joinPaths(location, name);
Václav Kubernátb96eef72018-05-04 19:10:22 +020058 m_nodes.emplace(key, std::unordered_map<std::string, NodeType>());
Václav Kubernátd6662962018-03-22 17:41:33 +010059}
60
61
Václav Kubernát814fa412018-05-25 19:47:18 +020062bool CTree::listHasKey(const path_& location, const std::string& name, const std::string& key) const
Václav Kubernát624a8872018-03-02 17:28:47 +010063{
Václav Kubernát814fa412018-05-25 19:47:18 +020064 std::string locationString = pathToString(location);
Václav Kubernátb96eef72018-05-04 19:10:22 +020065 assert(isList(location, name));
66
Václav Kubernát814fa412018-05-25 19:47:18 +020067 const auto& child = children(locationString).at(name);
Václav Kubernátb96eef72018-05-04 19:10:22 +020068 const auto& list = boost::get<schema::list>(child);
69 return list.m_keys.find(key) != list.m_keys.end();
70}
71
Václav Kubernát814fa412018-05-25 19:47:18 +020072const std::set<std::string>& CTree::listKeys(const path_& location, const std::string& name) const
Václav Kubernátb96eef72018-05-04 19:10:22 +020073{
Václav Kubernát814fa412018-05-25 19:47:18 +020074 std::string locationString = pathToString(location);
Václav Kubernátb96eef72018-05-04 19:10:22 +020075 assert(isList(location, name));
76
Václav Kubernát814fa412018-05-25 19:47:18 +020077 const auto& child = children(locationString).at(name);
Václav Kubernátb96eef72018-05-04 19:10:22 +020078 const auto& list = boost::get<schema::list>(child);
79 return list.m_keys;
80}
81
Václav Kubernát814fa412018-05-25 19:47:18 +020082bool CTree::isList(const path_& location, const std::string& name) const
Václav Kubernátb96eef72018-05-04 19:10:22 +020083{
Václav Kubernát814fa412018-05-25 19:47:18 +020084 std::string locationString = pathToString(location);
85 if (!nodeExists(locationString, name))
Václav Kubernátb96eef72018-05-04 19:10:22 +020086 return false;
Václav Kubernát814fa412018-05-25 19:47:18 +020087 const auto& child = children(locationString).at(name);
Václav Kubernátb96eef72018-05-04 19:10:22 +020088 if (child.type() != typeid(schema::list))
89 return false;
90
91 return true;
92}
93
94void 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