UniSet @VERSION@
DependencyResolver.h
1/*
2 * Copyright (c) 2026 Pavel Vainerman.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as
6 * published by the Free Software Foundation, version 2.1.
7 */
8// -------------------------------------------------------------------------
9#ifndef DependencyResolver_H_
10#define DependencyResolver_H_
11// -------------------------------------------------------------------------
12#include <string>
13#include <vector>
14#include <set>
15#include <map>
16#include <stdexcept>
17// -------------------------------------------------------------------------
18namespace uniset
19{
20 class CyclicDependencyException : public std::runtime_error
21 {
22 public:
23 explicit CyclicDependencyException(const std::string& msg)
24 : std::runtime_error(msg) {}
25 };
26
27 class UnknownDependencyException : public std::runtime_error
28 {
29 public:
30 explicit UnknownDependencyException(const std::string& msg)
31 : std::runtime_error(msg) {}
32 };
33
39 {
40 public:
41 DependencyResolver() = default;
42
44 void addGroup(const std::string& name, const std::set<std::string>& depends = {});
45
47 void addDependency(const std::string& group, const std::string& dependsOn);
48
50 bool hasGroup(const std::string& name) const;
51
53 void clear();
54
61 std::vector<std::string> resolve() const;
62
67 std::vector<std::string> resolveReverse() const;
68
70 std::set<std::string> getDependencies(const std::string& name) const;
71
72 private:
73 struct Node
74 {
75 std::string name;
76 std::set<std::string> depends;
77 };
78
79 enum class VisitState { White, Gray, Black };
80
81 void dfs(const std::string& name,
82 std::map<std::string, VisitState>& visited,
83 std::vector<std::string>& result) const;
84
85 std::map<std::string, Node> nodes_;
86 };
87
88} // end of namespace uniset
89// -------------------------------------------------------------------------
90#endif // DependencyResolver_H_
91// -------------------------------------------------------------------------
Definition DependencyResolver.h:21
Definition DependencyResolver.h:39
std::vector< std::string > resolve() const
Definition DependencyResolver.cc:48
void addGroup(const std::string &name, const std::set< std::string > &depends={})
Definition DependencyResolver.cc:15
bool hasGroup(const std::string &name) const
Definition DependencyResolver.cc:28
void clear()
Definition DependencyResolver.cc:33
std::set< std::string > getDependencies(const std::string &name) const
Definition DependencyResolver.cc:38
void addDependency(const std::string &group, const std::string &dependsOn)
Definition DependencyResolver.cc:20
std::vector< std::string > resolveReverse() const
Definition DependencyResolver.cc:79
Definition DependencyResolver.h:28
Definition AccessConfig.h:30