|
| 1 | +/* |
| 2 | +* Copyright (C) 2019 YY Inc. All rights reserved. |
| 3 | +* |
| 4 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +* you may not use this file except in compliance with the License. |
| 6 | +* You may obtain a copy of the License at |
| 7 | +* |
| 8 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +* |
| 10 | +* Unless required by applicable law or agreed to in writing, |
| 11 | +* software distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +* See the License for the specific language governing permissions and |
| 14 | +* limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | + |
| 18 | +#ifndef __X_TO_TRAITS_H |
| 19 | +#define __X_TO_TRAITS_H |
| 20 | + |
| 21 | +#if __GXX_EXPERIMENTAL_CXX0X__ || _MSC_VER>=1700 |
| 22 | +#include <type_traits> |
| 23 | +#endif |
| 24 | + |
| 25 | +using namespace std; |
| 26 | + |
| 27 | +#if __GXX_EXPERIMENTAL_CXX0X__ || _MSC_VER>=1700 |
| 28 | +//template <class T> |
| 29 | +//using x_for_class = typename std::enable_if<std::is_class<T>::value, int>::type; |
| 30 | + |
| 31 | +//template <class T> |
| 32 | +//using x_for_enum = typename std::enable_if<std::is_enum<T>::value, int>::type; |
| 33 | + |
| 34 | +// <c++11 not support using template. so we use macro all |
| 35 | +#define x_for_class(T) typename std::enable_if<std::is_class<T>::value, int>::type |
| 36 | +#define x_for_enum(T) typename std::enable_if<std::is_enum<T>::value, int>::type |
| 37 | +#else |
| 38 | +template<bool B, class T = void> |
| 39 | +struct x_enable_if {}; |
| 40 | + |
| 41 | +template <class T> |
| 42 | +struct x_enable_if<true, T> { typedef T type; }; |
| 43 | + |
| 44 | + |
| 45 | +template <class T> |
| 46 | +struct x_tovoid {typedef void type;}; |
| 47 | + |
| 48 | +template <class T, typename=void> |
| 49 | +struct x_is_class{static bool const value = false;}; |
| 50 | + |
| 51 | +template <class T> |
| 52 | +struct x_is_class<T, typename x_tovoid<int T::*>::type >{static bool const value = true;}; |
| 53 | + |
| 54 | + |
| 55 | +#define x_for_class(T) typename x_enable_if<x_is_class<T>::value, int>::type |
| 56 | +// too complicated to implement is_enum |
| 57 | +#define x_for_enum(T) typename x_enable_if<!x_is_class<T>::value, int>::type |
| 58 | + |
| 59 | +#endif |
| 60 | + |
| 61 | +#endif |
| 62 | + |
0 commit comments