LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
OpDetVisibilityData.h
Go to the documentation of this file.
1 
11 #ifndef LARSIM_PHOTONPROPAGATION_LIBRARYMAPPINGTOOLS_OPDETVISIBILITYDATA_H
12 #define LARSIM_PHOTONPROPAGATION_LIBRARYMAPPINGTOOLS_OPDETVISIBILITYDATA_H
13 
14 // LArSoft libraries
16 
17 // C++ standard libraries
18 #include <iterator> // std::size()
19 #include <type_traits> // std::enable_if_t<>
20 #include <utility> // std::forward()
21 
22 namespace phot {
23 
58  template <typename Cont, typename Enable = void>
60 
71  template <typename Cont>
72  bool isValidLibraryData(Cont&& cont);
73 
116  template <typename Cont, typename Mapping>
117  class OpDetVisibilityData : public util::MappedContainer<Cont, Mapping> {
119 
120  public:
121  // import types from base class
122  using typename ContainerBase_t::DataContainer_t;
123  using typename ContainerBase_t::Mapping_t;
124  using typename ContainerBase_t::size_type;
125  using typename ContainerBase_t::value_type;
126 
127  private:
129  static size_type effectiveSize(DataContainer_t const& cont, size_type allegedSize);
130 
132  static size_type effectiveSize(DataContainer_t const& cont, Mapping_t const& mapping);
133 
134  public:
135  // --- BEGIN Constructors --------------------------------------------------
136 
140  OpDetVisibilityData() = default;
141 
155  Mapping_t const& mapping,
156  size_type size,
157  value_type defValue)
158  : ContainerBase_t(cont, mapping, effectiveSize(cont, size), defValue)
159  {}
160 
173  : ContainerBase_t(cont, mapping, effectiveSize(cont, size))
174  {}
175 
189  OpDetVisibilityData(DataContainer_t const& cont, Mapping_t const& mapping)
190  : ContainerBase_t(cont, mapping, effectiveSize(cont, mapping))
191  {}
192 
193  // --- END Constructors ----------------------------------------------------
194 
195  // --- BEGIN Validity information ------------------------------------------
198 
200  bool isValid() const { return !ContainerBase_t::empty(); }
201 
203  operator bool() const { return isValid(); }
204 
206  bool operator!() const { return !isValid(); }
207 
209  // --- END Validity information --------------------------------------------
210 
211  }; // OpDetVisibilityData
212 
213 } // namespace phot
214 
215 //------------------------------------------------------------------------------
216 //--- template implementation
217 //------------------------------------------------------------------------------
218 
219 namespace phot {
220 
221  namespace details {
222 
223  //--------------------------------------------------------------------------
224  template <typename Cont>
225  auto generic_size(Cont&& cont)
226  {
227  using std::size;
228  return size(std::forward<Cont>(cont));
229  }
230 
231  //--------------------------------------------------------------------------
232  // layered implementation of `LibraryDataValidatorStructStandardImpl`:
233 
234  // This is the last layer: if compiler points here,
235  // the type `Cont` is not being supported yet:
236  // - std::size() (or better, `generic_size()`)
237  template <typename Cont, typename = void>
239 
240  template <typename Cont>
242  Cont,
243  std::enable_if_t<util::always_true_v<decltype(generic_size(std::declval<Cont>()))>>> {
244  static bool isValid(Cont const& cont) { return generic_size(cont) > 0U; }
245  }; // struct LibraryDataValidatorStructStandardImpl_size
246 
247  // - Cont::empty()
248  template <typename Cont, typename = void>
251 
252  template <typename Cont>
254  Cont,
255  std::enable_if_t<util::always_true_v<decltype(std::declval<Cont const>().empty())>>> {
256  static bool isValid(Cont const& cont) { return !cont.empty(); }
257  }; // struct LibraryDataValidatorStructStandardImpl_empty
258 
259  // - Cont::is_valid()
260  template <typename Cont, typename = void>
263 
264  template <typename Cont>
266  Cont,
267  std::enable_if_t<util::always_true_v<decltype(std::declval<Cont const>().is_valid())>>> {
268  static bool isValid(Cont const& cont) { return cont.is_valid(); }
269  }; // struct LibraryDataValidatorStructStandardImpl_is_valid
270 
271  // - Cont::isValid()
272  template <typename Cont, typename = void>
275 
276  template <typename Cont>
278  Cont,
279  std::enable_if_t<util::always_true_v<decltype(std::declval<Cont const>().isValid())>>> {
280  static bool isValid(Cont const& cont) { return cont.isValid(); }
281  }; // struct LibraryDataValidatorStructStandardImpl_isValid
282 
283  // - std::unique_ptr<> (using `enable_if` to catch all qualifiers)
284  template <typename Cont, typename = void>
287 
288  template <typename Cont>
290  Cont,
291  std::enable_if_t<util::is_unique_ptr_v<Cont>>> {
292  static bool isValid(Cont const& cont) { return bool(cont); }
293  }; // struct LibraryDataValidatorStructStandardImpl_unique_ptr
294 
295  // C pointer types:
296  template <typename T>
298 
299  // - T*
300  template <typename T>
302  static bool isValid(T* ptr) { return bool(ptr); }
303  };
304 
305  // - T[]
306  template <typename T>
309 
310  // - T[N]
311  template <typename T, std::size_t N>
313  static bool isValid(T (&)[N]) { return N > 0U; }
314  };
315 
316  // - entry point
317  template <typename Cont, typename = void>
320 
321  template <typename Cont>
323  Cont,
324  std::enable_if_t<std::is_pointer_v<std::decay_t<Cont>>>>
326 
327  // - entry point
328  template <typename Cont>
331 
332  //--------------------------------------------------------------------------
333 
334  } // namespace details
335 
336  //----------------------------------------------------------------------------
337  // we pick a standard implementation of our own, and let users add here
338  template <typename Cont, typename /* = void */>
340  : public details::LibraryDataValidatorStructStandardImpl<std::remove_reference_t<Cont>> {};
341 
342  //----------------------------------------------------------------------------
343  template <typename Cont>
344  bool isValidLibraryData(Cont&& cont)
345  {
347  } // isValidLibraryData()
348 
349  //----------------------------------------------------------------------------
350  template <typename Cont, typename Mapping>
352  size_type allegedSize) -> size_type
353  {
354  return isValidLibraryData(cont) ? allegedSize : 0U;
355  }
356 
357  template <typename Cont, typename Mapping>
359  Mapping_t const& mapping) -> size_type
360  {
361  return isValidLibraryData(cont) ? ContainerBase_t::minimal_size(cont, mapping) : 0U;
362  } // OpDetVisibilityData::effectiveSize(DataContainer_t, Mapping_t)
363 
364  //----------------------------------------------------------------------------
365 
366 } // namespace phot
367 
368 #endif // LARSIM_PHOTONPROPAGATION_LIBRARYMAPPINGTOOLS_OPDETVISIBILITYDATA_H
Provides MappedContainer class.
auto generic_size(Cont &&cont)
A meta-container providing transparent mapping on top of another.
Mapping Mapping_t
Type of the mapping object.
OpDetVisibilityData(DataContainer_t const &cont, Mapping_t const &mapping, size_type size)
Constructor: acquires data and mapping.
size_type minimal_size() const
Returns the minimum size to include all mapped values.
OpDetVisibilityData(DataContainer_t const &cont, Mapping_t const &mapping)
Constructor: acquires data and mapping.
STL namespace.
size_type size() const
Returns the nominal size of the container (after mapping).
OpDetVisibilityData(DataContainer_t const &cont, Mapping_t const &mapping, size_type size, value_type defValue)
Constructor: acquires data, mapping and a default value.
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:101
static size_type effectiveSize(DataContainer_t const &cont, size_type allegedSize)
Determines whether the current content should be considered valid.
bool operator!() const
Returns whether the container has no valid data.
Cont DataContainer_t
Type of the original container.
bool isValidLibraryData(Cont &&cont)
Returns the validity of content of library data in the container.
General LArSoft Utilities.
A container for photon visibility mapping data.
bool isValid() const
Returns whether the contained data is valid or not.
bool empty() const
Returns whether the container has no elements.
OpDetVisibilityData()=default
Constructor: acquires data, mapping and a default value.
Trait for determining the validity of library data in a container.