Skip to content

Commit 9573424

Browse files
committed
serialization
1 parent a59198a commit 9573424

20 files changed

+297
-233
lines changed

include/boost/leaf/context.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
77

88
#include <boost/leaf/config.hpp>
9-
#include <boost/leaf/detail/diagnostics_writer.hpp>
9+
#include <boost/leaf/serialization/diagnostics_writer.hpp>
1010
#include <boost/leaf/error.hpp>
1111

1212
#if !defined(BOOST_LEAF_NO_THREADS) && !defined(NDEBUG)
@@ -204,7 +204,7 @@ namespace detail
204204
tuple_for_each<I-1,Tup>::unload(tup, err_id);
205205
}
206206

207-
static void write_to(writer & w, void const * tup, error_id id)
207+
static void write_to(serialization::writer & w, void const * tup, error_id id)
208208
{
209209
BOOST_LEAF_ASSERT(tup != nullptr);
210210
tuple_for_each<I-1,Tup>::write_to(w, tup, id);
@@ -218,11 +218,11 @@ namespace detail
218218
BOOST_LEAF_CONSTEXPR static void activate( Tup & ) noexcept { }
219219
BOOST_LEAF_CONSTEXPR static void deactivate( Tup & ) noexcept { }
220220
BOOST_LEAF_CONSTEXPR static void unload( Tup &, int ) noexcept { }
221-
BOOST_LEAF_CONSTEXPR static void write_to(writer &, void const *, error_id) { }
221+
BOOST_LEAF_CONSTEXPR static void write_to(serialization::writer &, void const *, error_id) { }
222222
};
223223

224224
template <class Tup>
225-
BOOST_LEAF_CONSTEXPR void serialize_tuple_contents(writer & w, void const * tup, error_id id)
225+
BOOST_LEAF_CONSTEXPR void serialize_tuple_contents(serialization::writer & w, void const * tup, error_id id)
226226
{
227227
tuple_for_each<std::tuple_size<Tup>::value, Tup>::write_to(w, tup, id);
228228
}
@@ -368,15 +368,15 @@ class context
368368
return is_active_;
369369
}
370370

371-
void write_to( writer & w ) const
371+
void write_to( serialization::writer & w ) const
372372
{
373373
detail::serialize_tuple_contents<Tup>(w, &tup_, error_id());
374374
}
375375

376376
template <class CharT, class Traits>
377377
friend std::ostream & operator<<( std::basic_ostream<CharT, Traits> & os, context const & ctx )
378378
{
379-
detail::diagnostics_writer w(os);
379+
serialization::diagnostics_writer w(os);
380380
w.set_prefix("Contents:");
381381
ctx.write_to(w);
382382
return os;

include/boost/leaf/detail/all.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@
1111
#include <boost/leaf/on_error.hpp>
1212
#include <boost/leaf/pred.hpp>
1313
#include <boost/leaf/result.hpp>
14+
#include <boost/leaf/serialization/diagnostics_writer.hpp>
15+
#include <boost/leaf/serialization/json_writer.hpp>
16+
#include <boost/leaf/serialization/writer.hpp>
1417
#include <boost/leaf/to_variant.hpp>
15-
#include <boost/leaf/writer.hpp>

include/boost/leaf/detail/capture_list.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
77

88
#include <boost/leaf/config.hpp>
9-
#include <boost/leaf/writer.hpp>
109

1110
#if BOOST_LEAF_CFG_CAPTURE
1211

1312
namespace boost { namespace leaf {
1413

14+
class error_id;
15+
namespace serialization { class writer; }
16+
1517
namespace detail
1618
{
1719

@@ -27,7 +29,7 @@ namespace detail
2729
friend class capture_list;
2830

2931
virtual void unload( int err_id ) = 0;
30-
virtual void write_to(writer &, error_id const &) const = 0;
32+
virtual void write_to(serialization::writer &, error_id const &) const = 0;
3133

3234
protected:
3335

@@ -88,7 +90,7 @@ namespace detail
8890
} );
8991
}
9092

91-
void write_to(writer & w, error_id const & id) const
93+
void write_to(serialization::writer & w, error_id const & id) const
9294
{
9395
if( first_ )
9496
{

include/boost/leaf/detail/demangle.hpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
# include <string>
2424
#endif
2525

26-
#if BOOST_LEAF_CFG_DIAGNOSTICS
27-
2826
// __has_include is currently supported by GCC and Clang. However GCC 4.9 may have issues and
2927
// returns 1 for 'defined( __has_include )', while '__has_include' is actually not supported:
3028
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63662
@@ -46,8 +44,6 @@
4644
# endif
4745
#endif
4846

49-
#endif // #if BOOST_LEAF_CFG_DIAGNOSTICS
50-
5147
namespace boost { namespace leaf {
5248

5349
namespace detail
@@ -135,6 +131,15 @@ namespace n
135131
{
136132
return os.write(pn.name, pn.len);
137133
}
134+
135+
template <std::size_t S>
136+
friend char * to_zstr(char (&zstr)[S], r const & pn) noexcept
137+
{
138+
std::size_t n = pn.len < S - 1 ? pn.len : S - 1;
139+
std::memcpy(zstr, pn.name, n);
140+
zstr[n] = 0;
141+
return zstr;
142+
}
138143
};
139144

140145
template <class T>

include/boost/leaf/detail/exception_base.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ namespace detail
2222
exception_base() noexcept { }
2323
~exception_base() noexcept { }
2424
};
25-
} // namespace detail
25+
}
2626

27-
} } // namespace boost::leaf
27+
} }
2828

2929
#endif // #ifndef BOOST_LEAF_DETAIL_EXCEPTION_BASE_HPP_INCLUDED

include/boost/leaf/diagnostics.hpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@
77

88
#include <boost/leaf/config.hpp>
99
#include <boost/leaf/context.hpp>
10-
#include <boost/leaf/detail/diagnostics_writer.hpp>
1110
#include <boost/leaf/handle_errors.hpp>
1211

1312
namespace boost { namespace leaf {
1413

1514
class diagnostic_info: public error_info
1615
{
1716
void const * tup_;
18-
void (*serialize_tuple_contents_)(writer &, void const *, error_id);
17+
void (*serialize_tuple_contents_)(serialization::writer &, void const *, error_id);
1918

2019
protected:
2120

@@ -48,7 +47,7 @@ class diagnostic_info: public error_info
4847
template <class CharT, class Traits>
4948
friend std::ostream & operator<<( std::basic_ostream<CharT, Traits> & os, diagnostic_info const & x )
5049
{
51-
detail::diagnostics_writer w(os, x.error(), x.source_location(), x.exception());
50+
serialization::diagnostics_writer w(os, x.error(), x.source_location(), x.exception());
5251
#if BOOST_LEAF_CFG_DIAGNOSTICS
5352
x.write_to_(w);
5453
#else
@@ -119,7 +118,7 @@ class diagnostic_details: public diagnostic_info
119118
template <class CharT, class Traits>
120119
friend std::ostream & operator<<( std::basic_ostream<CharT, Traits> & os, diagnostic_details const & x )
121120
{
122-
detail::diagnostics_writer w(os, x.error(), x.source_location(), x.exception());
121+
serialization::diagnostics_writer w(os, x.error(), x.source_location(), x.exception());
123122
#if BOOST_LEAF_CFG_DIAGNOSTICS
124123
x.diagnostic_info::write_to_(w);
125124
w.set_prefix("\nDiagnostic details:" BOOST_LEAF_CFG_DIAGNOSTICS_FIRST_DELIMITER);
@@ -179,7 +178,7 @@ class diagnostic_details: public diagnostic_info
179178
template <class CharT, class Traits>
180179
friend std::ostream & operator<<( std::basic_ostream<CharT, Traits> & os, diagnostic_details const & x )
181180
{
182-
detail::diagnostics_writer w(os, x.error(), x.source_location(), x.exception());
181+
serialization::diagnostics_writer w(os, x.error(), x.source_location(), x.exception());
183182
#if BOOST_LEAF_CFG_DIAGNOSTICS
184183
x.diagnostic_info::write_to_(w);
185184
os << "\nboost::leaf::diagnostic_details N/A due to BOOST_LEAF_CFG_CAPTURE=0";

include/boost/leaf/error.hpp

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@
99
#include <boost/leaf/detail/optional.hpp>
1010
#include <boost/leaf/detail/function_traits.hpp>
1111
#include <boost/leaf/detail/capture_list.hpp>
12-
13-
#if BOOST_LEAF_CFG_DIAGNOSTICS
14-
# include <boost/leaf/detail/diagnostics_writer.hpp>
15-
# include <ostream>
16-
#endif
12+
#include <boost/leaf/serialization/diagnostics_writer.hpp>
1713

1814
#include <cstring>
1915

@@ -93,21 +89,15 @@ struct show_in_diagnostics<e_source_location>: std::false_type
9389

9490
////////////////////////////////////////
9591

96-
template <class Writer, class E>
97-
void serialize(Writer &, E const &)
98-
{
99-
}
100-
10192
namespace detail
10293
{
10394
template <class Writer, class E>
10495
void serialize_(Writer & w, E const & e)
10596
{
97+
using namespace serialization;
10698
serialize(w, e);
107-
#if BOOST_LEAF_CFG_DIAGNOSTICS
108-
if( detail::diagnostics_writer * ow = w.template get<detail::diagnostics_writer>() )
99+
if( diagnostics_writer * ow = w.template get<diagnostics_writer>() )
109100
ow->write(e);
110-
#endif
111101
}
112102
}
113103

@@ -168,7 +158,7 @@ namespace detail
168158
void unload( int err_id ) noexcept(!BOOST_LEAF_CFG_CAPTURE);
169159

170160
template <class ErrorID>
171-
void write_to(writer & w, ErrorID id) const
161+
void write_to(serialization::writer & w, ErrorID id) const
172162
{
173163
if( int k = this->key() )
174164
{
@@ -236,7 +226,7 @@ namespace detail
236226
{
237227
impl::unload(err_id);
238228
}
239-
void write_to(writer & w, error_id const & id) const override
229+
void write_to(serialization::writer & w, error_id const & id) const override
240230
{
241231
impl::write_to(w, id);
242232
}
@@ -271,7 +261,7 @@ namespace detail
271261
{
272262
std::rethrow_exception(ex_);
273263
}
274-
void write_to(writer &, error_id const &) const override
264+
void write_to(serialization::writer &, error_id const &) const override
275265
{
276266
}
277267
std::exception_ptr const ex_;
@@ -447,7 +437,7 @@ namespace detail
447437
}
448438

449439
template <class ErrorID>
450-
void write_to(writer &, ErrorID) const
440+
void write_to(serialization::writer &, ErrorID) const
451441
{
452442
}
453443
}; // slot specialization for dynamic_allocator

include/boost/leaf/handle_errors.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#include <boost/leaf/config.hpp>
99
#include <boost/leaf/context.hpp>
10-
#include <boost/leaf/detail/diagnostics_writer.hpp>
10+
#include <boost/leaf/serialization/diagnostics_writer.hpp>
1111
#include <typeinfo>
1212

1313
namespace boost { namespace leaf {
@@ -91,7 +91,7 @@ class error_info
9191
template <class CharT, class Traits>
9292
friend std::ostream & operator<<(std::basic_ostream<CharT, Traits> & os, error_info const & x)
9393
{
94-
detail::diagnostics_writer w(os, x.error(), x.source_location(), x.exception());
94+
serialization::diagnostics_writer w(os, x.error(), x.source_location(), x.exception());
9595
return os;
9696
}
9797
}; // class error_info

include/boost/leaf/result.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
77

88
#include <boost/leaf/config.hpp>
9-
#include <boost/leaf/detail/capture_list.hpp>
10-
#include <boost/leaf/detail/diagnostics_writer.hpp>
119
#include <boost/leaf/exception.hpp>
10+
#include <boost/leaf/serialization/diagnostics_writer.hpp>
11+
#include <boost/leaf/detail/capture_list.hpp>
1212

1313
#include <functional>
1414

@@ -29,7 +29,7 @@ class bad_result:
2929

3030
namespace detail
3131
{
32-
template <class T, bool Printable = is_printable<T>::value>
32+
template <class T, bool Printable = serialization::is_printable<T>::value>
3333
struct result_value_printer;
3434

3535
template <class T>
@@ -336,7 +336,7 @@ class BOOST_LEAF_ATTRIBUTE_NODISCARD result
336336
result_discriminant const what = what_;
337337
BOOST_LEAF_ASSERT(what.kind() != result_discriminant::val);
338338
error_id const err_id = what.get_error_id();
339-
detail::diagnostics_writer w(os);
339+
serialization::diagnostics_writer w(os);
340340
os << "Error serial #" << err_id;
341341
if( what.kind() == result_discriminant::err_id_capture_list )
342342
{

include/boost/leaf/detail/diagnostics_writer.hpp renamed to include/boost/leaf/serialization/diagnostics_writer.hpp

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
1-
#ifndef BOOST_LEAF_DETAIL_DIAGNOSTICS_WRITER_HPP_INCLUDED
2-
#define BOOST_LEAF_DETAIL_DIAGNOSTICS_WRITER_HPP_INCLUDED
1+
#ifndef BOOST_LEAF_SERIALIZATION_DIAGNOSTICS_WRITER_HPP_INCLUDED
2+
#define BOOST_LEAF_SERIALIZATION_DIAGNOSTICS_WRITER_HPP_INCLUDED
33

44
// Copyright 2018-2025 Emil Dotchevski and Reverge Studios, Inc.
55
// Distributed under the Boost Software License, Version 1.0. (See accompanying
66
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
77

8-
#include <boost/leaf/writer.hpp>
8+
#include <boost/leaf/serialization/writer.hpp>
99
#include <boost/leaf/detail/exception_base.hpp>
1010

11-
#include <iosfwd>
11+
#include <type_traits>
12+
13+
#if BOOST_LEAF_CFG_DIAGNOSTICS
14+
# include <iostream>
15+
#else
16+
# include <iosfwd>
17+
#endif
1218

1319
namespace boost { namespace leaf {
1420

15-
namespace detail
21+
template <class E>
22+
struct show_in_diagnostics: std::integral_constant<bool, BOOST_LEAF_CFG_DIAGNOSTICS>
23+
{
24+
};
25+
26+
namespace serialization
1627
{
1728
template <class T, class E = void>
1829
struct is_printable: std::false_type
@@ -126,10 +137,10 @@ namespace detail
126137
if( ex )
127138
{
128139
os << "\nCaught:" BOOST_LEAF_CFG_DIAGNOSTICS_FIRST_DELIMITER;
129-
if( auto eb = dynamic_cast<exception_base const *>(ex) )
140+
if( auto eb = dynamic_cast<detail::exception_base const *>(ex) )
130141
os << eb->type_name();
131142
else
132-
os << demangler(typeid(*ex).name()).get();
143+
os << detail::demangler(typeid(*ex).name()).get();
133144
os << ": \"" << ex->what() << '"';
134145
}
135146
else
@@ -224,8 +235,8 @@ namespace detail
224235
}
225236
};
226237

227-
} // namespace detail
238+
} // namespace serialization
228239

229240
} } // namespace boost::leaf
230241

231-
#endif // #ifndef BOOST_LEAF_DETAIL_DIAGNOSTICS_WRITER_HPP_INCLUDED
242+
#endif // #ifndef BOOST_LEAF_SERIALIZATION_DIAGNOSTICS_WRITER_HPP_INCLUDED

0 commit comments

Comments
 (0)