TarsType.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #ifndef __TARS_TYPE_H__
  17. #define __TARS_TYPE_H__
  18. #ifdef __linux__
  19. #include <netinet/in.h>
  20. #elif _WIN32
  21. #include <WinSock2.h>
  22. #endif
  23. // #include <netinet/in.h>
  24. #include <iostream>
  25. #include <cassert>
  26. #include <vector>
  27. #include <map>
  28. #include <string>
  29. #include <stdexcept>
  30. #include <stdint.h>
  31. #include <string.h>
  32. #include <limits.h>
  33. namespace tars
  34. {
  35. /////////////////////////////////////////////////////////////////////////////////////
  36. typedef bool Bool;
  37. typedef char Char;
  38. typedef short Short;
  39. typedef float Float;
  40. typedef double Double;
  41. typedef int Int32;
  42. typedef unsigned char UInt8;
  43. typedef unsigned short UInt16;
  44. typedef unsigned int UInt32;
  45. #if __WORDSIZE == 64 || defined _WIN64
  46. typedef long Int64;
  47. #else
  48. typedef long long Int64;
  49. #endif
  50. #ifndef TARS_MAX_STRING_LENGTH
  51. #define TARS_MAX_STRING_LENGTH (100 * 1024 * 1024)
  52. #endif
  53. /*
  54. #define tars__bswap_constant_32(x) \
  55. ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
  56. (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
  57. #define tars__bswap_constant_64(x) \
  58. ((((x) & 0xff00000000000000ull) >> 56) \
  59. | (((x) & 0x00ff000000000000ull) >> 40) \
  60. | (((x) & 0x0000ff0000000000ull) >> 24) \
  61. | (((x) & 0x000000ff00000000ull) >> 8) \
  62. | (((x) & 0x00000000ff000000ull) << 8) \
  63. | (((x) & 0x0000000000ff0000ull) << 24) \
  64. | (((x) & 0x000000000000ff00ull) << 40) \
  65. | (((x) & 0x00000000000000ffull) << 56))
  66. */
  67. #ifdef __APPLE__
  68. # ifndef __LITTLE_ENDIAN
  69. # define __LITTLE_ENDIAN 1234
  70. # endif
  71. # ifndef __BIG_ENDIAN
  72. # define __BIG_ENDIAN 4321
  73. # endif
  74. # ifndef __BYTE_ORDER
  75. # define __BYTE_ORDER __LITTLE_ENDIAN
  76. # endif
  77. #endif
  78. #if __BYTE_ORDER == __BIG_ENDIAN
  79. # define tars_ntohll(x) (x)
  80. # define tars_htonll(x) (x)
  81. # define tars_ntohf(x) (x)
  82. # define tars_htonf(x) (x)
  83. # define tars_ntohd(x) (x)
  84. # define tars_htond(x) (x)
  85. #else
  86. # if __BYTE_ORDER == __LITTLE_ENDIAN
  87. # define tars_ntohll(x) tars_htonll(x)
  88. //# define tars_htonll(x) tars__bswap_constant_64(x)
  89. namespace detail
  90. {
  91. union bswap_helper
  92. {
  93. Int64 i64;
  94. Int32 i32[2];
  95. };
  96. }
  97. inline Int64 tars_htonll(Int64 x)
  98. {
  99. detail::bswap_helper h;
  100. h.i64 = x;
  101. Int32 tmp = htonl(h.i32[1]);
  102. h.i32[1] = htonl(h.i32[0]);
  103. h.i32[0] = tmp;
  104. return h.i64;
  105. }
  106. inline Float tars_ntohf(Float x)
  107. {
  108. union {
  109. Float f;
  110. Int32 i32;
  111. } helper;
  112. helper.f = x;
  113. helper.i32 = htonl( helper.i32 );
  114. return helper.f;
  115. }
  116. # define tars_htonf(x) tars_ntohf(x)
  117. inline Double tars_ntohd(Double x)
  118. {
  119. union {
  120. Double d;
  121. Int64 i64;
  122. } helper;
  123. helper.d = x;
  124. helper.i64 = tars_htonll( helper.i64 );
  125. return helper.d;
  126. }
  127. # define tars_htond(x) tars_ntohd(x)
  128. # endif
  129. #endif
  130. //type2name
  131. template<typename T> struct TarsClass { static std::string name() { return T::className(); } };
  132. template<> struct TarsClass<tars::Bool> { static std::string name() { return "bool"; } };
  133. template<> struct TarsClass<tars::Char> { static std::string name() { return "char"; } };
  134. template<> struct TarsClass<tars::Short> { static std::string name() { return "short"; } };
  135. template<> struct TarsClass<tars::Float> { static std::string name() { return "float"; } };
  136. template<> struct TarsClass<tars::Double> { static std::string name() { return "double"; } };
  137. template<> struct TarsClass<tars::Int32> { static std::string name() { return "int32"; } };
  138. template<> struct TarsClass<tars::Int64> { static std::string name() { return "int64"; } };
  139. template<> struct TarsClass<tars::UInt8> { static std::string name() { return "short"; } };
  140. template<> struct TarsClass<tars::UInt16> { static std::string name() { return "int32"; } };
  141. template<> struct TarsClass<tars::UInt32> { static std::string name() { return "int64"; } };
  142. template<> struct TarsClass<std::string> { static std::string name() { return "string"; } };
  143. template<typename T> struct TarsClass<std::vector<T> > { static std::string name() { return std::string("list<") + TarsClass<T>::name() + ">"; } };
  144. template<typename T, typename U> struct TarsClass<std::map<T, U> > { static std::string name() { return std::string("map<") + TarsClass<T>::name() + "," + TarsClass<U>::name() + ">"; } };
  145. namespace detail
  146. {
  147. // is_convertible
  148. template<int N> struct type_of_size { char elements[N]; };
  149. typedef type_of_size<1> yes_type;
  150. typedef type_of_size<2> no_type;
  151. namespace meta_detail
  152. {
  153. struct any_conversion
  154. {
  155. template <typename T> any_conversion(const volatile T&);
  156. template <typename T> any_conversion(T&);
  157. };
  158. template <typename T> struct conversion_checker
  159. {
  160. static no_type _m_check(any_conversion ...);
  161. static yes_type _m_check(T, int);
  162. };
  163. }
  164. template<typename From, typename To>
  165. class is_convertible
  166. {
  167. static From _m_from;
  168. public:
  169. enum { value = sizeof( meta_detail::conversion_checker<To>::_m_check(_m_from, 0) ) == sizeof(yes_type) };
  170. };
  171. template<typename T>
  172. struct type2type { typedef T type; };
  173. template<typename T, typename U>
  174. struct is_same_type
  175. {
  176. enum { value = is_convertible< type2type<T>, type2type<U> >::value };
  177. };
  178. // enable_if
  179. template<bool B, class T = void> struct enable_if_c { typedef T type; };
  180. template<class T> struct enable_if_c<false, T> {};
  181. template<class Cond, class T = void>
  182. struct enable_if : public enable_if_c<Cond::value, T> {};
  183. template<bool B, class T = void> struct disable_if_c { typedef T type; };
  184. template<class T> struct disable_if_c<true, T> {};
  185. template<class Cond, class T = void>
  186. struct disable_if : public disable_if_c<Cond::value, T> {};
  187. }
  188. ////////////////////////////////////////////////////////////////////////////////////////////////////
  189. }
  190. #endif