Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-01-07 10:02:04

0001 //------------------------------- -*- C++ -*- -------------------------------//
0002 // Copyright Celeritas contributors: see top-level COPYRIGHT file for details
0003 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0004 //---------------------------------------------------------------------------//
0005 //! \file corecel/io/JsonUtils.json.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <string_view>
0010 #include <nlohmann/json.hpp>
0011 
0012 #include "corecel/io/EnumStringMapper.hh"
0013 #include "corecel/io/Logger.hh"
0014 
0015 //---------------------------------------------------------------------------//
0016 // MACROS
0017 //---------------------------------------------------------------------------//
0018 /*!
0019  * Load a required field into a struct.
0020  */
0021 #define CELER_JSON_LOAD_REQUIRED(OBJ, STRUCT, NAME) \
0022     OBJ.at(#NAME).get_to(STRUCT.NAME)
0023 
0024 /*!
0025  * Load an optional field.
0026  *
0027  * If the field is missing or null, it is omitted.
0028  */
0029 #define CELER_JSON_LOAD_OPTION(OBJ, STRUCT, NAME)  \
0030     do                                             \
0031     {                                              \
0032         if (auto iter = OBJ.find(#NAME);           \
0033             iter != OBJ.end() && !iter->is_null()) \
0034         {                                          \
0035             iter->get_to(STRUCT.NAME);             \
0036         }                                          \
0037     } while (0)
0038 
0039 /*!
0040  * Load a field if present and set a default value otherwise.
0041  */
0042 #define CELER_JSON_LOAD_DEFAULT(OBJ, STRUCT, NAME, DEFAULT)                 \
0043     do                                                                      \
0044     {                                                                       \
0045         if (auto iter = OBJ.find(#NAME);                                    \
0046             iter != OBJ.end() && !iter->is_null())                          \
0047         {                                                                   \
0048             iter->get_to(STRUCT.NAME);                                      \
0049         }                                                                   \
0050         else                                                                \
0051         {                                                                   \
0052             STRUCT.NAME = DEFAULT;                                          \
0053             CELER_LOG(debug) << "Set '" << #NAME << "' to " << STRUCT.NAME; \
0054         }                                                                   \
0055     } while (0)
0056 
0057 /*!
0058  * Load an optional field.
0059  *
0060  * If the field is missing or null, it is omitted.
0061  */
0062 #define CELER_JSON_LOAD_DEPRECATED(OBJ, STRUCT, OLD, NEW)         \
0063     do                                                            \
0064     {                                                             \
0065         if (auto iter = OBJ.find(#OLD); iter != OBJ.end())        \
0066         {                                                         \
0067             ::celeritas::warn_deprecated_json_option(#OLD, #NEW); \
0068             iter->get_to(STRUCT.NEW);                             \
0069         }                                                         \
0070     } while (0)
0071 
0072 /*!
0073  * Save a field to a JSON object.
0074  */
0075 #define CELER_JSON_SAVE(OBJ, STRUCT, NAME) OBJ[#NAME] = STRUCT.NAME
0076 
0077 /*!
0078  * Save a field if the condition is met.
0079  *
0080  * If not met, a "null" placeholder is saved.
0081  */
0082 #define CELER_JSON_SAVE_WHEN(OBJ, STRUCT, NAME, COND) \
0083     do                                                \
0084     {                                                 \
0085         if ((COND))                                   \
0086         {                                             \
0087             CELER_JSON_SAVE(OBJ, STRUCT, NAME);       \
0088         }                                             \
0089         else                                          \
0090         {                                             \
0091             OBJ[#NAME] = nullptr;                     \
0092         }                                             \
0093     } while (0)
0094 
0095 /*!
0096  * Construct a key/value pair for a JSON object.
0097  */
0098 #define CELER_JSON_PAIR(STRUCT, NAME) {#NAME, STRUCT.NAME}
0099 
0100 //---------------------------------------------------------------------------//
0101 
0102 namespace celeritas
0103 {
0104 //---------------------------------------------------------------------------//
0105 // Print a warning about a deprecated input option
0106 void warn_deprecated_json_option(char const* old_name, char const* new_name);
0107 
0108 // Save a format and version marker
0109 void save_format(nlohmann::json& j, std::string const& format);
0110 
0111 // Save units
0112 void save_units(nlohmann::json& j);
0113 
0114 // Load and check for a format and compatible version marker
0115 void check_format(nlohmann::json const& j, std::string_view format);
0116 
0117 // Check units for consistency
0118 void check_units(nlohmann::json const& j, std::string_view format);
0119 
0120 //---------------------------------------------------------------------------//
0121 }  // namespace celeritas