forked from celeritas-project/celeritas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsonPimpl.hh
66 lines (61 loc) · 1.61 KB
/
JsonPimpl.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//----------------------------------*-C++-*----------------------------------//
// Copyright 2022-2024 UT-Battelle, LLC, and other Celeritas developers.
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: (Apache-2.0 OR MIT)
//---------------------------------------------------------------------------//
//! \file corecel/io/JsonPimpl.hh
//---------------------------------------------------------------------------//
#pragma once
#include "celeritas_config.h"
#include "corecel/Assert.hh"
#if CELERITAS_USE_JSON
# include <nlohmann/json.hpp>
#endif
namespace celeritas
{
//---------------------------------------------------------------------------//
/*!
* Wrapper class for exporting JSON output.
*
* The caller is expected to use the value directly, so replace \c obj with the
* JSON object.
*
* \code
void output(JsonPimpl* json) const
{
#if CELERITAS_USE_JSON
json->obj = value_;
#else
CELER_DISCARD(json);
#endif
}
* \endcode
*/
struct JsonPimpl
{
#if CELERITAS_USE_JSON
nlohmann::json obj;
#else
JsonPimpl() = delete;
#endif
};
//---------------------------------------------------------------------------//
/*!
* Helper function to write an object to JSON.
*
* This hides the "not configured" boilerplate.
*/
template<class T>
void to_json_pimpl(JsonPimpl* jp, T const& self)
{
#if CELERITAS_USE_JSON
CELER_EXPECT(jp);
to_json(jp->obj, self);
#else
CELER_DISCARD(jp);
CELER_DISCARD(self);
CELER_NOT_CONFIGURED("JSON");
#endif
}
//---------------------------------------------------------------------------//
} // namespace celeritas