Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //----------------------------------*-C++-*----------------------------------//
0002 // Copyright 2021-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/cont/ArrayIO.json.hh
0007 //---------------------------------------------------------------------------//
0008 #pragma once
0009 
0010 #include <nlohmann/json.hpp>
0011 
0012 #include "corecel/Assert.hh"
0013 
0014 #include "Array.hh"
0015 
0016 namespace celeritas
0017 {
0018 //---------------------------------------------------------------------------//
0019 /*!
0020  * Read an array from a JSON file.
0021  */
0022 template<class T, size_type N>
0023 void from_json(nlohmann::json const& j, Array<T, N>& value)
0024 {
0025     CELER_VALIDATE(j.size() == N,
0026                    << "unexpected array size (" << j.size()
0027                    << ") in JSON input: should be " << N);
0028     for (size_type i = 0; i != N; ++i)
0029     {
0030         value[i] = j[i].get<T>();
0031     }
0032 }
0033 
0034 //---------------------------------------------------------------------------//
0035 /*!
0036  * Write an array to a JSON file.
0037  */
0038 template<class T, size_type N>
0039 void to_json(nlohmann::json& j, Array<T, N> const& value)
0040 {
0041     j = nlohmann::json::array();
0042     for (size_type i = 0; i != N; ++i)
0043     {
0044         j.push_back(value[i]);
0045     }
0046 }
0047 
0048 //---------------------------------------------------------------------------//
0049 }  // namespace celeritas