diff options
chore(frame): Implement rule of 4 and move implementations from header to source
1 files changed, 55 insertions, 1 deletions
| -rw-r--r-- | src/frame.cpp | 56 |
diff --git a/src/frame.cpp b/src/frame.cpp index 05ec58d..babfef7 100644 --- a/src/frame.cpp +++ b/src/frame.cpp @@ -1,3 +1,4 @@ +#include <cstring> #include "frame.h" unsigned char hex_byte_to_byte(unsigned char byte) { @@ -20,12 +21,14 @@ unsigned char next_hex_byte(const unsigned char*& iso_tp_frame) { return byte; } +/* Private */ + // b0 b1 b2 b3 b4 b5 b6 b7 // 02 10 C0 00 00 00 00 00 void Frame::parse_frame_str(const unsigned char* iso_tp_frame) { unsigned char byte0 = next_hex_byte(iso_tp_frame); - this->type = (FrameType)((byte0 & 0b11110000) >> 4); + this->type =(FrameType)((byte0 & 0b11110000) >> 4); this->size = byte0 & 0b00001111; switch (this->type) { @@ -57,6 +60,57 @@ void Frame::parse_frame_str(const unsigned char* iso_tp_frame) { } } +void Frame::free() { + delete[] this->data; +} + +void Frame::copyFrom(const Frame& other) { + this->type = other.type; + this->size = other.size; + this->data_size = other.data_size; + + this->data = new unsigned char[this->data_size]; + std::memcpy(this->data, other.data, this->data_size); +} + +/* Public */ + +Frame::Frame(const char* iso_tp_frame) { + this->parse_frame_str((const unsigned char*)iso_tp_frame); +} + +Frame::~Frame() { + free(); +} + +Frame::Frame(const Frame& other) { + copyFrom(other); +} + +Frame& Frame::operator=(const Frame& other) { + if (this != &other) { + free(); + copyFrom(other); + } + return *this; +} + +FrameType Frame::get_type() const { + return this->type; +} + +unsigned int Frame::get_size() const { + return this->size; +} + +unsigned int Frame::get_data_size() const { + return this->data_size; +} + +const unsigned char* Frame::get_data() const { + return this->data; +} + std::ostream& operator<<(std::ostream& os, const Frame& f) { for (int i = 0; i < f.data_size; ++i) { os << (unsigned)f.data[i]; |
