diff --git a/BAN/include/BAN/Iterators.h b/BAN/include/BAN/Iterators.h index c09ec178..782b59e2 100644 --- a/BAN/include/BAN/Iterators.h +++ b/BAN/include/BAN/Iterators.h @@ -9,7 +9,7 @@ namespace BAN { template - It next(It it, size_t count) + constexpr It next(It it, size_t count) { for (size_t i = 0; i < count; i++) ++it; @@ -18,13 +18,13 @@ namespace BAN template requires requires(It it, size_t n) { requires is_same_v; } - It next(It it, size_t count) + constexpr It next(It it, size_t count) { return it + count; } template - It prev(It it, size_t count) + constexpr It prev(It it, size_t count) { for (size_t i = 0; i < count; i++) --it; @@ -33,13 +33,13 @@ namespace BAN template requires requires(It it, size_t n) { requires is_same_v; } - It prev(It it, size_t count) + constexpr It prev(It it, size_t count) { return it - count; } template - size_t distance(It it1, It it2) + constexpr size_t distance(It it1, It it2) { size_t dist = 0; while (it1 != it2) @@ -52,7 +52,7 @@ namespace BAN template requires requires(It it1, It it2) { requires is_integral_v; } - size_t distance(It it1, It it2) + constexpr size_t distance(It it1, It it2) { return it2 - it1; } @@ -64,109 +64,109 @@ namespace BAN using value_type = T; public: - IteratorSimpleGeneral() = default; + constexpr IteratorSimpleGeneral() = default; template> - IteratorSimpleGeneral(const IteratorSimpleGeneral& other) + constexpr IteratorSimpleGeneral(const IteratorSimpleGeneral& other) : m_pointer(other.m_pointer) , m_valid(other.m_valid) { } - const T& operator*() const + constexpr const T& operator*() const { ASSERT(m_pointer); return *m_pointer; } template - enable_if_t operator*() + constexpr enable_if_t operator*() { ASSERT(*this); ASSERT(m_pointer); return *m_pointer; } - const T* operator->() const + constexpr const T* operator->() const { ASSERT(*this); ASSERT(m_pointer); return m_pointer; } template - enable_if_t operator->() + constexpr enable_if_t operator->() { ASSERT(*this); ASSERT(m_pointer); return m_pointer; } - IteratorSimpleGeneral& operator++() + constexpr IteratorSimpleGeneral& operator++() { ASSERT(*this); ASSERT(m_pointer); ++m_pointer; return *this; } - IteratorSimpleGeneral operator++(int) + constexpr IteratorSimpleGeneral operator++(int) { auto temp = *this; ++(*this); return temp; } - IteratorSimpleGeneral& operator--() + constexpr IteratorSimpleGeneral& operator--() { ASSERT(*this); ASSERT(m_pointer); --m_pointer; return *this; } - IteratorSimpleGeneral operator--(int) + constexpr IteratorSimpleGeneral operator--(int) { auto temp = *this; --(*this); return temp; } - size_t operator-(const IteratorSimpleGeneral& other) const + constexpr size_t operator-(const IteratorSimpleGeneral& other) const { ASSERT(*this && other); return m_pointer - other.m_pointer; } - IteratorSimpleGeneral operator+(size_t offset) const + constexpr IteratorSimpleGeneral operator+(size_t offset) const { return IteratorSimpleGeneral(m_pointer + offset); } - IteratorSimpleGeneral operator-(size_t offset) const + constexpr IteratorSimpleGeneral operator-(size_t offset) const { return IteratorSimpleGeneral(m_pointer - offset); } - bool operator<(const IteratorSimpleGeneral& other) const + constexpr bool operator<(const IteratorSimpleGeneral& other) const { ASSERT(*this); return m_pointer < other.m_pointer; } - bool operator==(const IteratorSimpleGeneral& other) const + constexpr bool operator==(const IteratorSimpleGeneral& other) const { ASSERT(*this); return m_pointer == other.m_pointer; } - bool operator!=(const IteratorSimpleGeneral& other) const + constexpr bool operator!=(const IteratorSimpleGeneral& other) const { ASSERT(*this); return !(*this == other); } - explicit operator bool() const + constexpr explicit operator bool() const { return m_valid; } private: - IteratorSimpleGeneral(maybe_const_t* pointer) + constexpr IteratorSimpleGeneral(maybe_const_t* pointer) : m_pointer(pointer) , m_valid(true) { @@ -193,16 +193,16 @@ namespace BAN using value_type = T; public: - IteratorDoubleGeneral() = default; + constexpr IteratorDoubleGeneral() = default; template> - IteratorDoubleGeneral(const IteratorDoubleGeneral& other) + constexpr IteratorDoubleGeneral(const IteratorDoubleGeneral& other) : m_outer_end(other.m_outer_end) , m_outer_current(other.m_outer_current) , m_inner_current(other.m_inner_current) { } - const T& operator*() const + constexpr const T& operator*() const { ASSERT(*this); ASSERT(m_outer_current != m_outer_end); @@ -210,7 +210,7 @@ namespace BAN return m_inner_current.operator*(); } template - enable_if_t operator*() + constexpr enable_if_t operator*() { ASSERT(*this); ASSERT(m_outer_current != m_outer_end); @@ -218,7 +218,7 @@ namespace BAN return m_inner_current.operator*(); } - const T* operator->() const + constexpr const T* operator->() const { ASSERT(*this); ASSERT(m_outer_current != m_outer_end); @@ -226,7 +226,7 @@ namespace BAN return m_inner_current.operator->(); } template - enable_if_t operator->() + constexpr enable_if_t operator->() { ASSERT(*this); ASSERT(m_outer_current != m_outer_end); @@ -234,7 +234,7 @@ namespace BAN return m_inner_current.operator->(); } - IteratorDoubleGeneral& operator++() + constexpr IteratorDoubleGeneral& operator++() { ASSERT(*this); ASSERT(m_outer_current != m_outer_end); @@ -243,14 +243,14 @@ namespace BAN find_valid_or_end(); return *this; } - IteratorDoubleGeneral operator++(int) + constexpr IteratorDoubleGeneral operator++(int) { auto temp = *this; ++(*this); return temp; } - bool operator==(const IteratorDoubleGeneral& other) const + constexpr bool operator==(const IteratorDoubleGeneral& other) const { ASSERT(*this && other); if (m_outer_end != other.m_outer_end) @@ -262,18 +262,18 @@ namespace BAN ASSERT(m_inner_current && other.m_inner_current); return m_inner_current == other.m_inner_current; } - bool operator!=(const IteratorDoubleGeneral& other) const + constexpr bool operator!=(const IteratorDoubleGeneral& other) const { return !(*this == other); } - explicit operator bool() const + constexpr explicit operator bool() const { return !!m_outer_current; } private: - IteratorDoubleGeneral(const OuterIterator& outer_end, const OuterIterator& outer_current) + constexpr IteratorDoubleGeneral(const OuterIterator& outer_end, const OuterIterator& outer_current) : m_outer_end(outer_end) , m_outer_current(outer_current) { @@ -284,7 +284,7 @@ namespace BAN } } - IteratorDoubleGeneral(const OuterIterator& outer_end, const OuterIterator& outer_current, const InnerIterator& inner_current) + constexpr IteratorDoubleGeneral(const OuterIterator& outer_end, const OuterIterator& outer_current, const InnerIterator& inner_current) : m_outer_end(outer_end) , m_outer_current(outer_current) , m_inner_current(inner_current) @@ -292,7 +292,7 @@ namespace BAN find_valid_or_end(); } - void find_valid_or_end() + constexpr void find_valid_or_end() { while (m_inner_current == m_outer_current->end()) { @@ -303,8 +303,8 @@ namespace BAN } } - OuterIterator outer_current() { return m_outer_current; } - InnerIterator inner_current() { return m_inner_current; } + constexpr OuterIterator outer_current() { return m_outer_current; } + constexpr InnerIterator inner_current() { return m_inner_current; } private: OuterIterator m_outer_end; diff --git a/BAN/include/BAN/Optional.h b/BAN/include/BAN/Optional.h index 0ebb22a0..946ccc05 100644 --- a/BAN/include/BAN/Optional.h +++ b/BAN/include/BAN/Optional.h @@ -13,35 +13,35 @@ namespace BAN class Optional { public: - Optional(); - Optional(Optional&&); - Optional(const Optional&); - Optional(const T&); - Optional(T&&); + constexpr Optional(); + constexpr Optional(Optional&&); + constexpr Optional(const Optional&); + constexpr Optional(const T&); + constexpr Optional(T&&); template - Optional(Args&&...); + constexpr Optional(Args&&...); ~Optional(); - Optional& operator=(Optional&&); - Optional& operator=(const Optional&); + constexpr Optional& operator=(Optional&&); + constexpr Optional& operator=(const Optional&); template - Optional& emplace(Args&&...); + constexpr Optional& emplace(Args&&...); - T* operator->(); - const T* operator->() const; + constexpr T* operator->(); + constexpr const T* operator->() const; - T& operator*(); - const T& operator*() const; + constexpr T& operator*(); + constexpr const T& operator*() const; - bool has_value() const; + constexpr bool has_value() const; - T release_value(); - T& value(); - const T& value() const; + constexpr T release_value(); + constexpr T& value(); + constexpr const T& value() const; - void clear(); + constexpr void clear(); private: alignas(T) uint8_t m_storage[sizeof(T)]; @@ -49,12 +49,12 @@ namespace BAN }; template - Optional::Optional() + constexpr Optional::Optional() : m_has_value(false) {} template - Optional::Optional(Optional&& other) + constexpr Optional::Optional(Optional&& other) : m_has_value(other.has_value()) { if (other.has_value()) @@ -62,7 +62,7 @@ namespace BAN } template - Optional::Optional(const Optional& other) + constexpr Optional::Optional(const Optional& other) : m_has_value(other.has_value()) { if (other.has_value()) @@ -70,14 +70,14 @@ namespace BAN } template - Optional::Optional(const T& value) + constexpr Optional::Optional(const T& value) : m_has_value(true) { new (m_storage) T(value); } template - Optional::Optional(T&& value) + constexpr Optional::Optional(T&& value) : m_has_value(true) { new (m_storage) T(move(value)); @@ -85,7 +85,7 @@ namespace BAN template template - Optional::Optional(Args&&... args) + constexpr Optional::Optional(Args&&... args) : m_has_value(true) { new (m_storage) T(forward(args)...); @@ -98,7 +98,7 @@ namespace BAN } template - Optional& Optional::operator=(Optional&& other) + constexpr Optional& Optional::operator=(Optional&& other) { clear(); m_has_value = other.has_value(); @@ -108,7 +108,7 @@ namespace BAN } template - Optional& Optional::operator=(const Optional& other) + constexpr Optional& Optional::operator=(const Optional& other) { clear(); m_has_value = other.has_value(); @@ -119,7 +119,7 @@ namespace BAN template template - Optional& Optional::emplace(Args&&... args) + constexpr Optional& Optional::emplace(Args&&... args) { clear(); m_has_value = true; @@ -128,41 +128,41 @@ namespace BAN } template - T* Optional::operator->() + constexpr T* Optional::operator->() { ASSERT(has_value()); return &value(); } template - const T* Optional::operator->() const + constexpr const T* Optional::operator->() const { ASSERT(has_value()); return &value(); } template - T& Optional::operator*() + constexpr T& Optional::operator*() { ASSERT(has_value()); return value(); } template - const T& Optional::operator*() const + constexpr const T& Optional::operator*() const { ASSERT(has_value()); return value(); } template - bool Optional::has_value() const + constexpr bool Optional::has_value() const { return m_has_value; } template - T Optional::release_value() + constexpr T Optional::release_value() { ASSERT(has_value()); T released_value = move(value()); @@ -172,21 +172,21 @@ namespace BAN } template - T& Optional::value() + constexpr T& Optional::value() { ASSERT(has_value()); return (T&)m_storage; } template - const T& Optional::value() const + constexpr const T& Optional::value() const { ASSERT(has_value()); return (const T&)m_storage; } template - void Optional::clear() + constexpr void Optional::clear() { if (m_has_value) value().~T(); diff --git a/BAN/include/BAN/StringView.h b/BAN/include/BAN/StringView.h index 6f3d57f6..4484979c 100644 --- a/BAN/include/BAN/StringView.h +++ b/BAN/include/BAN/StringView.h @@ -16,40 +16,44 @@ namespace BAN using const_iterator = ConstIteratorSimple; public: - StringView() {} - StringView(const String&); - StringView(const char* string, size_type len = -1) + constexpr StringView() {} + constexpr StringView(const char* string, size_type len = -1) { if (len == size_type(-1)) len = strlen(string); m_data = string; m_size = len; } + StringView(const String&); - const_iterator begin() const { return const_iterator(m_data); } - const_iterator end() const { return const_iterator(m_data + m_size); } + constexpr const_iterator begin() const { return const_iterator(m_data); } + constexpr const_iterator end() const { return const_iterator(m_data + m_size); } - char operator[](size_type index) const + constexpr char operator[](size_type index) const { ASSERT(index < m_size); return m_data[index]; } - bool operator==(StringView other) const + constexpr bool operator==(StringView other) const { if (m_size != other.m_size) return false; - return memcmp(m_data, other.m_data, m_size) == 0; + for (size_type i = 0; i < m_size; i++) + if (m_data[i] != other.m_data[i]) + return false; + return true; } - bool operator==(const char* other) const + constexpr bool operator==(const char* other) const { - if (memcmp(m_data, other, m_size)) - return false; + for (size_type i = 0; i < m_size; i++) + if (m_data[i] != other[i]) + return false; return other[m_size] == '\0'; } - StringView substring(size_type index, size_type len = -1) const + constexpr StringView substring(size_type index, size_type len = -1) const { ASSERT(index <= m_size); if (len == size_type(-1)) @@ -133,13 +137,13 @@ namespace BAN return result; } - char back() const + constexpr char back() const { ASSERT(m_size > 0); return m_data[m_size - 1]; } - char front() const + constexpr char front() const { ASSERT(m_size > 0); return m_data[0]; @@ -161,7 +165,7 @@ namespace BAN return {}; } - bool contains(char ch) const + constexpr bool contains(char ch) const { for (size_type i = 0; i < m_size; i++) if (m_data[i] == ch) @@ -169,7 +173,7 @@ namespace BAN return false; } - size_type count(char ch) const + constexpr size_type count(char ch) const { size_type result = 0; for (size_type i = 0; i < m_size; i++) @@ -178,9 +182,9 @@ namespace BAN return result; } - bool empty() const { return m_size == 0; } - size_type size() const { return m_size; } - const char* data() const { return m_data; } + constexpr bool empty() const { return m_size == 0; } + constexpr size_type size() const { return m_size; } + constexpr const char* data() const { return m_data; } private: const char* m_data = nullptr; @@ -189,7 +193,7 @@ namespace BAN } -inline BAN::StringView operator""sv(const char* str, BAN::StringView::size_type len) { return BAN::StringView(str, len); } +inline constexpr BAN::StringView operator""sv(const char* str, BAN::StringView::size_type len) { return BAN::StringView(str, len); } namespace BAN::Formatter {