traits.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright (C) 2021 Duowan 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. #ifndef __X_PACK_TRAITS_H
  17. #define __X_PACK_TRAITS_H
  18. #if __GXX_EXPERIMENTAL_CXX0X__ || _MSC_VER>=1700
  19. #define X_PACK_SUPPORT_CXX0X 1
  20. #endif
  21. namespace xpack {
  22. // implement std::enable_if
  23. template<bool B, class T = void>
  24. struct x_enable_if {};
  25. template <class T>
  26. struct x_enable_if<true, T> { typedef T type; };
  27. // mark XPACK_OUT
  28. template <class T>
  29. struct is_xpack_out{static bool const value = false;};
  30. // for bitfield, declare raw type. thx https://stackoverflow.com/a/12199635/5845104
  31. template<int N> struct x_size { char value[N]; };
  32. x_size<1> x_decltype_encode(char);
  33. x_size<2> x_decltype_encode(signed char);
  34. x_size<3> x_decltype_encode(unsigned char);
  35. x_size<4> x_decltype_encode(short);
  36. x_size<5> x_decltype_encode(unsigned short);
  37. x_size<6> x_decltype_encode(int);
  38. x_size<7> x_decltype_encode(unsigned int);
  39. x_size<8> x_decltype_encode(long);
  40. x_size<9> x_decltype_encode(unsigned long);
  41. x_size<10> x_decltype_encode(long long);
  42. x_size<11> x_decltype_encode(unsigned long long);
  43. template<int N> struct x_decltype_decode {};
  44. template <> struct x_decltype_decode<1> {typedef char type;};
  45. template <> struct x_decltype_decode<2> {typedef signed char type;};
  46. template <> struct x_decltype_decode<3> {typedef unsigned char type;};
  47. template <> struct x_decltype_decode<4> {typedef short type;};
  48. template <> struct x_decltype_decode<5> {typedef unsigned short type;};
  49. template <> struct x_decltype_decode<6> {typedef int type;};
  50. template <> struct x_decltype_decode<7> {typedef unsigned int type;};
  51. template <> struct x_decltype_decode<8> {typedef long type;};
  52. template <> struct x_decltype_decode<9> {typedef unsigned long type;};
  53. template <> struct x_decltype_decode<10> {typedef long long type;};
  54. template <> struct x_decltype_decode<11> {typedef unsigned long long type;};
  55. class noncopyable {
  56. public:
  57. noncopyable(){}
  58. ~noncopyable(){}
  59. private:
  60. noncopyable(const noncopyable&v);
  61. noncopyable& operator = (const noncopyable&v);
  62. };
  63. }
  64. #define x_pack_decltype(T) typename xpack::x_decltype_decode<sizeof(xpack::x_decltype_encode(T))>::type
  65. #endif