373 lines
12 KiB
C++
373 lines
12 KiB
C++
/**
|
|
* @file include/ActivMiddleware/Misc/StringLiteral.h
|
|
*
|
|
* @brief Template string-literal class.
|
|
*/
|
|
|
|
#if (!defined ACTIV_MISC_STRING_LITERAL_H)
|
|
#define ACTIV_MISC_STRING_LITERAL_H
|
|
|
|
#include "ActivMiddleware/ActivMiddleware.h"
|
|
#include "ActivMiddleware/Misc/StringView.h"
|
|
|
|
#include "ActivMiddleware/CxxYZ/type_traits.h"
|
|
|
|
// Boost 1.59 defines ACTIV_BOOST_NO_CXX11_CONSTEXPR for vc140, but constexpr works for us here regardless.
|
|
#if (!defined ACTIV_BOOST_NO_CXX11_VARIADIC_TEMPLATES) && \
|
|
((!defined ACTIV_BOOST_NO_CXX11_CONSTEXPR) || \
|
|
ACTIV_IS_COMPILER_VERSION(ACTIV_COMPILER_VISUAL_C, ==, ACTIV_MAKE_COMPILER_VERSION(14, 0)))
|
|
#define ACTIV_IS_STRING_LITERAL_AVAILABLE
|
|
#include ACTIV_BOOST_INCLUDE(preprocessor/punctuation/comma_if.hpp)
|
|
#include ACTIV_BOOST_INCLUDE(preprocessor/repetition/repeat.hpp)
|
|
#endif
|
|
|
|
//! Activ namespace.
|
|
namespace Activ
|
|
{
|
|
|
|
//! Test a type for being a StringLiteral. Always available even if StringLiteral itself is not.
|
|
template<typename T>
|
|
class IsStringLiteral : public CxxYZ::false_type
|
|
{
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
#if (defined ACTIV_IS_STRING_LITERAL_AVAILABLE)
|
|
|
|
namespace StringLiteralDetail
|
|
{
|
|
|
|
//! Special value for invalid length.
|
|
constexpr int invalidLengthMarker = -1;
|
|
|
|
//! Maximum supported length of a string literal.
|
|
#define ACTIV_MAX_STRING_LITERAL_LENGTH 64
|
|
|
|
// Retrieve characters from a c-string literal by index. This works because "hello"[2] is valid C++11 syntax. Who knew?
|
|
// A hack to use invalidLengthMarker if the string is too long. The length template will then static_assert if
|
|
// it sees a character of that value.
|
|
#define ACTIV_STRING_LITERAL_GET_CHAR(z, n, str) \
|
|
ACTIV_BOOST_PP_COMMA_IF(n) \
|
|
(ACTIV_ARRAY_SIZE(str) > (ACTIV_MAX_STRING_LITERAL_LENGTH + 1)) ? \
|
|
Activ::StringLiteralDetail::invalidLengthMarker : \
|
|
(((n) < ACTIV_ARRAY_SIZE(str)) ? str[n] : 0)
|
|
|
|
template<size_t LENGTH, int... CHARS>
|
|
class LengthImpl1;
|
|
|
|
template<size_t LENGTH, int C, int... TAIL>
|
|
class LengthImpl2
|
|
{
|
|
public:
|
|
static_assert(C != invalidLengthMarker,
|
|
"StringLiteral too long; maximum length is " ACTIV_STRINGIZE(ACTIV_MAX_STRING_LITERAL_LENGTH));
|
|
|
|
// Terminate once a nul character is seen.
|
|
static constexpr size_t value = (0 == C) ? 0 : (1 + LengthImpl1<LENGTH - 1, TAIL...>::value);
|
|
};
|
|
|
|
template<size_t LENGTH, int... CHARS>
|
|
class LengthImpl1
|
|
{
|
|
public:
|
|
static constexpr size_t value = LengthImpl2<LENGTH, CHARS...>::value;
|
|
};
|
|
|
|
template<int... CHARS>
|
|
class LengthImpl1<0, CHARS...>
|
|
{
|
|
public:
|
|
// Terminal case.
|
|
static constexpr size_t value = 0;
|
|
};
|
|
|
|
template<int... CHARS>
|
|
class Length
|
|
{
|
|
public:
|
|
static constexpr size_t value = LengthImpl1<sizeof...(CHARS), CHARS...>::value;
|
|
};
|
|
|
|
template<size_t INDEX, char CHAR, char... CHARS>
|
|
class ArrayOperatorImpl
|
|
{
|
|
public:
|
|
static constexpr char value = ArrayOperatorImpl<INDEX - 1, CHARS...>::value;
|
|
};
|
|
|
|
template<char CHAR, char... CHARS>
|
|
class ArrayOperatorImpl<0, CHAR, CHARS...>
|
|
{
|
|
public:
|
|
static constexpr char value = CHAR;
|
|
};
|
|
|
|
} // namespace StringLiteralDetail
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/*
|
|
* @brief String literal template.
|
|
*
|
|
* @tparam CHARS sequence of characters.
|
|
*
|
|
* Usage is, for example:
|
|
*
|
|
* using MyStringLiteral = ACTIV_MAKE_STRING_LITERAL("Hello World!");
|
|
*
|
|
* Note int is used such that int(-1) can be used for length checking.
|
|
*/
|
|
template<int... CHARS>
|
|
class StringLiteral
|
|
{
|
|
public:
|
|
using value_type = char;
|
|
using const_pointer = const char *;
|
|
using const_iterator = const char *;
|
|
|
|
//! Get a pointer to the string.
|
|
static const_pointer data() noexcept
|
|
{
|
|
/*
|
|
VS, Clang & G++ successfully optimize this to a single load of an address from the string table.
|
|
|
|
int main()
|
|
{
|
|
using MyString = ACTIV_MAKE_STRING_LITERAL("Hello World!");
|
|
|
|
puts(MyString::c_str());
|
|
|
|
return 0;
|
|
}
|
|
|
|
VS2015:
|
|
|
|
00007FF60EA91030 sub rsp, 28h
|
|
00007FF60EA91034 lea rcx, [`Activ::StringLiteral<72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0>::data'::`2'::str(07FF60EA92220h)]
|
|
00007FF60EA9103B call qword ptr[__imp_puts(07FF60EA92178h)]
|
|
00007FF60EA91041 xor eax, eax
|
|
00007FF60EA91043 add rsp, 28h
|
|
00007FF60EA91047 ret
|
|
|
|
Clang 3.5:
|
|
|
|
0x0000000000400b20 <+0>: push %rax
|
|
0x0000000000400b21 <+1>: mov $0x400be0,%edi
|
|
0x0000000000400b26 <+6>: callq 0x4009c0 <puts@plt>
|
|
0x0000000000400b2b <+11>: xor %eax,%eax
|
|
0x0000000000400b2d <+13>: pop %rdx
|
|
0x0000000000400b2e <+14>: retq
|
|
|
|
GCC 4.9:
|
|
|
|
0x0000000000400fd0 <+0>: lea 0x529(%rip),%rdi # 0x401500 <_ZZN5Activ13StringLiteralIILi72ELi101ELi108ELi108ELi111ELi32ELi87ELi111ELi114ELi108ELi100ELi33ELi0ELi0ELi0ELi0ELi0ELi0ELi0ELi0ELi0ELi0ELi0ELi0ELi0ELi0ELi0ELi0ELi0ELi0ELi0ELi0ELi0ELi0ELi0ELi0ELi0ELi0ELi0ELi0ELi0ELi0ELi0ELi0ELi0ELi0ELi0ELi0ELi0ELi0ELi0ELi0ELi0ELi0ELi0ELi0ELi0ELi0ELi0ELi0ELi0ELi0ELi0ELi0ELi0EEE4dataEvE3str>
|
|
0x0000000000400fd7 <+7>: sub $0x8,%rsp
|
|
0x0000000000400fdb <+11>: callq 0x400f80 <puts@plt>
|
|
0x0000000000400fe0 <+16>: xor %eax,%eax
|
|
0x0000000000400fe2 <+18>: add $0x8,%rsp
|
|
0x0000000000400fe6 <+22>: retq
|
|
*/
|
|
|
|
// Ensure there's a nul terminator (only relevant if sizeof...(CHARS) == ACTIV_MAX_STRING_LITERAL_LENGTH).
|
|
static constexpr value_type str[] { CHARS..., 0 };
|
|
|
|
return str;
|
|
}
|
|
|
|
//! Get a pointer to the string.
|
|
static const_pointer c_str() noexcept
|
|
{
|
|
return data();
|
|
}
|
|
|
|
//! Get the length of the string, excluding terminating nul character.
|
|
static constexpr size_t length() noexcept
|
|
{
|
|
return StringLiteralDetail::Length<CHARS...>::value;
|
|
}
|
|
|
|
static constexpr const_iterator begin() noexcept
|
|
{
|
|
return data();
|
|
}
|
|
|
|
static constexpr const_iterator cbegin() noexcept
|
|
{
|
|
return begin();
|
|
}
|
|
|
|
static constexpr const_iterator end() noexcept
|
|
{
|
|
return data() + length();
|
|
}
|
|
|
|
static constexpr const_iterator cend() noexcept
|
|
{
|
|
return end();
|
|
}
|
|
|
|
template<size_t INDEX>
|
|
static constexpr char ArrayOperator() noexcept
|
|
{
|
|
static_assert(INDEX < length(), "INDEX out of range");
|
|
|
|
return StringLiteralDetail::ArrayOperatorImpl<INDEX, CHARS...>::value;
|
|
}
|
|
|
|
//! Convenience function to create a CxxYZ::string_view.
|
|
static CxxYZ::string_view ToStringView() noexcept
|
|
{
|
|
return CxxYZ::string_view(data(), length());
|
|
}
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Helper macro to create a StringLiteral from a c-string.
|
|
*
|
|
* Usage is, for example:
|
|
*
|
|
* using MyStringLiteral = ACTIV_MAKE_STRING_LITERAL("Hello World!");
|
|
*
|
|
* @param str a c-string.
|
|
*/
|
|
#define ACTIV_MAKE_STRING_LITERAL(str) \
|
|
Activ::StringLiteral<ACTIV_BOOST_PP_REPEAT(ACTIV_MAX_STRING_LITERAL_LENGTH, ACTIV_STRING_LITERAL_GET_CHAR, str)>
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<int... CHARS, typename T>
|
|
inline bool operator==(const T &lhs, StringLiteral<CHARS...> rhs) noexcept
|
|
{
|
|
return ToStringType<CxxYZ::string_view>(lhs) == rhs.ToStringView();
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<int... CHARS, typename T>
|
|
inline bool operator==(StringLiteral<CHARS...> lhs, const T &rhs) noexcept
|
|
{
|
|
return lhs.ToStringView() == ToStringType<CxxYZ::string_view>(rhs);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<int... CHARS, typename T>
|
|
inline bool operator!=(const T &lhs, StringLiteral<CHARS...> rhs) noexcept
|
|
{
|
|
return ToStringType<CxxYZ::string_view>(lhs) != rhs.ToStringView();
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<int... CHARS, typename T>
|
|
inline bool operator!=(StringLiteral<CHARS...> lhs, const T &rhs) noexcept
|
|
{
|
|
return lhs.ToStringView() != ToStringType<CxxYZ::string_view>(rhs);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<int... CHARS, typename T>
|
|
inline bool operator<(const T &lhs, StringLiteral<CHARS...> rhs) noexcept
|
|
{
|
|
return ToStringType<CxxYZ::string_view>(lhs) < rhs.ToStringView();
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<int... CHARS, typename T>
|
|
inline bool operator<(StringLiteral<CHARS...> lhs, const T &rhs) noexcept
|
|
{
|
|
return lhs.ToStringView() < ToStringType<CxxYZ::string_view>(rhs);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<int... CHARS, typename T>
|
|
inline bool operator>(const T &lhs, StringLiteral<CHARS...> rhs) noexcept
|
|
{
|
|
return ToStringType<CxxYZ::string_view>(lhs) > rhs.ToStringView();
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<int... CHARS, typename T>
|
|
inline bool operator>(StringLiteral<CHARS...> lhs, const T &rhs) noexcept
|
|
{
|
|
return lhs.ToStringView() > ToStringType<CxxYZ::string_view>(rhs);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<int... CHARS, typename T>
|
|
inline bool operator<=(const T &lhs, StringLiteral<CHARS...> rhs) noexcept
|
|
{
|
|
return ToStringType<CxxYZ::string_view>(lhs) <= rhs.ToStringView();
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<int... CHARS, typename T>
|
|
inline bool operator<=(StringLiteral<CHARS...> lhs, const T &rhs) noexcept
|
|
{
|
|
return lhs.ToStringView() <= ToStringType<CxxYZ::string_view>(rhs);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<int... CHARS, typename T>
|
|
inline bool operator>=(const T &lhs, StringLiteral<CHARS...> rhs) noexcept
|
|
{
|
|
return ToStringType<CxxYZ::string_view>(lhs) >= rhs.ToStringView();
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<int... CHARS, typename T>
|
|
inline bool operator>=(StringLiteral<CHARS...> lhs, const T &rhs) noexcept
|
|
{
|
|
return lhs.ToStringView() >= ToStringType<CxxYZ::string_view>(rhs);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<int... CHARS>
|
|
inline std::size_t hash_value(StringLiteral<CHARS...> stringLiteral)
|
|
{
|
|
return Boost::hash_range(stringLiteral.cbegin(), stringLiteral.cend());
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<int... CHARS>
|
|
std::ostream &operator<<(std::ostream &os, StringLiteral<CHARS...> stringLiteral)
|
|
{
|
|
return operator<<(os, stringLiteral.ToStringView());
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<int... CHARS>
|
|
class IsStringLiteral<StringLiteral<CHARS...>> : public CxxYZ::true_type
|
|
{
|
|
};
|
|
|
|
#else
|
|
|
|
#define ACTIV_MAKE_STRING_LITERAL(str) void
|
|
|
|
#endif // (defined ACTIV_IS_STRING_LITERAL_AVAILABLE)
|
|
|
|
} // namespace Activ
|
|
|
|
#endif // (!defined ACTIV_MISC_STRING_LITERAL_H)
|