diff options
chore(frame): Implement rule of 4 and move implementations from header to source
2 files changed, 70 insertions, 22 deletions
| -rw-r--r-- | src/frame.cpp | 56 | ||||
| -rw-r--r-- | src/frame.h | 36 |
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]; diff --git a/src/frame.h b/src/frame.h index 92b9c8f..989ee38 100644 --- a/src/frame.h +++ b/src/frame.h @@ -15,31 +15,25 @@ class Frame { void parse_frame_str(const unsigned char* iso_tp_frame); -public: - Frame(const char* iso_tp_frame) { - this->parse_frame_str((const unsigned char*)iso_tp_frame); - } - - ~Frame() { - delete[] this->data; - } + void free(); + void copyFrom(const Frame& other); - FrameType get_type() const { - return this->type; - } - - unsigned int get_size() const { - return this->size; - } +public: + Frame(const char* iso_tp_frame); - unsigned int get_data_size() const { - return this->data_size; - } + /* Rule of 4 */ + Frame() = default; + ~Frame(); + Frame(const Frame& other); + Frame& operator=(const Frame& other); - const unsigned char* get_data() const { - return this->data; - } + /* Getters */ + FrameType get_type() const; + unsigned int get_size() const; + unsigned int get_data_size() const; + const unsigned char* get_data() const; + /* IO */ friend std::ostream& operator<<(std::ostream& os, const Frame& f); }; |
