util/TemplateString: rewrite as constexpr functions
Hooray C++17!
This commit is contained in:
parent
e7c4e87ac4
commit
02314ac7dd
@ -103,7 +103,7 @@ public:
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
AppendMessageIter &AppendEmptyArray() {
|
AppendMessageIter &AppendEmptyArray() {
|
||||||
return AppendMessageIter(*this, DBUS_TYPE_ARRAY,
|
return AppendMessageIter(*this, DBUS_TYPE_ARRAY,
|
||||||
T::TypeAsString::value)
|
T::as_string.value)
|
||||||
.CloseContainer(*this);
|
.CloseContainer(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,7 +120,7 @@ public:
|
|||||||
AppendMessageIter &AppendVariant(const T &value) {
|
AppendMessageIter &AppendVariant(const T &value) {
|
||||||
typedef VariantTypeTraits Traits;
|
typedef VariantTypeTraits Traits;
|
||||||
return AppendMessageIter(*this, Traits::TYPE,
|
return AppendMessageIter(*this, Traits::TYPE,
|
||||||
Traits::TypeAsString::value)
|
Traits::as_string.value)
|
||||||
.Append(value)
|
.Append(value)
|
||||||
.CloseContainer(*this);
|
.CloseContainer(*this);
|
||||||
}
|
}
|
||||||
@ -146,7 +146,7 @@ public:
|
|||||||
typedef typename W::ContainedTraits ContainedTraits;
|
typedef typename W::ContainedTraits ContainedTraits;
|
||||||
|
|
||||||
return AppendMessageIter(*this, Traits::TYPE,
|
return AppendMessageIter(*this, Traits::TYPE,
|
||||||
ContainedTraits::TypeAsString::value)
|
ContainedTraits::as_string.value)
|
||||||
.Append(value.value)
|
.Append(value.value)
|
||||||
.CloseContainer(*this);
|
.CloseContainer(*this);
|
||||||
}
|
}
|
||||||
@ -158,7 +158,7 @@ public:
|
|||||||
typedef typename W::ContainedTraits ContainedTraits;
|
typedef typename W::ContainedTraits ContainedTraits;
|
||||||
|
|
||||||
return AppendMessageIter(*this, Traits::TYPE,
|
return AppendMessageIter(*this, Traits::TYPE,
|
||||||
ContainedTraits::TypeAsString::value)
|
ContainedTraits::as_string.value)
|
||||||
.AppendFixedArray(value.value)
|
.AppendFixedArray(value.value)
|
||||||
.CloseContainer(*this);
|
.CloseContainer(*this);
|
||||||
}
|
}
|
||||||
|
@ -42,7 +42,7 @@ namespace ODBus {
|
|||||||
template<int type>
|
template<int type>
|
||||||
struct BasicTypeTraits {
|
struct BasicTypeTraits {
|
||||||
static constexpr int TYPE = type;
|
static constexpr int TYPE = type;
|
||||||
using TypeAsString = TemplateString::CharAsString<TYPE>;
|
static constexpr auto as_string = TemplateString::CharAsString(TYPE);
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@ -73,20 +73,20 @@ struct ArrayTypeTraits {
|
|||||||
using ContainedTraits = T;
|
using ContainedTraits = T;
|
||||||
|
|
||||||
static constexpr int TYPE = DBUS_TYPE_ARRAY;
|
static constexpr int TYPE = DBUS_TYPE_ARRAY;
|
||||||
using TypeAsString =
|
static constexpr auto as_string =
|
||||||
TemplateString::InsertBefore<TYPE,
|
TemplateString::Concat(TemplateString::CharAsString(TYPE),
|
||||||
typename ContainedTraits::TypeAsString>;
|
ContainedTraits::as_string);
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename KeyT, typename ValueT>
|
template<typename KeyT, typename ValueT>
|
||||||
struct DictEntryTypeTraits {
|
struct DictEntryTypeTraits {
|
||||||
static constexpr int TYPE = DBUS_TYPE_DICT_ENTRY;
|
static constexpr int TYPE = DBUS_TYPE_DICT_ENTRY;
|
||||||
|
|
||||||
using TypeAsString =
|
static constexpr auto as_string =
|
||||||
TemplateString::Concat<TemplateString::CharAsString<DBUS_DICT_ENTRY_BEGIN_CHAR>,
|
TemplateString::Concat(TemplateString::CharAsString(DBUS_DICT_ENTRY_BEGIN_CHAR),
|
||||||
typename KeyT::TypeAsString,
|
KeyT::as_string,
|
||||||
typename ValueT::TypeAsString,
|
ValueT::as_string,
|
||||||
TemplateString::CharAsString<DBUS_DICT_ENTRY_END_CHAR>>;
|
TemplateString::CharAsString(DBUS_DICT_ENTRY_END_CHAR));
|
||||||
};
|
};
|
||||||
|
|
||||||
using VariantTypeTraits = BasicTypeTraits<DBUS_TYPE_VARIANT>;
|
using VariantTypeTraits = BasicTypeTraits<DBUS_TYPE_VARIANT>;
|
||||||
@ -95,21 +95,25 @@ using VariantTypeTraits = BasicTypeTraits<DBUS_TYPE_VARIANT>;
|
|||||||
* Concatenate all TypeAsString members to one string.
|
* Concatenate all TypeAsString members to one string.
|
||||||
*/
|
*/
|
||||||
template<typename T, typename... ContainedTraits>
|
template<typename T, typename... ContainedTraits>
|
||||||
struct ConcatTypeAsString
|
struct ConcatTypeAsString {
|
||||||
: TemplateString::Concat<typename T::TypeAsString,
|
static constexpr auto as_string =
|
||||||
ConcatTypeAsString<ContainedTraits...>> {};
|
TemplateString::Concat(T::as_string,
|
||||||
|
ConcatTypeAsString<ContainedTraits...>::as_string);
|
||||||
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
struct ConcatTypeAsString<T> : T::TypeAsString {};
|
struct ConcatTypeAsString<T> {
|
||||||
|
static constexpr auto as_string = T::as_string;
|
||||||
|
};
|
||||||
|
|
||||||
template<typename... ContainedTraits>
|
template<typename... ContainedTraits>
|
||||||
struct StructTypeTraits {
|
struct StructTypeTraits {
|
||||||
static constexpr int TYPE = DBUS_TYPE_STRUCT;
|
static constexpr int TYPE = DBUS_TYPE_STRUCT;
|
||||||
|
|
||||||
using TypeAsString =
|
static constexpr auto as_string =
|
||||||
TemplateString::Concat<TemplateString::CharAsString<DBUS_STRUCT_BEGIN_CHAR>,
|
TemplateString::Concat(TemplateString::CharAsString(DBUS_STRUCT_BEGIN_CHAR),
|
||||||
ConcatTypeAsString<ContainedTraits...>,
|
ConcatTypeAsString<ContainedTraits...>::as_string,
|
||||||
TemplateString::CharAsString<DBUS_STRUCT_END_CHAR>>;
|
TemplateString::CharAsString(DBUS_STRUCT_END_CHAR));
|
||||||
};
|
};
|
||||||
|
|
||||||
} /* namespace ODBus */
|
} /* namespace ODBus */
|
||||||
|
@ -245,7 +245,7 @@ UdisksNeighborExplorer::HandleMessage(DBusConnection *, DBusMessage *message) no
|
|||||||
|
|
||||||
if (dbus_message_is_signal(message, DBUS_OM_INTERFACE,
|
if (dbus_message_is_signal(message, DBUS_OM_INTERFACE,
|
||||||
"InterfacesAdded") &&
|
"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){
|
RecurseInterfaceDictEntry(ReadMessageIter(*message), [this](const char *path, auto &&i){
|
||||||
UDisks2::Object o(path);
|
UDisks2::Object o(path);
|
||||||
UDisks2::ParseObject(o, std::forward<decltype(i)>(i));
|
UDisks2::ParseObject(o, std::forward<decltype(i)>(i));
|
||||||
@ -256,7 +256,7 @@ UdisksNeighborExplorer::HandleMessage(DBusConnection *, DBusMessage *message) no
|
|||||||
return DBUS_HANDLER_RESULT_HANDLED;
|
return DBUS_HANDLER_RESULT_HANDLED;
|
||||||
} else if (dbus_message_is_signal(message, DBUS_OM_INTERFACE,
|
} else if (dbus_message_is_signal(message, DBUS_OM_INTERFACE,
|
||||||
"InterfacesRemoved") &&
|
"InterfacesRemoved") &&
|
||||||
dbus_message_has_signature(message, InterfacesRemovedType::value)) {
|
dbus_message_has_signature(message, InterfacesRemovedType::as_string.value)) {
|
||||||
Remove(ReadMessageIter(*message).GetString());
|
Remove(ReadMessageIter(*message).GetString());
|
||||||
return DBUS_HANDLER_RESULT_HANDLED;
|
return DBUS_HANDLER_RESULT_HANDLED;
|
||||||
} else
|
} else
|
||||||
|
@ -30,95 +30,93 @@
|
|||||||
#ifndef TEMPLATE_STRING_HXX
|
#ifndef TEMPLATE_STRING_HXX
|
||||||
#define TEMPLATE_STRING_HXX
|
#define TEMPLATE_STRING_HXX
|
||||||
|
|
||||||
|
#include <array> // for std::size()
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
|
||||||
namespace TemplateString {
|
namespace TemplateString {
|
||||||
|
|
||||||
/**
|
template<std::size_t _size>
|
||||||
* Construct a null-terminated string from a list of chars.
|
struct Buffer {
|
||||||
*/
|
static constexpr std::size_t size = _size;
|
||||||
template<char... _value>
|
char value[size + 1];
|
||||||
struct Construct {
|
|
||||||
static constexpr char value[] = {_value..., 0};
|
|
||||||
static constexpr std::size_t size = sizeof...(_value);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An empty string.
|
* An empty string.
|
||||||
*/
|
*/
|
||||||
struct Empty : Construct<> {};
|
constexpr auto
|
||||||
|
Empty() noexcept
|
||||||
|
{
|
||||||
|
return Buffer<0>{};
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A string consisting of a single character.
|
* A string consisting of a single character.
|
||||||
*/
|
*/
|
||||||
template<char ch>
|
constexpr auto
|
||||||
struct CharAsString : Construct<ch> {};
|
CharAsString(char ch) noexcept
|
||||||
|
{
|
||||||
|
Buffer<1> result{};
|
||||||
|
result.value[0] = ch;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
namespace detail {
|
||||||
* 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");
|
|
||||||
};
|
|
||||||
|
|
||||||
template<template<char...> class F,
|
constexpr auto
|
||||||
const char *src, std::size_t length,
|
size(const char &) noexcept
|
||||||
char... _value>
|
{
|
||||||
struct VariadicChars<F, src, 0, length, _value...> : F<_value...> {};
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
constexpr const char *
|
||||||
* Like #VariadicChars, but pass an additional argument to #F.
|
data(const char &ch) noexcept
|
||||||
*/
|
{
|
||||||
template<template<typename Arg, char...> class F, typename Arg,
|
return &ch;
|
||||||
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");
|
|
||||||
};
|
|
||||||
|
|
||||||
template<template<typename Arg, char...> class F, typename Arg,
|
template<std::size_t s>
|
||||||
const char *src, std::size_t length,
|
constexpr auto
|
||||||
char... _value>
|
size(const Buffer<s> &b) noexcept
|
||||||
struct VariadicChars1<F, Arg, src, 0, length, _value...> : F<Arg, _value...> {};
|
{
|
||||||
|
return b.size;
|
||||||
|
}
|
||||||
|
|
||||||
template<const char *src, std::size_t length, char... value>
|
template<std::size_t size>
|
||||||
struct _BuildString : VariadicChars<Construct, src, length, 0,
|
constexpr const char *
|
||||||
value...> {};
|
data(const Buffer<size> &b) noexcept
|
||||||
|
{
|
||||||
|
return b.value;
|
||||||
|
}
|
||||||
|
|
||||||
template<char ch, typename S>
|
constexpr char *
|
||||||
struct InsertBefore : _BuildString<S::value, S::size, ch> {};
|
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>
|
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>
|
constexpr std::size_t total_size = (std::size_t(0) + ... + size(args));
|
||||||
struct _Concat2 : _BuildString<Second::value, Second::size,
|
Buffer<total_size> result{};
|
||||||
_value...> {};
|
|
||||||
|
|
||||||
template<typename First, typename Second>
|
char *p = result.value;
|
||||||
struct Concat<First, Second>
|
((p = copy_n(data(args), size(args), p)), ...);
|
||||||
:VariadicChars1<_Concat2, Second,
|
|
||||||
First::value, First::size, 0> {};
|
|
||||||
|
|
||||||
template<typename First>
|
return result;
|
||||||
struct Concat<First> : First {};
|
}
|
||||||
|
|
||||||
template<>
|
|
||||||
struct Concat<> : Empty {};
|
|
||||||
|
|
||||||
} // namespace TemplateString
|
} // namespace TemplateString
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user