Created
July 7, 2020 18:54
-
-
Save adventurist/9168014e49fbf1617d3061583feac5b3 to your computer and use it in GitHub Desktop.
Decoder shared_ptr (cpp)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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