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

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

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