cast.hpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #pragma once
  2. //convert any (supported) type to a const char* without constructing a new nall::string
  3. //this is used inside string{...} to build nall::string values
  4. namespace nall {
  5. //booleans
  6. template<> struct stringify<bool> {
  7. stringify(bool value) : _value(value) {}
  8. auto data() const -> const char* { return _value ? "true" : "false"; }
  9. auto size() const -> uint { return _value ? 4 : 5; }
  10. bool _value;
  11. };
  12. template<> struct stringify<Boolean> {
  13. stringify(bool value) : _value(value) {}
  14. auto data() const -> const char* { return _value ? "true" : "false"; }
  15. auto size() const -> uint { return _value ? 4 : 5; }
  16. bool _value;
  17. };
  18. //characters
  19. template<> struct stringify<char> {
  20. stringify(char source) { _data[0] = source; _data[1] = 0; }
  21. auto data() const -> const char* { return _data; }
  22. auto size() const -> uint { return 1; }
  23. char _data[2];
  24. };
  25. //signed integers
  26. template<> struct stringify<signed char> {
  27. stringify(signed char source) { fromInteger(_data, source); }
  28. auto data() const -> const char* { return _data; }
  29. auto size() const -> uint { return strlen(_data); }
  30. char _data[2 + sizeof(signed char) * 3];
  31. };
  32. template<> struct stringify<signed short> {
  33. stringify(signed short source) { fromInteger(_data, source); }
  34. auto data() const -> const char* { return _data; }
  35. auto size() const -> uint { return strlen(_data); }
  36. char _data[2 + sizeof(signed short) * 3];
  37. };
  38. template<> struct stringify<signed int> {
  39. stringify(signed int source) { fromInteger(_data, source); }
  40. auto data() const -> const char* { return _data; }
  41. auto size() const -> uint { return strlen(_data); }
  42. char _data[2 + sizeof(signed int) * 3];
  43. };
  44. template<> struct stringify<signed long> {
  45. stringify(signed long source) { fromInteger(_data, source); }
  46. auto data() const -> const char* { return _data; }
  47. auto size() const -> uint { return strlen(_data); }
  48. char _data[2 + sizeof(signed long) * 3];
  49. };
  50. template<> struct stringify<signed long long> {
  51. stringify(signed long long source) { fromInteger(_data, source); }
  52. auto data() const -> const char* { return _data; }
  53. auto size() const -> uint { return strlen(_data); }
  54. char _data[2 + sizeof(signed long long) * 3];
  55. };
  56. #if defined(__SIZEOF_INT128__)
  57. template<> struct stringify<int128_t> {
  58. stringify(int128_t source) { fromInteger(_data, source); }
  59. auto data() const -> const char* { return _data; }
  60. auto size() const -> uint { return strlen(_data); }
  61. char _data[2 + sizeof(int128_t) * 3];
  62. };
  63. #endif
  64. template<uint Bits> struct stringify<Integer<Bits>> {
  65. stringify(Integer<Bits> source) { fromInteger(_data, source); }
  66. auto data() const -> const char* { return _data; }
  67. auto size() const -> uint { return strlen(_data); }
  68. char _data[2 + sizeof(int64_t) * 3];
  69. };
  70. //unsigned integers
  71. template<> struct stringify<unsigned char> {
  72. stringify(unsigned char source) { fromNatural(_data, source); }
  73. auto data() const -> const char* { return _data; }
  74. auto size() const -> uint { return strlen(_data); }
  75. char _data[1 + sizeof(unsigned char) * 3];
  76. };
  77. template<> struct stringify<unsigned short> {
  78. stringify(unsigned short source) { fromNatural(_data, source); }
  79. auto data() const -> const char* { return _data; }
  80. auto size() const -> uint { return strlen(_data); }
  81. char _data[1 + sizeof(unsigned short) * 3];
  82. };
  83. template<> struct stringify<unsigned int> {
  84. stringify(unsigned int source) { fromNatural(_data, source); }
  85. auto data() const -> const char* { return _data; }
  86. auto size() const -> uint { return strlen(_data); }
  87. char _data[1 + sizeof(unsigned int) * 3];
  88. };
  89. template<> struct stringify<unsigned long> {
  90. stringify(unsigned long source) { fromNatural(_data, source); }
  91. auto data() const -> const char* { return _data; }
  92. auto size() const -> uint { return strlen(_data); }
  93. char _data[1 + sizeof(unsigned long) * 3];
  94. };
  95. template<> struct stringify<unsigned long long> {
  96. stringify(unsigned long long source) { fromNatural(_data, source); }
  97. auto data() const -> const char* { return _data; }
  98. auto size() const -> uint { return strlen(_data); }
  99. char _data[1 + sizeof(unsigned long long) * 3];
  100. };
  101. #if defined(__SIZEOF_INT128__)
  102. template<> struct stringify<uint128_t> {
  103. stringify(uint128_t source) { fromNatural(_data, source); }
  104. auto data() const -> const char* { return _data; }
  105. auto size() const -> uint { return strlen(_data); }
  106. char _data[1 + sizeof(uint128_t) * 3];
  107. };
  108. #endif
  109. template<uint Bits> struct stringify<Natural<Bits>> {
  110. stringify(Natural<Bits> source) { fromNatural(_data, source); }
  111. auto data() const -> const char* { return _data; }
  112. auto size() const -> uint { return strlen(_data); }
  113. char _data[1 + sizeof(uint64_t) * 3];
  114. };
  115. //floating-point
  116. template<> struct stringify<float> {
  117. stringify(float source) { fromReal(_data, source); }
  118. auto data() const -> const char* { return _data; }
  119. auto size() const -> uint { return strlen(_data); }
  120. char _data[256];
  121. };
  122. template<> struct stringify<double> {
  123. stringify(double source) { fromReal(_data, source); }
  124. auto data() const -> const char* { return _data; }
  125. auto size() const -> uint { return strlen(_data); }
  126. char _data[256];
  127. };
  128. template<> struct stringify<long double> {
  129. stringify(long double source) { fromReal(_data, source); }
  130. auto data() const -> const char* { return _data; }
  131. auto size() const -> uint { return strlen(_data); }
  132. char _data[256];
  133. };
  134. template<uint Bits> struct stringify<Real<Bits>> {
  135. stringify(Real<Bits> source) { fromReal(_data, source); }
  136. auto data() const -> const char* { return _data; }
  137. auto size() const -> uint { return strlen(_data); }
  138. char _data[256];
  139. };
  140. //arrays
  141. template<> struct stringify<vector<uint8_t>> {
  142. stringify(vector<uint8_t> source) {
  143. _text.resize(source.size());
  144. memory::copy(_text.data(), source.data(), source.size());
  145. }
  146. auto data() const -> const char* { return _text.data(); }
  147. auto size() const -> uint { return _text.size(); }
  148. vector<char> _text;
  149. };
  150. template<> struct stringify<const vector<uint8_t>&> {
  151. stringify(const vector<uint8_t>& source) {
  152. _text.resize(source.size());
  153. memory::copy(_text.data(), source.data(), source.size());
  154. }
  155. auto data() const -> const char* { return _text.data(); }
  156. auto size() const -> uint { return _text.size(); }
  157. vector<char> _text;
  158. };
  159. //char arrays
  160. template<> struct stringify<char*> {
  161. stringify(char* source) : _data(source ? source : "") {}
  162. auto data() const -> const char* { return _data; }
  163. auto size() const -> uint { return strlen(_data); }
  164. const char* _data;
  165. };
  166. template<> struct stringify<const char*> {
  167. stringify(const char* source) : _data(source ? source : "") {}
  168. auto data() const -> const char* { return _data; }
  169. auto size() const -> uint { return strlen(_data); }
  170. const char* _data;
  171. };
  172. //strings
  173. template<> struct stringify<string> {
  174. stringify(const string& source) : _text(source) {}
  175. auto data() const -> const char* { return _text.data(); }
  176. auto size() const -> uint { return _text.size(); }
  177. const string& _text;
  178. };
  179. template<> struct stringify<const string&> {
  180. stringify(const string& source) : _text(source) {}
  181. auto data() const -> const char* { return _text.data(); }
  182. auto size() const -> uint { return _text.size(); }
  183. const string& _text;
  184. };
  185. template<> struct stringify<string_view> {
  186. stringify(const string_view& source) : _view(source) {}
  187. auto data() const -> const char* { return _view.data(); }
  188. auto size() const -> uint { return _view.size(); }
  189. const string_view& _view;
  190. };
  191. template<> struct stringify<const string_view&> {
  192. stringify(const string_view& source) : _view(source) {}
  193. auto data() const -> const char* { return _view.data(); }
  194. auto size() const -> uint { return _view.size(); }
  195. const string_view& _view;
  196. };
  197. template<> struct stringify<array_view<uint8_t>> {
  198. stringify(const array_view<uint8_t>& source) : _view(source) {}
  199. auto data() const -> const char* { return _view.data<const char>(); }
  200. auto size() const -> uint { return _view.size(); }
  201. const array_view<uint8_t>& _view;
  202. };
  203. template<> struct stringify<const array_view<uint8_t>&> {
  204. stringify(const array_view<uint8_t>& source) : _view(source) {}
  205. auto data() const -> const char* { return _view.data<const char>(); }
  206. auto size() const -> uint { return _view.size(); }
  207. const array_view<uint8_t>& _view;
  208. };
  209. template<> struct stringify<string_pascal> {
  210. stringify(const string_pascal& source) : _text(source) {}
  211. auto data() const -> const char* { return _text.data(); }
  212. auto size() const -> uint { return _text.size(); }
  213. const string_pascal& _text;
  214. };
  215. template<> struct stringify<const string_pascal&> {
  216. stringify(const string_pascal& source) : _text(source) {}
  217. auto data() const -> const char* { return _text.data(); }
  218. auto size() const -> uint { return _text.size(); }
  219. const string_pascal& _text;
  220. };
  221. //pointers
  222. //note: T = char* is matched by stringify<string_view>
  223. template<typename T> struct stringify<T*> {
  224. stringify(const T* source) {
  225. if(!source) {
  226. memory::copy(_data, "(nullptr)", 10);
  227. } else {
  228. memory::copy(_data, "0x", 2);
  229. fromNatural(_data + 2, (uintptr)source);
  230. }
  231. }
  232. auto data() const -> const char* { return _data; }
  233. auto size() const -> uint { return strlen(_data); }
  234. char _data[256];
  235. };
  236. //
  237. template<typename T> auto make_string(T value) -> stringify<T> {
  238. return stringify<T>(forward<T>(value));
  239. }
  240. }