LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
ProductRegistryHelper.h
Go to the documentation of this file.
1 #ifndef art_Framework_Core_ProductRegistryHelper_h
2 #define art_Framework_Core_ProductRegistryHelper_h
3 // vim: set sw=2:
4 
5 // -----------------------------------------------------------------
6 //
7 // ProductRegistryHelper: This class provides the produces()
8 // and reconstitutes() function templates used by modules to
9 // register what products they create or read in respectively.
10 //
11 // The constructors of an EDProducer or an EDFilter should call
12 // produces() for each product inserted into a principal.
13 // Instance names should be provided only when the module
14 // makes more than one instance of the same product per event.
15 //
16 // The constructors of an InputSource should call reconstitutes()
17 // for each product if and only if it does not update the
18 // MasterProductRegistry with a product list.
19 //
20 // -----------------------------------------------------------------
21 
33 #include "cetlib/exempt_ptr.h"
34 #include "cetlib_except/exception.h"
35 
36 #include <memory>
37 #include <set>
38 #include <string>
39 
40 namespace art {
41  class MasterProductRegistry;
42  class ModuleDescription;
43  class ProductRegistryHelper;
44 }
45 
46 namespace {
47 
48  inline void
49  verifyFriendlyClassName(std::string const& fcn)
50  {
51  std::string errMsg;
52  if (!art::detail::checkFriendlyName(fcn, errMsg)) {
54  << errMsg
55  << "In particular, underscores are not permissible anywhere in the "
56  "fully-scoped\n"
57  "class name, including namespaces.\n";
58  }
59  }
60 
61  inline void
62  verifyModuleLabel(std::string const& ml)
63  {
64  std::string errMsg;
65  if (!art::detail::checkModuleLabel(ml, errMsg)) {
67  }
68  }
69 
70  inline void
71  verifyInstanceName(std::string const& instanceName)
72  {
73  std::string errMsg;
74  if (!art::detail::checkInstanceName(instanceName, errMsg)) {
76  }
77  }
78 
79 } // unnamed namespace
80 
82 public:
83  // Used by an input source to provide a product list to be merged
84  // into the master product registry later by registerProducts().
85  void
87  {
88  productList_.reset(p);
89  }
90 
92  ProductDescriptions& productsToRegister,
93  ModuleDescription const& md);
94 
95  // Record the production of an object of type P, with optional
96  // instance name, in the Event (by default), Run, or SubRun.
97  template <typename P, BranchType B = InEvent>
98  void produces(std::string const& instanceName = {},
99  Persistable const persistable = Persistable::Yes);
100 
101  // Record the reconstitution of an object of type P, in either the
102  // Run, SubRun, or Event, recording that this object was
103  // originally created by a module with label modLabel, and with an
104  // optional instance name.
105  template <typename P, BranchType B>
106  TypeLabel const& reconstitutes(std::string const& modLabel,
107  std::string const& instanceName = {});
108 
109  template <BranchType B>
110  std::set<TypeLabel> const&
112  {
113  return typeLabelList_[B];
114  }
115 
116 private:
117  TypeLabel const&
118  insertOrThrow(BranchType const bt, TypeLabel const& tl)
119  {
120  auto result = typeLabelList_[bt].insert(tl);
121  if (!result.second) {
122  throw Exception(errors::LogicError, "RegistrationFailure")
123  << "The module being constructed attempted to "
124  << "register conflicting products with:\n"
125  << "friendlyClassName: " << tl.friendlyClassName()
126  << " and instanceName: " << tl.productInstanceName() << ".\n";
127  }
128  return *result.first;
129  }
130 
131  std::array<std::set<TypeLabel>, NumBranchTypes> typeLabelList_;
132 
133  // Set by an input source for merging into the master product
134  // registry by registerProducts(). Ownership is released to
135  // MasterProductRegistry.
136  std::unique_ptr<ProductList> productList_;
137 };
138 
139 template <typename P, art::BranchType B>
140 inline void
141 art::ProductRegistryHelper::produces(std::string const& instanceName,
142  Persistable const persistable)
143 {
144  verifyInstanceName(instanceName);
145  TypeID const productType{typeid(P)};
146  verifyFriendlyClassName(productType.friendlyClassName());
147  bool const isTransient = (persistable == Persistable::No);
149  B,
150  TypeLabel{productType, instanceName, SupportsView<P>::value, isTransient});
151 }
152 
153 template <typename P, art::BranchType B>
154 art::TypeLabel const&
155 art::ProductRegistryHelper::reconstitutes(std::string const& emulatedModule,
156  std::string const& instanceName)
157 {
158  verifyModuleLabel(emulatedModule);
159  verifyInstanceName(instanceName);
160  TypeID const productType{typeid(P)};
161  verifyFriendlyClassName(productType.friendlyClassName());
162  return insertOrThrow(
163  B,
164  TypeLabel{
165  productType, instanceName, SupportsView<P>::value, emulatedModule});
166 }
167 
168 #endif /* art_Framework_Core_ProductRegistryHelper_h */
169 
170 // Local Variables:
171 // mode: c++
172 // End:
void productList(ProductList *p)
std::string const & productInstanceName() const
Definition: TypeLabel.h:51
Persistable
Definition: Persistable.h:5
void verifyFriendlyClassName(std::string const &fcn)
Definition: verify_names.cc:16
void registerProducts(MasterProductRegistry &mpr, ProductDescriptions &productsToRegister, ModuleDescription const &md)
Int_t B
Definition: plot.C:25
void produces(std::string const &instanceName={}, Persistable const persistable=Persistable::Yes)
std::map< BranchKey, BranchDescription > ProductList
Definition: ProductList.h:15
std::vector< BranchDescription > ProductDescriptions
TypeLabel const & reconstitutes(std::string const &modLabel, std::string const &instanceName={})
bool checkInstanceName(std::string const &instanceName, std::string &errMsg)
std::unique_ptr< ProductList > productList_
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
bool checkFriendlyName(std::string const &friendlyName, std::string &errMsg)
std::string friendlyClassName() const
Definition: TypeLabel.h:45
TypeLabel const & insertOrThrow(BranchType const bt, TypeLabel const &tl)
std::set< TypeLabel > const & expectedProducts() const
std::array< std::set< TypeLabel >, NumBranchTypes > typeLabelList_
BranchType
Definition: BranchType.h:18
HLT enums.
bool checkModuleLabel(std::string const &moduleLabel, std::string &errMsg)
void verifyInstanceName(std::string const &in)
Definition: verify_names.cc:5