Follow code style with noexcept, add Const() function to BorrowedPtr to return a const pointer of itself.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-06-20 17:45:41 +02:00
parent 893aa969d5
commit f59ec6c957
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
8 changed files with 107 additions and 96 deletions

View File

@ -17,7 +17,7 @@ namespace ArbUt {
explicit Dictionary(size_t capacity) : _map(capacity) {}
explicit Dictionary(const std::initializer_list<std::pair<const KeyT, ValueT>>& 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<KeyT, ValueT>& GetStdMap() const { return _map; }
std::unordered_map<KeyT, ValueT>& GetStdMap() { return _map; }
const std::unordered_map<KeyT, ValueT>& GetStdMap() const noexcept { return _map; }
std::unordered_map<KeyT, ValueT>& GetStdMap() noexcept { return _map; }
};
}

View File

@ -15,12 +15,12 @@ namespace ArbUt {
using const_iterator = typename std::vector<ValueT>::const_iterator;
public:
List() : _vector() {}
List() noexcept : _vector() {}
explicit List(size_t capacity) : _vector() { _vector.reserve(capacity); }
List(const std::initializer_list<ValueT>& l) : _vector(l) {}
List(const ValueT* begin, const ValueT* end) : _vector(begin, end) {}
List(const std::initializer_list<ValueT>& 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<ValueT>& GetStdList() const { return _vector; }
std::vector<ValueT>& GetStdList() { return _vector; }
const std::vector<ValueT>& GetStdList() const noexcept { return _vector; }
std::vector<ValueT>& GetStdList() noexcept { return _vector; }
};
}

View File

@ -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; }
};
}

View File

@ -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<uint32_t>(*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<uint32_t>(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<int>(value); \
/*If we haven't found a value, we want to stringify the number*/ \
auto v = static_cast<type>(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<name> 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<name> 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; \
} \

View File

@ -12,14 +12,14 @@ namespace ArbUt {
T* _raw;
public:
inline BorrowedPtr<T>() : _raw(nullptr){};
inline BorrowedPtr<T>(T* ptr) : _raw(ptr){};
inline BorrowedPtr(const BorrowedPtr<T>& other) : _raw(other._raw){};
inline BorrowedPtr(const std::unique_ptr<T>& other) : _raw(other.get()){};
inline BorrowedPtr<T>() noexcept : _raw(nullptr){};
inline BorrowedPtr<T>(T* ptr) noexcept : _raw(ptr){};
inline BorrowedPtr<T>(const BorrowedPtr<T>& other) noexcept : _raw(other._raw){};
inline BorrowedPtr<T>(const std::unique_ptr<T>& other) noexcept : _raw(other.get()){};
~BorrowedPtr() = default;
~BorrowedPtr() noexcept = default;
inline BorrowedPtr<T>& operator=(const BorrowedPtr<T>& rhs) {
inline BorrowedPtr<T>& operator=(const BorrowedPtr<T>& 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 T> Const() const noexcept {
return BorrowedPtr<const T>(_raw);
}
template <class TCast> inline BorrowedPtr<TCast> As() const {
auto cast = dynamic_cast<TCast*>(_raw);
return BorrowedPtr<TCast>(cast);
}
template <class TCast> inline bool TryAs(BorrowedPtr<TCast>& out) const {
template <class TCast> inline bool TryAs(BorrowedPtr<TCast>& out) const noexcept {
auto cast = dynamic_cast<TCast*>(_raw);
if (cast == nullptr)
return false;
@ -45,7 +49,7 @@ namespace ArbUt {
return true;
}
template <class TCast> inline BorrowedPtr<TCast> ForceAs() const {
template <class TCast> inline BorrowedPtr<TCast> ForceAs() const noexcept {
auto cast = reinterpret_cast<TCast*>(_raw);
return BorrowedPtr<TCast>(cast);
}
@ -53,11 +57,9 @@ namespace ArbUt {
}
namespace std {
template <class T> struct hash<ArbUt::BorrowedPtr<T>> {
std::size_t operator()(const ArbUt::BorrowedPtr<T>& k) const { return (size_t)k.GetRaw(); }
};
}
#endif // ARBUTILS_BORROWEDPTR_HPP

View File

@ -15,17 +15,17 @@ namespace ArbUt {
using const_iterator = typename std::vector<ValueT*>::const_iterator;
public:
inline UniquePtrList() : _vector() {}
inline UniquePtrList(std::vector<ValueT*> vec) : _vector(vec) {}
inline UniquePtrList() noexcept : _vector() {}
inline UniquePtrList(const std::vector<ValueT*>& vec) noexcept : _vector(vec) {}
explicit inline UniquePtrList(size_t capacity) : _vector() { _vector.reserve(capacity); }
inline UniquePtrList(const std::initializer_list<ValueT*>& l) : _vector(l) {}
inline UniquePtrList(ValueT* const* begin, ValueT* const* end) : _vector(begin, end) {}
inline UniquePtrList(const std::initializer_list<ValueT*>& l) noexcept : _vector(l) {}
inline UniquePtrList(ValueT* const* begin, ValueT* const* end) noexcept : _vector(begin, end) {}
UniquePtrList(const UniquePtrList<ValueT>&) = delete;
UniquePtrList<ValueT>& operator=(const UniquePtrList<ValueT>&) = 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<ValueT>& value) const {
inline bool Contains(const BorrowedPtr<ValueT>& 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<ValueT>& value) const {
inline size_t IndexOf(const BorrowedPtr<ValueT>& 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<ValueT> 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<ValueT*>& GetStdList() const { return _vector; }
std::vector<ValueT*>& GetStdList() { return _vector; }
const std::vector<ValueT*>& GetStdList() const noexcept { return _vector; }
std::vector<ValueT*>& GetStdList() noexcept { return _vector; }
};
}

View File

@ -77,8 +77,8 @@ namespace ArbUt {
class Random : public BaseRandom<pcg32> {
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

View File

@ -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); } \
}; \
} \
\