Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 47 additions & 10 deletions Framework/Core/include/Framework/StringHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <cstdint>
#include <utility>
#include <type_traits>
#include <string_view>

// CRC32 Table (zlib polynomial) static
constexpr uint32_t crc_table[256] = {0x0L, 0x77073096L, 0xee0e612cL,
Expand Down Expand Up @@ -102,6 +103,15 @@ constexpr bool is_const_str_v(T)
return is_const_str<T>::value;
}

template <char... chars1, char... chars2>
constexpr auto operator+(const ConstStr<chars1...>&, const ConstStr<chars2...>&)
{
return ConstStr<chars1..., chars2...>{};
}

namespace const_str_details
{

template <typename T, std::size_t... Is>
constexpr auto as_chars_impl(std::index_sequence<Is...>)
{
Expand All @@ -112,18 +122,45 @@ template <typename T>
constexpr auto as_chars()
{
return as_chars_impl<T>(
std::make_index_sequence<sizeof(T::str()) - 1>{});
std::make_index_sequence<T::size() - 1>{});
}

template <int N>
constexpr const char* const get_str(const char (&str)[N])
{
return str;
}

template <int N>
constexpr const int get_size(const char (&str)[N])
{
return N;
}

constexpr const char* const get_str(const std::string_view& str)
{
return str.data();
}

constexpr const int get_size(const std::string_view& str)
{
return str.size() + 1;
}
} // namespace const_str_details

#define CONST_STR(literal) \
[] { \
struct literal_to_chars { \
static constexpr decltype(auto) str() \
{ \
return literal; \
} \
}; \
return as_chars<literal_to_chars>(); \
#define CONST_STR(literal) \
[] { \
struct literal_to_chars { \
static constexpr decltype(auto) str() \
{ \
return const_str_details::get_str(literal); \
} \
static constexpr decltype(auto) size() \
{ \
return const_str_details::get_size(literal); \
} \
}; \
return const_str_details::as_chars<literal_to_chars>(); \
}()

#endif // O2_FRAMEWORK_STRINGHELPERS_H
18 changes: 18 additions & 0 deletions Framework/Core/test/test_StringHelpers.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,22 @@ BOOST_AUTO_TEST_CASE(StringHelpersConstStr)
if constexpr (is_const_str_v(myConstStr)) {
std::cout << "myConstStr is a compile-time string" << std::endl;
}

auto myConstStr2 = CONST_STR("hello") + CONST_STR("Universe");
printString(myConstStr2);
static_assert(std::is_same_v<decltype(myConstStr2), ConstStr<'h', 'e', 'l', 'l', 'o', 'U', 'n', 'i', 'v', 'e', 'r', 's', 'e'>>);

enum ParticleSpecies {
kPion,
kKaon
};
static constexpr std::string_view hist[] = {"ptDist", "etaDist"};
static constexpr std::string_view particleSuffix[] = {"_pions", "_kaons"};

printString(CONST_STR(hist[0]) + CONST_STR(particleSuffix[kPion]));
printString(CONST_STR(hist[0]) + CONST_STR(particleSuffix[kKaon]));
printString(CONST_STR(hist[1]) + CONST_STR(particleSuffix[kPion]));
printString(CONST_STR(hist[1]) + CONST_STR(particleSuffix[kKaon]));

BOOST_CHECK_EQUAL(CONST_STR(hist[0]).hash, CONST_STR("ptDist").hash);
}