Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.12 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <assert.h>
  4. #include <bitset>
  5. #include <cstdint>
  6. #include <limits>
  7. #include <type_traits>
  8.  
  9. using u8 = std::uint8_t;
  10. using u16 = std::uint16_t;
  11. using u32 = std::uint32_t;
  12. using u64 = std::uint64_t;
  13.  
  14. template <typename T>
  15. struct Register {
  16. T data;
  17. T value;
  18. bitset<sizeof(T) * CHAR_BIT> bits;
  19.  
  20. Register() : data(), value() {}
  21.  
  22. template <typename P>
  23. explicit Register(const P val) : data(static_cast<T>(val)), value(data), bits(data) {}
  24.  
  25. template <typename P>
  26. Register(const P val, const unsigned char idx) : data(static_cast<T>((val >> std::size(bits) * idx) & numeric_limits<make_unsigned_t<T>>::max())), value(data), bits(data) {
  27. assert(idx == '' || idx < sizeof(P) / sizeof(T));
  28. }
  29.  
  30. template <typename P>
  31. Register(const Register<P>& reg) : data(static_cast<T>(reg.data)), value(data), bits(data) {}
  32. };
  33.  
  34. using Reg8 = Register<u8>;
  35. using Reg16 = Register<u16>;
  36. using Reg32 = Register<u32>;
  37. using Reg64 = Register<u64>;
  38.  
  39. void someFunc() {
  40. // by const value
  41. Reg64 r64{ 0x0123456789ABCDEF };
  42. Reg32 r32{ 0x89ABCDEF };
  43. Reg16 r16{ 0xCDEF };
  44. Reg8 r8{ 0xEf };
  45.  
  46. // unsigned type
  47. u8 byte = 8;
  48. u16 word = 16;
  49. u32 dword = 32;
  50. u64 qword = 64;
  51.  
  52. Reg8 r8a{byte};
  53. Reg16 r16a{word};
  54. Reg32 r32a{dword};
  55. Reg64 r64a{qword};
  56.  
  57. // Or from any other Register<T> type.
  58. }
  59.  
  60. #pragma once
  61.  
  62. #include <assert.h>
  63. #include <bitset>
  64. #include <cstdint>
  65. #include <limits>
  66. #include <type_traits>
  67.  
  68. using u8 = std::uint8_t;
  69. using u16 = std::uint16_t;
  70. using u32 = std::uint32_t;
  71. using u64 = std::uint64_t;
  72.  
  73. template<typename T>
  74. struct Register {
  75. T data;
  76. T value;
  77. std::bitset<sizeof(T)* CHAR_BIT> bits;
  78.  
  79. Register() : data{ 0 }, value{ 0 }, bits{ 0 } {}
  80.  
  81. template<typename P, std::enable_if_t<(sizeof(P) > sizeof(T))>* = nullptr>
  82. Register(const P val, const u8 idx) :
  83. data{ static_cast<T>((val >> std::size(bits) * idx) &
  84. std::numeric_limits<std::make_unsigned_t<T>>::max()) },
  85. value{ data },
  86. bits{ data }
  87. {
  88.  
  89. constexpr u16 sizeT = sizeof(T);
  90. constexpr u16 sizeP = sizeof(P);
  91. assert((idx >= 0) && (idx <= ((sizeP / sizeT) - 1)) );
  92. }
  93.  
  94.  
  95. template<typename P, std::enable_if_t<(sizeof(P) < sizeof(T))>* = nullptr>
  96. Register(const P val, const u8 idx) :
  97. data{ /*static_cast<T>((val >> std::size(bits) * idx) &
  98. std::numeric_limits<std::make_unsigned_t<T>>::max()) },*/
  99. static_cast<T>(val)
  100. },
  101. value{ data },
  102. bits{ data }
  103. {
  104. constexpr u16 sizeT = sizeof(T);
  105. constexpr u16 sizeP = sizeof(P);
  106. assert((idx >= 0) && (idx <= ((sizeT / sizeP) - 1)) );
  107. }
  108.  
  109.  
  110. template<typename P, std::enable_if_t<(sizeof(P) == sizeof(T))>* = nullptr>
  111. Register(const P val, const u8 idx = 0) :
  112. data{ static_cast<T>( val ) }, value{ data }, bits{ data }
  113. {}
  114.  
  115. template<typename P>
  116. Register(const Register<P>& reg, const u8 idx = 0) : Register(reg.data, idx) {}
  117.  
  118. };
  119.  
  120. using Reg8 = Register<u8>;
  121. using Reg16 = Register<u16>;
  122. using Reg32 = Register<u32>;
  123. using Reg64 = Register<u64>;
  124.  
  125. 1>------ Build started: Project: TestRegister, Configuration: Debug x64 ------
  126. 1>main.cpp
  127. 1>c:***main.cpp(54): error C2440: 'initializing': cannot convert from 'initializer list' to 'vpc::Register<vpc::u16>'
  128. 1>c:***main.cpp(54): note: No constructor could take the source type, or constructor overload resolution was ambiguous
  129. 1>c:***main.cpp(55): error C2440: 'initializing': cannot convert from 'initializer list' to 'vpc::Register<vpc::u8>'
  130. 1>c:***main.cpp(55): note: No constructor could take the source type, or constructor overload resolution was ambiguous
  131. 1>c:***main.cpp(195): error C2665: 'vpc::Register<vpc::u8>::Register': none of the 3 overloads could convert all the argument types
  132. 1>c:***register.h(73): note: could be 'vpc::Register<vpc::u8>::Register(vpc::Register<vpc::u8> &&)'
  133. 1>c:***register.h(73): note: or 'vpc::Register<vpc::u8>::Register(const vpc::Register<vpc::u8> &)'
  134. 1>c:***register.h(34): note: or 'vpc::Register<vpc::u8>::Register(void)'
  135. 1>c:***main.cpp(195): note: while trying to match the argument list '(u16)'
  136. 1>c:***main.cpp(196): error C2665: 'vpc::Register<vpc::u8>::Register': none of the 3 overloads could convert all the argument types
  137. 1>c:***register.h(73): note: could be 'vpc::Register<vpc::u8>::Register(vpc::Register<vpc::u8> &&)'
  138. 1>c:***register.h(73): note: or 'vpc::Register<vpc::u8>::Register(const vpc::Register<vpc::u8> &)'
  139. 1>c:***register.h(34): note: or 'vpc::Register<vpc::u8>::Register(void)'
  140. 1>c:***main.cpp(196): note: while trying to match the argument list '(u32)'
  141. 1>c:***main.cpp(197): error C2665: 'vpc::Register<vpc::u8>::Register': none of the 3 overloads could convert all the argument types
  142. 1>c:***register.h(73): note: could be 'vpc::Register<vpc::u8>::Register(vpc::Register<vpc::u8> &&)'
  143. 1>c:***register.h(73): note: or 'vpc::Register<vpc::u8>::Register(const vpc::Register<vpc::u8> &)'
  144. 1>c:***register.h(34): note: or 'vpc::Register<vpc::u8>::Register(void)'
  145. 1>c:***main.cpp(197): note: while trying to match the argument list '(u64)'
  146. 1>c:***main.cpp(238): error C2665: 'vpc::Register<vpc::u16>::Register': none of the 3 overloads could convert all the argument types
  147. 1>c:***register.h(73): note: could be 'vpc::Register<vpc::u16>::Register(vpc::Register<vpc::u16> &&)'
  148. 1>c:***register.h(73): note: or 'vpc::Register<vpc::u16>::Register(const vpc::Register<vpc::u16> &)'
  149. 1>c:***register.h(34): note: or 'vpc::Register<vpc::u16>::Register(void)'
  150. 1>c:***main.cpp(238): note: while trying to match the argument list '(u8)'
  151. 1>c:***main.cpp(239): error C2665: 'vpc::Register<vpc::u16>::Register': none of the 3 overloads could convert all the argument types
  152. 1>c:***register.h(73): note: could be 'vpc::Register<vpc::u16>::Register(vpc::Register<vpc::u16> &&)'
  153. 1>c:***register.h(73): note: or 'vpc::Register<vpc::u16>::Register(const vpc::Register<vpc::u16> &)'
  154. 1>c:***register.h(34): note: or 'vpc::Register<vpc::u16>::Register(void)'
  155. 1>c:***main.cpp(239): note: while trying to match the argument list '(u8)'
  156. 1>c:***main.cpp(241): error C2665: 'vpc::Register<vpc::u16>::Register': none of the 3 overloads could convert all the argument types
  157. 1>c:***register.h(73): note: could be 'vpc::Register<vpc::u16>::Register(vpc::Register<vpc::u16> &&)'
  158. 1>c:***register.h(73): note: or 'vpc::Register<vpc::u16>::Register(const vpc::Register<vpc::u16> &)'
  159. 1>c:***register.h(34): note: or 'vpc::Register<vpc::u16>::Register(void)'
  160. 1>c:***main.cpp(241): note: while trying to match the argument list '(u32)'
  161. 1>c:***main.cpp(242): error C2665: 'vpc::Register<vpc::u16>::Register': none of the 3 overloads could convert all the argument types
  162. 1>c:***register.h(73): note: could be 'vpc::Register<vpc::u16>::Register(vpc::Register<vpc::u16> &&)'
  163. 1>c:***register.h(73): note: or 'vpc::Register<vpc::u16>::Register(const vpc::Register<vpc::u16> &)'
  164. 1>c:***register.h(34): note: or 'vpc::Register<vpc::u16>::Register(void)'
  165. 1>c:***main.cpp(242): note: while trying to match the argument list '(u64)'
  166. 1>c:***main.cpp(267): error C2665: 'vpc::Register<vpc::u32>::Register': none of the 3 overloads could convert all the argument types
  167. 1>c:***register.h(73): note: could be 'vpc::Register<vpc::u32>::Register(vpc::Register<vpc::u32> &&)'
  168. 1>c:***register.h(73): note: or 'vpc::Register<vpc::u32>::Register(const vpc::Register<vpc::u32> &)'
  169. 1>c:***register.h(34): note: or 'vpc::Register<vpc::u32>::Register(void)'
  170. 1>c:***main.cpp(267): note: while trying to match the argument list '(u8)'
  171. 1>c:***main.cpp(268): error C2665: 'vpc::Register<vpc::u32>::Register': none of the 3 overloads could convert all the argument types
  172. 1>c:***register.h(73): note: could be 'vpc::Register<vpc::u32>::Register(vpc::Register<vpc::u32> &&)'
  173. 1>c:***register.h(73): note: or 'vpc::Register<vpc::u32>::Register(const vpc::Register<vpc::u32> &)'
  174. 1>c:***register.h(34): note: or 'vpc::Register<vpc::u32>::Register(void)'
  175. 1>c:***main.cpp(268): note: while trying to match the argument list '(u8)'
  176. 1>c:***main.cpp(269): error C2665: 'vpc::Register<vpc::u32>::Register': none of the 3 overloads could convert all the argument types
  177. 1>c:***register.h(73): note: could be 'vpc::Register<vpc::u32>::Register(vpc::Register<vpc::u32> &&)'
  178. 1>c:***register.h(73): note: or 'vpc::Register<vpc::u32>::Register(const vpc::Register<vpc::u32> &)'
  179. 1>c:***register.h(34): note: or 'vpc::Register<vpc::u32>::Register(void)'
  180. 1>c:***main.cpp(269): note: while trying to match the argument list '(u16)'
  181. 1>c:***main.cpp(271): error C2665: 'vpc::Register<vpc::u32>::Register': none of the 3 overloads could convert all the argument types
  182. 1>c:***register.h(73): note: could be 'vpc::Register<vpc::u32>::Register(vpc::Register<vpc::u32> &&)'
  183. 1>c:***register.h(73): note: or 'vpc::Register<vpc::u32>::Register(const vpc::Register<vpc::u32> &)'
  184. 1>c:***register.h(34): note: or 'vpc::Register<vpc::u32>::Register(void)'
  185. 1>c:***main.cpp(271): note: while trying to match the argument list '(u64)'
  186. 1>c:***main.cpp(285): error C2665: 'vpc::Register<vpc::u64>::Register': none of the 3 overloads could convert all the argument types
  187. 1>c:***register.h(73): note: could be 'vpc::Register<vpc::u64>::Register(vpc::Register<vpc::u64> &&)'
  188. 1>c:***register.h(73): note: or 'vpc::Register<vpc::u64>::Register(const vpc::Register<vpc::u64> &)'
  189. 1>c:***register.h(34): note: or 'vpc::Register<vpc::u64>::Register(void)'
  190. 1>c:***main.cpp(285): note: while trying to match the argument list '(u8)'
  191. 1>c:***main.cpp(286): error C2665: 'vpc::Register<vpc::u64>::Register': none of the 3 overloads could convert all the argument types
  192. 1>c:***register.h(73): note: could be 'vpc::Register<vpc::u64>::Register(vpc::Register<vpc::u64> &&)'
  193. 1>c:***register.h(73): note: or 'vpc::Register<vpc::u64>::Register(const vpc::Register<vpc::u64> &)'
  194. 1>c:***register.h(34): note: or 'vpc::Register<vpc::u64>::Register(void)'
  195. 1>c:***main.cpp(286): note: while trying to match the argument list '(u8)'
  196. 1>c:***main.cpp(287): error C2665: 'vpc::Register<vpc::u64>::Register': none of the 3 overloads could convert all the argument types
  197. 1>c:***register.h(73): note: could be 'vpc::Register<vpc::u64>::Register(vpc::Register<vpc::u64> &&)'
  198. 1>c:***register.h(73): note: or 'vpc::Register<vpc::u64>::Register(const vpc::Register<vpc::u64> &)'
  199. 1>c:***register.h(34): note: or 'vpc::Register<vpc::u64>::Register(void)'
  200. 1>c:***main.cpp(287): note: while trying to match the argument list '(u16)'
  201. 1>c:***main.cpp(288): error C2665: 'vpc::Register<vpc::u64>::Register': none of the 3 overloads could convert all the argument types
  202. 1>c:***register.h(73): note: could be 'vpc::Register<vpc::u64>::Register(vpc::Register<vpc::u64> &&)'
  203. 1>c:***register.h(73): note: or 'vpc::Register<vpc::u64>::Register(const vpc::Register<vpc::u64> &)'
  204. 1>c:***register.h(34): note: or 'vpc::Register<vpc::u64>::Register(void)'
  205. 1>c:usersskilz99sourcerepostestregistertestregistermain.cpp(288): note: while trying to match the argument list '(u32)'
  206. 1>Done building project "TestRegister.vcxproj" -- FAILED.
  207. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement