CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_1/src/DetectorDescription/Core/src/DDPartSelection.cc

Go to the documentation of this file.
00001 #include "DetectorDescription/Base/interface/Singleton.h"
00002 #include "DetectorDescription/Core/interface/DDPartSelection.h"
00003 #include "DetectorDescription/Base/interface/DDdebug.h"
00004 #include "DetectorDescription/Core/interface/DDSplit.h"
00005 
00006 #include "boost/spirit/include/classic.hpp"
00007 
00008 // Message logger.
00009 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00010 
00011 #include <map>
00012 
00013 namespace boost { namespace spirit { namespace classic { } } } using namespace boost::spirit::classic;
00014 
00015 struct DDSelLevelCollector
00016 {
00017   std::string namespace_;
00018   std::string name_;
00019   int copyNo_;
00020   bool isCopyNoValid_;
00021   bool isChild_;
00022   std::vector<DDPartSelRegExpLevel>* p_;
00023 
00024   std::vector<DDPartSelRegExpLevel>* path(std::vector<DDPartSelRegExpLevel>* p=0) {
00025     if (p) {
00026       p_=p; 
00027       namespace_="";
00028       name_="";
00029       copyNo_=0;
00030       isCopyNoValid_=false;
00031       isChild_=false;
00032     }
00033     return p_;
00034   }
00035 };
00036 
00037 
00038 void noNameSpace(char const * /*first*/, char const* /*last*/) {
00039   DDI::Singleton<DDSelLevelCollector>::instance().namespace_="";
00040 }
00041 /* Functor for the parser; it does not consume memory -
00042   pointers are only used to store references to memory
00043   managed elsewhere 
00044 */
00045 struct DDSelLevelFtor
00046 {
00047   DDSelLevelFtor() 
00048     : c_(DDI::Singleton<DDSelLevelCollector>::instance())
00049   { }
00050   
00051   // parser calls this whenever a selection has been parsed ( //ns:nm[cn], /nm, //ns:nm, .... ) 
00052   void operator() (char const* /*first*/, char const* /*last*/) const {
00053    if(c_.path()){
00054     if (c_.isCopyNoValid_ && c_.isChild_) {
00055       c_.path()->push_back(DDPartSelRegExpLevel(c_.namespace_,c_.name_,c_.copyNo_,ddchildposp));
00056       //edm::LogInfo("DDPartSelection")  << namespace_ << name_ << copyNo_ << ' ' << ddchildposp << std::endl;
00057     } else
00058     if (c_.isCopyNoValid_ && !c_.isChild_) {
00059       c_.path()->push_back(DDPartSelRegExpLevel(c_.namespace_,c_.name_,c_.copyNo_,ddanyposp));
00060       //      edm::LogInfo("DDPartSelection")  << namespace_ << name_ << copyNo_ << ' ' << ddanyposp << std::endl;
00061     } else
00062     if (!c_.isCopyNoValid_ && c_.isChild_) {
00063       c_.path()->push_back(DDPartSelRegExpLevel(c_.namespace_,c_.name_,c_.copyNo_,ddchildlogp));
00064       //      edm::LogInfo("DDPartSelection")  << namespace_ << name_ << copyNo_ << ' ' << ddchildlogp << std::endl;
00065     } else
00066     if (!c_.isCopyNoValid_ && !c_.isChild_) {
00067       c_.path()->push_back(DDPartSelRegExpLevel(c_.namespace_,c_.name_,c_.copyNo_,ddanylogp));
00068       //      edm::LogInfo("DDPartSelection")  << namespace_ << name_ << copyNo_ << ' ' << ddanylogp << std::endl;
00069     } 
00070     c_.namespace_="";
00071     c_.name_="";
00072     c_.isCopyNoValid_=false;   
00073    } 
00074   }
00075      
00076   DDSelLevelCollector & c_;    
00077 };
00078 
00079 struct DDIsChildFtor
00080 {
00081   
00082   void operator()(char const* first, char const* last) const {
00083    DDSelLevelCollector & sl = DDI::Singleton<DDSelLevelCollector>::instance();
00084    if ( (last-first) > 1) 
00085      sl.isChild_=false;
00086    if ( (last-first) ==1 )
00087      sl.isChild_=true;
00088    //edm::LogInfo("DDPartSelection")  << "DDIsChildFtor  isChild=" << (last-first) << std::endl;
00089   }
00090  
00091 };
00092 
00093 
00094 struct DDNameSpaceFtor
00095 {
00096   
00097   void operator()(char const* first, char const* last) const {
00098     DDSelLevelCollector & sl = DDI::Singleton<DDSelLevelCollector>::instance();  
00099     sl.namespace_.assign(first,last);
00100     // edm::LogInfo("DDPartSelection")  << "DDNameSpaceFtor singletonname=" << DDI::Singleton<DDSelLevelCollector>::instance().namespace_ << std::endl;
00101   }
00102   
00103   DDSelLevelFtor* selLevelFtor_;
00104 };
00105 
00106 
00107 struct DDNameFtor
00108 {
00109  
00110   void operator()(char const* first, char const* last) const {
00111     DDSelLevelCollector & sl = DDI::Singleton<DDSelLevelCollector>::instance();
00112     sl.name_.assign(first,last);  
00113     // edm::LogInfo("DDPartSelection")  << "DDNameFtor singletonname=" << Singleton<DDSelLevelCollector>::instance().name_ << std::endl;
00114   }
00115   
00116 };
00117 
00118 
00119 struct DDCopyNoFtor
00120 {
00121   
00122   void operator()(int i) const {
00123     DDSelLevelCollector & sl = DDI::Singleton<DDSelLevelCollector>::instance();  
00124     sl.copyNo_ = i;
00125     sl.isCopyNoValid_ = true;
00126     // edm::LogInfo("DDPartSelection")  << "DDCopyNoFtor ns=" << i;
00127   }
00128  
00129 };
00130 
00132 struct SpecParParser : public grammar<SpecParParser>
00133 {
00134   template <typename ScannerT>
00135   struct definition
00136   {
00137     definition(SpecParParser const& /*self*/) {
00138          
00139         Selection  //= FirstStep[selLevelFtor()] 
00140                   //>> *SelectionStep[selLevelFtor()]
00141                    = +SelectionStep[selLevelFtor()]
00142                    ;
00143 
00144         FirstStep  = Descendant 
00145                   >> Part
00146                    ; 
00147 
00148         Part       = PartNameCopyNumber 
00149                    | PartName
00150                    ;
00151 
00152         PartNameCopyNumber = PartName 
00153                   >> CopyNumber
00154                    ;
00155 
00156         SelectionStep = NavigationalElement[isChildFtor()] 
00157                   >> Part
00158                    ;
00159 
00160         NavigationalElement = Descendant 
00161                    | Child
00162                    ;
00163 
00164         CopyNumber = ch_p('[') 
00165                   >> int_p[copyNoFtor()] 
00166                   >> ch_p(']')
00167                    ;
00168 
00169         PartName   = NameSpaceName 
00170                    | SimpleName[nameFtor()][&noNameSpace]
00171                    ;
00172 
00173         SimpleName = +( alnum_p | ch_p('_') | ch_p('.') | ch_p('*') )
00174                    ;
00175 
00176         NameSpaceName = SimpleName[nameSpaceFtor()] 
00177                   >> ':' 
00178                   >> SimpleName[nameFtor()]
00179                    ;
00180 
00181         Descendant = ch_p('/') 
00182                   >> ch_p('/')
00183                    ;
00184 
00185         Child      = ch_p('/')
00186                     ;
00187   
00188          }
00189   
00190     rule<ScannerT> Selection, FirstStep, Part, SelectionStep, NavigationalElement,
00191         CopyNumber, PartName, PartNameCopyNumber, NameSpaceName, SimpleName, 
00192         Descendant, Child; 
00193 
00194     rule<ScannerT> const& start() const { return Selection; }
00195     
00196     DDSelLevelFtor & selLevelFtor() {
00197       return DDI::Singleton<DDSelLevelFtor>::instance();
00198     }
00199     
00200     DDNameFtor & nameFtor() {
00201      static DDNameFtor f_;
00202      return f_;
00203     }
00204     
00205     DDNameSpaceFtor & nameSpaceFtor() {
00206      static DDNameSpaceFtor f_;
00207      return f_;
00208     }
00209     
00210     DDIsChildFtor & isChildFtor() {
00211      static DDIsChildFtor f_;
00212      return f_;
00213     }
00214 
00215     DDCopyNoFtor & copyNoFtor() {
00216      static DDCopyNoFtor f_;
00217      return f_;
00218     }    
00219   };
00220   
00221 };
00222 
00223 DDPartSelectionLevel::DDPartSelectionLevel(const DDLogicalPart & lp, int c, ddselection_type t)
00224  : lp_(lp), copyno_(c), selectionType_(t)
00225 {}
00226 
00227 void DDTokenize2(const std::string & sel, std::vector<DDPartSelRegExpLevel> & path)
00228 {
00229   static SpecParParser parser;
00230   DDI::Singleton<DDSelLevelCollector>::instance().path(&path);
00231   bool result = parse(sel.c_str(), parser).full;
00232   if (!result) {
00233     edm::LogError("DDPartSelection") << "DDTokenize2() error in parsing of " << sel << std::endl;
00234   }
00235 }
00236 
00237 std::ostream & operator<<(std::ostream & o, const DDPartSelection & p)
00238 {
00239   DDPartSelection::const_iterator it(p.begin()), ed(p.end());
00240   for (; it != ed; ++it) {
00241     const DDPartSelectionLevel lv  =*it;
00242     switch (lv.selectionType_) {
00243     case ddanylogp:
00244       o << "//" << lv.lp_.ddname();
00245       break;
00246     case ddanyposp:
00247       o << "//" << lv.lp_.ddname() << '[' << lv.copyno_ << ']';
00248       break;
00249     case ddchildlogp:
00250       o << "/" << lv.lp_.ddname();
00251       break;
00252     case ddchildposp:
00253       o << "/" << lv.lp_.ddname() << '[' << lv.copyno_ << ']';
00254       break;
00255     default:
00256       o << "{Syntax ERROR}";
00257     }
00258   }
00259   return o;
00260 }
00261 
00262 std::ostream & operator<<(std::ostream & os, const std::vector<DDPartSelection> & v)
00263 {
00264   std::vector<DDPartSelection>::const_iterator it(v.begin()), ed(v.end());
00265   for (; it != (ed-1); ++it) {
00266     os << *it << std::endl;
00267   }
00268   if ( it != ed ) {
00269     ++it;
00270     os << *it;
00271   }
00272   return os;
00273 }
00274 
00275 // explicit template instantiation.
00276 
00277 template class DDI::Singleton<DDSelLevelFtor>;
00278 //template class DDI::Singleton<DDI::Store<DDName, DDSelLevelCollector> >;
00279 template class DDI::Singleton<DDSelLevelCollector>;
00280 #include <DetectorDescription/Base/interface/Singleton.icc>