Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 08:54:06

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/OutputInterfaceAdapter.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <memory>
0010 #include <string>
0011 
0012 #include "corecel/Config.hh"
0013 
0014 #include "corecel/Assert.hh"
0015 
0016 #include "JsonPimpl.hh"
0017 #include "OutputInterface.hh"
0018 
0019 namespace celeritas
0020 {
0021 //---------------------------------------------------------------------------//
0022 /*!
0023  * Adapter class for writing a JSON-serializable data to output.
0024  *
0025  * This class is to be used only when JSON is available and when a \c to_json
0026  * free function has been defined for \c T.
0027  */
0028 template<class T>
0029 class OutputInterfaceAdapter final : public OutputInterface
0030 {
0031   public:
0032     //!@{
0033     //! \name Type aliases
0034     using SPConstT = std::shared_ptr<T const>;
0035     using SPThis = std::shared_ptr<OutputInterfaceAdapter<T>>;
0036     //!@}
0037 
0038   public:
0039     // DANGEROUS helper function
0040     static inline SPThis
0041     from_const_ref(Category cat, std::string label, T const& obj);
0042 
0043     // Construct by capturing an object
0044     static inline SPThis
0045     from_rvalue_ref(Category cat, std::string label, T&& obj);
0046 
0047     // Construct from category, label, and shared pointer
0048     inline OutputInterfaceAdapter(Category cat, std::string label, SPConstT obj);
0049 
0050     //! Category of data to write
0051     Category category() const final { return cat_; }
0052 
0053     //! Label of the entry inside the category.
0054     std::string_view label() const final { return label_; }
0055 
0056     // Write output to the given JSON object
0057     void output(JsonPimpl* jp) const final;
0058 
0059   private:
0060     Category cat_;
0061     std::string label_;
0062     SPConstT obj_;
0063 };
0064 
0065 //---------------------------------------------------------------------------//
0066 // INLINE DEFINITIONS
0067 //---------------------------------------------------------------------------//
0068 /*!
0069  * Construct from a long-lived const reference.
0070  *
0071  * This is a dangerous but sometimes necessary way to make an interface
0072  * adapter. The object's lifespan *must be guaranteed* to exceed that of the
0073  * \c OutputManager.
0074  *
0075  * Example: \code
0076  * output_manager.insert(OutputInterfaceAdapter<Device>::from_const_ref(
0077  *      OutputInterface::Category::system,
0078  *      "device",
0079  *      celeritas::device()));
0080  * \endcode
0081  */
0082 template<class T>
0083 auto OutputInterfaceAdapter<T>::from_const_ref(Category cat,
0084                                                std::string label,
0085                                                T const& obj) -> SPThis
0086 {
0087     auto null_deleter = [](T const*) {};
0088 
0089     return std::make_shared<OutputInterfaceAdapter<T>>(
0090         cat, std::move(label), std::shared_ptr<T const>(&obj, null_deleter));
0091 }
0092 
0093 //---------------------------------------------------------------------------//
0094 /*!
0095  * Construct by capturing an object.
0096  */
0097 template<class T>
0098 auto OutputInterfaceAdapter<T>::from_rvalue_ref(Category cat,
0099                                                 std::string label,
0100                                                 T&& obj) -> SPThis
0101 {
0102     return std::make_shared<OutputInterfaceAdapter<T>>(
0103         cat, std::move(label), std::make_shared<T>(std::move(obj)));
0104 }
0105 
0106 //---------------------------------------------------------------------------//
0107 /*!
0108  * Construct from category, label, and shared pointer.
0109  */
0110 template<class T>
0111 OutputInterfaceAdapter<T>::OutputInterfaceAdapter(Category cat,
0112                                                   std::string label,
0113                                                   SPConstT obj)
0114     : cat_(cat), label_(std::move(label)), obj_(std::move(obj))
0115 {
0116     CELER_EXPECT(cat != Category::size_);
0117     CELER_EXPECT(obj_);
0118 }
0119 
0120 //---------------------------------------------------------------------------//
0121 /*!
0122  * Write output to the given JSON object.
0123  */
0124 template<class T>
0125 void OutputInterfaceAdapter<T>::output(JsonPimpl* j) const
0126 {
0127     CELER_EXPECT(j);
0128     to_json(j->obj, *obj_);
0129 }
0130 
0131 //---------------------------------------------------------------------------//
0132 }  // namespace celeritas