Skip to content

Instantly share code, notes, and snippets.

@adventurist
Created July 7, 2020 18:54
Show Gist options
  • Save adventurist/9168014e49fbf1617d3061583feac5b3 to your computer and use it in GitHub Desktop.
Save adventurist/9168014e49fbf1617d3061583feac5b3 to your computer and use it in GitHub Desktop.
Decoder shared_ptr (cpp)
#include <memory>
#include <vector>
class Decoder {
public:
void init(std::shared_ptr<std::vector<unsigned char>> s_buffer) {
buffer = s_buffer;
}
void decode(unsigned char* data, uint32_t size) {
auto b = buffer.get();
if (size == 10) {
b->insert(b->end(), data, data + size);
} else {
b->insert(b->end(), data, data + size);
}
}
private:
std::shared_ptr<std::vector<unsigned char>> buffer = nullptr;
};
unsigned char big_data[10]{5};
unsigned char small_data[5]{4};
class Link {
public:
Link(Decoder decoder_) : decoder(decoder_) {
buffer->reserve(4990);
decoder.init(buffer);
}
void process(bool big) {
if (big) {
decoder.decode(big_data, 10);
} else {
decoder.decode(small_data, 4);
}
}
private:
Decoder decoder;
std::shared_ptr<std::vector<unsigned char>> buffer;
};
int main() {
Decoder decoder{};
Link link{decoder};
link.process(false);
link.process(false);
link.process(false);
link.process(false);
link.process(false);
link.process(true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment