Skip to content

Commit

Permalink
fix: TopicNode incomplete type
Browse files Browse the repository at this point in the history
  • Loading branch information
guglielmo-boi committed Apr 3, 2024
1 parent ed11cf1 commit 66341d5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
32 changes: 16 additions & 16 deletions src/templates/message_parser.cpp.j2
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ void MessageParser::TopicNode::addNode(const std::string& topic) {
auto iter = this->adjacent.find(subTopic);

if(iter == this->adjacent.end()) {
iter = this->adjacent.emplace(subTopic, TopicNode()).first;
iter = this->adjacent.emplace(subTopic, std::make_unique<TopicNode>(TopicNode())).first;
}

if(slash != topic.end()) {
iter->second.addNode(std::string(slash + 1, topic.end()));
iter->second->addNode(std::string(slash + 1, topic.end()));
}
}

MessageParser::TopicNode* MessageParser::TopicNode::findNode(const std::string& topic) {
std::unique_ptr<MessageParser::TopicNode>* MessageParser::TopicNode::findNode(const std::string& topic) {
auto slash = std::find(topic.begin(), topic.end(), '/');

std::string subTopic = std::string(topic.begin(), slash);
Expand All @@ -47,18 +47,18 @@ MessageParser::TopicNode* MessageParser::TopicNode::findNode(const std::string&
if(slash == topic.end()) {
return &(iter->second);
} else {
return iter->second.findNode(std::string(slash + 1, topic.end()));
return iter->second->findNode(std::string(slash + 1, topic.end()));
}
}

std::vector<MessageParser::TopicNode*> MessageParser::TopicNode::findNodesVariables(const std::string& topic, const Variables& variables) {
std::vector<TopicNode*> ret;
std::vector<std::unique_ptr<MessageParser::TopicNode>*> MessageParser::TopicNode::findNodesVariables(const std::string& topic, const Variables& variables) {
std::vector<std::unique_ptr<TopicNode>*> ret;
this->findNodesVariablesRec(topic, variables, false, ret);

return ret;
}

void MessageParser::TopicNode::findNodesVariablesRec(const std::string& topic, const Variables& variables, bool hashtag, std::vector<TopicNode*>& ret) {
void MessageParser::TopicNode::findNodesVariablesRec(const std::string& topic, const Variables& variables, bool hashtag, std::vector<std::unique_ptr<TopicNode>*>& ret) {
auto slash = std::find(topic.begin(), topic.end(), '/');
std::string subTopic = std::string(topic.begin(), slash);

Expand All @@ -69,7 +69,7 @@ void MessageParser::TopicNode::findNodesVariablesRec(const std::string& topic, c
if(hashtag) {
for(auto& next : this->adjacent) {
ret.push_back(&(next.second));
next.second.findNodesVariablesRec("", variables, hashtag, ret);
next.second->findNodesVariablesRec("", variables, hashtag, ret);
}
} else {
for(auto& next : this->adjacent) {
Expand All @@ -87,7 +87,7 @@ void MessageParser::TopicNode::findNodesVariablesRec(const std::string& topic, c
if(slash == topic.end()) {
ret.push_back(&(next.second));
} else {
next.second.findNodesVariablesRec(std::string(slash + 1, topic.end()), variables, hashtag, ret);
next.second->findNodesVariablesRec(std::string(slash + 1, topic.end()), variables, hashtag, ret);
}
}
}
Expand All @@ -96,30 +96,30 @@ void MessageParser::TopicNode::findNodesVariablesRec(const std::string& topic, c

MessageParser::MessageParser() {
{% for topic in topics -%}
this->tree.addNode("{{ topic['topic'] }}");
this->tree->addNode("{{ topic['topic'] }}");
{%- if not loop.last%}
{%endif -%}
{%- endfor %}
}

void MessageParser::setMessageParse(Topic topic, parse_t parse, void* argument) {
auto node = this->tree.findNode(GetTopic(topic).topic);
auto node = this->tree->findNode(GetTopic(topic).topic);

if(node != nullptr) {
node->parse = std::make_unique<parse_t>(parse);
(*node)->parse = std::make_unique<parse_t>(parse);

if(argument != nullptr) {
node->argument = argument;
(*node)->argument = argument;
}
}
}

void MessageParser::parseMessage(const Variables& variables, const std::string& topic, const std::string& payload) {
auto nodes = this->tree.findNodesVariables(topic, variables);
auto nodes = this->tree->findNodesVariables(topic, variables);

for(auto& node : nodes) {
if(node->parse != nullptr) {
(*node->parse)(payload, node->argument);
if((*node)->parse != nullptr) {
(*(*node)->parse)(payload, (*node)->argument);
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/templates/message_parser.h.j2
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ private:
{
std::unique_ptr<parse_t> parse;
void* argument = nullptr;
std::unordered_map<std::string, TopicNode> adjacent;
std::unordered_map<std::string, std::unique_ptr<TopicNode>> adjacent;

void addNode(const std::string& topic);
TopicNode* findNode(const std::string& topic);
std::vector<TopicNode*> findNodesVariables(const std::string& topic, const Variables& variables);
void findNodesVariablesRec(const std::string& topic, const Variables& variables, bool hashtag, std::vector<TopicNode*>& ret);
std::unique_ptr<TopicNode>* findNode(const std::string& topic);
std::vector<std::unique_ptr<TopicNode>*> findNodesVariables(const std::string& topic, const Variables& variables);
void findNodesVariablesRec(const std::string& topic, const Variables& variables, bool hashtag, std::vector<std::unique_ptr<TopicNode>*>& ret);
};

public:
Expand All @@ -50,7 +50,7 @@ public:
void parseMessage(const Variables& variables, const std::string& topic, const std::string& payload);

private:
MessageParser::TopicNode tree;
std::unique_ptr<TopicNode> tree;
};
}

Expand Down

0 comments on commit 66341d5

Please sign in to comment.