util/TemplateString: rewrite as constexpr functions

Hooray C++17!
This commit is contained in:
Max Kellermann 2020-10-23 12:06:39 +02:00 committed by Max Kellermann
parent e7c4e87ac4
commit 02314ac7dd
4 changed files with 89 additions and 87 deletions

View File

@ -103,7 +103,7 @@ public:
template<typename T>
AppendMessageIter &AppendEmptyArray() {
return AppendMessageIter(*this, DBUS_TYPE_ARRAY,
T::TypeAsString::value)
T::as_string.value)
.CloseContainer(*this);
}
@ -120,7 +120,7 @@ public:
AppendMessageIter &AppendVariant(const T &value) {
typedef VariantTypeTraits Traits;
return AppendMessageIter(*this, Traits::TYPE,
Traits::TypeAsString::value)
Traits::as_string.value)
.Append(value)
.CloseContainer(*this);
}
@ -146,7 +146,7 @@ public:
typedef typename W::ContainedTraits ContainedTraits;
return AppendMessageIter(*this, Traits::TYPE,
ContainedTraits::TypeAsString::value)
ContainedTraits::as_string.value)
.Append(value.value)
.CloseContainer(*this);
}
@ -158,7 +158,7 @@ public:
typedef typename W::ContainedTraits ContainedTraits;
return AppendMessageIter(*this, Traits::TYPE,
ContainedTraits::TypeAsString::value)
ContainedTraits::as_string.value)
.AppendFixedArray(value.value)
.CloseContainer(*this);
}

View File

@ -42,7 +42,7 @@ namespace ODBus {
template<int type>
struct BasicTypeTraits {
static constexpr int TYPE = type;
using TypeAsString = TemplateString::CharAsString<TYPE>;
static constexpr auto as_string = TemplateString::CharAsString(TYPE);
};
template<typename T>
@ -73,20 +73,20 @@ struct ArrayTypeTraits {
using ContainedTraits = T;
static constexpr int TYPE = DBUS_TYPE_ARRAY;
using TypeAsString =
TemplateString::InsertBefore<TYPE,
typename ContainedTraits::TypeAsString>;
static constexpr auto as_string =
TemplateString::Concat(TemplateString::CharAsString(TYPE),
ContainedTraits::as_string);
};
template<typename KeyT, typename ValueT>
struct DictEntryTypeTraits {
static constexpr int TYPE = DBUS_TYPE_DICT_ENTRY;
using TypeAsString =
TemplateString::Concat<TemplateString::CharAsString<DBUS_DICT_ENTRY_BEGIN_CHAR>,
typename KeyT::TypeAsString,
typename ValueT::TypeAsString,
TemplateString::CharAsString<DBUS_DICT_ENTRY_END_CHAR>>;
static constexpr auto as_string =
TemplateString::Concat(TemplateString::CharAsString(DBUS_DICT_ENTRY_BEGIN_CHAR),
KeyT::as_string,
ValueT::as_string,
TemplateString::CharAsString(DBUS_DICT_ENTRY_END_CHAR));
};
using VariantTypeTraits = BasicTypeTraits<DBUS_TYPE_VARIANT>;
@ -95,21 +95,25 @@ using VariantTypeTraits = BasicTypeTraits<DBUS_TYPE_VARIANT>;
* Concatenate all TypeAsString members to one string.
*/
template<typename T, typename... ContainedTraits>
struct ConcatTypeAsString
: TemplateString::Concat<typename T::TypeAsString,
ConcatTypeAsString<ContainedTraits...>> {};
struct ConcatTypeAsString {
static constexpr auto as_string =
TemplateString::Concat(T::as_string,
ConcatTypeAsString<ContainedTraits...>::as_string);
};
template<typename T>
struct ConcatTypeAsString<T> : T::TypeAsString {};
struct ConcatTypeAsString<T> {
static constexpr auto as_string = T::as_string;
};
template<typename... ContainedTraits>
struct StructTypeTraits {
static constexpr int TYPE = DBUS_TYPE_STRUCT;
using TypeAsString =
TemplateString::Concat<TemplateString::CharAsString<DBUS_STRUCT_BEGIN_CHAR>,
ConcatTypeAsString<ContainedTraits...>,
TemplateString::CharAsString<DBUS_STRUCT_END_CHAR>>;
static constexpr auto as_string =
TemplateString::Concat(TemplateString::CharAsString(DBUS_STRUCT_BEGIN_CHAR),
ConcatTypeAsString<ContainedTraits...>::as_string,
TemplateString::CharAsString(DBUS_STRUCT_END_CHAR));
};
} /* namespace ODBus */

View File

@ -245,7 +245,7 @@ UdisksNeighborExplorer::HandleMessage(DBusConnection *, DBusMessage *message) no
if (dbus_message_is_signal(message, DBUS_OM_INTERFACE,
"InterfacesAdded") &&
dbus_message_has_signature(message, InterfacesAddedType::value)) {
dbus_message_has_signature(message, InterfacesAddedType::as_string.value)) {
RecurseInterfaceDictEntry(ReadMessageIter(*message), [this](const char *path, auto &&i){
UDisks2::Object o(path);
UDisks2::ParseObject(o, std::forward<decltype(i)>(i));
@ -256,7 +256,7 @@ UdisksNeighborExplorer::HandleMessage(DBusConnection *, DBusMessage *message) no
return DBUS_HANDLER_RESULT_HANDLED;
} else if (dbus_message_is_signal(message, DBUS_OM_INTERFACE,
"InterfacesRemoved") &&
dbus_message_has_signature(message, InterfacesRemovedType::value)) {
dbus_message_has_signature(message, InterfacesRemovedType::as_string.value)) {
Remove(ReadMessageIter(*message).GetString());
return DBUS_HANDLER_RESULT_HANDLED;
} else

View File

@ -30,95 +30,93 @@
#ifndef TEMPLATE_STRING_HXX
#define TEMPLATE_STRING_HXX
#include <array> // for std::size()
#include <cstddef>
namespace TemplateString {
/**
* Construct a null-terminated string from a list of chars.
*/
template<char... _value>
struct Construct {
static constexpr char value[] = {_value..., 0};
static constexpr std::size_t size = sizeof...(_value);
template<std::size_t _size>
struct Buffer {
static constexpr std::size_t size = _size;
char value[size + 1];
};
/**
* An empty string.
*/
struct Empty : Construct<> {};
constexpr auto
Empty() noexcept
{
return Buffer<0>{};
}
/**
* A string consisting of a single character.
*/
template<char ch>
struct CharAsString : Construct<ch> {};
constexpr auto
CharAsString(char ch) noexcept
{
Buffer<1> result{};
result.value[0] = ch;
return result;
}
/**
* Invoke #F, pass all characters in #src from #i to #length
* as variadic arguments.
*/
template<template<char...> class F,
const char *src, std::size_t length, std::size_t i,
char... _value>
struct VariadicChars : VariadicChars<F, src, length - 1, i + 1, _value..., src[i]> {
static_assert(length > 0, "Wrong length");
};
namespace detail {
template<template<char...> class F,
const char *src, std::size_t length,
char... _value>
struct VariadicChars<F, src, 0, length, _value...> : F<_value...> {};
constexpr auto
size(const char &) noexcept
{
return 1;
}
/**
* Like #VariadicChars, but pass an additional argument to #F.
*/
template<template<typename Arg, char...> class F, typename Arg,
const char *src, std::size_t length, std::size_t i,
char... _value>
struct VariadicChars1 : VariadicChars1<F, Arg,
src, length - 1, i + 1, _value..., src[i]> {
static_assert(length > 0, "Wrong length");
};
constexpr const char *
data(const char &ch) noexcept
{
return &ch;
}
template<template<typename Arg, char...> class F, typename Arg,
const char *src, std::size_t length,
char... _value>
struct VariadicChars1<F, Arg, src, 0, length, _value...> : F<Arg, _value...> {};
template<std::size_t s>
constexpr auto
size(const Buffer<s> &b) noexcept
{
return b.size;
}
template<const char *src, std::size_t length, char... value>
struct _BuildString : VariadicChars<Construct, src, length, 0,
value...> {};
template<std::size_t size>
constexpr const char *
data(const Buffer<size> &b) noexcept
{
return b.value;
}
template<char ch, typename S>
struct InsertBefore : _BuildString<S::value, S::size, ch> {};
constexpr char *
copy_n(const char *src, std::size_t n, char *dest) noexcept
{
for (std::size_t i = 0; i < n; ++i)
dest[i] = src[i];
return dest + n;
}
/**
* Concatenate several strings.
*/
template<typename... Args>
struct Concat;
template<typename First, typename Second, typename... Args>
struct _Concat : Concat<Concat<First, Second>, Args...> {};
}
template<typename... Args>
struct Concat : _Concat<Args...> {};
constexpr auto
Concat(Args... args) noexcept
{
using std::size;
using std::data;
using detail::size;
using detail::data;
using detail::copy_n;
template<typename Second, char... _value>
struct _Concat2 : _BuildString<Second::value, Second::size,
_value...> {};
constexpr std::size_t total_size = (std::size_t(0) + ... + size(args));
Buffer<total_size> result{};
template<typename First, typename Second>
struct Concat<First, Second>
:VariadicChars1<_Concat2, Second,
First::value, First::size, 0> {};
char *p = result.value;
((p = copy_n(data(args), size(args), p)), ...);
template<typename First>
struct Concat<First> : First {};
template<>
struct Concat<> : Empty {};
return result;
}
} // namespace TemplateString