MongoDB C++ Driver mongocxx-3.11.0
Loading...
Searching...
No Matches
functor.hpp
Go to the documentation of this file.
1// Copyright 2009-present 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
15#pragma once
16
17#include <functional>
18#include <type_traits>
19
21
22BSONCXX_PRAGMA(
23 message("bsoncxx/util/functor.hpp header is deprecated and will be removed in an upcoming "
24 "major release"))
25
26namespace bsoncxx {
27namespace v_noabi {
28namespace util {
29
30// TODO(MSVC): VS2015U1 Completely falls apart trying to honor the
31// simple definition of is_functor since is_convertible returns the
32// wrong results for std::function, so we fall back to a bunch of
33// other template metaprogramming there.
34
35#if !defined(_MSC_VER)
36
37template <typename FunctionLike, typename Signature>
38using is_functor = std::is_convertible<FunctionLike, std::function<Signature>>;
39
40#else
41
42namespace functor {
43
44template <typename, typename>
45struct build_free_function;
46
47template <typename F, typename R, typename... Args>
48struct build_free_function<F, R(Args...)> {
49 typedef R (*type)(Args...);
50};
51
52template <typename, typename>
53struct build_class_function;
54
55template <typename C, typename R, typename... Args>
56struct build_class_function<C, R(Args...)> {
57 typedef R (C::*type)(Args...);
58};
59
60template <typename>
61struct strip_cv_from_class_function;
62
63template <typename C, typename R, typename... Args>
64struct strip_cv_from_class_function<R (C::*)(Args...)> {
65 typedef R (C::*type)(Args...);
66};
67
68template <typename C, typename R, typename... Args>
69struct strip_cv_from_class_function<R (C::*)(Args...) const> {
70 typedef R (C::*type)(Args...);
71};
72
73template <typename C, typename R, typename... Args>
74struct strip_cv_from_class_function<R (C::*)(Args...) volatile> {
75 typedef R (C::*type)(Args...);
76};
77
78template <typename C, typename S>
79struct is_class_method_with_signature {
80 typedef int yes;
81 typedef char no;
82
83 // T stands for SFINAE
84 template <typename T>
85 static typename std::enable_if<std::is_convertible<typename build_class_function<C, S>::type,
86 typename strip_cv_from_class_function<
87 decltype(&T::operator())>::type>::value,
88 yes>::type
89 sfinae(void*);
90
91 template <typename>
92 static no sfinae(...);
93
94 static bool constexpr value = sizeof(sfinae<C>(nullptr)) == sizeof(yes);
95};
96
97template <typename F, typename S>
98struct is_function_with_signature
99 : std::is_convertible<F, typename build_free_function<F, S>::type> {};
100
101template <typename C, typename S, bool>
102struct is_functor_impl : is_class_method_with_signature<C, S> {};
103
104template <typename F, typename S>
105struct is_functor_impl<F, S, false> : is_function_with_signature<F, S> {};
106
107} // namespace functor
108
109template <typename C, typename S>
110struct is_functor : functor::is_functor_impl<C, S, std::is_class<C>::value> {};
111
112#endif
113
114} // namespace util
115} // namespace v_noabi
116} // namespace bsoncxx
117
119
The bsoncxx macro guard postlude header.
The bsoncxx macro guard prelude header.
BSONCXX_DEPRECATED typedef types::bson_value::view value
Equivalent to bsoncxx::v_noabi::types::bson_value::view.
Definition value.hpp:30
The top-level namespace within which all bsoncxx library entities are declared.