@@ -30,16 +30,27 @@ struct show_in_diagnostics: std::true_type
3030
3131class writer
3232{
33- virtual parsed type () const noexcept = 0 ;
33+ parsed const type_ ;
3434
3535protected:
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
3848public:
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
137143public:
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
152160template <class Writer , class E >
153161void 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>
228236template <class Json , class E >
229237auto 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
236243template <class Json >
237244void 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
246264template <class Json >
247265void 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