File indexing completed on 2025-09-17 08:54:06
0001
0002
0003
0004
0005
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
0024
0025
0026
0027
0028 template<class T>
0029 class OutputInterfaceAdapter final : public OutputInterface
0030 {
0031 public:
0032
0033
0034 using SPConstT = std::shared_ptr<T const>;
0035 using SPThis = std::shared_ptr<OutputInterfaceAdapter<T>>;
0036
0037
0038 public:
0039
0040 static inline SPThis
0041 from_const_ref(Category cat, std::string label, T const& obj);
0042
0043
0044 static inline SPThis
0045 from_rvalue_ref(Category cat, std::string label, T&& obj);
0046
0047
0048 inline OutputInterfaceAdapter(Category cat, std::string label, SPConstT obj);
0049
0050
0051 Category category() const final { return cat_; }
0052
0053
0054 std::string_view label() const final { return label_; }
0055
0056
0057 void output(JsonPimpl* jp) const final;
0058
0059 private:
0060 Category cat_;
0061 std::string label_;
0062 SPConstT obj_;
0063 };
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
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
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
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
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 }