1// SPDX-License-Identifier: GPL-3.0-or-later
2#include "unit/unit.hpp"
3
4bool Unit::start()
5{
6 if (m_status == UnitStatus::STOPPED || m_status == UnitStatus::FAILED)
7 {
8 m_status = UnitStatus::STARTING;
9 if (do_start())
10 m_status = UnitStatus::RUNNING;
11 else
12 m_status = UnitStatus::FAILED;
13 }
14
15 return m_status == UnitStatus::RUNNING;
16}
17
18void Unit::stop()
19{
20 if (m_status == UnitStatus::RUNNING || m_status == UnitStatus::FAILED)
21 {
22 m_status = UnitStatus::STOPPING;
23 if (do_stop())
24 m_status = UnitStatus::STOPPED;
25 else
26 m_status = UnitStatus::FAILED;
27 }
28}
29
30std::ostream &operator<<(std::ostream &os, const Unit &unit)
31{
32 os << unit.description << " (" << unit.id << ")" << std::endl;
33 os << " depends_on: ";
34 for (const auto &dep : unit.depends_on)
35 os << dep << " ";
36 if (unit.depends_on.empty())
37 os << "(none)";
38 os << std::endl;
39 os << " part_of: ";
40 for (const auto &part : unit.part_of)
41 os << part << " ";
42 if (unit.part_of.empty())
43 os << "(none)";
44 os << std::endl;
45 unit.do_print(os);
46 return os;
47}
48void Unit::load(const toml::table &data)
49{
50 this->type = data["type"].value_or(default_value: "unknown");
51 this->description = data["description"].value_or(default_value: "unknown");
52 data["depends_on"].visit(visitor: array_append_visitor(this->depends_on));
53 data["part_of"].visit(visitor: array_append_visitor(this->part_of));
54 do_load(data);
55}
56