LArSoft  v07_13_02
Liquid Argon Software toolkit - http://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 <string>
16 #include <sstream>
17 #include <type_traits>
18 
19 
20 namespace lar {
21 
22 
24  namespace dump {
25 
26  namespace details {
27 
28  template <typename Coll>
29  auto ptr_cbegin(Coll const& v) { using std::cbegin; return cbegin(v); }
30 
31  template <typename T>
32  std::add_const_t<T>* ptr_cbegin(T* ptr) { return ptr; }
33 
34 
36  template <typename Stream, typename Array>
37  void dumpArray(Stream&& out, Array&& a, size_t n) {
38  out << "{";
39  if (n == 0) { out << "}"; return; }
40  auto it = ptr_cbegin(a);
41  out << " " << *it;
42  std::size_t i = 0;
43  while (++i < n) out << "; " << (*++it);
44  out << " }";
45  } // dumpArray()
46 
47  } // namespace details
48 
59  template <typename Array>
60  struct ArrayDumper {
61  using Array_t = Array;
63 
64  Array_t const& a;
65  size_t n;
66 
67  ArrayDumper(Array_t const& a, size_t n): a(a), n(n) {}
68 
69  // constructors ahead
70  ArrayDumper(This_t const& from) = default;
71  ArrayDumper(This_t&& from) = default;
72  ArrayDumper& operator=(This_t const& from) = delete;
73  ArrayDumper& operator=(This_t&& from) = delete;
74 
76  template <typename Stream>
77  void operator() (Stream&& out) const
78  { details::dumpArray(std::forward<Stream>(out), a, n); }
79 
81  explicit operator std::string() const
82  { std::ostringstream sstr; this->operator()(sstr); return sstr.str(); }
83 
84  }; // struct ArrayDumper
85 
86 
87  template <typename T>
88  struct ArrayDumper<T*> {
89  using Array_t = T*;
91 
93  size_t n;
94 
95  ArrayDumper(Array_t a, size_t n): a(a), n(n) {}
96 
98  template <typename Stream>
99  void operator() (Stream&& out) const
100  { details::dumpArray(std::forward<Stream>(out), a, n); }
101 
103  explicit operator std::string() const
104  { std::ostringstream sstr; this->operator()(sstr); return sstr.str(); }
105 
106  }; // struct ArrayDumper<T*>
107 
108 
154  template <typename Vector>
155  struct VectorDumper {
156  using Vector_t = Vector;
158 
159  Vector_t const& v;
160 
161  explicit VectorDumper(Vector_t const& v): v(v) {}
162 
163  // constructors ahead
164  VectorDumper(This_t const& from) = default;
165  VectorDumper(This_t&& from) = default;
166  VectorDumper& operator=(This_t const& from) = delete;
167  VectorDumper& operator=(This_t&& from) = delete;
168 
170  template <typename Stream>
171  void operator() (Stream&& out) const
172  { out << "{ " << v.X() << "; " << v.Y() << "; " << v.Z() << " }"; }
173 
175  explicit operator std::string() const
176  { std::ostringstream sstr; this->operator()(sstr); return sstr.str(); }
177 
178  }; // struct VectorDumper<>
179 
180 
181  // Specialization for bare pointers.
182  template <typename T>
183  struct VectorDumper<T*>: public ArrayDumper<T const*> {
184  explicit VectorDumper(T* v): ArrayDumper<T const*>(v, 3U) {}
185  }; // VectorDumper<T*>
186 
187  // Specialization for C-style arrays.
188  template <typename T>
189  struct VectorDumper<T[3]>: public ArrayDumper<T const*> {
190  explicit VectorDumper(T const* v): ArrayDumper<T const*>(v, 3U) {}
191  }; // VectorDumper<T*>
192 
193 
194 
227  template <size_t N, typename Array>
228  auto array(Array const& a) { return ArrayDumper<Array>(a, N); }
229 
230 
264  template <typename Vector>
265  auto vector(Vector const& v) { return ArrayDumper<Vector>(v, v.size()); }
266 
267 
268 
300  template <typename Vector3D>
301  auto vector3D(Vector3D const& v) { return VectorDumper<Vector3D>(v); }
302 
303 
304 
335  template <typename Stream, typename Array>
336  Stream& operator<< (Stream&& out, ArrayDumper<Array>&& manip)
337  { manip(std::forward<Stream>(out)); return out; }
338 
339 
369  template <typename Stream, typename Vector>
370  Stream& operator<< (Stream&& out, VectorDumper<Vector>&& manip)
371  { manip(std::forward<Stream>(out)); return out; }
372 
403  template <typename String, typename Vector>
404  String operator+ (String const& s, VectorDumper<Vector> const& manip)
405  { return s + std::string(manip);}
406 
408  template <typename Vector>
409  std::string operator+ (const char* s, VectorDumper<Vector> const& manip)
410  { return std::string(s) + manip;}
411 
413  template <typename String, typename Vector>
414  String operator+ (VectorDumper<Vector> const& manip, String const& s)
415  { return std::string(manip) + s;}
416 
418  template <typename Vector>
419  std::string operator+ (VectorDumper<Vector> const& manip, const char* s)
420  { return manip + std::string(s);}
421 
423  template <typename String, typename Vector>
424  String& operator+= (String& s, VectorDumper<Vector> const& manip)
425  { return s += std::string(manip); }
426 
427  } // namespace dump
428 
429 
430 } // namespace lar
431 
432 
433 #endif // LARCORE_COREUTILS_DUMPUTILS_H
Float_t s
Definition: plot.C:23
auto vector3D(Vector3D const &v)
Returns a manipulator which will print the specified vector.
Definition: DumpUtils.h:301
String operator+(String const &s, VectorDumper< Vector > const &manip)
Concatenates a vector to the specified string.
Definition: DumpUtils.h:404
void dumpArray(Stream &&out, Array &&a, size_t n)
Inserts n of elements of a in the specified stream.
Definition: DumpUtils.h:37
recob::tracking::Vector_t Vector3D
Definition: Utilities.h:29
size_t n
Number of elements to be printed.
Definition: DumpUtils.h:65
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:265
ArrayDumper(Array_t a, size_t n)
Definition: DumpUtils.h:95
auto array(Array const &a)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:228
Vector_t const & v
A reference to the vector to be printed.
Definition: DumpUtils.h:159
auto ptr_cbegin(Coll const &v)
Definition: DumpUtils.h:29
Array_t const & a
A reference to the array to be printed.
Definition: DumpUtils.h:64
VectorDumper(Vector_t const &v)
Definition: DumpUtils.h:161
Dumps the first N elements of an array.
Definition: DumpUtils.h:60
String & operator+=(String &s, VectorDumper< Vector > const &manip)
Appends a string rendering of a vector to the specified string.
Definition: DumpUtils.h:424
Array_t a
A reference to the array to be printed.
Definition: DumpUtils.h:92
LArSoft-specific namespace.
ArrayDumper(Array_t const &a, size_t n)
Definition: DumpUtils.h:67
Char_t n[5]
size_t n
Number of elements to be printed.
Definition: DumpUtils.h:93
Manipulator managing the dump of the vector content into a stream.
Definition: DumpUtils.h:155