Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-01-09 23:33:15

0001 #ifndef CommonTools_Utils_MethodChain_h
0002 #define CommonTools_Utils_MethodChain_h
0003 
0004 /* \class reco::parser::MethodChain
0005  *
0006  * Chain of methods
0007  * Based on ExpressionBase and ExpressionVar, but remove final conversion to double
0008  *
0009  */
0010 
0011 #include "CommonTools/Utils/interface/parser/MethodInvoker.h"
0012 #include "CommonTools/Utils/interface/TypeCode.h"
0013 
0014 #include <vector>
0015 #include <oneapi/tbb/concurrent_queue.h>
0016 
0017 namespace reco {
0018   namespace parser {
0019 
0020     /// Based on Expression, but its value method returns an edm::ObjectWithDict instead of a double
0021     class MethodChainBase {
0022     public:  // Public Methods
0023       virtual ~MethodChainBase() {}
0024       virtual edm::ObjectWithDict value(const edm::ObjectWithDict&) const = 0;
0025     };
0026 
0027     /// Shared ptr to MethodChainBase
0028     typedef std::shared_ptr<MethodChainBase> MethodChainPtr;
0029 
0030     /// Evaluate an object's method or datamember (or chain of them)
0031     class MethodChain : public MethodChainBase {
0032     private:  // Private Data Members
0033       std::vector<MethodInvoker> methods_;
0034       using Objects = std::vector<std::pair<edm::ObjectWithDict, bool>>;
0035       mutable oneapi::tbb::concurrent_queue<Objects> objectsCache_;
0036 
0037     private:  // Private Methods
0038       [[nodiscard]] Objects initObjects_() const;
0039 
0040       Objects borrowObjects() const;
0041       void returnObjects(Objects&&) const;
0042 
0043     public:  // Public Static Methods
0044       /// allocate an object to hold the result of a given member (if needed)
0045       /// this method is used also from the LazyInvoker code
0046       /// returns true if objects returned from this will require a destructor
0047       static bool makeStorage(edm::ObjectWithDict& obj, const edm::TypeWithDict& retType);
0048 
0049       /// delete an objecty, if needed
0050       /// this method is used also from the LazyInvoker code
0051       static void delStorage(edm::ObjectWithDict&);
0052 
0053     public:  // Public Methods
0054       MethodChain(const std::vector<MethodInvoker>& methods);
0055       MethodChain(const MethodChain&);
0056       ~MethodChain();
0057       edm::ObjectWithDict value(const edm::ObjectWithDict&) const override;
0058     };
0059 
0060     /// Same as MethodChain but with lazy resolution of object methods
0061     /// using the dynamic type of the object, and not the one fixed at compile time
0062     class LazyMethodChain : public MethodChainBase {
0063     private:  // Private Data Members
0064       std::vector<LazyInvoker> methods_;
0065 
0066     public:
0067       LazyMethodChain(const std::vector<LazyInvoker>& methods);
0068       ~LazyMethodChain() override;
0069       edm::ObjectWithDict value(const edm::ObjectWithDict&) const override;
0070     };
0071 
0072   }  // namespace parser
0073 }  // namespace reco
0074 
0075 #endif  // CommonTools_Utils_MethodChain_h