DataIOmanip.h
Go to the documentation of this file.
1 /**
2  * @file DataIOmanip.h
3  * @brief Provides I/O manipulators for streaming data
4  * @author Gianluca Petrillo (petrillo@fnal.gov)
5  * @date January 4th, 2016
6  *
7  *
8  * This is a template library with no implementation file and no additional
9  * linking required.
10  */
11 
12 #ifndef LARDATAOBJ_UTILITIES_DATAIOMANIP_H
13 #define LARDATAOBJ_UTILITIES_DATAIOMANIP_H
14 
15 // C++ standard library
16 #include <iosfwd> // std::ostream
17 #include <utility> // std::forward()
18 
19 
20 namespace util {
21 
22  namespace manip {
23 
24 
25  namespace details {
26 
27  /**
28  * @brief Utility class to store and print a 3D vector
29  * @tparam Vect type of the vector to be printed (see the requirements)
30  *
31  * The printing operation is stored internally just for convenience.
32  *
33  * The `Vect` type is required to provide member functions `X()`, `Y()` and
34  * `Z()`, whose return value can be directly streamed.
35  */
36  template <typename Vect>
38  Vect const& v; ///< Vector to be printed
39 
40  public:
41  /// Constructor: print the specified vector
42  Vector3DStruct(Vect const& vector): v(vector) {}
43 
44  /// The printing operator
45  template <typename Stream>
46  Stream& operator() (Stream&& out) const
47  {
48  out << "( " << v.X() << " ; " << v.Y() << " ; " << v.Z() << " )";
49  return out;
50  } // operator()
51 
52  }; // class vector3D
53 
54  /// Operator to print the manipulator
55  template <typename Vect>
56  std::ostream& operator<<
57  (std::ostream& out, Vector3DStruct<Vect> const& vmanip)
58  { return vmanip(out); }
59 
60  } // namespace details
61 
62 
63  /**
64  * @brief Produces a manipulator to print a vector
65  * @tparam Vect type of vector to be printed
66  * @param v vector to be printed
67  *
68  * The typical usage is:
69  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
70  * TVector3 Tv(2., 3., 4.);
71  * recob::Track::Point_t p(5., 6., 7.);
72  *
73  * std::cout << "Tv = " << util::manip::vector3D(Tv)
74  * << "; point: " << util::manip::vector3D(p)
75  * << std::endl;
76  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
77  *
78  */
79  template <typename Vect>
80  details::Vector3DStruct<Vect> vector3D(Vect const& v) { return { v }; }
81 
82 
83  } // namespace manip
84 
85 
86 } // namespace util
87 
88 
89 #endif // LARDATAOBJ_UTILITIES_DATAIOMANIP_H
Namespace for general, non-LArSoft-specific utilities.
details::Vector3DStruct< Vect > vector3D(Vect const &v)
Produces a manipulator to print a vector.
Definition: DataIOmanip.h:80
Vector3DStruct(Vect const &vector)
Constructor: print the specified vector.
Definition: DataIOmanip.h:42
Stream & operator()(Stream &&out) const
The printing operator.
Definition: DataIOmanip.h:46
Utility class to store and print a 3D vector.
Definition: DataIOmanip.h:37
Vect const & v
Vector to be printed.
Definition: DataIOmanip.h:38