From f1ffe1e7a3e81c07a07a8416ce307e4413ec8fbc Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Sun, 1 Jul 2018 22:48:38 +0900 Subject: Event types system remade to be extensible There were two common points that had to be updated every time a new event is introduced: the EventType enumeration and one of 3 doMakeEvent<> specialisations. The new code has a template class, EventFactory<>, that uses a list of static factory methods to create events instead of typelists used in doMakeEvent<>(); the EventType enumeration is replaced with a namespace populated with constants as necessary. In general, EventType is considered a deprecated mechanism altogether; instead, a set of facilities is provided: is<>() to check if an event has a certain type (to replace comparison against an EventType value) and visit<>() to execute actions based on the event type (replacing switch statements over EventType values). Closes #129. --- lib/util.h | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'lib/util.h') diff --git a/lib/util.h b/lib/util.h index d6e1cef6..28315429 100644 --- a/lib/util.h +++ b/lib/util.h @@ -59,6 +59,42 @@ namespace QMatrixClient return std::unique_ptr(static_cast(p.release())); } + /** Determine traits of an arbitrary function/lambda/functor + * This only works with arity of 1 (1-argument) for now but is extendable + * to other cases. Also, doesn't work with generic lambdas and function + * objects that have operator() overloaded + * \sa https://stackoverflow.com/questions/7943525/is-it-possible-to-figure-out-the-parameter-type-and-return-type-of-a-lambda#7943765 + */ + template + struct function_traits : public function_traits + { }; // A generic function object that has (non-overloaded) operator() + + // Specialisation for a function + template + struct function_traits + { + using return_type = ReturnT; + using arg_type = ArgT; + }; + + // Specialisation for a member function + template + struct function_traits + : function_traits + { }; + + // Specialisation for a const member function + template + struct function_traits + : function_traits + { }; + + template + using fn_return_t = typename function_traits::return_type; + + template + using fn_arg_t = typename function_traits::arg_type; + #if QT_VERSION < QT_VERSION_CHECK(5, 7, 0) // Copy-pasted from Qt 5.10 template @@ -68,6 +104,11 @@ namespace QMatrixClient static void qAsConst(const T &&) Q_DECL_EQ_DELETE; #endif + inline auto operator"" _ls(const char* s, std::size_t size) + { + return QLatin1String(s, int(size)); + } + /** An abstraction over a pair of iterators * This is a very basic range type over a container with iterators that * are at least ForwardIterators. Inspired by Ranges TS. -- cgit v1.2.3