byte17.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * cxxomfort example: byte17
  3. *
  4. * This program provides sample usage of the cxxomfort backport
  5. * for C++17's std::byte type.
  6. *
  7. * The program will read text from the standard input, after that
  8. * will collect and map the frequencies of all
  9. * alphanumeric characters. The input is assumed to be in ASCII.
  10. *
  11. *
  12. *
  13. */
  14. //#define CXXOMFORT_NOTICES 1
  15. #include <cxxomfort/base.hpp>
  16. #include <cxxomfort/cstddef.hpp>
  17. #include <cxxomfort/library/type_name.hpp>
  18. #include <cxxomfort/type_traits.hpp>
  19. #include <cxxomfort/ostream.hpp>
  20. #include <cassert>
  21. #include <cstddef> // std::byte
  22. #include <string>
  23. #include <iostream>
  24. #include <cstddef>
  25. #include <iomanip>
  26. void byte_traits();
  27. // custom, unofficial std::byte stream output operator
  28. static inline std::ostream& operator<< (std::ostream& os, std::byte const& b) {
  29. using namespace std;
  30. return os<< to_integer<unsigned char>(b);
  31. }
  32. int main () {
  33. using namespace std;
  34. cxxomfort::output_info();
  35. cout<< endl;
  36. using cxxomfort::fix::to_byte;
  37. struct bytetests {
  38. enum BT1 {
  39. test1= std::is_same<unsigned char, std::byte>::value ,
  40. test2= std::is_arithmetic<byte>::value,
  41. };
  42. } ;
  43. static_assert(bytetests::test1==false, "byte is a typedef!");
  44. static_assert(bytetests::test2==false, "byte not arithmetic!");
  45. byte S0 = to_byte(1);
  46. (void)S0;
  47. cout<< "unsigned char is : "
  48. << cxxomfort::type_name<unsigned char>()
  49. << endl;
  50. cout<< "byte is : "
  51. << cxxomfort::type_name<byte>()
  52. << " , sizeof:"<< sizeof(byte)
  53. << " , alignof:"<< alignof(byte)<< endl;
  54. cout<< "byte implementation is: "<< (CXXOMFORT_IMPLEMENTS_byte)
  55. << " B"<< CXXO_IMPLSTATUS_BACKPORT()
  56. << " E"<< CXXO_IMPLSTATUS_EMULATION()
  57. << " N"<< CXXO_IMPLSTATUS_NATIVE()<< endl;
  58. #if (CXXOMFORT_CXX_STD>=1997)
  59. std::byte by[2] = { to_byte(0x1), to_byte(0x2) };
  60. (void)by;
  61. cout<< "byte[2] : ";
  62. cout<< alignof(byte[2])<< endl;
  63. cout<< endl;
  64. // end section
  65. byte by0; (void)by0; // GCC7 check says it's default constructible
  66. byte by1 = to_byte(0x44);
  67. //byte err1 = (unsigned char)0x31; (void)err1; // should not compile
  68. //byte err1 ='a'; (void)err2a; // should not compile
  69. byte by2 = to_byte('2'); (void)by2;
  70. //byte by3a = 112; // should not compile
  71. byte by3 = to_byte(112);
  72. // according to cppreference and stackoverflow, the following two should work:
  73. byte by4 = to_byte(0x10); (void)by4;
  74. byte by5 = to_byte('a'); (void)by5;
  75. // ...but the following one is not guaranteed to:
  76. // byte errorfloat = to_byte(3.14f); (void)errorfloat;
  77. #endif
  78. // byte by7a = 15.4f; (void)by7a; // should not work: conversion from scalar to non-scalar requested
  79. // byte errorfloat(15.4f); (void)errorfloat;
  80. byte by6 = to_byte('r'); (void)by6; // should not compile
  81. byte temp = by4;
  82. by1 |=by4;
  83. temp= temp | by3;
  84. by1 &= by5;
  85. temp= temp & by2;
  86. by1 ^= by6;
  87. byte a = to_byte(0x01);
  88. byte b = to_byte(0x3c);
  89. (void)b;
  90. byte c= a;
  91. (void)c;
  92. //a= 100; // should not compile;
  93. //a= (byte)'4'; // valid in c++17; can't be valid before with enum class
  94. a= to_byte(101u); // valid always
  95. assert (a == to_byte(101u));
  96. a= to_byte(0xcc);
  97. cout<< dec<< "Test: convert to char: "<< flush;
  98. unsigned char ca= explicit_cast<unsigned char>(a);
  99. printf("%02x", ca);
  100. cout<< endl;
  101. //cout<< a<< endl;
  102. cout<< "Test: convert to int: "<< endl;
  103. int aint = to_integer<int>(a);
  104. cout<< " a: "<< a<< endl;
  105. cout<< " aint: "<< aint<< endl;
  106. //cout<< (a == x) << endl;
  107. byte b1 = to_byte(0x5a);
  108. byte b2 = to_byte(0xa5);
  109. byte b3 = b1 ^ b2;
  110. byte b4 = b1 | b2;
  111. byte b5 = b1 >> 1;
  112. cout<< hex;
  113. cout<< to_integer<int>(b3)<< '\t';
  114. cout<< to_integer<int>(b4)<< '\t';
  115. cout<< to_integer<int>(b5)<< '\t';
  116. b5 = ~b5;
  117. cout<< to_integer<int>(b5)<< endl;
  118. cout<< "Byte shuffling test: \t";
  119. cout<< dec;
  120. b1 = to_byte(0xff);
  121. while (to_integer<unsigned int>(b1)) {
  122. cout<< hex<< to_integer<int>(b1)<< dec<< " \t";
  123. b1>>= 2;
  124. }
  125. cout<< endl;
  126. cout<< "Byte cxxomfort additions test: "<< endl;
  127. // byte e1 = explicit_cast<byte>('X');
  128. //cout<< e1<< endl;
  129. cout<< endl;
  130. #if (CXXOMFORT_CXX_STD >= 2011)
  131. cout<< "\n"<< "Byte constexpr test: "<< endl;
  132. constexpr byte c1 = to_byte(0x18);
  133. constexpr byte c2 = to_byte(0x51);
  134. static_assert ( c1 != c2 , "equality test 1");
  135. static_assert ( c2 == to_byte(0x51) , "equality test 2");
  136. constexpr byte c3 = c1 & c2;
  137. static_assert (c3 == to_byte(0x10), "logical AND test");
  138. constexpr byte c4 = ~c2;
  139. static_assert (c4 == to_byte(0xAE), "logical NOT test");
  140. #endif
  141. #ifdef PRINT_TRAITS_HPP
  142. cout<< "\nType Traits information:"<< endl;
  143. print_traits<byte>(cout);
  144. cout<< endl;
  145. print_traits_advanced<byte>(cout);
  146. cout<< endl;
  147. print_traits_construction<byte>(cout);
  148. cout<< endl;
  149. print_traits_cxxo<byte>(cout);
  150. cout<< endl;
  151. #endif
  152. }