TarsType.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. #include "util/tc_platform.h"
  34. namespace tars
  35. {
  36. /////////////////////////////////////////////////////////////////////////////////////
  37. typedef bool Bool;
  38. typedef char Char;
  39. typedef short Short;
  40. typedef float Float;
  41. typedef double Double;
  42. typedef int Int32;
  43. struct TarsStructBase;
  44. typedef unsigned char UInt8;
  45. typedef unsigned short UInt16;
  46. typedef unsigned int UInt32;
  47. #if __WORDSIZE == 64
  48. typedef long Int64;
  49. #else
  50. typedef long long Int64;
  51. #endif
  52. #ifndef TARS_MAX_STRING_LENGTH
  53. #define TARS_MAX_STRING_LENGTH (100 * 1024 * 1024)
  54. #endif
  55. /*
  56. #define tars__bswap_constant_32(x) \
  57. ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
  58. (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
  59. #define tars__bswap_constant_64(x) \
  60. ((((x) & 0xff00000000000000ull) >> 56) \
  61. | (((x) & 0x00ff000000000000ull) >> 40) \
  62. | (((x) & 0x0000ff0000000000ull) >> 24) \
  63. | (((x) & 0x000000ff00000000ull) >> 8) \
  64. | (((x) & 0x00000000ff000000ull) << 8) \
  65. | (((x) & 0x0000000000ff0000ull) << 24) \
  66. | (((x) & 0x000000000000ff00ull) << 40) \
  67. | (((x) & 0x00000000000000ffull) << 56))
  68. */
  69. #if (defined(__APPLE__) || defined(_WIN32))
  70. # ifndef __LITTLE_ENDIAN
  71. # define __LITTLE_ENDIAN 1234
  72. # endif
  73. # ifndef __BIG_ENDIAN
  74. # define __BIG_ENDIAN 4321
  75. # endif
  76. # ifndef __BYTE_ORDER
  77. # define __BYTE_ORDER __LITTLE_ENDIAN
  78. # endif
  79. #endif
  80. #if __BYTE_ORDER == __BIG_ENDIAN
  81. # define tars_ntohll(x) (x)
  82. # define tars_htonll(x) (x)
  83. # define tars_ntohf(x) (x)
  84. # define tars_htonf(x) (x)
  85. # define tars_ntohd(x) (x)
  86. # define tars_htond(x) (x)
  87. #else
  88. # if __BYTE_ORDER == __LITTLE_ENDIAN
  89. # define tars_ntohll(x) tars_htonll(x)
  90. //# define tars_htonll(x) tars__bswap_constant_64(x)
  91. namespace detail
  92. {
  93. union bswap_helper
  94. {
  95. Int64 i64;
  96. Int32 i32[2];
  97. };
  98. }
  99. inline Int64 tars_htonll(Int64 x)
  100. {
  101. detail::bswap_helper h;
  102. h.i64 = x;
  103. Int32 tmp = htonl(h.i32[1]);
  104. h.i32[1] = htonl(h.i32[0]);
  105. h.i32[0] = tmp;
  106. return h.i64;
  107. }
  108. inline Float tars_ntohf(Float x)
  109. {
  110. union {
  111. Float f;
  112. Int32 i32;
  113. } helper;
  114. helper.f = x;
  115. helper.i32 = htonl( helper.i32 );
  116. return helper.f;
  117. }
  118. # define tars_htonf(x) tars_ntohf(x)
  119. inline Double tars_ntohd(Double x)
  120. {
  121. union {
  122. Double d;
  123. Int64 i64;
  124. } helper;
  125. helper.d = x;
  126. helper.i64 = tars_htonll( helper.i64 );
  127. return helper.d;
  128. }
  129. # define tars_htond(x) tars_ntohd(x)
  130. # endif
  131. #endif
  132. //type2name
  133. template<typename T> struct TarsClass { static std::string name() { return T::className(); } };
  134. template<> struct TarsClass<tars::Bool> { static std::string name() { return "bool"; } };
  135. template<> struct TarsClass<tars::Char> { static std::string name() { return "char"; } };
  136. template<> struct TarsClass<tars::Short> { static std::string name() { return "short"; } };
  137. template<> struct TarsClass<tars::Float> { static std::string name() { return "float"; } };
  138. template<> struct TarsClass<tars::Double> { static std::string name() { return "double"; } };
  139. template<> struct TarsClass<tars::Int32> { static std::string name() { return "int32"; } };
  140. template<> struct TarsClass<tars::Int64> { static std::string name() { return "int64"; } };
  141. template<> struct TarsClass<tars::UInt8> { static std::string name() { return "short"; } };
  142. template<> struct TarsClass<tars::UInt16> { static std::string name() { return "int32"; } };
  143. template<> struct TarsClass<tars::UInt32> { static std::string name() { return "int64"; } };
  144. template<> struct TarsClass<std::string> { static std::string name() { return "string"; } };
  145. template<typename T> struct TarsClass<std::vector<T> > { static std::string name() { return std::string("list<") + TarsClass<T>::name() + ">"; } };
  146. 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() + ">"; } };
  147. namespace detail
  148. {
  149. // is_convertible
  150. template<int N> struct type_of_size { char elements[N]; };
  151. typedef type_of_size<1> yes_type;
  152. typedef type_of_size<2> no_type;
  153. namespace meta_detail
  154. {
  155. struct any_conversion
  156. {
  157. template <typename T> any_conversion(const volatile T&);
  158. template <typename T> any_conversion(T&);
  159. };
  160. template <typename T> struct conversion_checker
  161. {
  162. static no_type _m_check(any_conversion ...);
  163. static yes_type _m_check(T, int);
  164. };
  165. }
  166. template<typename From, typename To>
  167. class is_convertible
  168. {
  169. static From _m_from;
  170. public:
  171. enum { value = sizeof( meta_detail::conversion_checker<To>::_m_check(_m_from, 0) ) == sizeof(yes_type) };
  172. };
  173. template<typename T>
  174. struct type2type { typedef T type; };
  175. template<typename T, typename U>
  176. struct is_same_type
  177. {
  178. enum { value = is_convertible< type2type<T>, type2type<U> >::value };
  179. };
  180. // enable_if
  181. template<bool B, class T = void> struct enable_if_c { typedef T type; };
  182. template<class T> struct enable_if_c<false, T> {};
  183. template<class Cond, class T = void>
  184. struct enable_if : public enable_if_c<Cond::value, T> {};
  185. template<bool B, class T = void> struct disable_if_c { typedef T type; };
  186. template<class T> struct disable_if_c<true, T> {};
  187. template<class Cond, class T = void>
  188. struct disable_if : public disable_if_c<Cond::value, T> {};
  189. }
  190. ////////////////////////////////////////////////////////////////////////////////////////////////////
  191. }
  192. #endif