Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:03:42

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