From f59ec6c957fb692e1c00a31da116a20d97fab808 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sat, 20 Jun 2020 17:45:41 +0200 Subject: [PATCH] Follow code style with noexcept, add Const() function to BorrowedPtr to return a const pointer of itself. --- src/Collections/Dictionary.hpp | 16 ++++----- src/Collections/List.hpp | 26 +++++++------- src/ConstString.hpp | 10 +++--- src/Enum.hpp | 25 ++++++------- src/Memory/BorrowedPtr.hpp | 26 +++++++------- src/Memory/UniquePtrList.hpp | 32 ++++++++--------- src/Random.hpp | 4 +-- src/__ConstStringCore.hpp | 64 +++++++++++++++++++--------------- 8 files changed, 107 insertions(+), 96 deletions(-) diff --git a/src/Collections/Dictionary.hpp b/src/Collections/Dictionary.hpp index 6cfa62b..94a6f51 100644 --- a/src/Collections/Dictionary.hpp +++ b/src/Collections/Dictionary.hpp @@ -17,7 +17,7 @@ namespace ArbUt { explicit Dictionary(size_t capacity) : _map(capacity) {} explicit Dictionary(const std::initializer_list>& l) : _map(l) {} - inline void Clear() { _map.clear(); } + inline void Clear() noexcept { _map.clear(); } inline void Insert(const KeyT& key, const ValueT& value) { [[maybe_unused]] const auto& v = _map.insert({key, value}); @@ -39,7 +39,7 @@ namespace ArbUt { [[nodiscard]] inline const ValueT& Get(const KeyT& key) const { return _map.at(key); } - inline bool TryGet(const KeyT& key, ValueT& out) const { + inline bool TryGet(const KeyT& key, ValueT& out) const noexcept { const auto& find = _map.find(key); if (find == _map.end()) { return false; @@ -50,21 +50,21 @@ namespace ArbUt { inline void Remove(const KeyT& key) { _map.erase(key); } - [[nodiscard]] inline size_t Count() const { return _map.size(); } + [[nodiscard]] inline size_t Count() const noexcept { return _map.size(); } inline bool Has(const KeyT& key) const noexcept { return _map.find(key) != _map.end(); } inline ValueT& operator[](const KeyT& key) { return Get(key); } inline const ValueT& operator[](const KeyT& key) const { return Get(key); } - iterator begin() { return _map.begin(); } - const_iterator begin() const { return _map.begin(); } + iterator begin() noexcept { return _map.begin(); } + const_iterator begin() const noexcept { return _map.begin(); } - iterator end() { return _map.end(); } + iterator end() noexcept { return _map.end(); } const_iterator end() const { return _map.end(); } - const std::unordered_map& GetStdMap() const { return _map; } - std::unordered_map& GetStdMap() { return _map; } + const std::unordered_map& GetStdMap() const noexcept { return _map; } + std::unordered_map& GetStdMap() noexcept { return _map; } }; } diff --git a/src/Collections/List.hpp b/src/Collections/List.hpp index ffce578..867f063 100644 --- a/src/Collections/List.hpp +++ b/src/Collections/List.hpp @@ -15,12 +15,12 @@ namespace ArbUt { using const_iterator = typename std::vector::const_iterator; public: - List() : _vector() {} + List() noexcept : _vector() {} explicit List(size_t capacity) : _vector() { _vector.reserve(capacity); } - List(const std::initializer_list& l) : _vector(l) {} - List(const ValueT* begin, const ValueT* end) : _vector(begin, end) {} + List(const std::initializer_list& l) noexcept : _vector(l) {} + List(const ValueT* begin, const ValueT* end) noexcept : _vector(begin, end) {} - inline void Clear() { _vector.clear(); } + inline void Clear() noexcept { _vector.clear(); } inline reference At(size_t index) { #ifndef NO_ASSERT @@ -64,7 +64,7 @@ namespace ArbUt { /// Find the index of the first occurrence of a value in the list, return -1 if none is found. /// \param value The value we want the index for. /// \return The index of the first occurrence of the value in the list, or -1 if none is found. - inline size_t IndexOf(const ValueT& value) const { + inline size_t IndexOf(const ValueT& value) const noexcept { const auto& it = std::find(_vector.begin(), _vector.end(), value); if (it == _vector.end()) return -1; @@ -78,23 +78,23 @@ namespace ArbUt { inline reference operator[](size_t index) { return At(index); } inline const const_reference& operator[](size_t index) const { return At(index); } - inline size_t Count() const { return _vector.size(); } + inline size_t Count() const noexcept { return _vector.size(); } inline void Reserve(size_t size) { _vector.reserve(size); } inline void Resize(size_t size) { _vector.resize(size); } inline void Resize(size_t size, const ValueT& defaultValue) { _vector.resize(size, defaultValue); } inline void Remove(size_t index) { _vector.erase(_vector.begin() + index); } - iterator begin() { return _vector.begin(); } - const_iterator begin() const { return _vector.begin(); } + iterator begin() noexcept { return _vector.begin(); } + const_iterator begin() const noexcept { return _vector.begin(); } - iterator end() { return _vector.end(); } - const_iterator end() const { return _vector.end(); } + iterator end() noexcept { return _vector.end(); } + const_iterator end() const noexcept { return _vector.end(); } - const ValueT* RawData() const { return _vector.data(); } + const ValueT* RawData() const noexcept { return _vector.data(); } - const std::vector& GetStdList() const { return _vector; } - std::vector& GetStdList() { return _vector; } + const std::vector& GetStdList() const noexcept { return _vector; } + std::vector& GetStdList() noexcept { return _vector; } }; } diff --git a/src/ConstString.hpp b/src/ConstString.hpp index 36b0c8c..546e69a 100644 --- a/src/ConstString.hpp +++ b/src/ConstString.hpp @@ -17,19 +17,19 @@ namespace ArbUt { __ConstStringCharHolder& operator=(const __ConstStringCharHolder& other) = delete; public: - __ConstStringCharHolder(const char* value, size_t size) : _value(new char[size + 1]), _references(1) { + __ConstStringCharHolder(const char* value, size_t size) noexcept : _value(new char[size + 1]), _references(1) { strncpy(_value, value, size + 1); } - ~__ConstStringCharHolder() { delete[] _value; } + ~__ConstStringCharHolder() noexcept { delete[] _value; } - inline void RemoveReference() { + inline void RemoveReference() noexcept { if (--_references <= 0) { delete this; } } - inline void AddReference() { _references++; } - inline constexpr const char* GetValue() const { return _value; } + inline void AddReference() noexcept { _references++; } + inline constexpr const char* GetValue() const noexcept { return _value; } }; } diff --git a/src/Enum.hpp b/src/Enum.hpp index e08aef2..3cd042f 100644 --- a/src/Enum.hpp +++ b/src/Enum.hpp @@ -52,21 +52,22 @@ MACRO_UTILS_FOR_EACH_WITH_VALUE(ENUM_VALUE, startValue + ___MACRO_UTILS_NARGS(values) - 1, values) \ }; \ class name##Helper { \ - inline static uint32_t constexpr ConstHash(char const* input) { \ + inline static uint32_t constexpr ConstHash(char const* input) noexcept { \ return *input ? static_cast(*input) + 33 * ConstHash(input + 1) : 5381; \ } \ - inline static constexpr char charToLower(const char c) { \ + inline static constexpr char charToLower(const char c) noexcept { \ return (c >= 'A' && c <= 'Z') ? c + ('a' - 'A') : c; \ } \ - inline static uint32_t constexpr ConstHashCI(char const* input) { \ + inline static uint32_t constexpr ConstHashCI(char const* input) noexcept { \ return charToLower(*input) ? static_cast(charToLower(*input)) + 33 * ConstHashCI(input + 1) \ : 5381; \ } \ \ public: \ - constexpr static const char* ToString(name value) { \ + constexpr static const char* ToString(name value) noexcept { \ switch (value) { MACRO_UTILS_FOR_EACH(ENUM_CASE, name, values) } \ - auto v = static_cast(value); \ + /*If we haven't found a value, we want to stringify the number*/ \ + auto v = static_cast(value); \ auto size = (int)((ceil(log10(v)) + 1) * sizeof(char)); \ char* snum = new char[size + 1]; \ sprintf(snum, "%d", v); \ @@ -78,21 +79,21 @@ switch (ConstHash(input)) { MACRO_UTILS_FOR_EACH(ENUM_PARSE_CASE, name, values) } \ throw std::runtime_error("Invalid " #name " string."); \ } \ - constexpr static bool TryParse(const char* input, name& out, bool caseInsensitive = false) { \ + constexpr static bool TryParse(const char* input, name& out, bool caseInsensitive = false) noexcept { \ if (caseInsensitive) \ return TryParseCaseInsensitive(input, out); \ switch (ConstHash(input)) { MACRO_UTILS_FOR_EACH(ENUM_TRY_PARSE_CASE, name, values) } \ return false; \ } \ - static std::vector GetValues() { return {MACRO_UTILS_FOR_EACH(ARRAY_NAME, name, values)}; } \ - constexpr static name Last() { return name::MACRO_UTILS_GET_LAST(values); } \ - constexpr static name First() { return name::MACRO_UTILS_GET_FIRST(values); } \ - constexpr static name Highest() { \ + static std::vector GetValues() noexcept { return {MACRO_UTILS_FOR_EACH(ARRAY_NAME, name, values)}; } \ + constexpr static name Last() noexcept { return name::MACRO_UTILS_GET_LAST(values); } \ + constexpr static name First() noexcept { return name::MACRO_UTILS_GET_FIRST(values); } \ + constexpr static name Highest() noexcept { \ int64_t highest = -9223372036854775807; \ MACRO_UTILS_FOR_EACH(ENUM_GET_HIGHEST, name, values) \ return (name)highest; \ } \ - constexpr static name Lowest() { \ + constexpr static name Lowest() noexcept { \ int64_t lowest = 9223372036854775807; \ MACRO_UTILS_FOR_EACH(ENUM_GET_LOWEST, name, values) \ return (name)lowest; \ @@ -103,7 +104,7 @@ switch (ConstHashCI(input)) { MACRO_UTILS_FOR_EACH(ENUM_PARSE_CASE_INSENSITIVE, name, values) } \ throw std::runtime_error("Invalid " #name " string."); \ } \ - constexpr static bool TryParseCaseInsensitive(const char* input, name& out) { \ + constexpr static bool TryParseCaseInsensitive(const char* input, name& out) noexcept { \ switch (ConstHashCI(input)) { MACRO_UTILS_FOR_EACH(ENUM_TRY_PARSE_CASE_INSENSITIVE, name, values) } \ return false; \ } \ diff --git a/src/Memory/BorrowedPtr.hpp b/src/Memory/BorrowedPtr.hpp index 438c711..ee8eefa 100644 --- a/src/Memory/BorrowedPtr.hpp +++ b/src/Memory/BorrowedPtr.hpp @@ -12,14 +12,14 @@ namespace ArbUt { T* _raw; public: - inline BorrowedPtr() : _raw(nullptr){}; - inline BorrowedPtr(T* ptr) : _raw(ptr){}; - inline BorrowedPtr(const BorrowedPtr& other) : _raw(other._raw){}; - inline BorrowedPtr(const std::unique_ptr& other) : _raw(other.get()){}; + inline BorrowedPtr() noexcept : _raw(nullptr){}; + inline BorrowedPtr(T* ptr) noexcept : _raw(ptr){}; + inline BorrowedPtr(const BorrowedPtr& other) noexcept : _raw(other._raw){}; + inline BorrowedPtr(const std::unique_ptr& other) noexcept : _raw(other.get()){}; - ~BorrowedPtr() = default; + ~BorrowedPtr() noexcept = default; - inline BorrowedPtr& operator=(const BorrowedPtr& rhs) { + inline BorrowedPtr& operator=(const BorrowedPtr& rhs) noexcept { _raw = rhs._raw; return *this; } @@ -27,17 +27,21 @@ namespace ArbUt { inline T* operator->() const noexcept { return _raw; } inline T* GetRaw() const noexcept { return _raw; } - inline bool operator==(const BorrowedPtr& rhs) const { return _raw == rhs._raw; } - inline bool operator!=(const BorrowedPtr& rhs) const { return _raw != rhs._raw; } + inline bool operator==(const BorrowedPtr& rhs) const noexcept { return _raw == rhs._raw; } + inline bool operator!=(const BorrowedPtr& rhs) const noexcept { return _raw != rhs._raw; } [[nodiscard]] inline constexpr bool IsNull() const noexcept { return _raw == nullptr; } + inline BorrowedPtr Const() const noexcept { + return BorrowedPtr(_raw); + } + template inline BorrowedPtr As() const { auto cast = dynamic_cast(_raw); return BorrowedPtr(cast); } - template inline bool TryAs(BorrowedPtr& out) const { + template inline bool TryAs(BorrowedPtr& out) const noexcept { auto cast = dynamic_cast(_raw); if (cast == nullptr) return false; @@ -45,7 +49,7 @@ namespace ArbUt { return true; } - template inline BorrowedPtr ForceAs() const { + template inline BorrowedPtr ForceAs() const noexcept { auto cast = reinterpret_cast(_raw); return BorrowedPtr(cast); } @@ -53,11 +57,9 @@ namespace ArbUt { } namespace std { - template struct hash> { std::size_t operator()(const ArbUt::BorrowedPtr& k) const { return (size_t)k.GetRaw(); } }; - } #endif // ARBUTILS_BORROWEDPTR_HPP diff --git a/src/Memory/UniquePtrList.hpp b/src/Memory/UniquePtrList.hpp index cdb4a58..712d322 100644 --- a/src/Memory/UniquePtrList.hpp +++ b/src/Memory/UniquePtrList.hpp @@ -15,17 +15,17 @@ namespace ArbUt { using const_iterator = typename std::vector::const_iterator; public: - inline UniquePtrList() : _vector() {} - inline UniquePtrList(std::vector vec) : _vector(vec) {} + inline UniquePtrList() noexcept : _vector() {} + inline UniquePtrList(const std::vector& vec) noexcept : _vector(vec) {} explicit inline UniquePtrList(size_t capacity) : _vector() { _vector.reserve(capacity); } - inline UniquePtrList(const std::initializer_list& l) : _vector(l) {} - inline UniquePtrList(ValueT* const* begin, ValueT* const* end) : _vector(begin, end) {} + inline UniquePtrList(const std::initializer_list& l) noexcept : _vector(l) {} + inline UniquePtrList(ValueT* const* begin, ValueT* const* end) noexcept : _vector(begin, end) {} UniquePtrList(const UniquePtrList&) = delete; UniquePtrList& operator=(const UniquePtrList&) = delete; - ~UniquePtrList() { Clear(); } + ~UniquePtrList() noexcept { Clear(); } - inline void Clear() { + inline void Clear() noexcept { for (auto& i : _vector) { delete i; } @@ -54,14 +54,14 @@ namespace ArbUt { _vector.erase(_vector.begin() + index); } - inline bool Contains(const BorrowedPtr& value) const { + inline bool Contains(const BorrowedPtr& value) const noexcept { return std::find(_vector.begin(), _vector.end(), value) != _vector.end(); } /// Find the index of the first occurrence of a value in the list, return -1 if none is found. /// \param value The value we want the index for. /// \return The index of the first occurrence of the value in the list, or -1 if none is found. - inline size_t IndexOf(const BorrowedPtr& value) const { + inline size_t IndexOf(const BorrowedPtr& value) const noexcept { const auto& it = std::find(_vector.begin(), _vector.end(), value); if (it == _vector.end()) return -1; @@ -71,17 +71,17 @@ namespace ArbUt { inline void Append(ValueT* value) { _vector.push_back(value); } inline BorrowedPtr operator[](size_t index) const { return At(index); } - inline size_t Count() const { return _vector.size(); } + inline size_t Count() const noexcept { return _vector.size(); } - iterator begin() { return _vector.begin(); } - iterator end() { return _vector.end(); } - const_iterator begin() const { return _vector.begin(); } - const_iterator end() const { return _vector.end(); } + iterator begin() noexcept { return _vector.begin(); } + iterator end() noexcept { return _vector.end(); } + const_iterator begin() const noexcept { return _vector.begin(); } + const_iterator end() const noexcept { return _vector.end(); } - ValueT* const* RawData() const { return _vector.data(); } + ValueT* const* RawData() const noexcept { return _vector.data(); } - const std::vector& GetStdList() const { return _vector; } - std::vector& GetStdList() { return _vector; } + const std::vector& GetStdList() const noexcept { return _vector; } + std::vector& GetStdList() noexcept { return _vector; } }; } diff --git a/src/Random.hpp b/src/Random.hpp index ff80f70..e5773e4 100644 --- a/src/Random.hpp +++ b/src/Random.hpp @@ -77,8 +77,8 @@ namespace ArbUt { class Random : public BaseRandom { public: - constexpr Random() : BaseRandom() {} - explicit constexpr Random(uint_fast32_t seed) : BaseRandom(seed) {} + constexpr Random() noexcept : BaseRandom() {} + explicit constexpr Random(uint_fast32_t seed) noexcept : BaseRandom(seed) {} }; } #endif // ARBUTILS_RANDOM_HPP diff --git a/src/__ConstStringCore.hpp b/src/__ConstStringCore.hpp index cf3a3f4..cd25aae 100644 --- a/src/__ConstStringCore.hpp +++ b/src/__ConstStringCore.hpp @@ -26,16 +26,19 @@ } \ \ public: \ - name(const char* str, size_t size) \ + name(const char* str, size_t size) noexcept \ : _str(new __ConstStringCharHolder(str, size)), _length(size), _hash(Hash(str)) {} \ \ - name() : _str(GetEmptyString()), _length(0), _hash(Hash("")) { GetEmptyString()->AddReference(); }; \ - explicit name(const char* str) : name(str, Length(str)){}; \ - explicit name(const std::string& str) : name(str.c_str(), str.size()){}; \ + name() noexcept : _str(GetEmptyString()), _length(0), _hash(Hash("")) { \ + GetEmptyString()->AddReference(); \ + }; \ + explicit name(const char* str) noexcept : name(str, Length(str)){}; \ + explicit name(const std::string& str) noexcept : name(str.c_str(), str.size()){}; \ \ /* Copy operators */ \ - name(const name& other) : _str(other.CloneHolder()), _length(other._length), _hash(other._hash) {} \ - name& operator=(const name& other) { \ + name(const name& other) noexcept \ + : _str(other.CloneHolder()), _length(other._length), _hash(other._hash) {} \ + name& operator=(const name& other) noexcept { \ if (_str == other._str) { \ _str->AddReference(); \ return *this; \ @@ -47,10 +50,12 @@ return *this; \ } \ \ - ~name() { _str->RemoveReference(); } \ + ~name() noexcept { _str->RemoveReference(); } \ \ [[nodiscard]] inline constexpr const char* c_str() const noexcept { return _str->GetValue(); } \ - [[nodiscard]] inline std::string std_str() const { return std::string(_str->GetValue(), _length); } \ + [[nodiscard]] inline std::string std_str() const noexcept { \ + return std::string(_str->GetValue(), _length); \ + } \ \ [[nodiscard]] inline constexpr size_t Length() const noexcept { return _length; } \ \ @@ -58,21 +63,23 @@ inline constexpr bool Empty() const noexcept { return _length == 0; } \ \ constexpr std::size_t operator()(name const& s) const noexcept { return s.GetHash(); } \ - inline constexpr operator uint32_t() const { return _hash; } \ + inline constexpr operator uint32_t() const noexcept { return _hash; } \ \ - inline constexpr bool operator==(const name& rhs) const { return _hash == rhs._hash; } \ - inline constexpr bool operator!=(const name& rhs) const { return _hash != rhs._hash; } \ - inline STDSTRINGCONSTEXPR bool operator==(const std::string& rhs) const { \ + inline constexpr bool operator==(const name& rhs) const noexcept { return _hash == rhs._hash; } \ + inline constexpr bool operator!=(const name& rhs) const noexcept { return _hash != rhs._hash; } \ + inline STDSTRINGCONSTEXPR bool operator==(const std::string& rhs) const noexcept { \ return _hash == Hash(rhs.c_str()); \ } \ - inline STDSTRINGCONSTEXPR bool operator!=(const std::string& rhs) const { \ + inline STDSTRINGCONSTEXPR bool operator!=(const std::string& rhs) const noexcept { \ return _hash != Hash(rhs.c_str()); \ } \ - inline constexpr bool operator==(const char* rhs) const { return _hash == Hash(rhs); } \ - inline constexpr bool operator!=(const char* rhs) const { return _hash != Hash(rhs); } \ + inline constexpr bool operator==(const char* rhs) const noexcept { return _hash == Hash(rhs); } \ + inline constexpr bool operator!=(const char* rhs) const noexcept { return _hash != Hash(rhs); } \ \ - inline static constexpr uint32_t GetHash(const char* val) { return Hash(val); } \ - inline static STDSTRINGCONSTEXPR uint32_t GetHash(const std::string& val) { return Hash(val.c_str()); } \ + inline static constexpr uint32_t GetHash(const char* val) noexcept { return Hash(val); } \ + inline static STDSTRINGCONSTEXPR uint32_t GetHash(const std::string& val) noexcept { \ + return Hash(val.c_str()); \ + } \ }; \ \ class name##_Literal { \ @@ -83,11 +90,12 @@ \ hashFunction; \ \ - inline static int constexpr Length(const char* str) { return *str ? 1 + Length(str + 1) : 0; } \ + inline static int constexpr Length(const char* str) noexcept { return *str ? 1 + Length(str + 1) : 0; } \ \ public: \ - constexpr name##_Literal(const char* str, size_t size) : _str(str), _length(size), _hash(Hash(str)) {} \ - explicit name##_Literal(const char* str) : name##_Literal(str, Length(str)){}; \ + constexpr name##_Literal(const char* str, size_t size) noexcept \ + : _str(str), _length(size), _hash(Hash(str)) {} \ + explicit name##_Literal(const char* str) noexcept : name##_Literal(str, Length(str)){}; \ [[nodiscard]] inline constexpr const char* c_str() const noexcept { return _str; } \ [[nodiscard]] inline constexpr size_t Length() const noexcept { return _length; } \ \ @@ -95,19 +103,19 @@ inline constexpr bool Empty() const noexcept { return _length == 0; } \ \ constexpr std::size_t operator()(name##_Literal const& s) const noexcept { return s.GetHash(); } \ - inline constexpr operator uint32_t() const { return _hash; } \ - inline operator name() const { return name(_str, _length); } \ + inline constexpr operator uint32_t() const noexcept { return _hash; } \ + inline operator name() const noexcept { return name(_str, _length); } \ \ - inline constexpr bool operator==(const name##_Literal& rhs) const { return _hash == rhs._hash; } \ - inline constexpr bool operator!=(const name##_Literal& rhs) const { return _hash != rhs._hash; } \ - inline STDSTRINGCONSTEXPR bool operator==(const std::string& rhs) const { \ + inline constexpr bool operator==(const name##_Literal& rhs) const noexcept { return _hash == rhs._hash; } \ + inline constexpr bool operator!=(const name##_Literal& rhs) const noexcept { return _hash != rhs._hash; } \ + inline STDSTRINGCONSTEXPR bool operator==(const std::string& rhs) const noexcept { \ return _hash == Hash(rhs.c_str()); \ } \ - inline STDSTRINGCONSTEXPR bool operator!=(const std::string& rhs) const { \ + inline STDSTRINGCONSTEXPR bool operator!=(const std::string& rhs) const noexcept { \ return _hash != Hash(rhs.c_str()); \ } \ - inline constexpr bool operator==(const char* rhs) const { return _hash == Hash(rhs); } \ - inline constexpr bool operator!=(const char* rhs) const { return _hash != Hash(rhs); } \ + inline constexpr bool operator==(const char* rhs) const noexcept { return _hash == Hash(rhs); } \ + inline constexpr bool operator!=(const char* rhs) const noexcept { return _hash != Hash(rhs); } \ }; \ } \ \