LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
DumpUtils.h
Go to the documentation of this file.
1 
11 #ifndef LARCORE_COREUTILS_DUMPUTILS_H
12 #define LARCORE_COREUTILS_DUMPUTILS_H
13 
14 // C++ libraries
15 #include <sstream>
16 #include <string>
17 #include <type_traits>
18 
19 namespace lar {
20 
22  namespace dump {
23 
24  namespace details {
25 
26  template <typename Coll>
27  auto ptr_cbegin(Coll const& v)
28  {
29  using std::cbegin;
30  return cbegin(v);
31  }
32 
33  template <typename T>
34  std::add_const_t<T>* ptr_cbegin(T* ptr)
35  {
36  return ptr;
37  }
38 
40  template <typename Stream, typename Array>
41  void dumpArray(Stream&& out, Array&& a, size_t n)
42  {
43  out << "{";
44  if (n == 0) {
45  out << "}";
46  return;
47  }
48  auto it = ptr_cbegin(a);
49  out << " " << *it;
50  std::size_t i = 0;
51  while (++i < n)
52  out << "; " << (*++it);
53  out << " }";
54  } // dumpArray()
55 
56  } // namespace details
57 
68  template <typename Array>
69  struct ArrayDumper {
70  using Array_t = Array;
72 
73  Array_t const& a;
74  size_t n;
75 
76  ArrayDumper(Array_t const& a, size_t n) : a(a), n(n) {}
77 
78  // constructors ahead
79  ArrayDumper(This_t const& from) = default;
80  ArrayDumper(This_t&& from) = default;
81  ArrayDumper& operator=(This_t const& from) = delete;
82  ArrayDumper& operator=(This_t&& from) = delete;
83 
85  template <typename Stream>
86  void operator()(Stream&& out) const
87  {
88  details::dumpArray(std::forward<Stream>(out), a, n);
89  }
90 
92  explicit operator std::string() const
93  {
94  std::ostringstream sstr;
95  this->operator()(sstr);
96  return sstr.str();
97  }
98 
99  }; // struct ArrayDumper
100 
101  template <typename T>
102  struct ArrayDumper<T*> {
103  using Array_t = T*;
105 
107  size_t n;
108 
109  ArrayDumper(Array_t a, size_t n) : a(a), n(n) {}
110 
112  template <typename Stream>
113  void operator()(Stream&& out) const
114  {
115  details::dumpArray(std::forward<Stream>(out), a, n);
116  }
117 
119  explicit operator std::string() const
120  {
121  std::ostringstream sstr;
122  this->operator()(sstr);
123  return sstr.str();
124  }
125 
126  }; // struct ArrayDumper<T*>
127 
173  template <typename Vector>
174  struct VectorDumper {
175  using Vector_t = Vector;
177 
178  Vector_t const& v;
179 
180  explicit VectorDumper(Vector_t const& v) : v(v) {}
181 
182  // constructors ahead
183  VectorDumper(This_t const& from) = default;
184  VectorDumper(This_t&& from) = default;
185  VectorDumper& operator=(This_t const& from) = delete;
186  VectorDumper& operator=(This_t&& from) = delete;
187 
189  template <typename Stream>
190  void operator()(Stream&& out) const
191  {
192  out << "{ " << v.X() << "; " << v.Y() << "; " << v.Z() << " }";
193  }
194 
196  explicit operator std::string() const
197  {
198  std::ostringstream sstr;
199  this->operator()(sstr);
200  return sstr.str();
201  }
202 
203  }; // struct VectorDumper<>
204 
205  // Specialization for bare pointers.
206  template <typename T>
207  struct VectorDumper<T*> : public ArrayDumper<T const*> {
208  explicit VectorDumper(T* v) : ArrayDumper<T const*>(v, 3U) {}
209  }; // VectorDumper<T*>
210 
211  // Specialization for C-style arrays.
212  template <typename T>
213  struct VectorDumper<T[3]> : public ArrayDumper<T const*> {
214  explicit VectorDumper(T const* v) : ArrayDumper<T const*>(v, 3U) {}
215  }; // VectorDumper<T*>
216 
249  template <size_t N, typename Array>
250  auto array(Array const& a)
251  {
252  return ArrayDumper<Array>(a, N);
253  }
254 
288  template <typename Vector>
289  auto vector(Vector const& v)
290  {
291  return ArrayDumper<Vector>(v, v.size());
292  }
293 
325  template <typename Vector3D>
326  auto vector3D(Vector3D const& v)
327  {
328  return VectorDumper<Vector3D>(v);
329  }
330 
361  template <typename Stream, typename Array>
362  Stream& operator<<(Stream&& out, ArrayDumper<Array>&& manip)
363  {
364  manip(std::forward<Stream>(out));
365  return out;
366  }
367 
397  template <typename Stream, typename Vector>
398  Stream& operator<<(Stream&& out, VectorDumper<Vector>&& manip)
399  {
400  manip(std::forward<Stream>(out));
401  return out;
402  }
403 
434  template <typename String, typename Vector>
435  String operator+(String const& s, VectorDumper<Vector> const& manip)
436  {
437  return s + std::string(manip);
438  }
439 
441  template <typename Vector>
442  std::string operator+(const char* s, VectorDumper<Vector> const& manip)
443  {
444  return std::string(s) + manip;
445  }
446 
448  template <typename String, typename Vector>
449  String operator+(VectorDumper<Vector> const& manip, String const& s)
450  {
451  return std::string(manip) + s;
452  }
453 
455  template <typename Vector>
456  std::string operator+(VectorDumper<Vector> const& manip, const char* s)
457  {
458  return manip + std::string(s);
459  }
460 
462  template <typename String, typename Vector>
463  String& operator+=(String& s, VectorDumper<Vector> const& manip)
464  {
465  return s += std::string(manip);
466  }
467 
468  } // namespace dump
469 
470 } // namespace lar
471 
472 #endif // LARCORE_COREUTILS_DUMPUTILS_H
void operator()(Stream &&out) const
Inserts the content of the referenced array into the specified stream.
Definition: DumpUtils.h:113
auto vector3D(Vector3D const &v)
Returns a manipulator which will print the specified vector.
Definition: DumpUtils.h:326
void operator()(Stream &&out) const
Inserts the content of the referenced array into the specified stream.
Definition: DumpUtils.h:86
String operator+(String const &s, VectorDumper< Vector > const &manip)
Concatenates a vector to the specified string.
Definition: DumpUtils.h:435
void dumpArray(Stream &&out, Array &&a, size_t n)
Inserts n of elements of a in the specified stream.
Definition: DumpUtils.h:41
recob::tracking::Vector_t Vector3D
Definition: Utilities.h:34
size_t n
Number of elements to be printed.
Definition: DumpUtils.h:74
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:289
ArrayDumper(Array_t a, size_t n)
Definition: DumpUtils.h:109
auto array(Array const &a)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:250
Vector_t const & v
A reference to the vector to be printed.
Definition: DumpUtils.h:178
auto ptr_cbegin(Coll const &v)
Definition: DumpUtils.h:27
Array_t const & a
A reference to the array to be printed.
Definition: DumpUtils.h:73
VectorDumper(Vector_t const &v)
Definition: DumpUtils.h:180
Dumps the first N elements of an array.
Definition: DumpUtils.h:69
String & operator+=(String &s, VectorDumper< Vector > const &manip)
Appends a string rendering of a vector to the specified string.
Definition: DumpUtils.h:463
void operator()(Stream &&out) const
Inserts the content of the stored vector into the specified stream.
Definition: DumpUtils.h:190
Array_t a
A reference to the array to be printed.
Definition: DumpUtils.h:106
Collection of utilities for dumping data on screen.
Definition: DumperBase.h:28
LArSoft-specific namespace.
decltype(auto) constexpr cbegin(T &&obj)
ADL-aware version of std::cbegin.
Definition: StdUtils.h:85
ArrayDumper(Array_t const &a, size_t n)
Definition: DumpUtils.h:76
Char_t n[5]
size_t n
Number of elements to be printed.
Definition: DumpUtils.h:107
Manipulator managing the dump of the vector content into a stream.
Definition: DumpUtils.h:174