From 9b3a26806a8af4d4a5ea685f156ce08fbc19de21 Mon Sep 17 00:00:00 2001 From: jakcron Date: Sun, 24 Jun 2018 12:45:45 +0800 Subject: [PATCH] [fnd] Added Vec, changed List. Remove MemoryBlob, replaced with Vec. --- lib/libfnd/fnd.vcxproj | 5 +- lib/libfnd/fnd.vcxproj.filters | 21 +- lib/libfnd/include/fnd/ISerialisable.h | 21 ++ lib/libfnd/include/fnd/ISerialiseableBinary.h | 17 - lib/libfnd/include/fnd/List.h | 299 +++++++++++------- lib/libfnd/include/fnd/MemoryBlob.h | 42 --- lib/libfnd/include/fnd/Vec.h | 188 +++++++++++ lib/libfnd/source/MemoryBlob.cpp | 89 ------ 8 files changed, 404 insertions(+), 278 deletions(-) create mode 100644 lib/libfnd/include/fnd/ISerialisable.h delete mode 100644 lib/libfnd/include/fnd/ISerialiseableBinary.h delete mode 100644 lib/libfnd/include/fnd/MemoryBlob.h create mode 100644 lib/libfnd/include/fnd/Vec.h delete mode 100644 lib/libfnd/source/MemoryBlob.cpp diff --git a/lib/libfnd/fnd.vcxproj b/lib/libfnd/fnd.vcxproj index 90edb79..24cb65f 100644 --- a/lib/libfnd/fnd.vcxproj +++ b/lib/libfnd/fnd.vcxproj @@ -126,9 +126,9 @@ - + - + @@ -138,7 +138,6 @@ - diff --git a/lib/libfnd/fnd.vcxproj.filters b/lib/libfnd/fnd.vcxproj.filters index 36b0ec3..b335370 100644 --- a/lib/libfnd/fnd.vcxproj.filters +++ b/lib/libfnd/fnd.vcxproj.filters @@ -24,15 +24,6 @@ Header Files - - Header Files - - - Header Files - - - Header Files - Header Files @@ -57,6 +48,15 @@ Header Files + + Header Files + + + Header Files + + + Header Files + @@ -65,9 +65,6 @@ Source Files - - Source Files - Source Files diff --git a/lib/libfnd/include/fnd/ISerialisable.h b/lib/libfnd/include/fnd/ISerialisable.h new file mode 100644 index 0000000..a484ea0 --- /dev/null +++ b/lib/libfnd/include/fnd/ISerialisable.h @@ -0,0 +1,21 @@ +#pragma once +#include +#include + +namespace fnd +{ + class ISerialisable + { + public: + // serialise + virtual void toBytes() = 0; + // deserialise + virtual void fromBytes(const byte_t* data, size_t len) = 0; + + // get byte vector + virtual const fnd::Vec& getBytes() const = 0; + + // clear data + virtual void clear() = 0; + }; +} \ No newline at end of file diff --git a/lib/libfnd/include/fnd/ISerialiseableBinary.h b/lib/libfnd/include/fnd/ISerialiseableBinary.h deleted file mode 100644 index 5928c81..0000000 --- a/lib/libfnd/include/fnd/ISerialiseableBinary.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once -#include - -namespace fnd -{ - class ISerialiseableBinary - { - public: - virtual const byte_t* getBytes() const = 0; - virtual size_t getSize() const = 0; - - virtual void exportBinary() = 0; - virtual void importBinary(const byte_t* bytes, size_t len) = 0; - - virtual void clear() = 0; - }; -} \ No newline at end of file diff --git a/lib/libfnd/include/fnd/List.h b/lib/libfnd/include/fnd/List.h index b4dd398..c11aa34 100644 --- a/lib/libfnd/include/fnd/List.h +++ b/lib/libfnd/include/fnd/List.h @@ -1,6 +1,6 @@ #pragma once #include -#include +#include namespace fnd { @@ -8,130 +8,199 @@ namespace fnd class List { public: - List() : - mElements() - { - } + // constructors + List(); + List(const List& other); - List(const T* elements, size_t num) : - mElements(num) - { - initList(elements, num); - } + // copy operator + const List& operator=(const List& other); - // assignment operator - const List& operator=(const List& other) - { - mElements.clear(); - for (size_t i = 0; i < other.getSize(); i++) - { - mElements.push_back(other[i]); - } + // equivalence operators + bool operator==(const List& other) const; + bool operator!=(const List& other) const; - return *this; - } + // back relative insertion + void addElement(const T& element); + + // element access + const T& operator[](size_t index) const; + T& operator[](size_t index); + const T& atBack() const; + T& atBack(); - // comparision operator - bool operator==(const List& other) const - { - if (other.getSize() != this->getSize()) - { - return false; - } + // element num + size_t size() const; - for (size_t i = 0; i < this->getSize(); i++) - { - if (getElement(i) != other[i]) - { - return false; - } - } + // clear List + void clear(); - return true; - } - - bool operator!=(const List& other) const - { - return !operator==(other); - } - - // access operators - const T& getElement(size_t index) const - { - return mElements[index]; - } - T& getElement(size_t index) - { - if (index == mElements.size()) mElements.push_back(T()); - return mElements[index]; - } - - const T& operator[](size_t index) const { return getElement(index); } - T& operator[](size_t index) { return getElement(index); } - const T& atBack() const { return getElement(getSize() - 1); } - T& atBack() { return getElement(getSize() - 1); } - - // functions - void addElement(const T& element) { mElements.push_back(element); } - size_t getIndexOf(const T& key) const - { - for (size_t i = 0; i < getSize(); i++) - { - if (getElement(i) == key) return i; - } - - throw Exception("LIST", "Element does not exist"); - } - bool hasElement(const T& key) const - { - try - { - getIndexOf(key); - } catch (const Exception&) - { - return false; - } - - return true; - } - - // special - template - size_t getIndexOf(const X& key) const - { - for (size_t i = 0; i < getSize(); i++) - { - if (getElement(i) == key) return i; - } - - throw Exception("LIST", "Element does not exist"); - } - template - bool hasElement(const X& key) const - { - try - { - getIndexOf(key); - } catch (const Exception&) - { - return false; - } - - return true; - } - size_t getSize() const { return mElements.size(); } - void clear() { mElements.clear(); } + // element access by key + template + bool hasElement(const K& key) const; + template + const T& getElement(const K& key) const; + template + T& getElement(const K& key); + private: - std::vector mElements; + static const size_t kDefaultSize = 20; - void initList(T* elements, size_t num) + fnd::Vec m_Vec; + size_t m_Num; + }; + + template + inline List::List() : + m_Vec(), + m_Num(0) + { + m_Vec.alloc(kDefaultSize); + } + + template + inline List::List(const List& other) : + m_Vec(other.m_Vec), + m_Size(other.m_Size) + {} + + template + inline const List& List::operator=(const List& other) + { + m_Vec = other.m_Vec; + m_Size = other.m_Size; + } + + template + inline bool List::operator==(const List& other) const + { + bool isEqual = true; + + if (m_Num == other.m_Num) { - mElements.clear(); - for (size_t i = 0; i < num; i++) + for (size_t i = 0; i < m_Num && isEqual == true; i++) { - mElements.push_back(elements[i]); + if ((*this)[i] != other[i]) + isEqual = false; } } - }; -} + else + { + isEqual = false; + } + return isEqual; + } + + template + inline bool List::operator!=(const List& other) const + { + return !(*this == other); + } + + template + inline void List::addElement(const T & element) + { + (*this)[m_Num] = element; + } + + template + inline const T & List::operator[](size_t index) const + { + if (index >= m_Num) + { + throw fnd::Exception("List", "Out of bound read"); + } + + return m_Vec[index]; + } + + template + inline T & List::operator[](size_t index) + { + if (index > m_Num) + { + throw fnd::Exception("List", "Out of bound read"); + } + else if (index == m_Num) + { + if ((m_Num * 2) >= m_Vec.size()) + { + m_Vec.alloc((m_Num + 1) * 2); + } + + m_Num++; + } + + return m_Vec[index]; + } + + template + inline const T & List::atBack() const + { + return m_Vec[m_Num - 1]; + } + + template + inline T & List::atBack() + { + return m_Vec[m_Num - 1]; + } + + template + inline size_t List::size() const + { + return m_Num; + } + + template + inline void List::clear() + { + m_Num = 0; + m_Vec.clear(); + } + + template + template + inline bool List::hasElement(const K & key) const + { + for (size_t i = 0; i < m_Num; i++) + { + if (m_List[i] == key) + { + return true; + } + } + + return false; + } + + template + template + inline const T & List::getElement(const K & key) const + { + for (size_t i = 0; i < m_Num; i++) + { + if (m_List[i] == key) + { + return m_List[i]; + } + } + + throw fnd::Exception("getElement(): element does not exist"); + } + + template + template + inline T & List::getElement(const K & key) + { + for (size_t i = 0; i < m_Num; i++) + { + if (m_List[i] == key) + { + return m_List[i]; + } + } + + throw fnd::Exception("getElement(): element does not exist"); + } +} diff --git a/lib/libfnd/include/fnd/MemoryBlob.h b/lib/libfnd/include/fnd/MemoryBlob.h deleted file mode 100644 index 465f257..0000000 --- a/lib/libfnd/include/fnd/MemoryBlob.h +++ /dev/null @@ -1,42 +0,0 @@ -#pragma once -#include -#include -#include -#include -#include -#include - -namespace fnd -{ - class MemoryBlob - { - public: - MemoryBlob(); - MemoryBlob(const byte_t* bytes, size_t len); - - bool operator==(const MemoryBlob& other) const; - bool operator!=(const MemoryBlob& other) const; - void operator=(const MemoryBlob& other); - - void alloc(size_t size); - void extend(size_t new_size); - void clear(); - - inline byte_t& operator[](size_t index) { return mData[index]; } - inline const byte_t& operator[](size_t index) const { return mData[index]; } - - inline byte_t* getBytes() { return mData.data(); } - inline const byte_t* getBytes() const { return mData.data(); } - inline size_t getSize() const { return mVisableSize; } - private: - const std::string kModuleName = "MEMORY_BLOB"; - static const size_t kAllocBlockSize = 0x1000; - - std::vector mData; - size_t mSize; - size_t mVisableSize; - - void allocateMemory(size_t size); - void clearMemory(); - }; -} diff --git a/lib/libfnd/include/fnd/Vec.h b/lib/libfnd/include/fnd/Vec.h new file mode 100644 index 0000000..91c9145 --- /dev/null +++ b/lib/libfnd/include/fnd/Vec.h @@ -0,0 +1,188 @@ +#pragma once +#include + +namespace fnd +{ + template + class Vec + { + public: + // constructors + Vec(); + Vec(const Vec& other); + Vec(const T* array, size_t num); + ~Vec(); + + // copy operator + const Vec& operator=(const Vec& other); + + // equivalence operators + bool operator==(const Vec& other) const; + bool operator!=(const Vec& other) const; + + // element access + const T& operator[](size_t index) const; + T& operator[](size_t index); + + // raw access + const T* data() const; + T* data(); + + // element num + size_t size() const; + + // allocate vector + void alloc(size_t new_size); + + // clear vector + void clear(); + private: + T * m_Vec; + size_t m_Size; + + void copyFrom(const T * array, size_t num); + }; + + template + inline Vec::Vec() : + m_Vec(nullptr), + m_Size(0) + {} + + template + inline Vec::Vec(const Vec& other) : + Vec() + { + copyFrom(other.data(), other.size()); + } + + template + inline Vec::Vec(const T * array, size_t num) : + Vec() + { + copyFrom(array, num); + } + + template + inline Vec::~Vec() + { + clear(); + } + + template + inline const Vec& Vec::operator=(const Vec& other) + { + copyFrom(other.data(), other.size()); + } + + template + inline bool Vec::operator==(const Vec& other) const + { + bool isEqual = true; + + if (m_Size == other.m_Size) + { + for (size_t i = 0; i < m_Size && isEqual; i++) + { + if (m_Vec[i] != other.m_Vec[i]) + { + isEqual = false; + } + } + } + else + { + isEqual = false; + } + + return isEqual; + } + + template + inline bool Vec::operator!=(const Vec& other) const + { + return !(*this == other); + } + + template + inline const T & Vec::operator[](size_t index) const + { + return m_Vec[index]; + } + + template + inline T & Vec::operator[](size_t index) + { + return m_Vec[index]; + } + + template + inline const T * Vec::data() const + { + return m_Vec; + } + + template + inline T * Vec::data() + { + return m_Vec; + } + + template + inline size_t Vec::size() const + { + return m_Size; + } + + template + inline void Vec::alloc(size_t new_size) + { + if (m_Vec != nullptr) + { + T* new_vec = new T[new_size]; + if (new_vec == nullptr) + { + fnd::Exception("Vec", "Failed to allocate memory for vector"); + } + + for (size_t i = 0; i < MIN(m_Size, new_size); i++) + { + new_vec[i] = m_Vec[i]; + } + delete[] m_Vec; + m_Vec = new_vec; + m_Size = new_size; + } + else + { + m_Vec = new T[new_size]; + if (m_Vec == nullptr) + { + fnd::Exception("Vec", "Failed to allocate memory for vector"); + } + m_Size = new_size; + } + } + + template + inline void Vec::clear() + { + if (m_Vec != nullptr) + { + delete[] m_Vec; + } + m_Vec = nullptr; + m_Size = 0; + } + + template + inline void Vec::copyFrom(const T * array, size_t num) + { + clear(); + alloc(num); + for (size_t i = 0; i < m_Size; i++) + { + m_Vec[i] = array[i]; + } + } +} \ No newline at end of file diff --git a/lib/libfnd/source/MemoryBlob.cpp b/lib/libfnd/source/MemoryBlob.cpp deleted file mode 100644 index 2456972..0000000 --- a/lib/libfnd/source/MemoryBlob.cpp +++ /dev/null @@ -1,89 +0,0 @@ -#include - -using namespace fnd; - -MemoryBlob::MemoryBlob() : - mData(), - mSize(0), - mVisableSize(0) -{ - -} - -fnd::MemoryBlob::MemoryBlob(const byte_t * bytes, size_t len) : - mData(), - mSize(0), - mVisableSize(0) -{ - alloc(len); - memcpy(getBytes(), bytes, getSize()); -} - -bool fnd::MemoryBlob::operator==(const MemoryBlob & other) const -{ - bool isEqual = true; - - if (this->getSize() == other.getSize()) - { - isEqual = memcmp(this->getBytes(), other.getBytes(), this->getSize()) == 0; - } - else - { - isEqual = false; - } - - - return isEqual; -} - -bool fnd::MemoryBlob::operator!=(const MemoryBlob & other) const -{ - return !operator==(other); -} - -void fnd::MemoryBlob::operator=(const MemoryBlob & other) -{ - alloc(other.getSize()); - memcpy(getBytes(), other.getBytes(), getSize()); -} - -void MemoryBlob::alloc(size_t size) -{ - if (size > mSize) - { - allocateMemory(size); - } - else - { - mVisableSize = size; - clearMemory(); - } -} - -void MemoryBlob::extend(size_t new_size) -{ - try { - mData.resize(new_size); - } - catch (...) { - throw fnd::Exception(kModuleName, "extend() failed to allocate memory"); - } -} - -void fnd::MemoryBlob::clear() -{ - mVisableSize = 0; -} - -void MemoryBlob::allocateMemory(size_t size) -{ - mSize = (size_t)align(size, kAllocBlockSize); - mVisableSize = size; - extend(mSize); - clearMemory(); -} - -void MemoryBlob::clearMemory() -{ - memset(mData.data(), 0, mSize); -}