File indexing completed on 2025-01-18 10:10:48
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef ROOT_RDF_RRESULTHANDLE
0012 #define ROOT_RDF_RRESULTHANDLE
0013
0014 #include "ROOT/RResultPtr.hxx"
0015 #include "ROOT/RDF/RLoopManager.hxx"
0016 #include "ROOT/RDF/RActionBase.hxx"
0017 #include "ROOT/RDF/Utils.hxx" // TypeID2TypeName
0018
0019 #include <memory>
0020 #include <sstream>
0021 #include <typeinfo>
0022 #include <stdexcept> // std::runtime_error
0023
0024 namespace ROOT {
0025 namespace RDF {
0026
0027
0028
0029
0030
0031
0032
0033 class RResultHandle {
0034 ROOT::Detail::RDF::RLoopManager *fLoopManager = nullptr;
0035
0036
0037 std::shared_ptr<ROOT::Internal::RDF::RActionBase> fActionPtr;
0038 std::shared_ptr<void> fObjPtr;
0039
0040
0041 std::shared_ptr<ROOT::Internal::RDF::RActionBase> fVariedActionPtr;
0042 const std::type_info *fType = nullptr;
0043
0044
0045 friend unsigned int RunGraphs(std::vector<RResultHandle>);
0046
0047
0048
0049
0050 void* Get()
0051 {
0052 if (!fActionPtr->HasRun())
0053 fLoopManager->Run();
0054 return fObjPtr.get();
0055 }
0056
0057
0058 void CheckType(const std::type_info& type)
0059 {
0060 if (*fType != type) {
0061 std::stringstream ss;
0062 ss << "Got the type " << ROOT::Internal::RDF::TypeID2TypeName(type)
0063 << " but the RResultHandle refers to a result of type " << ROOT::Internal::RDF::TypeID2TypeName(*fType)
0064 << ".";
0065 throw std::runtime_error(ss.str());
0066 }
0067 }
0068
0069 void ThrowIfNull()
0070 {
0071 if (fObjPtr == nullptr)
0072 throw std::runtime_error("Trying to access the contents of a null RResultHandle.");
0073 }
0074
0075 public:
0076 template <class T>
0077 RResultHandle(const RResultPtr<T> &resultPtr) : fLoopManager(resultPtr.fLoopManager), fActionPtr(resultPtr.fActionPtr), fObjPtr(resultPtr.fObjPtr), fType(&typeid(T)) {}
0078 template <class T>
0079 RResultHandle(const Experimental::RResultMap<T> &resultMap)
0080 : fLoopManager(resultMap.fLoopManager),
0081 fActionPtr(resultMap.fNominalAction),
0082 fObjPtr(resultMap.fMap.at("nominal")),
0083 fVariedActionPtr(resultMap.fVariedAction),
0084 fType(&typeid(T))
0085 {
0086 }
0087
0088 RResultHandle(const RResultHandle&) = default;
0089 RResultHandle(RResultHandle&&) = default;
0090 RResultHandle() = default;
0091
0092
0093
0094
0095
0096 template <class T>
0097 T* GetPtr()
0098 {
0099 ThrowIfNull();
0100 CheckType(typeid(T));
0101 return static_cast<T*>(Get());
0102 }
0103
0104
0105
0106
0107
0108 template <class T>
0109 const T& GetValue()
0110 {
0111 ThrowIfNull();
0112 CheckType(typeid(T));
0113 return *static_cast<T*>(Get());
0114 }
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125 bool IsReady() const {
0126 if (fActionPtr == nullptr)
0127 return false;
0128 return fActionPtr->HasRun();
0129 }
0130
0131 bool operator==(const RResultHandle &rhs) const
0132 {
0133 return fObjPtr == rhs.fObjPtr && fVariedActionPtr == rhs.fVariedActionPtr;
0134 }
0135
0136 bool operator!=(const RResultHandle &rhs) const
0137 {
0138 return !(fObjPtr == rhs.fObjPtr) && fVariedActionPtr == rhs.fVariedActionPtr;
0139 }
0140 };
0141
0142 }
0143 }
0144
0145 #endif