QtService  1.1.0
A platform independent library to easily create system services and use some of their features
qtservice_helpertypes.h
1 #ifndef QTSERVICE_HELPERTYPES_H
2 #define QTSERVICE_HELPERTYPES_H
3 
4 #include <functional>
5 #include <utility>
6 
7 #include <QtCore/qvariant.h>
8 
9 namespace QtService {
10 namespace __helpertypes {
11 
12 template <typename T>
13 struct fn_info : public fn_info<decltype(&T::operator())> {};
14 
15 template <typename TClass, typename TRet, typename... TArgs>
16 struct fn_info<TRet(TClass::*)(TArgs...) const>
17 {
18  template <typename TFunctor>
19  static inline std::function<QVariant(QVariantList)> pack(const TFunctor &fn) {
20  return pack(std::make_index_sequence<sizeof...(TArgs)>{}, fn);
21  }
22 
23  template <typename TFunctor, std::size_t... Is>
24  static inline std::function<QVariant(QVariantList)> pack(const std::index_sequence<Is...> &, const TFunctor &fn) {
25  return [fn](const QVariantList &args) {
26  Q_UNUSED(args)
27  return QVariant::fromValue<TRet>(fn(args[Is].template value<std::decay_t<TArgs>>()...));
28  };
29  }
30 };
31 
32 template <typename TClass, typename... TArgs>
33 struct fn_info<void(TClass::*)(TArgs...) const>
34 {
35  template <typename TFunctor>
36  static inline std::function<QVariant(QVariantList)> pack(const TFunctor &fn) {
37  return pack(std::make_index_sequence<sizeof...(TArgs)>{}, fn);
38  }
39 
40  template <typename TFunctor, std::size_t... Is>
41  static inline std::function<QVariant(QVariantList)> pack(const std::index_sequence<Is...> &, const TFunctor &fn) {
42  return [fn](const QVariantList &args) {
43  Q_UNUSED(args)
44  fn(args[Is].template value<std::decay_t<TArgs>>()...);
45  return QVariant{};
46  };
47  }
48 };
49 
50 template <typename TFunc>
51 inline std::function<QVariant(QVariantList)> pack_function(const TFunc &fn) {
52  return fn_info<TFunc>::pack(fn);
53 }
54 
55 }
56 }
57 #endif // QTSERVICE_HELPERTYPES_H
The primary namespace of the QtService library.