Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:54:48

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