LeechCraft  0.6.70-18450-gabe19ee3b0
Modular cross-platform feature rich live environment.
qobjectrefcast.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 <concepts>
12 #include <source_location>
13 #include <stdexcept>
14 #include <QObject>
15 #include "sllconfig.h"
16 
17 namespace LC::Util
18 {
19  struct BadQObjectCast : std::runtime_error
20  {
21  using std::runtime_error::runtime_error;
22  };
23 
24  namespace detail
25  {
26  [[noreturn, gnu::cold, gnu::noinline]]
27  void UTIL_SLL_API NotifyCastError (const QObject *object, const char *target, const std::source_location& loc);
28  }
29 }
30 
31 namespace LC
32 {
33  template<typename Target, std::derived_from<QObject> Src>
34  requires (!std::is_pointer_v<Target>)
35  decltype (auto) qobject_ref_cast (Src *obj, std::source_location loc = std::source_location::current ())
36  {
37  if (const auto result = qobject_cast<Target*> (obj))
38  return *result;
39 
40  Util::detail::NotifyCastError (obj, typeid (Target).name (), loc);
41  }
42 
43  template<typename Target, std::derived_from<QObject> Src>
44  requires (!std::is_pointer_v<Target>)
45  decltype (auto) qobject_ref_cast (Src& obj, std::source_location loc = std::source_location::current ())
46  {
47  return qobject_ref_cast<Target> (&obj, loc);
48  }
49 }
#define UTIL_SLL_API
Definition: sllconfig.h:16
std::source_location loc
STL namespace.
void NotifyCastError(const QObject *object, const char *target, const std::source_location &loc)
requires(!std::is_pointer_v< Target >) decltype(auto) qobject_ref_cast(Src *obj