Skip to content

Commit a8bef0b

Browse files
committed
refinements
1 parent a89a566 commit a8bef0b

File tree

6 files changed

+288
-149
lines changed

6 files changed

+288
-149
lines changed

include/boost/leaf/common.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,8 @@ struct e_errno
6262
template <class Json>
6363
friend void to_json(Json & j, e_errno const & e)
6464
{
65-
char zstr[256];
66-
Json & v = j[parse_to_zstr<e_errno>(zstr)];
67-
v["value"] = e.value;
68-
v["message"] = std::strerror(e.value);
65+
j["value"] = e.value;
66+
j["message"] = std::strerror(e.value);
6967
}
7068
};
7169

include/boost/leaf/detail/demangle.hpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
#include <cstdlib>
2020
#include <cstring>
2121

22+
#if BOOST_LEAF_CFG_STD_STRING
23+
# include <string>
24+
#endif
25+
2226
#if BOOST_LEAF_CFG_DIAGNOSTICS
2327

2428
// __has_include is currently supported by GCC and Clang. However GCC 4.9 may have issues and
@@ -131,6 +135,13 @@ namespace n
131135
{
132136
return os.write(pn.name, pn.len);
133137
}
138+
139+
#if BOOST_LEAF_CFG_STD_STRING
140+
friend std::string to_string(r const & pn)
141+
{
142+
return std::string(pn.name, pn.len);
143+
}
144+
#endif
134145
};
135146

136147
template <class T>
@@ -217,16 +228,6 @@ parsed parse()
217228
return n::p<T>();
218229
}
219230

220-
template <class T, int N>
221-
char const * parse_to_zstr(char (&zstr)[N]) noexcept
222-
{
223-
parsed p = parse<T>();
224-
std::size_t n = p.len < N - 1 ? p.len : N - 1;
225-
std::memcpy(zstr, p.name, n);
226-
zstr[n] = '\0';
227-
return zstr;
228-
}
229-
230231
} } // namespace boost::leaf
231232

232233
////////////////////////////////////////

include/boost/leaf/diagnostics.hpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ class diagnostic_info: public error_info
6161
void write_to(W & w) const
6262
{
6363
typename detail::dependent_writer<W>::type & wr = w;
64-
serialize(wr, error());
64+
#ifndef BOOST_LEAF_NO_EXCEPTIONS
65+
if( exception() )
66+
serialize(wr, *exception());
67+
#endif
6568
serialize_tuple_contents_(wr, tup_, error());
6669
}
6770
}; // class diagnostic_info
@@ -121,7 +124,10 @@ class diagnostic_info: public error_info
121124
void write_to(W & w) const
122125
{
123126
typename detail::dependent_writer<W>::type & wr = w;
124-
serialize(wr, error());
127+
#ifndef BOOST_LEAF_NO_EXCEPTIONS
128+
if( exception() )
129+
serialize(wr, *exception());
130+
#endif
125131
}
126132
}; // class diagnostic_info
127133

include/boost/leaf/error.hpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,9 @@ struct e_source_location
7979
template <class Json>
8080
friend void to_json(Json & j, e_source_location const & x)
8181
{
82-
char zstr[256];
83-
Json & v = j[parse_to_zstr<e_source_location>(zstr)];
84-
v["file"] = x.file;
85-
v["line"] = x.line;
86-
v["function"] = x.function;
82+
j["file"] = x.file;
83+
j["line"] = x.line;
84+
j["function"] = x.function;
8785
}
8886
};
8987

@@ -807,12 +805,6 @@ class error_id
807805
return os << (x.value_ / 4);
808806
}
809807

810-
template <class Json>
811-
friend void to_json(Json & j, error_id x)
812-
{
813-
j["serial number"] = x.value_ / 4;
814-
}
815-
816808
BOOST_LEAF_CONSTEXPR void load_source_location_( char const * file, int line, char const * function ) const noexcept(!BOOST_LEAF_CFG_CAPTURE)
817809
{
818810
BOOST_LEAF_ASSERT(file&&*file);

include/boost/leaf/serialize.hpp

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,27 @@ struct show_in_diagnostics: std::true_type
3030

3131
class writer
3232
{
33-
virtual parsed type() const noexcept = 0;
33+
parsed const type_;
3434

3535
protected:
36-
~writer() noexcept { }
36+
37+
template <class Derived>
38+
explicit writer(Derived * d) noexcept:
39+
type_(parse<Derived>())
40+
{
41+
BOOST_LEAF_ASSERT(d == this), (void) d;
42+
}
43+
44+
~writer() noexcept
45+
{
46+
}
3747

3848
public:
39-
template <class W>
40-
W * check_type(parsed tid) noexcept
49+
50+
template <class Derived>
51+
Derived * get() noexcept
4152
{
42-
return type() == tid ? static_cast<W *>(this) : nullptr;
53+
return type_ == parse<typename std::decay<Derived>::type>() ? static_cast<Derived *>(this) : nullptr;
4354
}
4455
};
4556

@@ -86,11 +97,6 @@ class ostream_writer: public writer
8697
char const * & prefix_;
8798
char const * const delimiter_;
8899

89-
parsed type() const noexcept override
90-
{
91-
return parse<ostream_writer>();
92-
}
93-
94100
template <class T, class CharT, class Traits>
95101
static void print_name(std::basic_ostream<CharT, Traits> & os, char const * & prefix, char const * delimiter)
96102
{
@@ -135,7 +141,9 @@ class ostream_writer: public writer
135141
struct diagnostic;
136142

137143
public:
144+
138145
ostream_writer(std::ostream & os, char const * & prefix, char const * delimiter) noexcept:
146+
writer(this),
139147
os_(os),
140148
prefix_(prefix),
141149
delimiter_(delimiter)
@@ -152,7 +160,7 @@ class ostream_writer: public writer
152160
template <class Writer, class E>
153161
void serialize(Writer & w, E const & e)
154162
{
155-
if( ostream_writer * ow = w.template check_type<ostream_writer>(parse<ostream_writer>()) )
163+
if( ostream_writer * ow = w.template get<ostream_writer>() )
156164
ow->write(e);
157165
}
158166

@@ -228,54 +236,63 @@ struct ostream_writer::diagnostic<Enum, true, false, false, false, true>
228236
template <class Json, class E>
229237
auto to_json(Json & j, E const & e) -> decltype(to_json(j, e.value), void())
230238
{
231-
char zstr[256];
232-
j[parse_to_zstr<E>(zstr)] = e.value;
239+
j["value"] = e.value;
233240
}
234241

235242
#if BOOST_LEAF_CFG_STD_SYSTEM_ERROR
236243
template <class Json>
237244
void to_json(Json & j, std::error_code const & ec)
238245
{
239-
Json & v = j["std::error_code"];
240-
v["category"] = ec.category().name();
241-
v["value"] = ec.value();
242-
v["message"] = ec.message();
246+
j["category"] = ec.category().name();
247+
j["value"] = ec.value();
248+
j["message"] = ec.message();
249+
}
250+
#endif
251+
252+
#ifndef BOOST_LEAF_NO_EXCEPTIONS
253+
template <class Json>
254+
void to_json(Json & j, std::exception const & ex)
255+
{
256+
j["typeid.name"] = detail::demangler(typeid(ex).name()).get();
257+
if( char const * w = ex.what() )
258+
j["what"] = w;
259+
else
260+
j["what"] = "<<nullptr>>";
243261
}
244262
#endif
245263

246264
template <class Json>
247265
void to_json(Json & j, std::exception_ptr const & ep)
248266
{
249-
Json & v = j["std::exception_ptr"];
250267
if( ep )
251268
{
252269
#ifdef BOOST_LEAF_NO_EXCEPTIONS
253-
v["typeid.name"] = "<<unknown>>";
254-
v["what"] = "N/A";
270+
j["typeid.name"] = "<<unknown>>";
271+
j["what"] = "N/A";
255272
#else
256273
try
257274
{
258275
std::rethrow_exception(ep);
259276
}
260277
catch( std::exception const & ex )
261278
{
262-
v["typeid.name"] = detail::demangler(typeid(ex).name()).get();
279+
j["typeid.name"] = detail::demangler(typeid(ex).name()).get();
263280
if( char const * w = ex.what() )
264-
v["what"] = w;
281+
j["what"] = w;
265282
else
266-
v["what"] = "<<nullptr>>";
283+
j["what"] = "<<nullptr>>";
267284
}
268285
catch( ... )
269286
{
270-
v["typeid.name"] = "<<unknown>>";
271-
v["what"] = "N/A";
287+
j["typeid.name"] = "<<unknown>>";
288+
j["what"] = "N/A";
272289
}
273290
#endif
274291
}
275292
else
276293
{
277-
v["typeid.name"] = "<<empty>>";
278-
v["what"] = "N/A";
294+
j["typeid.name"] = "<<empty>>";
295+
j["what"] = "N/A";
279296
}
280297
}
281298

0 commit comments

Comments
 (0)