Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:13:12

0001 #ifndef FWCore_Utilities_interface_OStreamColumn_h
0002 #define FWCore_Utilities_interface_OStreamColumn_h
0003 
0004 // -*- C++ -*-
0005 //
0006 // Package:     Utilities
0007 // Class  :     OStreamColumn
0008 //
0009 /**\class OStreamColumn OStreamColumn.h "FWCore/Utilities/interface/OStreamColumn.h"
0010 
0011  Description: Helper/IO manipulator for forming columns used in tabular output.
0012 
0013  Usage:
0014 
0015  An OStreamColumn object can be defined in two ways:
0016  \code
0017  edm::OStreamColumn col1 {"FirstNames"}; // Default width is size of the string "FirstNames"
0018  edm::OStreamColumn col2 {"LastNames", 30}; // Width is explicitly specifed as 30.
0019  \endcode
0020 
0021  After created the column objects we, one can use them with anything
0022  that supports insertion operations (e.g.):
0023 
0024  \code
0025  std::cout << col1 << col2 << '\n'; // Print column titles with the widths associated for col1 and col2.
0026  for (auto const& name : names)
0027    std::cout << col1(name.first()) << col2(name.last()) << '\n';
0028  \endcode
0029 
0030  The values 'name.first()' and 'name.last()' are printed into a column
0031  with the width associated with col1 and col2, respectively.
0032 
0033 */
0034 //
0035 // Original Author:  Kyle Knoepfel
0036 //         Created:
0037 // $Id$
0038 //
0039 
0040 #include <iomanip>
0041 #include <string>
0042 
0043 namespace edm {
0044 
0045   class OStreamColumn;
0046 
0047   template <typename T>
0048   struct OStreamColumnEntry {
0049     OStreamColumn const& col;
0050     T t;
0051   };
0052 
0053   class OStreamColumn {
0054   public:
0055     explicit OStreamColumn(std::string const& t);
0056     explicit OStreamColumn(std::string const& t, std::size_t const w);
0057 
0058     template <typename T>
0059     auto operator()(T const& t) const {
0060       return OStreamColumnEntry<T>{*this, t};
0061     }
0062 
0063     std::size_t width() const { return width_; }
0064 
0065   private:
0066     std::string title_;
0067     std::size_t width_;
0068 
0069     friend std::ostream& operator<<(std::ostream&, OStreamColumn const&);
0070 
0071     template <typename E>
0072     friend std::ostream& operator<<(std::ostream&, OStreamColumnEntry<E> const&);
0073   };
0074 
0075   std::ostream& operator<<(std::ostream& t, OStreamColumn const& c);
0076 
0077   template <typename E>
0078   std::ostream& operator<<(std::ostream& t, OStreamColumnEntry<E> const& ce) {
0079     t << std::setw(ce.col.width_) << ce.t;
0080     return t;
0081   }
0082 
0083 }  // namespace edm
0084 
0085 #endif