Skip to content

Commit f2eb992

Browse files
committed
optimize any to accept a normal function without function pointer; optimize template is_basic_type<T>.
1 parent b6e88a9 commit f2eb992

File tree

3 files changed

+11
-30
lines changed

3 files changed

+11
-30
lines changed

example/test0.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ Promise run(Promise &next){
9191
}).fail([](uint64_t n) {
9292
output_func_name();
9393
printf("n = %d\n", (int)n);
94-
}).then(&test1)
95-
.then(&test2)
94+
}).then(test1)
95+
.then(test2)
9696
.then(&test3);
9797
//.always([]() {
9898
// output_func_name();

include/any.hpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,16 @@ class any {
5454
}
5555

5656
template<typename ValueType>
57-
any(const ValueType & value)
57+
any(const ValueType &value)
5858
: content(new holder<ValueType>(value)) {
5959
}
6060

61-
any(const any & other)
61+
template<typename RET, typename ...ARG>
62+
any(RET value(ARG...))
63+
: content(new holder<RET (*)(ARG...)>(value)) {
64+
}
65+
66+
any(const any &other)
6267
: content(other.content ? other.content->clone() : 0) {
6368
}
6469

include/call_traits.hpp

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ namespace promise {
4747
template<typename T,
4848
bool is_basic_type = (std::is_void<T>::value
4949
|| std::is_fundamental<T>::value
50-
|| std::is_pointer<T>::value
50+
|| (std::is_pointer<T>::value && !std::is_function<typename std::remove_pointer<T>::type>::value)
5151
|| std::is_union<T>::value
5252
|| std::is_enum<T>::value
5353
|| std::is_array<T>::value) && !std::is_function<T>::value>
@@ -141,7 +141,7 @@ struct call_traits_impl<RET(T::*)(ARG...), false> {
141141
};
142142

143143
template<typename RET, class T, typename ...ARG>
144-
struct call_traits_impl<RET(T:: *)(ARG...) const, false> {
144+
struct call_traits_impl<RET(T::*)(ARG...) const, false> {
145145
static const bool is_callable = true;
146146
typedef std::function<RET(ARG...)> fun_type;
147147
typedef RET result_type;
@@ -183,30 +183,6 @@ struct call_traits_impl<RET(ARG...), false> {
183183
}
184184
};
185185

186-
template<typename RET, typename ...ARG>
187-
struct call_traits_impl<RET(*)(ARG...), true> {
188-
static const bool is_callable = true;
189-
typedef std::function<RET(ARG...)> fun_type;
190-
typedef RET result_type;
191-
typedef std::tuple<ARG...> argument_type;
192-
193-
static fun_type to_std_function(RET(*func)(ARG...)) {
194-
return func;
195-
}
196-
};
197-
198-
template<typename RET, typename ...ARG>
199-
struct call_traits_impl<RET(ARG...), true> {
200-
static const bool is_callable = true;
201-
typedef std::function<RET(ARG...)> fun_type;
202-
typedef RET result_type;
203-
typedef std::tuple<ARG...> argument_type;
204-
205-
static fun_type to_std_function(RET(*func)(ARG...)) {
206-
return func;
207-
}
208-
};
209-
210186
template<typename T>
211187
struct call_traits_impl<T, false> {
212188
static const bool is_callable = has_operator_parentheses<T>::type::value;

0 commit comments

Comments
 (0)