LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
Exploder_module.cc
Go to the documentation of this file.
1 
10 // framework libraries
15 #include "fhiclcpp/types/Atom.h"
16 
17 // C/C++ standard libraries
18 #include <vector>
19 #include <array>
20 #include <stdexcept> // std::out_of_range, std::length_error
21 #include <new> // std::bad_alloc
22 
23 
24 namespace lar {
25  namespace example {
26 
43  class Exploder: public art::EDAnalyzer {
44  public:
45 
46  struct Config {
47 
49  fhicl::Name("ManageBadAlloc"),
50  fhicl::Comment("whether to catch the std::bad_alloc we throw"),
51  true // default
52  };
53 
55  fhicl::Name("ManageOutOfRange"),
56  fhicl::Comment("whether to catch the std::out_of_range we throw"),
57  true // default
58  };
59 
61  fhicl::Name("ManageArtException"),
62  fhicl::Comment("whether to catch the art::Exception we throw"),
63  true // default
64  };
65 
66  }; // struct Config
67 
69 
71  Exploder(Parameters const& config);
72 
74  virtual void analyze(art::Event const&) override;
75 
76 
77  private:
78 
82 
84  static unsigned int throwOutOfRange();
85 
87  static void throwBadAlloc();
88 
90  static void throwArtException();
91 
92  }; // class Exploder
93 
94 
95  } // namespace example
96 } // namespace lar
97 
98 //------------------------------------------------------------------------------
100  : art::EDAnalyzer(config)
101  , fManageBadAlloc(config().ManageBadAlloc())
102  , fManageOutOfRange(config().ManageOutOfRange())
104  {}
105 
106 
107 //------------------------------------------------------------------------------
109 
110  //
111  // std::length_error
112  //
113  if (fManageBadAlloc) {
114  try {
115  throwBadAlloc();
116  }
117  catch (std::bad_alloc const&) {}
118  }
119  else {
120  throwBadAlloc();
121  }
122 
123  //
124  // std::out_of_range
125  //
126  if (fManageOutOfRange) {
127  try {
128  throwOutOfRange();
129  }
130  catch (std::out_of_range const&) {}
131  }
132  else {
133  throwOutOfRange();
134  }
135 
136  //
137  // art::Exception
138  //
139  if (fManageArtException) {
140  try {
142  }
143  catch (art::Exception const&) {}
144  }
145  else {
147  }
148 
149 } // lar::example::Exploder::analyze()
150 
151 
152 //------------------------------------------------------------------------------
154 
155  std::vector<int> intData(5, 0);
156 
157  int intTotal = 0;
158  for (unsigned int i = 0; i < 10; ++i) {
159  mf::LogVerbatim("Exploder") << "Starting TOOR iteration #" << i;
160 
161  // possible std::out_of_range throw
162  intTotal += intData.at(i);
163 
164  } // for
165  mf::LogVerbatim("Exploder") << "TOOR iterations completed.";
166 
167  return intTotal;
168 } // lar::example::Exploder::throwOutOfRange()
169 
170 
171 //------------------------------------------------------------------------------
173 
174  using OneMebibyte = std::array<unsigned char, 1048576U>;
175 
176  std::vector<OneMebibyte> manyMebibytes;
177 
178  // this is allowed, but we don't have enough memory
179  mf::LogVerbatim("Exploder") << "Now allocating: " << manyMebibytes.max_size()
180  << " x " << sizeof(OneMebibyte) << " bytes";
181  manyMebibytes.resize(manyMebibytes.max_size());
182 
183 } // lar::example::Exploder::throwBadAlloc()
184 
185 
186 //------------------------------------------------------------------------------
188 
190  << "I hate the world and I am vengeful.\n";
191 
192 } // lar::example::Exploder::throwArtException()
193 
194 
195 //------------------------------------------------------------------------------
197 
198 
199 //------------------------------------------------------------------------------
MaybeLogger_< ELseverityLevel::ELsev_info, true > LogVerbatim
Exploder(Parameters const &config)
Constructor.
static unsigned int throwOutOfRange()
Throws a std::length_error exception.
fhicl::Atom< bool > ManageArtException
fhicl::Atom< bool > ManageBadAlloc
virtual void analyze(art::Event const &) override
Executes the iterations.
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:42
static void throwBadAlloc()
Throws a std::out_of_range exception.
EDAnalyzer(Table< Config > const &config)
Definition: EDAnalyzer.h:100
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
LArSoft-specific namespace.
fhicl::Atom< bool > ManageOutOfRange
HLT enums.
static void throwArtException()
Throws a std::out_of_range exception.