diff --git a/example/example.cpp b/example/example.cpp index 3d08c76..4123867 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -50,7 +50,7 @@ void SomeMethod3() { template std::string SomeMethod4(U value) { - return std::string(NAMEOF(SomeMethod4)).append("<").append(NAMEOF_TYPE_T(T)).append(", ").append(NAMEOF_TYPE_T(U)).append(">(").append(NAMEOF_TYPE_T(U)).append(" value)"); + return NAMEOF(SomeMethod4) + "<" + NAMEOF_TYPE_T(T) + ", " + NAMEOF_TYPE_T(U) + ">(" + NAMEOF_TYPE_T(U) + " " + NAMEOF(value) + ")"; } template @@ -157,6 +157,7 @@ int main() { std::cout << NAMEOF(structvar.somefield + structvar.somefield) << std::endl; // ' somefield' std::cout << NAMEOF(42 + 42) << std::endl; // ' 42' std::cout << NAMEOF(NAMEOF(structvar)) << std::endl; // 'NAMEOF' + std::cout << NAMEOF((SomeMethod4)(1.0f)) << std::endl; // '' #endif #if 0 diff --git a/include/nameof.hpp b/include/nameof.hpp index a32b505..cff90c1 100644 --- a/include/nameof.hpp +++ b/include/nameof.hpp @@ -32,11 +32,15 @@ #include #include +#include #include #include #if defined(__cpp_constexpr) && __cpp_constexpr >= 201304L # define NAMEOF_HAS_CONSTEXPR14 1 +# define NAMEOF_CONSTEXPR14 constexpr +#else +# define NAMEOF_CONSTEXPR14 inline #endif #if (defined(__clang__) || defined(_MSC_VER)) || (defined(__GNUC__) && __GNUC__ >= 5) @@ -101,6 +105,8 @@ class cstring final { constexpr cstring(const char* str) noexcept : cstring{str, StrLen(str), 0, 0} {} + cstring(const std::string& str) noexcept : cstring{str.data(), str.size(), 0, 0} {} + cstring(const cstring&) = default; cstring(cstring&&) = default; @@ -115,6 +121,8 @@ class cstring final { constexpr std::size_t length() const noexcept { return size_; } + constexpr std::size_t max_size() const noexcept { return (std::numeric_limits::max)(); } + constexpr bool empty() const noexcept { return size_ == 0; } constexpr const char* begin() const noexcept { return str_; } @@ -127,6 +135,13 @@ class cstring final { constexpr const char& operator[](std::size_t i) const { return str_[i]; } + NAMEOF_CONSTEXPR14 const char& at(std::size_t i) const { + if (i < size_) { + return str_[i]; + } + throw std::out_of_range("cstring::at()"); + } + constexpr const char& front() const { return str_[0]; } constexpr const char& back() const { return str_[size_ - 1]; } @@ -153,6 +168,20 @@ class cstring final { return {str_ + pos, n}; } + int compare(cstring s) const { + auto result = std::char_traits::compare(str_, s.str_, s.size_ < size_ ? s.size_ : size_); + if (result != 0) { + return result; + } + if (size_ < s.size_) { + return -1; + } + if (size_ > s.size_) { + return 1; + } + return 0; + } + friend constexpr bool operator==(cstring lhs, cstring rhs) noexcept { return (lhs.size_ == rhs.size_) && StrEquals(lhs.str_, rhs.str_, lhs.size_); } @@ -161,6 +190,14 @@ class cstring final { return !(lhs == rhs); } + std::string append(cstring s) { + return std::string(str_, size_).append(s.str_, s.size_); + } + + friend std::string operator+(cstring lhs, cstring rhs) { + return std::string(lhs) + std::string(rhs); + } + friend std::ostream& operator<<(std::ostream& os, cstring str) { os.write(str.str_, str.size_); return os; diff --git a/test/test.cpp b/test/test.cpp index 84c5333..cc8923c 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -43,7 +43,7 @@ void SomeMethod3() { template std::string SomeMethod4(U value) { - return std::string(NAMEOF(SomeMethod4)).append("<").append(NAMEOF_TYPE_T(T)).append(", ").append(NAMEOF_TYPE_T(U)).append(">(").append(NAMEOF_TYPE_T(U)).append(" value)"); + return NAMEOF(SomeMethod4) + "<" + NAMEOF_TYPE_T(T) + ", " + NAMEOF_TYPE_T(U) + ">(" + NAMEOF_TYPE_T(U) + " " + NAMEOF(value) + ")"; } template @@ -389,3 +389,5 @@ TEST_CASE("Spaces and Tabs ignored") { REQUIRE(NAMEOF_TYPE_T( decltype(struct_var) ) == "SomeStruct"); } } + +TEST_CASE("cstring") {}