Arbutils/src/String/BasicStringView.hpp

40 lines
1.7 KiB
C++
Raw Normal View History

#ifndef ARBUTILS_BASICSTRINGVIEW_HPP
#define ARBUTILS_BASICSTRINGVIEW_HPP
#include <iostream>
#include <string>
namespace ArbUt {
class BasicStringView {
protected:
size_t _length = 0;
uint32_t _hash = 0;
constexpr BasicStringView(size_t length, uint32_t hash) : _length(length), _hash(hash) {}
2020-06-26 13:58:42 +00:00
public:
[[nodiscard]] inline constexpr size_t Length() const noexcept { return _length; }
[[nodiscard]] inline constexpr uint32_t GetHash() const noexcept { return _hash; }
[[nodiscard]] inline constexpr std::size_t operator()(BasicStringView const& s) const noexcept { return _hash; }
[[nodiscard]] inline constexpr operator uint32_t() const noexcept { return _hash; }
[[nodiscard]] inline constexpr bool operator==(const BasicStringView& rhs) const noexcept {
return _hash == rhs._hash;
}
inline constexpr bool operator!=(const BasicStringView& rhs) const noexcept { return _hash != rhs._hash; }
inline constexpr bool Empty() const noexcept { return Length() == 0; }
[[nodiscard]] virtual constexpr const char* c_str() const noexcept = 0;
[[nodiscard]] virtual constexpr std::string_view std_str() const noexcept = 0;
virtual constexpr bool operator==(const std::string_view& rhs) const noexcept = 0;
virtual constexpr bool operator!=(const std::string_view& rhs) const noexcept = 0;
virtual constexpr bool operator==(const char* rhs) const noexcept = 0;
virtual constexpr bool operator!=(const char* rhs) const noexcept = 0;
friend std::ostream& operator<<(std::ostream& out, const BasicStringView& c) {
out << c.c_str();
return out;
}
};
}
#endif // ARBUTILS_BASICSTRINGVIEW_HPP