any.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //#define CXXOMFORT_NOTICES 2
  2. #include <cxxomfort/cxxomfort.hpp>
  3. #include <cxxomfort/any.hpp>
  4. #include <cxxomfort/typeindex.hpp>
  5. #include <cxxomfort/library/type_name.hpp>
  6. #include <string>
  7. #include <iostream>
  8. #include <vector>
  9. #include <print_traits.hpp>
  10. #define HA(TID) type_index(TID).hash_code()
  11. struct any_print {
  12. explicit any_print (std::ostream& os_)
  13. : os(os_)
  14. {}
  15. void operator() (std::any const& A) const {
  16. using namespace std;
  17. //cout<< "* any_print: A: "<< HA(A.type())<< endl;
  18. {
  19. size_t intcodes[] = {HA(typeid(int)), HA(typeid(short))} ;
  20. size_t uintcodes[] = {HA(typeid(unsigned int)), HA(typeid(unsigned short))} ;
  21. if (false) {
  22. }
  23. else if (find(begin(intcodes), end(intcodes), HA(A.type()) ) != end(intcodes) ) {
  24. os<< any_cast<int>(A);
  25. }
  26. else if (find(begin(uintcodes), end(uintcodes), HA(A.type()) ) != end(uintcodes) ) {
  27. os<< any_cast<unsigned int>(A);
  28. }
  29. else if (HA(A.type()) == HA(typeid(std::string))) {
  30. os<< any_cast<std::string>(A);
  31. }
  32. else if (HA(A.type()) == HA(typeid(std::string_view))) {
  33. cout<< any_cast<std::string_view>(A);
  34. }
  35. else {
  36. os<< "{unrecognized_any}";
  37. }
  38. }
  39. }
  40. std::ostream& os;
  41. };
  42. inline
  43. std::ostream& operator<< (std::ostream& os, std::any const& A) {
  44. any_print K(os);
  45. K(A);
  46. return os;
  47. }
  48. int main () {
  49. using namespace std;
  50. using cxxomfort::library::typeid_demangle;
  51. cxxomfort::output_info(stdout);
  52. cout<< endl;
  53. static_assert0( cxxomfort::is_std_any< any >::value);
  54. cout<< "sizeof(any) : "<< sizeof(any)<< "\n";
  55. cout<< "std::any implementation info: ";
  56. cout<< CXXOMFORT_IMPLEMENTS_std_any<< "\n";
  57. cout<< "\n";
  58. any foo;
  59. try {
  60. cout<< "try:"<< endl;
  61. foo = 1;
  62. cout<< typeid_demangle(foo.type())<< " , "<< any_cast<int>(foo)<< endl;
  63. foo = 1.0f;
  64. cout<< typeid_demangle(foo.type())<< " , "<< any_cast<double>(foo)<< endl;
  65. return 0;
  66. } catch (std::exception& E) {
  67. cerr<< "Exception: "<< E.what()<< endl;
  68. cerr<< "Any: "<< foo.type().name()<< endl;
  69. }
  70. cout<< endl;
  71. any foo2(2.0);
  72. cout<< typeid_demangle(foo2.type())<< " , "<< any_cast<float>(&foo2)<< endl;
  73. try {
  74. cout<< "vector<any>: "<< endl;
  75. vector<any> VV;
  76. VV.push_back(10U); cout<< "1"<< flush;
  77. VV.push_back(reinterpret_cast<bool*>(0)); cout<< "2"<< flush;
  78. VV.push_back(cxxomfort::library::type_name<bool&>()); cout<< "3"<< flush;
  79. VV.push_back(wstring(L"ááá")); cout<< "4"<< flush;
  80. cout<< endl;
  81. for (unsigned i=0; i<VV.size(); ++i) {
  82. cout<< i<< ": \t"<< typeid_demangle(VV[i].type())<< endl;
  83. cout<< i<< ": \t"<< VV[i]<< endl;
  84. }
  85. } catch (std::exception& E) {
  86. cout<< E.what()<< endl;
  87. }
  88. cout<< "\n\n";
  89. cout<< "Test of assigning an array:"<< endl;
  90. any cs("Hello World");
  91. cout<< cxxomfort::typeid_demangle(cs.type())<< endl;
  92. }