Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-18 09:09:39

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 //---------------------------------------------------------------------------//
0013 // MACROS
0014 //---------------------------------------------------------------------------//
0015 /*!
0016  * Load a required field into a struct.
0017  */
0018 #define CELER_JSON_LOAD_REQUIRED(OBJ, STRUCT, NAME) \
0019     OBJ.at(#NAME).get_to(STRUCT.NAME)
0020 
0021 /*!
0022  * Load an optional field.
0023  *
0024  * If the field is missing or null, it is omitted.
0025  */
0026 #define CELER_JSON_LOAD_OPTION(OBJ, STRUCT, NAME)  \
0027     do                                             \
0028     {                                              \
0029         if (auto iter = OBJ.find(#NAME);           \
0030             iter != OBJ.end() && !iter->is_null()) \
0031         {                                          \
0032             iter->get_to(STRUCT.NAME);             \
0033         }                                          \
0034     } while (0)
0035 
0036 /*!
0037  * Load an optional field.
0038  *
0039  * If the field is missing or null, it is omitted.
0040  */
0041 #define CELER_JSON_LOAD_DEPRECATED(OBJ, STRUCT, OLD, NEW)         \
0042     do                                                            \
0043     {                                                             \
0044         if (auto iter = OBJ.find(#OLD); iter != OBJ.end())        \
0045         {                                                         \
0046             ::celeritas::warn_deprecated_json_option(#OLD, #NEW); \
0047             iter->get_to(STRUCT.NEW);                             \
0048         }                                                         \
0049     } while (0)
0050 
0051 /*!
0052  * Save a field to a JSON object.
0053  */
0054 #define CELER_JSON_SAVE(OBJ, STRUCT, NAME) OBJ[#NAME] = STRUCT.NAME
0055 
0056 /*!
0057  * Save a field if the condition is met.
0058  *
0059  * If not met, a "null" placeholder is saved.
0060  */
0061 #define CELER_JSON_SAVE_WHEN(OBJ, STRUCT, NAME, COND) \
0062     do                                                \
0063     {                                                 \
0064         if ((COND))                                   \
0065         {                                             \
0066             CELER_JSON_SAVE(OBJ, STRUCT, NAME);       \
0067         }                                             \
0068         else                                          \
0069         {                                             \
0070             OBJ[#NAME] = nullptr;                     \
0071         }                                             \
0072     } while (0)
0073 
0074 /*!
0075  * Construct a key/value pair for a JSON object.
0076  */
0077 #define CELER_JSON_PAIR(STRUCT, NAME) {#NAME, STRUCT.NAME}
0078 
0079 //---------------------------------------------------------------------------//
0080 
0081 namespace celeritas
0082 {
0083 //---------------------------------------------------------------------------//
0084 // Print a warning about a deprecated input option
0085 void warn_deprecated_json_option(char const* old_name, char const* new_name);
0086 
0087 // Save a format and version marker
0088 void save_format(nlohmann::json& j, std::string const& format);
0089 
0090 // Save units
0091 void save_units(nlohmann::json& j);
0092 
0093 // Load and check for a format and compatible version marker
0094 void check_format(nlohmann::json const& j, std::string_view format);
0095 
0096 // Check units for consistency
0097 void check_units(nlohmann::json const& j, std::string_view format);
0098 
0099 //---------------------------------------------------------------------------//
0100 }  // namespace celeritas