BinaryOperatorEvaluator

BinaryOperatorEvaluatorBase

Macros

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
#ifndef CommonTools_Utils_formulaBinaryOperatorEvaluator_h
#define CommonTools_Utils_formulaBinaryOperatorEvaluator_h
// -*- C++ -*-
//
// Package:     CommonTools/Utils
// Class  :     formulaBinaryOperatorEvaluator
//
/**\class reco::formula::BinaryOperatorEvaluator formulaBinaryOperatorEvaluator.h "formulaBinaryOperatorEvaluator.h"

 Description: [one line class summary]

 Usage:
    <usage>

*/
//
// Original Author:  Christopher Jones
//         Created:  Wed, 23 Sep 2015 17:41:33 GMT
//

// system include files
#include <memory>

// user include files
#include "formulaEvaluatorBase.h"

// forward declarations

namespace reco {
  namespace formula {
    class BinaryOperatorEvaluatorBase : public EvaluatorBase {
    public:
      BinaryOperatorEvaluatorBase(std::shared_ptr<EvaluatorBase> iLHS,
                                  std::shared_ptr<EvaluatorBase> iRHS,
                                  Precedence iPrec)
          : EvaluatorBase(iPrec), m_lhs(iLHS), m_rhs(iRHS) {}

      BinaryOperatorEvaluatorBase(Precedence iPrec) : EvaluatorBase(iPrec) {}

      void swapLeftEvaluator(std::shared_ptr<EvaluatorBase>& iNew) { m_lhs.swap(iNew); }

      void setLeftEvaluator(std::shared_ptr<EvaluatorBase> iOther) { m_lhs = std::move(iOther); }
      void setRightEvaluator(std::shared_ptr<EvaluatorBase> iOther) { m_rhs = std::move(iOther); }

      EvaluatorBase const* lhs() const { return m_lhs.get(); }
      EvaluatorBase const* rhs() const { return m_rhs.get(); }

    private:
      std::shared_ptr<EvaluatorBase> m_lhs;
      std::shared_ptr<EvaluatorBase> m_rhs;
    };

    template <typename Op>
    class BinaryOperatorEvaluator : public BinaryOperatorEvaluatorBase {
    public:
      BinaryOperatorEvaluator(std::shared_ptr<EvaluatorBase> iLHS,
                              std::shared_ptr<EvaluatorBase> iRHS,
                              Precedence iPrec)
          : BinaryOperatorEvaluatorBase(std::move(iLHS), std::move(iRHS), iPrec) {}

      BinaryOperatorEvaluator(Precedence iPrec) : BinaryOperatorEvaluatorBase(iPrec) {}

      // ---------- const member functions ---------------------
      double evaluate(double const* iVariables, double const* iParameters) const final {
        return m_operator(lhs()->evaluate(iVariables, iParameters), rhs()->evaluate(iVariables, iParameters));
      }

      std::vector<std::string> abstractSyntaxTree() const final {
        std::vector<std::string> ret;
        if (lhs()) {
          ret = shiftAST(lhs()->abstractSyntaxTree());
        } else {
          ret.emplace_back(".nullptr");
        }
        if (rhs()) {
          auto child = shiftAST(rhs()->abstractSyntaxTree());
          for (auto& v : child) {
            ret.emplace_back(std::move(v));
          }
        } else {
          ret.emplace_back(".nullptr");
        }
        ret.emplace(ret.begin(), std::string("op ") + std::to_string(precedence()));
        return ret;
      }

      BinaryOperatorEvaluator(const BinaryOperatorEvaluator&) = delete;

      const BinaryOperatorEvaluator& operator=(const BinaryOperatorEvaluator&) = delete;

    private:
      // ---------- member data --------------------------------
      Op m_operator;
    };
  }  // namespace formula
}  // namespace reco

#endif