1 |
#ifndef COLUMN_H
|
2 |
#define COLUMN_H
|
3 |
|
4 |
#include <string>
|
5 |
#include <vector>
|
6 |
#include <sstream>
|
7 |
|
8 |
#include <iostream>
|
9 |
|
10 |
//virtual base class
|
11 |
class TColumnBase
|
12 |
{
|
13 |
public:
|
14 |
TColumnBase():_header(""){};
|
15 |
TColumnBase(std::string header):_header(header){};
|
16 |
virtual ~TColumnBase(){};
|
17 |
|
18 |
std::string GetHeader(){return _header;};
|
19 |
void SetHeader(const std::string & header){_header=header;};
|
20 |
virtual void Add(const void * ){};
|
21 |
virtual void SetPrecision(unsigned p){};
|
22 |
virtual unsigned Size(){return 0;};
|
23 |
virtual std::string Str(const unsigned i){return "";};
|
24 |
virtual unsigned Width(){return 10;};
|
25 |
|
26 |
protected:
|
27 |
std::string _header;
|
28 |
};
|
29 |
|
30 |
|
31 |
//A templated class for each column of class table
|
32 |
template<typename T>
|
33 |
class TColumn : public TColumnBase {
|
34 |
public:
|
35 |
TColumn(std::string header){SetHeader(header);_precision=1;};
|
36 |
virtual ~TColumn() {
|
37 |
for (typename std::vector<const T*>::iterator it=_field.begin(); it!=_field.end(); ++it) delete *it;
|
38 |
_field.clear();}
|
39 |
|
40 |
virtual void SetPrecision(unsigned p){_precision=p;};
|
41 |
virtual void Add(const void * f){ _field.push_back( static_cast<const T*>(f));};
|
42 |
void Set(unsigned i, T f){_field.at(i)=f;};
|
43 |
T At(const unsigned i){return _field.at(i);};
|
44 |
virtual unsigned Size(){return _field.size();};
|
45 |
std::string Str(const unsigned i){
|
46 |
if (i<0||i>=_field.size()) return "";
|
47 |
std::stringstream ss;
|
48 |
ss<<std::fixed << std::setprecision(_precision)<<std::setw(_width)<< *_field[i];
|
49 |
return ss.str();
|
50 |
};
|
51 |
virtual unsigned Width(){
|
52 |
std::stringstream ss;
|
53 |
ss << _header;
|
54 |
unsigned max=ss.str().size();
|
55 |
for (typename std::vector<const T*>::const_iterator it=_field.begin();it!=_field.end();++it){
|
56 |
std::stringstream ss;
|
57 |
ss <<std::fixed << std::setprecision(_precision) << **it;
|
58 |
if (ss.str().size()>max) max=ss.str().size();
|
59 |
}
|
60 |
_width=max;
|
61 |
return max;
|
62 |
}
|
63 |
|
64 |
protected:
|
65 |
std::vector<const T*> _field;
|
66 |
unsigned _width;
|
67 |
unsigned _precision;
|
68 |
};
|
69 |
|
70 |
#endif
|