REDAC HybridController
Firmware for LUCIDAC/REDAC Teensy
Loading...
Searching...
No Matches
visitor.cpp
Go to the documentation of this file.
1
2#include "entity/visitor.h"
3
4#include "block/mblock_mul.h"
5#include "mode/mode.h"
6#include "proto/main.pb.h"
7
8namespace entities {
9
10
11pb_OverloadStatus_Element& OverloadVisitor::create(){
12 auto& elem = m_elements.emplace_back();
13 copy_entity(elem.entity);
14 return elem;
15}
16
17void OverloadVisitor::to(pb_OverloadStatus& status){
18 status.global_overload = mode::is_global_overload_active();
19 status.elements_count = m_elements.size();
20 status.elements = new pb_OverloadStatus_Element[status.elements_count];
21 std::copy(m_elements.begin(), m_elements.end(), status.elements);
22}
23
24void OverloadVisitor::handle_visit(Entity* entity) {
25 if (entity->is_entity_class(blocks::MMulBlock::CLASS_)) {
26 auto *mblock = static_cast<blocks::MBlock *>(entity);
27 mblock->collect_overloads(*this);
28 }
29}
30
31class Setup;
32
33pb_Config& ExtractVisitor::create(int config_type) const{
34 auto& config = m_setup.add_config();
35 config.which_kind = config_type;
36 config.has_entity = true;
37 copy_entity(config.entity);
38 return config;
39}
40
41void Setup::reset() {
42 m_configs.clear();
43}
44
45std::vector<pb_Config>& Setup::configs() {
46 return m_configs;
47}
48
49void Setup::extract(Entity* entity, bool recursive) {
50 ExtractVisitor collector(*this);
51 collector.visit(entity, recursive);
52}
53
54void Setup::set(const pb_ConfigBundle &setup) {
55 reset();
56 extend(setup.configs, setup.configs + setup.configs_count);
57}
58
59void Setup::get(pb_ConfigBundle &setup) const {
60 setup.configs_count = m_configs.size();
61 setup.configs = new pb_Config[setup.configs_count];
62 std::copy(m_configs.begin(), m_configs.end(), setup.configs);
63}
64
65
66void Setup::extend(const pb_Config* begin, const pb_Config* end) {
67 m_configs.reserve(m_configs.size() + std::distance(begin, end));
68 for (auto it = begin ; it != end ; ++it) {
69 m_configs.push_back(*it);
70 }
71}
72
73utils::status Setup::apply(Entity* entity, bool write_hardware) const{
74 for (auto& config : m_configs) {
75 Path path;
76 path.push(config.entity.path);
77
78 auto resolved_entity = entity->resolve_child_entity(path);
79
80 if (!resolved_entity) {
81 return utils::status(4, "Could not resolve entity to apply config to");
82 }
83
84 //empty config
85 if (config.which_kind == 0)
86 continue;
87
88 auto apply_res = resolved_entity->config(config);
89 if (apply_res.is_err())
90 return utils::status{apply_res.err_value()};
91
92 if (!apply_res.ok_value()) {
93 LOG4("unhandled config in ", resolved_entity->get_entity_id().c_str(), " with config id ", config.which_kind);
94 //config is not beeing applied to anything
95 //TODO: has to be handled properly
96 }
97 }
98
99 if (!write_hardware) return {};
100 return entity->write_to_hardware();
101}
102
103pb_Config& Setup::add_config(){
104 return m_configs.emplace_back();
105}
106
107}
utils::status status
Definition daq.h:21
void setup()