Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 10:51:39

0001 #include "DetectorDescription/Core/interface/Singleton.h"
0002 #include "DetectorDescription/Core/interface/DDLogicalPart.h"
0003 #include "DetectorDescription/Core/interface/DDName.h"
0004 #include "DetectorDescription/Core/interface/DDPartSelection.h"
0005 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0006 #include "boost/spirit/include/classic.hpp"
0007 
0008 namespace boost {
0009   namespace spirit {
0010     namespace classic {}
0011   }  // namespace spirit
0012 }  // namespace boost
0013 using namespace boost::spirit::classic;
0014 
0015 struct DDSelLevelCollector {
0016   std::string namespace_;
0017   std::string name_;
0018   int copyNo_;
0019   bool isCopyNoValid_;
0020   bool isChild_;
0021   std::vector<DDPartSelRegExpLevel>* p_;
0022 
0023   std::vector<DDPartSelRegExpLevel>* path(std::vector<DDPartSelRegExpLevel>* p = nullptr) {
0024     if (p) {
0025       p_ = p;
0026       namespace_ = "";
0027       name_ = "";
0028       copyNo_ = 0;
0029       isCopyNoValid_ = false;
0030       isChild_ = false;
0031     }
0032     return p_;
0033   }
0034 };
0035 
0036 void noNameSpace(char const* /*first*/, char const* /*last*/) {
0037   DDI::Singleton<DDSelLevelCollector>::instance().namespace_ = "";
0038 }
0039 
0040 /* Functor for the parser; it does not consume memory -
0041   pointers are only used to store references to memory
0042   managed elsewhere 
0043 */
0044 struct DDSelLevelFtor {
0045   DDSelLevelFtor() : c_(DDI::Singleton<DDSelLevelCollector>::instance()) {}
0046 
0047   // parser calls this whenever a selection has been parsed ( //ns:nm[cn], /nm, //ns:nm, .... )
0048   void operator()(char const* /*first*/, char const* /*last*/) const {
0049     if (c_.path()) {
0050       if (c_.isCopyNoValid_ && c_.isChild_) {
0051         c_.path()->emplace_back(DDPartSelRegExpLevel(c_.namespace_, c_.name_, c_.copyNo_, ddchildposp));
0052         //edm::LogInfo("DDPartSelection")  << namespace_ << name_ << copyNo_ << ' ' << ddchildposp << std::endl;
0053       } else if (c_.isCopyNoValid_ && !c_.isChild_) {
0054         c_.path()->emplace_back(DDPartSelRegExpLevel(c_.namespace_, c_.name_, c_.copyNo_, ddanyposp));
0055         //      edm::LogInfo("DDPartSelection")  << namespace_ << name_ << copyNo_ << ' ' << ddanyposp << std::endl;
0056       } else if (!c_.isCopyNoValid_ && c_.isChild_) {
0057         c_.path()->emplace_back(DDPartSelRegExpLevel(c_.namespace_, c_.name_, c_.copyNo_, ddchildlogp));
0058         //      edm::LogInfo("DDPartSelection")  << namespace_ << name_ << copyNo_ << ' ' << ddchildlogp << std::endl;
0059       } else if (!c_.isCopyNoValid_ && !c_.isChild_) {
0060         c_.path()->emplace_back(DDPartSelRegExpLevel(c_.namespace_, c_.name_, c_.copyNo_, ddanylogp));
0061         //      edm::LogInfo("DDPartSelection")  << namespace_ << name_ << copyNo_ << ' ' << ddanylogp << std::endl;
0062       }
0063       c_.namespace_ = "";
0064       c_.name_ = "";
0065       c_.isCopyNoValid_ = false;
0066     }
0067   }
0068 
0069   DDSelLevelCollector& c_;
0070 };
0071 
0072 struct DDIsChildFtor {
0073   void operator()(char const* first, char const* last) const {
0074     DDSelLevelCollector& sl = DDI::Singleton<DDSelLevelCollector>::instance();
0075     if ((last - first) > 1)
0076       sl.isChild_ = false;
0077     if ((last - first) == 1)
0078       sl.isChild_ = true;
0079     //edm::LogInfo("DDPartSelection")  << "DDIsChildFtor  isChild=" << (last-first) << std::endl;
0080   }
0081 };
0082 
0083 struct DDNameSpaceFtor {
0084   void operator()(char const* first, char const* last) const {
0085     DDSelLevelCollector& sl = DDI::Singleton<DDSelLevelCollector>::instance();
0086     sl.namespace_.assign(first, last);
0087     // edm::LogInfo("DDPartSelection")  << "DDNameSpaceFtor singletonname=" << DDI::Singleton<DDSelLevelCollector>::instance().namespace_ << std::endl;
0088   }
0089 
0090   DDSelLevelFtor* selLevelFtor_;
0091 };
0092 
0093 struct DDNameFtor {
0094   void operator()(char const* first, char const* last) const {
0095     DDSelLevelCollector& sl = DDI::Singleton<DDSelLevelCollector>::instance();
0096     sl.name_.assign(first, last);
0097     // edm::LogInfo("DDPartSelection")  << "DDNameFtor singletonname=" << Singleton<DDSelLevelCollector>::instance().name_ << std::endl;
0098   }
0099 };
0100 
0101 struct DDCopyNoFtor {
0102   void operator()(int i) const {
0103     DDSelLevelCollector& sl = DDI::Singleton<DDSelLevelCollector>::instance();
0104     sl.copyNo_ = i;
0105     sl.isCopyNoValid_ = true;
0106     // edm::LogInfo("DDPartSelection")  << "DDCopyNoFtor ns=" << i;
0107   }
0108 };
0109 
0110 /** A boost::spirit parser for the <SpecPar path="xxx"> syntax */
0111 struct SpecParParser : public grammar<SpecParParser> {
0112   template <typename ScannerT>
0113   struct definition {
0114     definition(SpecParParser const& /*self*/) {
0115       Selection  //= FirstStep[selLevelFtor()]
0116                  //>> *SelectionStep[selLevelFtor()]
0117           = +SelectionStep[selLevelFtor()];
0118 
0119       FirstStep = Descendant >> Part;
0120 
0121       Part = PartNameCopyNumber | PartName;
0122 
0123       PartNameCopyNumber = PartName >> CopyNumber;
0124 
0125       SelectionStep = NavigationalElement[isChildFtor()] >> Part;
0126 
0127       NavigationalElement = Descendant | Child;
0128 
0129       CopyNumber = ch_p('[') >> int_p[copyNoFtor()] >> ch_p(']');
0130 
0131       PartName = NameSpaceName | SimpleName[nameFtor()][&noNameSpace];
0132 
0133       SimpleName = +(alnum_p | ch_p('_') | ch_p('.') | ch_p('*'));
0134 
0135       NameSpaceName = SimpleName[nameSpaceFtor()] >> ':' >> SimpleName[nameFtor()];
0136 
0137       Descendant = ch_p('/') >> ch_p('/');
0138 
0139       Child = ch_p('/');
0140     }
0141 
0142     rule<ScannerT> Selection, FirstStep, Part, SelectionStep, NavigationalElement, CopyNumber, PartName,
0143         PartNameCopyNumber, NameSpaceName, SimpleName, Descendant, Child;
0144 
0145     rule<ScannerT> const& start() const { return Selection; }
0146 
0147     DDSelLevelFtor& selLevelFtor() { return DDI::Singleton<DDSelLevelFtor>::instance(); }
0148 
0149     DDNameFtor& nameFtor() {
0150       static DDNameFtor f_;
0151       return f_;
0152     }
0153 
0154     DDNameSpaceFtor& nameSpaceFtor() {
0155       static DDNameSpaceFtor f_;
0156       return f_;
0157     }
0158 
0159     DDIsChildFtor& isChildFtor() {
0160       static DDIsChildFtor f_;
0161       return f_;
0162     }
0163 
0164     DDCopyNoFtor& copyNoFtor() {
0165       static DDCopyNoFtor f_;
0166       return f_;
0167     }
0168   };
0169 };
0170 
0171 DDPartSelectionLevel::DDPartSelectionLevel(const DDLogicalPart& lp, int c, ddselection_type t)
0172     : lp_(lp), copyno_(c), selectionType_(t) {}
0173 
0174 void DDTokenize2(const std::string& sel, std::vector<DDPartSelRegExpLevel>& path) {
0175   static SpecParParser parser;
0176   DDI::Singleton<DDSelLevelCollector>::instance().path(&path);
0177   bool result = parse(sel.c_str(), parser).full;
0178   if (!result) {
0179     edm::LogError("DDPartSelection") << "DDTokenize2() error in parsing of " << sel << std::endl;
0180   }
0181 }
0182 
0183 std::ostream& operator<<(std::ostream& o, const DDPartSelection& p) {
0184   DDPartSelection::const_iterator it(p.begin()), ed(p.end());
0185   for (; it != ed; ++it) {
0186     const DDPartSelectionLevel& lv = *it;
0187     switch (lv.selectionType_) {
0188       case ddanylogp:
0189         o << "//" << lv.lp_.ddname();
0190         break;
0191       case ddanyposp:
0192         o << "//" << lv.lp_.ddname() << '[' << lv.copyno_ << ']';
0193         break;
0194       case ddchildlogp:
0195         o << "/" << lv.lp_.ddname();
0196         break;
0197       case ddchildposp:
0198         o << "/" << lv.lp_.ddname() << '[' << lv.copyno_ << ']';
0199         break;
0200       default:
0201         o << "{Syntax ERROR}";
0202     }
0203   }
0204   return o;
0205 }
0206 
0207 std::ostream& operator<<(std::ostream& os, const std::vector<DDPartSelection>& v) {
0208   std::vector<DDPartSelection>::const_iterator it(v.begin()), ed(v.end());
0209   for (; it != (ed - 1); ++it) {
0210     os << *it << std::endl;
0211   }
0212   if (it != ed) {
0213     ++it;
0214     os << *it;
0215   }
0216   return os;
0217 }
0218 
0219 // explicit template instantiation.
0220 
0221 template class DDI::Singleton<DDSelLevelFtor>;
0222 //template class DDI::Singleton<DDI::Store<DDName, DDSelLevelCollector> >;
0223 template class DDI::Singleton<DDSelLevelCollector>;
0224 #include <DetectorDescription/Core/interface/Singleton.icc>