1#pragma once
2
3#include <iostream>
4#include <string>
5#include <toml++/toml.hpp>
6#include <vector>
7
8enum class UnitStatus
9{
10 STOPPED = 0,
11 STARTING = 1,
12 RUNNING = 2,
13 STOPPING = 3,
14 EXITED = 4,
15 FAILED = 5,
16};
17
18struct Unit
19{
20 explicit Unit(const std::string &id) : id(id) {};
21 virtual ~Unit() = default;
22
23 const std::string id;
24
25 std::string type;
26 std::string description;
27 std::vector<std::string> depends_on;
28 std::vector<std::string> part_of;
29
30 public:
31 UnitStatus status() const
32 {
33 return m_status;
34 }
35
36 std::string error_reason() const
37 {
38 return m_error;
39 }
40
41 public:
42 bool start();
43 void stop();
44
45 protected:
46 virtual bool do_start() = 0;
47 virtual bool do_stop() = 0;
48 virtual bool do_load(const toml::table &) = 0;
49 virtual void do_print(std::ostream &) const {};
50
51 static constexpr const auto array_append_visitor = [](std::vector<std::string> &dest_container)
52 {
53 return [&](const toml::array &array) -> void
54 {
55 for (const auto &dep : array)
56 dest_container.push_back(x: dep.as_string()->get());
57 };
58 };
59
60 public:
61 void load(const toml::table &data);
62
63 friend std::ostream &operator<<(std::ostream &, const Unit &);
64
65 protected:
66 std::string m_error; ///< error message if failed to start
67
68 private:
69 UnitStatus m_status = UnitStatus::STOPPED;
70};
71
72std::ostream &operator<<(std::ostream &os, const Unit &unit);
73