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