common.hpp 2.9 KB

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