functor.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 2014 MongoDB Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #pragma once
  15. #include <functional>
  16. #include <type_traits>
  17. #include <bsoncxx/config/prelude.hpp>
  18. namespace bsoncxx {
  19. BSONCXX_INLINE_NAMESPACE_BEGIN
  20. namespace util {
  21. // TODO(MSVC): VS2015U1 Completely falls apart trying to honor the
  22. // simple definition of is_functor since is_convertible returns the
  23. // wrong results for std::function, so we fall back to a bunch of
  24. // other template metaprogramming there.
  25. #if !defined(_MSC_VER)
  26. template <typename FunctionLike, typename Signature>
  27. using is_functor = std::is_convertible<FunctionLike, std::function<Signature>>;
  28. #else
  29. namespace functor {
  30. template <typename, typename>
  31. struct build_free_function;
  32. template <typename F, typename R, typename... Args>
  33. struct build_free_function<F, R(Args...)> {
  34. typedef R (*type)(Args...);
  35. };
  36. template <typename, typename>
  37. struct build_class_function;
  38. template <typename C, typename R, typename... Args>
  39. struct build_class_function<C, R(Args...)> {
  40. typedef R (C::*type)(Args...);
  41. };
  42. template <typename>
  43. struct strip_cv_from_class_function;
  44. template <typename C, typename R, typename... Args>
  45. struct strip_cv_from_class_function<R (C::*)(Args...)> {
  46. typedef R (C::*type)(Args...);
  47. };
  48. template <typename C, typename R, typename... Args>
  49. struct strip_cv_from_class_function<R (C::*)(Args...) const> {
  50. typedef R (C::*type)(Args...);
  51. };
  52. template <typename C, typename R, typename... Args>
  53. struct strip_cv_from_class_function<R (C::*)(Args...) volatile> {
  54. typedef R (C::*type)(Args...);
  55. };
  56. template <typename C, typename S>
  57. struct is_class_method_with_signature {
  58. typedef int yes;
  59. typedef char no;
  60. // T stands for SFINAE
  61. template <typename T>
  62. static typename std::enable_if<std::is_convertible<typename build_class_function<C, S>::type,
  63. typename strip_cv_from_class_function<
  64. decltype(&T::operator())>::type>::value,
  65. yes>::type
  66. sfinae(void*);
  67. template <typename>
  68. static no sfinae(...);
  69. static bool constexpr value = sizeof(sfinae<C>(nullptr)) == sizeof(yes);
  70. };
  71. template <typename F, typename S>
  72. struct is_function_with_signature
  73. : std::is_convertible<F, typename build_free_function<F, S>::type> {};
  74. template <typename C, typename S, bool>
  75. struct is_functor_impl : is_class_method_with_signature<C, S> {};
  76. template <typename F, typename S>
  77. struct is_functor_impl<F, S, false> : is_function_with_signature<F, S> {};
  78. } // namespace functor
  79. template <typename C, typename S>
  80. struct is_functor : functor::is_functor_impl<C, S, std::is_class<C>::value> {};
  81. #endif
  82. } // namespace util
  83. BSONCXX_INLINE_NAMESPACE_END
  84. } // namespace bsoncxx
  85. #include <bsoncxx/config/postlude.hpp>