LeechCraft  0.6.70-15082-g543737046d
Modular cross-platform feature rich live environment.
introspectable.h
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Distributed under the Boost Software License, Version 1.0.
6  * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7  **********************************************************************/
8 
9 #pragma once
10 
11 #include <functional>
12 #include <QHash>
13 #include <QMetaType>
14 #include <QVariantMap>
15 #include "xpcconfig.h"
16 
17 namespace LC::Util
18 {
19  using Introspect_f = std::function<QVariantMap (QVariant)>;
20 
21  class UTIL_XPC_API Introspectable
22  {
23  QHash<int, Introspect_f> Intros_;
24 
25  Introspectable () = default;
26  public:
27  Introspectable (const Introspectable&) = delete;
28  Introspectable& operator= (const Introspectable&) = delete;
29 
30  static Introspectable& Instance ();
31 
32  template<typename T, typename U>
33  void Register (const U& intro)
34  {
35  if constexpr (std::is_invocable_v<U, QVariant>)
36  {
37  const auto id = qMetaTypeId<T> ();
38  Intros_ [id] = intro;
39  }
40  else
41  Register<T> ([intro] (const QVariant& var) { return std::invoke (intro, var.value<T> ()); });
42  }
43 
44  template<typename T>
45  QVariantMap operator() (const T& t) const
46  {
47  return (*this) (QVariant::fromValue<T> (t));
48  }
49 
50  QVariantMap operator() (const QVariant&) const;
51  };
52 }
xpcconfig.h
LC::Util
Definition: icoreproxy.h:33
LC::Util::Introspectable
Definition: introspectable.h:27
LC::Util::Introspect_f
std::function< QVariantMap(QVariant)> Introspect_f
Definition: introspectable.h:25
UTIL_XPC_API
#define UTIL_XPC_API
Definition: xpcconfig.h:16