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_Item& 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_items.clear();
43}
44
45std::vector<pb_Item>& Setup::configs() {
46 return m_items;
47}
48
49void Setup::extract(Entity* entity, ExtractSettings config) {
50 ExtractVisitor collector(*this, config);
51 collector.visit(entity, config.recursive);
52}
53
54void Setup::set(const pb_Module &setup) {
55 reset();
56 extend(setup.items, setup.items + setup.items_count);
57}
58
59void Setup::get(pb_Module &setup) const {
60 setup.items_count = m_items.size();
61 setup.items = new pb_Item[setup.items_count];
62 std::copy(m_items.begin(), m_items.end(), setup.items);
63}
64
65
66void Setup::extend(const pb_Item* begin, const pb_Item* end) {
67 m_items.reserve(m_items.size() + std::distance(begin, end));
68 for (auto it = begin ; it != end ; ++it) {
69 m_items.push_back(*it);
70 }
71}
72
73UnitResult Setup::apply(Entity *entity, bool write_hardware) const {
74 for (auto& config : m_items) {
75 Path path;
76 path.push(config.entity.path);
77
78 auto resolved_entity = entity->resolve_child_entity(path);
79
80 if (resolved_entity == nullptr) continue;
81 if (!resolved_entity) {
82 return UnitResult::err("Could not resolve entity to apply config to");
83 }
84
85 //empty config
86 if (config.which_kind == 0)
87 continue;
88
89 if (!TRY(resolved_entity->config(config))) {
90 LOG4("unhandled config in ", resolved_entity->get_entity_id().c_str(), " with config id ", config.which_kind);
91 //config is not beeing applied to anything
92 //TODO: has to be handled properly
93 }
94 }
95
96 if (!write_hardware) return UnitResult::ok();
97 return entity->write_to_hardware();
98}
99
100pb_Item& Setup::add_config(){
101 return m_items.emplace_back();
102}
103
104}
UnitResult status
Definition daq.h:21
void setup()