Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:01:19

0001 #ifndef CommonTools_Utils_ExpressionVar_h
0002 #define CommonTools_Utils_ExpressionVar_h
0003 
0004 /* \class reco::parser::ExpressionVar
0005  *
0006  * Variable expression
0007  *
0008  * \author original version: Chris Jones, Cornell,
0009  *         adapted by Luca Lista, INFN
0010  *
0011  */
0012 
0013 #include "CommonTools/Utils/interface/parser/ExpressionBase.h"
0014 #include "CommonTools/Utils/interface/parser/MethodInvoker.h"
0015 #include "CommonTools/Utils/interface/TypeCode.h"
0016 
0017 #include <vector>
0018 #include <oneapi/tbb/concurrent_queue.h>
0019 
0020 namespace reco {
0021   namespace parser {
0022 
0023     /// Evaluate an object's method or datamember (or chain of them) to get a number
0024     class ExpressionVar : public ExpressionBase {
0025     private:  // Private Data Members
0026       std::vector<MethodInvoker> methods_;
0027       using Objects = std::vector<std::pair<edm::ObjectWithDict, bool>>;
0028       mutable oneapi::tbb::concurrent_queue<Objects> objectsCache_;
0029       method::TypeCode retType_;
0030 
0031     private:  // Private Methods
0032       [[nodiscard]] Objects initObjects_() const;
0033 
0034       Objects borrowObjects() const;
0035       void returnObjects(Objects&&) const;
0036 
0037     public:  // Public Static Methods
0038       static bool isValidReturnType(method::TypeCode);
0039 
0040       /// performs the needed conversion from void* to double
0041       /// this method is used also from the ExpressionLazyVar code
0042       static double objToDouble(const edm::ObjectWithDict& obj, method::TypeCode type);
0043 
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       ExpressionVar(const std::vector<MethodInvoker>& methods, method::TypeCode retType);
0055       ExpressionVar(const ExpressionVar&);
0056       ~ExpressionVar() override;
0057       double value(const edm::ObjectWithDict&) const override;
0058     };
0059 
0060     /// Same as ExpressionVar 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 ExpressionLazyVar : public ExpressionBase {
0063     private:  // Private Data Members
0064       std::vector<LazyInvoker> methods_;
0065 
0066     public:
0067       ExpressionLazyVar(const std::vector<LazyInvoker>& methods);
0068       ~ExpressionLazyVar() override;
0069       double value(const edm::ObjectWithDict&) const override;
0070     };
0071 
0072   }  // namespace parser
0073 }  // namespace reco
0074 
0075 #endif  // CommonTools_Utils_ExpressionVar_h