Skip to content

Commit 37f4cd5

Browse files
committed
nlohmann json output support
1 parent 1c5dfc1 commit 37f4cd5

21 files changed

+730
-218
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,8 @@ jobs:
261261
- name: Generate headers
262262
run: |
263263
cd ../boost-root/libs/leaf
264-
python3 gen/generate_single_header.py -i include/boost/leaf/detail/all.hpp -p include -o test/leaf.hpp boost/leaf
264+
python3 scripts/generate_single_header.py -i include/boost/leaf/detail/all.hpp -p include -o test/leaf.hpp boost/leaf
265+
python3 scripts/download_nlohmann_json.py
265266
266267
- name: Run tests
267268
run: |
@@ -329,7 +330,8 @@ jobs:
329330
- name: Generate headers
330331
run: |
331332
cd ../boost-root/libs/leaf
332-
python3 gen/generate_single_header.py -i include/boost/leaf/detail/all.hpp -p include -o test/leaf.hpp boost/leaf
333+
python3 scripts/generate_single_header.py -i include/boost/leaf/detail/all.hpp -p include -o test/leaf.hpp boost/leaf
334+
python3 scripts/download_nlohmann_json.py
333335
334336
- name: Run tests
335337
shell: cmd

.github/workflows/gh-pages.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ jobs:
4242
run: |
4343
REF=$(git show-ref $GITHUB_REF --hash)
4444
cd ../boost-root/libs/leaf
45-
python gen/generate_single_header.py -i include/boost/leaf/detail/all.hpp -p include -o doc/html/leaf.hpp boost/leaf --hash "$REF"
45+
python scripts/generate_single_header.py -i include/boost/leaf/detail/all.hpp -p include -o doc/html/leaf.hpp boost/leaf --hash "$REF"
4646
4747
- name: Deploy to GitHub Pages
4848
uses: JamesIves/github-pages-deploy-action@4.0.0

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ subprojects/*/
1515
.vscode/settings.json
1616
.vscode/c_cpp_properties.json
1717
test/leaf.hpp
18+
test/nlohmann/*
1819
doc/leaf.html

.vscode/tasks.json

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,13 @@
4848
{
4949
"label": "Generate leaf.hpp",
5050
"type": "shell",
51-
"command": "cd ${workspaceRoot} && python gen/generate_single_header.py -i include/boost/leaf/detail/all.hpp -p ${workspaceRoot}/include -o ${workspaceRoot}/test/leaf.hpp boost/leaf",
51+
"command": "cd ${workspaceRoot} && python scripts/generate_single_header.py -i include/boost/leaf/detail/all.hpp -p ${workspaceRoot}/include -o ${workspaceRoot}/test/leaf.hpp boost/leaf",
52+
"problemMatcher": []
53+
},
54+
{
55+
"label": "Download nlohmann/json.hpp",
56+
"type": "shell",
57+
"command": "cd ${workspaceRoot} && python scripts/download_nlohmann_json.py",
5258
"problemMatcher": []
5359
},
5460
{
@@ -81,7 +87,8 @@
8187
"label": "Run all unit tests (debug)",
8288
"type": "shell",
8389
"dependsOn": [
84-
"Generate leaf.hpp"
90+
"Generate leaf.hpp",
91+
"Download nlohmann/json.hpp"
8592
],
8693
"command": "cd ${workspaceRoot}/_bld/debug && ninja && meson test && cd ${workspaceRoot}/_bld/debug && ninja && meson test",
8794
"problemMatcher": {
@@ -106,7 +113,8 @@
106113
"label": "Run all unit tests (release)",
107114
"type": "shell",
108115
"dependsOn": [
109-
"Generate leaf.hpp"
116+
"Generate leaf.hpp",
117+
"Download nlohmann/json.hpp"
110118
],
111119
"command": "cd ${workspaceRoot}/_bld/release && ninja && meson test && cd ${workspaceRoot}/_bld/release_single_header && ninja && meson test",
112120
"problemMatcher": {
@@ -131,7 +139,8 @@
131139
"label": "Run all unit tests (b2, all configurations)",
132140
"type": "shell",
133141
"dependsOn": [
134-
"Generate leaf.hpp"
142+
"Generate leaf.hpp",
143+
"Download nlohmann/json.hpp"
135144
],
136145
"command": "../../b2 --abbreviate-paths test link=shared,static variant=debug,release,leaf_debug_diag0,leaf_release_diag0,leaf_debug_single_header,leaf_release_single_header,leaf_debug_embedded,leaf_release_embedded exception-handling=off rtti=off cxxstd=11,14,1z,17 && ../../b2 --abbreviate-paths test link=shared,static variant=debug,release,leaf_debug_diag0,leaf_release_diag0,leaf_debug_single_header,leaf_release_single_header exception-handling=on,off cxxstd=11,14,1z,17",
137146
"problemMatcher": {

include/boost/leaf/common.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ struct e_errno
5858
{
5959
return os << err.value << ", \"" << std::strerror(err.value) << '"';
6060
}
61+
62+
template <class Json>
63+
friend void to_json(Json & j, e_errno const & e)
64+
{
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);
69+
}
6170
};
6271

6372
struct e_type_info_name { char const * value; };

include/boost/leaf/context.hpp

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,11 @@ namespace detail
203203
tuple_for_each<I-1,Tup>::unload(tup, err_id);
204204
}
205205

206-
template <class CharT, class Traits>
207-
static void print(std::basic_ostream<CharT, Traits> & os, void const * tup, error_id to_print, char const * & prefix)
206+
static void write_to(writer & w, void const * tup, error_id id)
208207
{
209208
BOOST_LEAF_ASSERT(tup != nullptr);
210-
tuple_for_each<I-1,Tup>::print(os, tup, to_print, prefix);
211-
std::get<I-1>(*static_cast<Tup const *>(tup)).print(os, to_print, prefix);
209+
tuple_for_each<I-1,Tup>::write_to(w, tup, id);
210+
std::get<I-1>(*static_cast<Tup const *>(tup)).write_to(w, id);
212211
}
213212
};
214213

@@ -218,14 +217,13 @@ namespace detail
218217
BOOST_LEAF_CONSTEXPR static void activate( Tup & ) noexcept { }
219218
BOOST_LEAF_CONSTEXPR static void deactivate( Tup & ) noexcept { }
220219
BOOST_LEAF_CONSTEXPR static void unload( Tup &, int ) noexcept { }
221-
template <class CharT, class Traits>
222-
BOOST_LEAF_CONSTEXPR static void print(std::basic_ostream<CharT, Traits> &, void const *, error_id, char const * &) { }
220+
BOOST_LEAF_CONSTEXPR static void write_to(writer &, void const *, error_id) { }
223221
};
224222

225-
template <class Tup, class CharT, class Traits>
226-
BOOST_LEAF_CONSTEXPR void print_tuple_contents(std::basic_ostream<CharT, Traits> & os, void const * tup, error_id to_print, char const * & prefix)
223+
template <class Tup>
224+
BOOST_LEAF_CONSTEXPR void serialize_tuple_contents(writer & w, void const * tup, error_id id)
227225
{
228-
tuple_for_each<std::tuple_size<Tup>::value, Tup>::print(os, tup, to_print, prefix);
226+
tuple_for_each<std::tuple_size<Tup>::value, Tup>::write_to(w, tup, id);
229227
}
230228
} // namespace detail
231229

@@ -369,17 +367,17 @@ class context
369367
return is_active_;
370368
}
371369

372-
template <class CharT, class Traits>
373-
void print( std::basic_ostream<CharT, Traits> & os ) const
370+
void write_to( writer & w ) const
374371
{
375-
char const * prefix = "Contents:";
376-
detail::print_tuple_contents<Tup>(os, &tup_, error_id(), prefix);
372+
detail::serialize_tuple_contents<Tup>(w, &tup_, error_id());
377373
}
378374

379375
template <class CharT, class Traits>
380376
friend std::ostream & operator<<( std::basic_ostream<CharT, Traits> & os, context const & ctx )
381377
{
382-
ctx.print(os);
378+
char const * prefix = "Contents:";
379+
ostream_writer w(os, prefix, BOOST_LEAF_CFG_DIAGNOSTICS_DELIMITER);
380+
ctx.write_to(w);
383381
return os << '\n';
384382
}
385383

include/boost/leaf/detail/all.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Distributed under the Boost Software License, Version 1.0. (See accompanying
33
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
44

5-
#include <boost/leaf/config.hpp>
65
#include <boost/leaf/common.hpp>
76
#include <boost/leaf/context.hpp>
87
#include <boost/leaf/diagnostics.hpp>
@@ -12,4 +11,5 @@
1211
#include <boost/leaf/on_error.hpp>
1312
#include <boost/leaf/pred.hpp>
1413
#include <boost/leaf/result.hpp>
14+
#include <boost/leaf/serialize.hpp>
1515
#include <boost/leaf/to_variant.hpp>

include/boost/leaf/detail/capture_list.hpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@
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/print.hpp>
9+
#include <boost/leaf/serialize.hpp>
1010

1111
#if BOOST_LEAF_CFG_CAPTURE
1212

13-
#include <iosfwd>
14-
1513
namespace boost { namespace leaf {
1614

1715
namespace detail
@@ -30,7 +28,7 @@ namespace detail
3028

3129
virtual void unload( int err_id ) = 0;
3230
#if BOOST_LEAF_CFG_DIAGNOSTICS
33-
virtual void print(std::ostream &, error_id const & to_print, char const * & prefix) const = 0;
31+
virtual void write_to(writer &, error_id const &) const = 0;
3432
#endif
3533

3634
protected:
@@ -92,22 +90,20 @@ namespace detail
9290
} );
9391
}
9492

95-
template <class CharT, class Traits>
96-
void print(std::basic_ostream<CharT, Traits> & os, error_id const & to_print, char const * & prefix) const
93+
void write_to(writer & w, error_id const & id) const
9794
{
9895
#if BOOST_LEAF_CFG_DIAGNOSTICS
9996
if( first_ )
10097
{
10198
for_each(
102-
[&os, &to_print, &prefix]( node const & n )
99+
[&w, &id]( node const & n )
103100
{
104-
n.print(os, to_print, prefix);
101+
n.write_to(w, id);
105102
} );
106103
}
107104
#else
108-
(void) os;
109-
(void) prefix;
110-
(void) to_print;
105+
(void) w;
106+
(void) id;
111107
#endif
112108
}
113109
}; // class capture_list

include/boost/leaf/detail/demangle.hpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <boost/leaf/config.hpp>
1818
#include <iosfwd>
1919
#include <cstdlib>
20+
#include <cstring>
2021

2122
#if BOOST_LEAF_CFG_DIAGNOSTICS
2223

@@ -118,12 +119,13 @@ namespace n
118119
struct r
119120
{
120121
char const * name;
121-
int len;
122-
r(char const * name, int len) noexcept:
123-
name(name),
124-
len(len)
122+
std::size_t len;
123+
124+
friend bool operator==(r const & a, r const & b) noexcept
125125
{
126+
return a.len == b.len && std::memcmp(a.name, b.name, a.len) == 0;
126127
}
128+
127129
template <class CharT, class Traits>
128130
friend std::ostream & operator<<(std::basic_ostream<CharT, Traits> & os, r const & pn)
129131
{
@@ -215,6 +217,16 @@ parsed parse()
215217
return n::p<T>();
216218
}
217219

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+
218230
} } // namespace boost::leaf
219231

220232
////////////////////////////////////////

0 commit comments

Comments
 (0)