CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Selector.h
Go to the documentation of this file.
1 #ifndef FWCore_Framework_Selector_h
2 #define FWCore_Framework_Selector_h
3 
4 /*----------------------------------------------------------------------
5 
6 Classes for all "selector" objects, used to select
7 EDProducts based on information in the associated Provenance.
8 
9 Developers who make their own Selector class should inherit
10 from SelectorBase.
11 
12 Users can use the classes defined below
13 
14  ModuleLabelSelector
15  ProcessNameSelector
16  ProductInstanceNameSelector
17 
18 Users can also use the class Selector, which can be constructed given a
19 logical expression formed from any other selectors, combined with &&
20 (the AND operator), || (the OR operator) or ! (the NOT operator).
21 
22 For example, to select only products produced by a module with label
23 "mymodule" and made in the process "PROD", one can use:
24 
25  Selector s( ModuleLabelSelector("mymodule") &&
26  ProcessNameSelector("PROD") );
27 
28 If a module (EDProducter, EDFilter, EDAnalyzer, or OutputModule) is
29 to use such a selector, it is best to initialize it directly upon
30 construction of the module, rather than creating a new Selector instance
31 for every event.
32 
33 ----------------------------------------------------------------------*/
34 
38 
39 #include "boost/utility/enable_if.hpp"
40 
41 #include <string>
42 
43 namespace edm {
44  //------------------------------------------------------------------
48  //------------------------------------------------------------------
49  template<typename T>
50  struct has_match {
52  };
53 
54  template <>
56  static const bool value = true;
57  };
58 
59  //------------------------------------------------------------------
60  //
67  //------------------------------------------------------------------
68 
70  public:
71  ProcessNameSelector(const std::string& pn) :
72  pn_(pn.empty() ? std::string("*") : pn) {
73  }
74 
75  virtual bool doMatch(ConstBranchDescription const& p) const {
76  return (pn_=="*") || (p.processName() == pn_);
77  }
78 
79  virtual ProcessNameSelector* clone() const {
80  return new ProcessNameSelector(*this);
81  }
82 
83  std::string const& name() const {
84  return pn_;
85  }
86 
87  private:
88  std::string pn_;
89  };
90 
91  //------------------------------------------------------------------
92  //
95  //
96  //------------------------------------------------------------------
97 
99  public:
100  ProductInstanceNameSelector(const std::string& pin) :
101  pin_(pin) {
102  }
103 
104  virtual bool doMatch(ConstBranchDescription const& p) const {
105  return p.productInstanceName() == pin_;
106  }
107 
109  return new ProductInstanceNameSelector(*this);
110  }
111  private:
112  std::string pin_;
113  };
114 
115  //------------------------------------------------------------------
116  //
119  //
120  //------------------------------------------------------------------
121 
123  public:
124  ModuleLabelSelector(const std::string& label) :
125  label_(label) {
126  }
127 
128  virtual bool doMatch(ConstBranchDescription const& p) const {
129  return p.moduleLabel() == label_;
130  }
131 
132  virtual ModuleLabelSelector* clone() const {
133  return new ModuleLabelSelector(*this);
134  }
135  private:
136  std::string label_;
137  };
138 
139  //------------------------------------------------------------------
140  //
143  //
144  //------------------------------------------------------------------
145 
147  public:
149  }
150 
151  virtual bool doMatch(ConstBranchDescription const&) const {
152  return true;
153  }
154 
155  virtual MatchAllSelector* clone() const {
156  return new MatchAllSelector;
157  }
158  };
159 
160  //----------------------------------------------------------
161  //
162  // AndHelper template.
163  // Used to form expressions involving && between other selectors.
164  //
165  //----------------------------------------------------------
166 
167  template<typename A, typename B>
168  class AndHelper {
169  public:
170  AndHelper(A const& a, B const& b) : a_(a), b_(b) { }
171  bool match(ConstBranchDescription const& p) const { return a_.match(p) && b_.match(p); }
172  private:
173  A a_;
174  B b_;
175  };
176 
177  template<typename A, typename B>
178  struct has_match<AndHelper<A, B> > {
179  static const bool value = true;
180  };
181 
182  template<typename A, typename B>
185  operator&&(A const& a, B const& b) {
186  return AndHelper<A, B>(a, b);
187  }
188 
189  //----------------------------------------------------------
190  //
191  // OrHelper template.
192  // Used to form expressions involving || between other selectors.
193  //
194  //----------------------------------------------------------
195 
196  template<typename A, typename B>
197  class OrHelper {
198  public:
199  OrHelper(A const& a, B const& b) : a_(a), b_(b) { }
200  bool match(ConstBranchDescription const& p) const { return a_.match(p) || b_.match(p); }
201  private:
202  A a_;
203  B b_;
204  };
205 
206  template<typename A, typename B>
207  struct has_match<OrHelper<A, B> > {
208  static const bool value = true;
209  };
210 
211  template<typename A, typename B>
214  operator||(A const& a, B const& b) {
215  return OrHelper<A, B>(a, b);
216  }
217 
218 
219  //----------------------------------------------------------
220  //
221  // NotHelper template.
222  // Used to form expressions involving ! acting on a selector.
223  //
224  //----------------------------------------------------------
225 
226  template<typename A>
227  class NotHelper {
228  public:
229  explicit NotHelper(A const& a) : a_(a) { }
230  bool match(ConstBranchDescription const& p) const { return ! a_.match(p); }
231  private:
232  A a_;
233  };
234 
235  template<typename A>
238  operator!(A const& a) {
239  return NotHelper<A>(a);
240  }
241 
242  template<typename A>
243  struct has_match<NotHelper<A> > {
244  static const bool value = true;
245  };
246 
247  //----------------------------------------------------------
248  //
249  // ComposedSelectorWrapper template
250  // Used to hold an expression formed from the various helpers.
251  //
252  //----------------------------------------------------------
253 
254  template<typename T>
256  public:
257  typedef T wrapped_type;
258  explicit ComposedSelectorWrapper(T const& t) : expression_(t) { }
260  virtual bool doMatch(ConstBranchDescription const& p) const { return expression_.match(p); }
262  private:
264  };
265 
266  //----------------------------------------------------------
267  //
268  // Selector
269  //
270  //----------------------------------------------------------
271 
272  class Selector : public SelectorBase {
273  public:
274  template<typename T> Selector(T const& expression);
275  void swap(Selector& other);
276  virtual ~Selector();
277  virtual Selector* clone() const;
278 
279  virtual bool doMatch(ConstBranchDescription const& p) const;
280 
281  private:
283  };
284 
285  template<typename T>
286  Selector::Selector(T const& expression) :
287  sel_(new ComposedSelectorWrapper<T>(expression)) {
288  }
289 }
290 
291 #endif
std::string const & processName() const
Selector(T const &expression)
Definition: Selector.h:286
type
Definition: HCALResponse.h:22
virtual bool doMatch(ConstBranchDescription const &p) const
Definition: Selector.h:75
virtual bool doMatch(ConstBranchDescription const &p) const
Definition: Selector.h:104
bool match(ConstBranchDescription const &p) const
Definition: Selector.h:171
virtual ModuleLabelSelector * clone() const
Definition: Selector.h:132
bool match(ConstBranchDescription const &p) const
Definition: Selector.h:200
virtual ~Selector()
Definition: Selector.cc:20
boost::enable_if_c< has_match< A >::value &&has_match< B >::value, AndHelper< A, B > >::type operator&&(A const &a, B const &b)
Definition: Selector.h:185
bool match(ConstBranchDescription const &p) const
Definition: Selector.h:230
virtual bool doMatch(ConstBranchDescription const &p) const
Definition: Selector.h:260
virtual bool doMatch(ConstBranchDescription const &) const
Definition: Selector.h:151
virtual ProcessNameSelector * clone() const
Definition: Selector.h:79
virtual Selector * clone() const
Definition: Selector.cc:23
OrHelper(A const &a, B const &b)
Definition: Selector.h:199
value_ptr< SelectorBase > sel_
Definition: Selector.h:282
void swap(Selector &other)
Definition: Selector.cc:16
virtual MatchAllSelector * clone() const
Definition: Selector.h:155
ComposedSelectorWrapper< T > * clone() const
Definition: Selector.h:261
std::string const & productInstanceName() const
NotHelper(A const &a)
Definition: Selector.h:229
static const bool value
Definition: Selector.h:51
virtual bool doMatch(ConstBranchDescription const &p) const
Definition: Selector.cc:28
ModuleLabelSelector(const std::string &label)
Definition: Selector.h:124
ComposedSelectorWrapper(T const &t)
Definition: Selector.h:258
virtual bool doMatch(ConstBranchDescription const &p) const
Definition: Selector.h:128
boost::enable_if_c< has_match< A >::value &&has_match< B >::value, OrHelper< A, B > >::type operator||(A const &a, B const &b)
Definition: Selector.h:214
double b
Definition: hdecay.h:120
AndHelper(A const &a, B const &b)
Definition: Selector.h:170
std::string const & name() const
Definition: Selector.h:83
double a
Definition: hdecay.h:121
boost::enable_if_c< has_match< A >::value, NotHelper< A > >::type operator!(A const &a)
Definition: Selector.h:238
long double T
ProcessNameSelector(const std::string &pn)
Definition: Selector.h:71
ProductInstanceNameSelector(const std::string &pin)
Definition: Selector.h:100
std::string const & moduleLabel() const
virtual ProductInstanceNameSelector * clone() const
Definition: Selector.h:108