common.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #ifndef BOOST_LEAF_COMMON_HPP_INCLUDED
  2. #define BOOST_LEAF_COMMON_HPP_INCLUDED
  3. // Copyright 2018-2024 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. #include <cstring>
  11. #if BOOST_LEAF_CFG_STD_STRING
  12. # include <string>
  13. #endif
  14. #if BOOST_LEAF_CFG_WIN32
  15. # include <windows.h>
  16. # include <cstring>
  17. # ifdef min
  18. # undef min
  19. # endif
  20. # ifdef max
  21. # undef max
  22. # endif
  23. #endif
  24. namespace boost { namespace leaf {
  25. struct BOOST_LEAF_SYMBOL_VISIBLE e_api_function { char const * value; };
  26. #if BOOST_LEAF_CFG_STD_STRING
  27. struct BOOST_LEAF_SYMBOL_VISIBLE e_file_name
  28. {
  29. std::string value;
  30. };
  31. #else
  32. struct BOOST_LEAF_SYMBOL_VISIBLE e_file_name
  33. {
  34. char const * value = "<unavailable>";
  35. BOOST_LEAF_CONSTEXPR explicit e_file_name( char const * ) { }
  36. };
  37. #endif
  38. struct BOOST_LEAF_SYMBOL_VISIBLE e_errno
  39. {
  40. int value;
  41. explicit e_errno(int val=errno): value(val) { }
  42. template <class CharT, class Traits>
  43. friend std::ostream & operator<<(std::basic_ostream<CharT, Traits> & os, e_errno const & err)
  44. {
  45. return os << err.value << ", \"" << std::strerror(err.value) << '"';
  46. }
  47. };
  48. struct BOOST_LEAF_SYMBOL_VISIBLE e_type_info_name { char const * value; };
  49. struct BOOST_LEAF_SYMBOL_VISIBLE e_at_line { int value; };
  50. namespace windows
  51. {
  52. struct e_LastError
  53. {
  54. unsigned value;
  55. explicit e_LastError(unsigned val): value(val) { }
  56. #if BOOST_LEAF_CFG_WIN32
  57. e_LastError(): value(GetLastError()) { }
  58. template <class CharT, class Traits>
  59. friend std::ostream & operator<<(std::basic_ostream<CharT, Traits> & os, e_LastError const & err)
  60. {
  61. struct msg_buf
  62. {
  63. LPVOID * p;
  64. msg_buf(): p(nullptr) { }
  65. ~msg_buf() noexcept { if(p) LocalFree(p); }
  66. };
  67. msg_buf mb;
  68. if( FormatMessageA(
  69. FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
  70. nullptr,
  71. err.value,
  72. MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
  73. (LPSTR)&mb.p,
  74. 0,
  75. nullptr) )
  76. {
  77. BOOST_LEAF_ASSERT(mb.p != nullptr);
  78. char * z = std::strchr((LPSTR)mb.p,0);
  79. if( z[-1] == '\n' )
  80. *--z = 0;
  81. if( z[-1] == '\r' )
  82. *--z = 0;
  83. return os << err.value << ", \"" << (LPCSTR)mb.p << '"';
  84. }
  85. return os;
  86. }
  87. #endif
  88. };
  89. }
  90. } }
  91. #endif // BOOST_LEAF_COMMON_HPP_INCLUDED