LeechCraft  0.6.70-15082-g543737046d
Modular cross-platform feature rich live environment.
functional.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 <type_traits>
12 
13 namespace LC
14 {
15 namespace Util
16 {
29  template<typename R, typename B, typename C, typename... Args>
30  auto BindMemFn (R (B::*fn) (Args...), C *c)
31  {
32  static_assert (std::is_base_of<B, C> {}, "Base class where the member pointer belongs must be convertible to the binded object's class.");
33  return [fn, c] (Args... args) { return (c->*fn) (args...); };
34  }
35 
36  template<typename R, typename B, typename C, typename... Args>
37  auto BindMemFn (R (B::*fn) (Args...) const, const C *c)
38  {
39  static_assert (std::is_base_of<B, C> {}, "Base class where the member pointer belongs must be convertible to the binded object's class.");
40  return [fn, c] (Args... args) { return (c->*fn) (args...); };
41  }
42 
43  template<typename To>
44  struct Caster
45  {
46  template<typename From>
47  std::enable_if_t<!std::is_base_of<To, std::decay_t<From>>::value, To> operator() (From&& from) const
48  {
49  return To { std::forward<From> (from) };
50  }
51 
52  template<typename From>
53  std::enable_if_t<std::is_base_of<To, std::decay_t<From>>::value, To> operator() (From&& from) const
54  {
55  return from;
56  }
57  };
58 
59  template<typename To>
60  struct Upcaster;
61 
62  template<typename To>
63  struct Upcaster<To*>
64  {
65  template<typename From, typename = std::enable_if_t<std::is_base_of_v<To, std::decay_t<From>>>>
66  To* operator() (From *from) const
67  {
68  return from;
69  }
70  };
71 
72  template<typename To>
73  constexpr auto Upcast = Upcaster<To> {};
74 }
75 }
LC::Util::Upcaster
Definition: functional.h:72
LC::Util::BindMemFn
auto BindMemFn(R(B::*fn)(Args...), C *c)
Binds an instance of an object to its member function.
Definition: functional.h:42
LC::Util::Upcast
constexpr auto Upcast
Definition: functional.h:85
LC::Util::Caster
Definition: functional.h:56
LC
Definition: constants.h:14
LC::Util::Caster::operator()
std::enable_if_t<!std::is_base_of< To, std::decay_t< From > >::value, To > operator()(From &&from) const
Definition: functional.h:59