Skip to content

Commit b72d2f1

Browse files
committed
Serialization refactoring
1 parent ea2e690 commit b72d2f1

30 files changed

+579
-899
lines changed

doc/leaf.adoc

Lines changed: 91 additions & 372 deletions
Large diffs are not rendered by default.

doc/zajo-dark.css

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -452,11 +452,11 @@ code{font-family:"Anonymous Pro","DejaVu Sans Mono",monospace;font-weight:400;co
452452
pre,pre>code{line-height:1.45;color:rgba(255,255,255,.67);font-family:"Anonymous Pro","DejaVu Sans Mono",monospace;font-weight:400;text-rendering:optimizeLegibility;font-size:1.05em;background-color:#101010}
453453
a:not(pre)>code:hover {color:#00cc99}
454454
kbd{font-family:"Anonymous Pro","DejaVu Sans Mono",monospace;display:inline-block;color:rgba(0,0,0,.8);font-size:.65em;line-height:1.45;background-color:#f7f7f7;border:1px solid #ccc;-webkit-border-radius:3px;border-radius:3px;-webkit-box-shadow:0 1px 0 rgba(0,0,0,.2),0 0 0 .1em white inset;box-shadow:0 1px 0 rgba(0,0,0,.2),0 0 0 .1em #fff inset;margin:0 .15em;padding:.2em .5em;vertical-align:middle;position:relative;top:-.1em;white-space:nowrap}
455-
h1 code{color:#00cc99; font-size:113%}
456-
h2 code{color:#00cc99; font-size:113%}
457-
h3 code{color:#00cc99; font-size:113%}
458-
h4 code{color:#00cc99; font-size:113%}
459-
h5 code{color:#00cc99; font-size:113%}
455+
h1 code{color:#00cc99; font-size:104%}
456+
h2 code{color:#00cc99; font-size:104%}
457+
h3 code{color:#00cc99; font-size:104%}
458+
h4 code{color:#00cc99; font-size:104%}
459+
h5 code{color:#00cc99; font-size:104%}
460460
#header>h1:first-child{font-family:"Poiret One";color:#00cc99;margin-top:2.25rem;margin-bottom:0;letter-spacing:-.07em}
461461
#author{color:#a366ff}
462462
#toc ul{font-family:"Quicksand","DejaVu Sans",sans-serif;list-style-type:none}

doc/zajo-light.css

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -446,11 +446,11 @@ code{font-family:"Anonymous Pro","DejaVu Sans Mono",monospace;font-weight:400;co
446446
pre,pre>code{line-height:1.45;color:rgba(0,0,0,.9);font-family:"Anonymous Pro","DejaVu Sans Mono",monospace;font-weight:400;text-rendering:optimizeLegibility;font-size:1.05em;background-color:#f7f8f7}
447447
a:not(pre)>code:hover {color:#4101a7}
448448
kbd{font-family:"Anonymous Pro","DejaVu Sans Mono",monospace;display:inline-block;color:rgba(0,0,0,.8);font-size:.65em;line-height:1.45;background-color:#f7f7f7;border:1px solid #ccc;-webkit-border-radius:3px;border-radius:3px;-webkit-box-shadow:0 1px 0 rgba(0,0,0,.2),0 0 0 .1em white inset;box-shadow:0 1px 0 rgba(0,0,0,.2),0 0 0 .1em #fff inset;margin:0 .15em;padding:.2em .5em;vertical-align:middle;position:relative;top:-.1em;white-space:nowrap}
449-
h1 code{color:#4101a7; font-size:113%}
450-
h2 code{color:#4101a7; font-size:113%}
451-
h3 code{color:#4101a7; font-size:113%}
452-
h4 code{color:#4101a7; font-size:113%}
453-
h5 code{color:#4101a7; font-size:113%}
449+
h1 code{color:#4101a7; font-size:104%}
450+
h2 code{color:#4101a7; font-size:104%}
451+
h3 code{color:#4101a7; font-size:104%}
452+
h4 code{color:#4101a7; font-size:104%}
453+
h5 code{color:#4101a7; font-size:104%}
454454
#header>h1:first-child{font-family:"Poiret One";color:#ff5100;margin-top:2.25rem;margin-bottom:0;letter-spacing:-.07em}
455455
#author{color: #4101a7;}
456456
#toc ul{font-family:"Quicksand","DejaVu Sans",sans-serif;list-style-type:none}

include/boost/leaf/common.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ struct e_errno
5757
{
5858
return os << err.value << ", \"" << std::strerror(err.value) << '"';
5959
}
60+
61+
template <class Writer>
62+
friend void write( Writer & w, e_errno const & e )
63+
{
64+
write_nested(w, e.value, "errno");
65+
write_nested(w, std::strerror(e.value), "strerror");
66+
}
6067
};
6168

6269
struct e_type_info_name { char const * value; };

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/serialization/diagnostics_writer.hpp>
9+
#include <boost/leaf/detail/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(serialization::writer & w, void const * tup, error_id id)
207+
static void write_to(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(serialization::writer &, void const *, error_id) { }
221+
BOOST_LEAF_CONSTEXPR static void write_to(writer &, void const *, error_id) { }
222222
};
223223

224224
template <class Tup>
225-
BOOST_LEAF_CONSTEXPR void serialize_tuple_contents(serialization::writer & w, void const * tup, error_id id)
225+
void serialize_tuple_contents(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( serialization::writer & w ) const
371+
void write_to( detail::diagnostics_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-
serialization::diagnostics_writer w(os);
379+
detail::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: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,5 @@
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>
14+
#include <boost/leaf/serialization/nlohmann_writer.hpp>
1715
#include <boost/leaf/to_variant.hpp>

include/boost/leaf/detail/capture_list.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
namespace boost { namespace leaf {
1313

1414
class error_id;
15-
namespace serialization { class writer; }
1615

1716
namespace detail
1817
{
18+
class writer;
1919

2020
class capture_list
2121
{
@@ -29,7 +29,7 @@ namespace detail
2929
friend class capture_list;
3030

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

3434
protected:
3535

@@ -90,7 +90,7 @@ namespace detail
9090
} );
9191
}
9292

93-
void write_to(serialization::writer & w, error_id const & id) const
93+
void write_to(writer & w, error_id const & id) const
9494
{
9595
if( first_ )
9696
{

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

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

44
// Copyright 2018-2026 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

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

1212
#include <type_traits>
@@ -28,7 +28,7 @@ struct show_in_diagnostics: std::integral_constant<bool, BOOST_LEAF_CFG_DIAGNOST
2828
{
2929
};
3030

31-
namespace serialization
31+
namespace detail
3232
{
3333
template <class T, class E = void>
3434
struct is_printable: std::false_type
@@ -50,16 +50,6 @@ namespace serialization
5050
{
5151
};
5252

53-
template <class T, class E = void>
54-
struct has_member_value: std::false_type
55-
{
56-
};
57-
58-
template <class T>
59-
struct has_member_value<T, decltype((void)std::declval<T const &>().value)>: std::true_type
60-
{
61-
};
62-
6353
////////////////////////////////////////
6454

6555
class diagnostics_writer: public writer
@@ -79,7 +69,7 @@ namespace serialization
7969
BOOST_LEAF_ASSERT(delimiter);
8070
char const * p = prefix;
8171
prefix = nullptr;
82-
os << (p ? p : delimiter) << get_type_name<T>();
72+
os << (p ? p : delimiter) << detail::get_type_name<T>();
8373
}
8474

8575
template <class T, class PrintableInfo, class CharT, class Traits>
@@ -171,10 +161,10 @@ namespace serialization
171161
delimiter_ = delimiter;
172162
}
173163

174-
template <class E>
175-
void write(E const & e)
164+
template <class T>
165+
void write(T const & x)
176166
{
177-
diagnostic<E>::print(os_, prefix_, delimiter_, e);
167+
diagnostic<T>::print(os_, prefix_, delimiter_, x);
178168
}
179169
}; // class diagnostics_writer
180170

@@ -245,8 +235,8 @@ namespace serialization
245235
}
246236
};
247237

248-
} // namespace serialization
238+
} // namespace detail
249239

250240
} } // namespace boost::leaf
251241

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

include/boost/leaf/detail/exception_base.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@
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/serialization/type_name.hpp>
109

1110
namespace boost { namespace leaf {
1211

1312
class error_id;
1413

1514
namespace detail
1615
{
16+
struct type_name;
17+
1718
class exception_base
1819
{
1920
public:
2021
virtual error_id get_error_id() const noexcept = 0;
21-
virtual serialization::type_name get_type_name() const = 0;
22+
virtual type_name get_type_name() const = 0;
2223
protected:
2324
exception_base() noexcept { }
2425
~exception_base() noexcept { }
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#ifndef BOOST_LEAF_DETAIL_TYPE_NAME_HPP_INCLUDED
2+
#define BOOST_LEAF_DETAIL_TYPE_NAME_HPP_INCLUDED
3+
4+
// Copyright 2018-2026 Emil Dotchevski and Reverge Studios, Inc.
5+
// Distributed under the Boost Software License, Version 1.0. (See accompanying
6+
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7+
8+
#include <boost/leaf/detail/demangle.hpp>
9+
10+
namespace boost { namespace leaf {
11+
12+
namespace detail
13+
{
14+
struct type_name
15+
{
16+
char const * name_not_zero_terminated_at_length;
17+
std::size_t length;
18+
std::size_t hash;
19+
20+
friend bool operator==(type_name const & a, type_name const & b) noexcept
21+
{
22+
BOOST_LEAF_ASSERT((a.hash == b.hash) == (a.length == b.length && std::memcmp(a.name_not_zero_terminated_at_length, b.name_not_zero_terminated_at_length, a.length) == 0));
23+
return a.hash == b.hash;
24+
}
25+
26+
friend bool operator!=(type_name const & a, type_name const & b) noexcept
27+
{
28+
return !(a == b);
29+
}
30+
31+
template <class CharT, class Traits>
32+
friend std::ostream & operator<<(std::basic_ostream<CharT, Traits> & os, type_name const & x)
33+
{
34+
return os.write(x.name_not_zero_terminated_at_length, x.length);
35+
}
36+
37+
template <std::size_t S>
38+
friend char * to_zstr(char (&zstr)[S], type_name const & x) noexcept
39+
{
40+
std::size_t n = x.length < S - 1 ? x.length : S - 1;
41+
std::memcpy(zstr, x.name_not_zero_terminated_at_length, n);
42+
zstr[n] = 0;
43+
return zstr;
44+
}
45+
};
46+
47+
template <class T>
48+
type_name get_type_name()
49+
{
50+
n::r parsed = n::p<T>();
51+
return { parsed.name_not_zero_terminated_at_length, parsed.length, parsed.hash };
52+
}
53+
} // namespace detail
54+
55+
} } // namespace boost::leaf
56+
57+
#endif // #ifndef BOOST_LEAF_DETAIL_TYPE_NAME_HPP_INCLUDED

0 commit comments

Comments
 (0)