common.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #ifndef BOOST_LEAF_COMMON_HPP_INCLUDED
  2. #define BOOST_LEAF_COMMON_HPP_INCLUDED
  3. // Copyright 2018-2023 Emil Dotchevski and Reverge Studios, Inc.
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #include <boost/leaf/config.hpp>
  7. #include <boost/leaf/detail/demangle.hpp>
  8. #include <iosfwd>
  9. #include <cerrno>
  10. #if BOOST_LEAF_CFG_STD_STRING
  11. # include <string>
  12. #endif
  13. #if BOOST_LEAF_CFG_WIN32
  14. # include <windows.h>
  15. # include <cstring>
  16. # ifdef min
  17. # undef min
  18. # endif
  19. # ifdef max
  20. # undef max
  21. # endif
  22. #endif
  23. namespace boost { namespace leaf {
  24. struct BOOST_LEAF_SYMBOL_VISIBLE e_api_function { char const * value; };
  25. #if BOOST_LEAF_CFG_STD_STRING
  26. struct BOOST_LEAF_SYMBOL_VISIBLE e_file_name
  27. {
  28. std::string value;
  29. };
  30. #else
  31. struct BOOST_LEAF_SYMBOL_VISIBLE e_file_name
  32. {
  33. constexpr static char const * const value = "<unavailable>";
  34. BOOST_LEAF_CONSTEXPR explicit e_file_name( char const * ) { }
  35. };
  36. #endif
  37. struct BOOST_LEAF_SYMBOL_VISIBLE e_errno
  38. {
  39. int value;
  40. explicit e_errno(int val=errno): value(val) { }
  41. template <class CharT, class Traits>
  42. friend std::ostream & operator<<(std::basic_ostream<CharT, Traits> & os, e_errno const & err)
  43. {
  44. return os << type<e_errno>() << ": " << err.value << ", \"" << std::strerror(err.value) << '"';
  45. }
  46. };
  47. struct BOOST_LEAF_SYMBOL_VISIBLE e_type_info_name { char const * value; };
  48. struct BOOST_LEAF_SYMBOL_VISIBLE e_at_line { int value; };
  49. namespace windows
  50. {
  51. struct e_LastError
  52. {
  53. unsigned value;
  54. explicit e_LastError(unsigned val): value(val) { }
  55. #if BOOST_LEAF_CFG_WIN32
  56. e_LastError(): value(GetLastError()) { }
  57. template <class CharT, class Traits>
  58. friend std::ostream & operator<<(std::basic_ostream<CharT, Traits> & os, e_LastError const & err)
  59. {
  60. struct msg_buf
  61. {
  62. LPVOID * p;
  63. msg_buf(): p(nullptr) { }
  64. ~msg_buf() noexcept { if(p) LocalFree(p); }
  65. };
  66. msg_buf mb;
  67. if( FormatMessageA(
  68. FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
  69. nullptr,
  70. err.value,
  71. MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
  72. (LPSTR)&mb.p,
  73. 0,
  74. nullptr) )
  75. {
  76. BOOST_LEAF_ASSERT(mb.p != nullptr);
  77. char * z = std::strchr((LPSTR)mb.p,0);
  78. if( z[-1] == '\n' )
  79. *--z = 0;
  80. if( z[-1] == '\r' )
  81. *--z = 0;
  82. return os << type<e_LastError>() << ": " << err.value << ", \"" << (LPCSTR)mb.p << '"';
  83. }
  84. return os;
  85. }
  86. #endif
  87. };
  88. }
  89. } }
  90. #endif