Skip to content

testing usage of linkedlists for cbor decoder singleton #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions src/cbor/MessageDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,21 @@ MessageDecoder::Status CBORMessageDecoderSingleton::decode(Message* msg, const u
return MessageDecoder::Status::Error;
}

auto decoder_it = decoders.begin();
struct llnode* decoder_it = decoders;

for(; decoder_it != decoders.end(); decoder_it++) {
if(decoder_it->first == tag) {
for(; decoder_it != nullptr; decoder_it = decoder_it->next) {
if(decoder_it->tag == tag) {
break;
}
}

// check if message.id exists on the decoders list or return error
if(decoder_it == decoders.end()) {
if(decoder_it == nullptr) {
return MessageDecoder::Status::Error;
}

// encode the message
if(decoder_it->second->_decode(&iter, msg) == MessageDecoder::Status::Error) {
if(decoder_it->decoder->_decode(&iter, msg) == MessageDecoder::Status::Error) {
return MessageDecoder::Status::Error;
}

Expand All @@ -59,17 +59,27 @@ CBORMessageDecoderSingleton& CBORMessageDecoderSingleton::getInstance() {
}

void CBORMessageDecoderSingleton::append(CBORTag tag, CBORMessageDecoderInterface* decoder) {
auto decoder_it = decoders.begin();
struct llnode* decoder_it = decoders;

for(; decoder_it != decoders.end(); decoder_it++) {
if(decoder_it->first == tag) {
for(; decoder_it != nullptr; decoder_it = decoder_it->next) {
if(decoder_it->tag == tag) {
return;
}
}

decoders.push_back(
std::make_pair(tag, decoder)
);
struct llnode* next = new llnode;

next->tag = tag;
next->decoder = decoder;

// this is the first element
if(decoders == nullptr) {
decoders = next;
} else {
decoders_last->next = next;
}

decoders_last = next;
}

CBORMessageDecoderInterface::CBORMessageDecoderInterface(const CBORTag tag, const MessageId id)
Expand Down
8 changes: 6 additions & 2 deletions src/cbor/MessageDecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,13 @@ class CBORMessageDecoderSingleton: public MessageDecoder {
*/
MessageDecoder::Status decode(Message* msg, const uint8_t* const buf, size_t &len);
private:
CBORMessageDecoderSingleton() {}
CBORMessageDecoderSingleton(): decoders(nullptr), decoders_last(nullptr) { }

std::vector<std::pair<CBORTag, CBORMessageDecoderInterface*>> decoders;
struct llnode {
struct llnode* next;
CBORTag tag;
CBORMessageDecoderInterface* decoder;
} *decoders, *decoders_last;
};

/**
Expand Down
Loading