XBotInterface  2.4.1
XBotInterface provides a generic API to model and control a robot.
table_printer.h
Go to the documentation of this file.
1 #ifndef BPRINTER_TABLE_PRINTER_H_
2 #define BPRINTER_TABLE_PRINTER_H_
3 
4 #include <iostream>
5 #include <iomanip>
6 #include <vector>
7 #include <string>
8 #include <sstream>
9 #include <cmath>
10 
11 namespace bprinter {
12 class endl{};
33 public:
34  TablePrinter(std::ostream * output, const std::string & separator = "|");
35  ~TablePrinter();
36 
37  int get_num_columns() const;
38  int get_table_width() const;
39  void set_separator(const std::string & separator);
40  void set_flush_left();
41  void set_flush_right();
42 
43  void AddColumn(const std::string & header_name, int column_width);
44  void PrintHeader();
45  void PrintFooter();
46 
48  while (j_ != 0){
49  *this << "";
50  }
51  return *this;
52  }
53 
54  // Can we merge these?
55  TablePrinter& operator<<(float input);
56  TablePrinter& operator<<(double input);
57 
58  template<typename T> TablePrinter& operator<<(T input){
59  if (j_ == 0)
60  *out_stream_ << "|";
61 
62  if(flush_left_)
63  *out_stream_ << std::left;
64  else
65  *out_stream_ << std::right;
66 
67  // Leave 3 extra space: One for negative sign, one for zero, one for decimal
68  *out_stream_ << std::setw(column_widths_.at(j_))
69  << input;
70 
71  if (j_ == get_num_columns()-1){
72  *out_stream_ << "|\n";
73  i_ = i_ + 1;
74  j_ = 0;
75  } else {
76  *out_stream_ << separator_;
77  j_ = j_ + 1;
78  }
79 
80  return *this;
81  }
82 
83 private:
84  void PrintHorizontalLine();
85 
86  template<typename T> void OutputDecimalNumber(T input);
87 
88  std::ostream * out_stream_;
89  std::vector<std::string> column_headers_;
90  std::vector<int> column_widths_;
91  std::string separator_;
92 
93  int i_; // index of current row
94  int j_; // index of current column
95 
96  int table_width_;
97  bool flush_left_;
98 };
99 
100 }
101 
102 #include "impl/table_printer.tpp.h"
103 #endif
table_printer.tpp.h
bprinter::TablePrinter::operator<<
TablePrinter & operator<<(endl input)
Definition: table_printer.h:47
bprinter::TablePrinter::set_separator
void set_separator(const std::string &separator)
Definition: table_printer.cpp:28
bprinter::endl
Definition: table_printer.h:12
bprinter::TablePrinter::~TablePrinter
~TablePrinter()
Definition: table_printer.cpp:16
bprinter::TablePrinter::operator<<
TablePrinter & operator<<(T input)
Definition: table_printer.h:58
bprinter::TablePrinter::PrintFooter
void PrintFooter()
Definition: table_printer.cpp:86
bprinter::TablePrinter::AddColumn
void AddColumn(const std::string &header_name, int column_width)
Add a column to our table.
Definition: table_printer.cpp:45
bprinter::TablePrinter::get_table_width
int get_table_width() const
Definition: table_printer.cpp:24
bprinter::TablePrinter::get_num_columns
int get_num_columns() const
Definition: table_printer.cpp:20
bprinter::TablePrinter::PrintHeader
void PrintHeader()
Definition: table_printer.cpp:65
bprinter::TablePrinter
Definition: table_printer.h:32
bprinter::TablePrinter::TablePrinter
TablePrinter(std::ostream *output, const std::string &separator="|")
Definition: table_printer.cpp:7
bprinter::TablePrinter::set_flush_left
void set_flush_left()
Definition: table_printer.cpp:32
bprinter::TablePrinter::set_flush_right
void set_flush_right()
Definition: table_printer.cpp:36
bprinter
Definition: table_printer.tpp.h:6