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