Nix (Dev) 3.5.10
dev - 3.5.10 - 1af9301
Loading...
Searching...
No Matches
utils.hpp
Go to the documentation of this file.
1#ifndef IV_SRC_UNITS_BUILDERS_UTILS_HPP_
2#define IV_SRC_UNITS_BUILDERS_UTILS_HPP_
3
6
7#include <optional>
8#include <regex>
9#include <utility>
10
11namespace iv::units::builders
12{
13
14namespace pmm
15{
16
18{
19 PmmAndChannelNames(std::string pmmName, std::string chName)
20 {
21 this->pmmName = std::move(pmmName);
22 this->chName = std::move(chName);
23 }
24 std::string pmmName;
25 std::string chName;
26};
27
29{
30 std::regex pattern(R"(^.{1,3}_.+$)");
31 return std::regex_match(chId, pattern);
32}
33
34inline std::optional<PmmAndChannelNames> getPmmAndChannelNames(const iv::types::channelId &channelId)
35{
36 if (not hasPmmChIdStructure(channelId))
37 {
38 return std::nullopt;
39 }
40
41 auto separatorPos = channelId.find('_');
42 std::string pmmName = channelId.substr(0, separatorPos);
43 std::string chName = channelId.substr(separatorPos + 1, channelId.size());
44 return {PmmAndChannelNames(pmmName, chName)};
45}
46
53inline std::optional<std::string> getPmmName(const iv::units::Unit &pmmUnit)
54{
56 {
57 return std::nullopt;
58 }
59
60 std::string lastOccurrence;
61 for (const auto &channelPair: pmmUnit.getChannels())
62 {
63 const auto pmmAndChannelNames = getPmmAndChannelNames(channelPair.first);
64 if (pmmAndChannelNames.has_value())
65 {
66 if (lastOccurrence != pmmAndChannelNames->pmmName)
67 {
68 lastOccurrence = pmmAndChannelNames->pmmName;
69 continue;
70 }
71
72 return pmmAndChannelNames->pmmName;
73 }
74 }
75
76 return std::nullopt;
77}
78
79inline std::pair<bool, std::shared_ptr<iv::channels::AbstractChannel>>
80hasPmmChannelByChName(const iv::units::Unit &pmmUnit, const std::string &channelName)
81{
82 const auto pmmChannels = pmmUnit.getChannels();
83 auto it = std::find_if(
84 pmmChannels.begin(), pmmChannels.end(),
85 [channelName](
86 const std::pair<iv::types::channelId, std::shared_ptr<iv::channels::AbstractChannel>> &channelPair)
87 {
88 const auto pmmAndChannelNames = getPmmAndChannelNames(channelPair.first);
89 if (pmmAndChannelNames.has_value() and pmmAndChannelNames->chName == channelName)
90 {
91 return true;
92 }
93
94 return false;
95 });
96
97 if (it == pmmChannels.end())
98 {
99 return {false, 0};
100 }
101
102 return {true, it->second};
103}
104
105}// namespace pmm
106
107}// namespace iv::units::builders
108
109#endif//IV_SRC_UNITS_BUILDERS_UTILS_HPP_
Definition Unit.hpp:14
const std::unordered_map< iv::types::channelId, std::shared_ptr< iv::channels::AbstractChannel > > & getChannels() const
Definition Unit.cpp:688
std::optional< iv::units::eUnitType > m_unitType
Definition Unit.hpp:109
std::string channelId
Definition types.hpp:66
bool hasPmmChIdStructure(iv::types::channelId chId)
Definition utils.hpp:28
std::optional< std::string > getPmmName(const iv::units::Unit &pmmUnit)
Get the name of the PMM from a unit.
Definition utils.hpp:53
std::optional< PmmAndChannelNames > getPmmAndChannelNames(const iv::types::channelId &channelId)
Definition utils.hpp:34
std::pair< bool, std::shared_ptr< iv::channels::AbstractChannel > > hasPmmChannelByChName(const iv::units::Unit &pmmUnit, const std::string &channelName)
Definition utils.hpp:80
Definition constants.hpp:7
std::string chName
Definition utils.hpp:25
std::string pmmName
Definition utils.hpp:24
PmmAndChannelNames(std::string pmmName, std::string chName)
Definition utils.hpp:19