1 | // |
---|
2 | // C++ Interface: userinfo |
---|
3 | // |
---|
4 | // Description: |
---|
5 | // |
---|
6 | // |
---|
7 | // Author: smidl <smidl@utia.cas.cz>, (C) 2008 |
---|
8 | // |
---|
9 | // Copyright: See COPYING file that comes with this distribution |
---|
10 | // |
---|
11 | // |
---|
12 | #include <itpp/itbase.h> |
---|
13 | #include <itpp/base/mat.h> |
---|
14 | |
---|
15 | using std::cout; |
---|
16 | using std::endl; |
---|
17 | |
---|
18 | /*! |
---|
19 | @brief User Info base class |
---|
20 | |
---|
21 | This class is used to store information about parameters of an object. It support loading and saving of the information and, potentially, interaction with the user. |
---|
22 | */ |
---|
23 | class uibase { |
---|
24 | protected: |
---|
25 | std::string comment; |
---|
26 | std::string help; |
---|
27 | uibase* parent; |
---|
28 | |
---|
29 | public: |
---|
30 | //!Default constructor |
---|
31 | uibase ( std::string com = "Abstract class, please ignore!") :comment ( com ) {parent=NULL;} |
---|
32 | //!Default constructor |
---|
33 | uibase ( std::string com,uibase *par ) :comment ( com ),parent ( par ) {} |
---|
34 | |
---|
35 | //! returns a summary of its contents (used in dialogs) |
---|
36 | virtual void getsummary ( std::string &S ) {}; |
---|
37 | |
---|
38 | //! interaction with the user |
---|
39 | virtual void askuser(){}; |
---|
40 | |
---|
41 | //! test if the info is valid |
---|
42 | virtual bool isvalid() {return true;} |
---|
43 | //! for future use |
---|
44 | virtual ~uibase(){}; |
---|
45 | }; |
---|
46 | |
---|
47 | //! User info for scalars |
---|
48 | template<class T> |
---|
49 | class uiscalar : public uibase { |
---|
50 | protected: |
---|
51 | T N; |
---|
52 | public: |
---|
53 | //!Default constructor |
---|
54 | uiscalar ( std::string com,uibase* par ) :uibase ( com,par ) {N=T ( 0 );}; |
---|
55 | uiscalar ( std::string com) :uibase ( com ) {N=T ( 0 );}; |
---|
56 | |
---|
57 | void getsummary ( std::string &S ) {S="Scalar";}; |
---|
58 | |
---|
59 | void askuser (){}; |
---|
60 | |
---|
61 | //! saving the info |
---|
62 | template<class T2> |
---|
63 | friend std::ostream &operator<< ( std::ostream &os, const uiscalar<T2> &ui ); |
---|
64 | |
---|
65 | //! saving the info |
---|
66 | template<class T2> |
---|
67 | friend std::istream &operator>> ( std::istream &is, const uiscalar<T2> &ui ); |
---|
68 | ~uiscalar(){}; |
---|
69 | }; |
---|
70 | |
---|
71 | //! User info for strings |
---|
72 | class uistring : public uibase { |
---|
73 | protected: |
---|
74 | std::string S; |
---|
75 | public: |
---|
76 | void getsummary ( std::string &S ) {S="String";}; |
---|
77 | |
---|
78 | //!Default constructor |
---|
79 | uistring ( std::string com ) :uibase ( com ) {} |
---|
80 | |
---|
81 | void askuser (){}; |
---|
82 | //! saving the info |
---|
83 | friend std::ostream &operator<< ( std::ostream &os, const uistring &ui ); |
---|
84 | |
---|
85 | //! saving the info |
---|
86 | friend std::istream &operator>> ( std::istream &is, const uistring &ui ); |
---|
87 | |
---|
88 | ~uistring(){}; |
---|
89 | }; |
---|
90 | |
---|
91 | //! User info for vectors |
---|
92 | template<class T> |
---|
93 | class uivector : public uibase { |
---|
94 | protected: |
---|
95 | itpp::Vec<T> V; |
---|
96 | public: |
---|
97 | void getsummary ( std::string &S ) { S="Vector of length "+ V.length();}; |
---|
98 | |
---|
99 | //!Default constructor |
---|
100 | uivector ( std::string com ) :uibase ( com ) {}; |
---|
101 | |
---|
102 | void askuser (){}; |
---|
103 | //! saving the info |
---|
104 | template<class T2> |
---|
105 | friend std::ostream &operator<< ( std::ostream &os, const uivector<T2> &ui ); |
---|
106 | |
---|
107 | //! saving the info |
---|
108 | template<class T2> |
---|
109 | friend std::istream &operator>> ( std::istream &is, const uivector<T2> &ui ); |
---|
110 | |
---|
111 | }; |
---|
112 | |
---|
113 | //! User info for matrices |
---|
114 | template<class T> |
---|
115 | class uimatrix : public uibase { |
---|
116 | itpp::Mat<T> M; |
---|
117 | |
---|
118 | //!Default constructor |
---|
119 | uimatrix ( std::string com ) :uibase ( com ) {} |
---|
120 | |
---|
121 | void getsummary ( std::string &S ) {sprintf ( S,"Matrix %dx%d",M.rows(),M.cols() );}; |
---|
122 | |
---|
123 | void askuser (){}; |
---|
124 | //! saving the info |
---|
125 | template<class T2> |
---|
126 | friend std::ostream &operator<< ( std::ostream &os, const uimatrix<T2> &ui ); |
---|
127 | |
---|
128 | //! saving the info |
---|
129 | template<class T2> |
---|
130 | friend std::istream &operator>> ( std::istream &is, const uimatrix<T2> &ui ); |
---|
131 | |
---|
132 | }; |
---|
133 | |
---|
134 | //!Compound user info |
---|
135 | class uicompound: public uibase { |
---|
136 | protected: |
---|
137 | uibase** elems; |
---|
138 | public: |
---|
139 | //!Default constructor |
---|
140 | uicompound ( const int n0 ) : elems(new uibase*[n0]) {}; |
---|
141 | ~uicompound(){delete elems;} |
---|
142 | }; |
---|
143 | |
---|
144 | typedef uimatrix<double> uimat; |
---|
145 | typedef uimatrix<int> uiimat; |
---|
146 | |
---|
147 | typedef uivector<double> uivec; |
---|
148 | typedef uivector<int> uiivec; |
---|
149 | |
---|
150 | |
---|
151 | template<class T> |
---|
152 | std::istream &operator>> ( std::istream &is, const uiscalar<T> &ui ) {is>>ui.N;return is;}; |
---|
153 | template<class T> |
---|
154 | std::ostream &operator<< ( std::ostream &os, const uiscalar<T> &ui ) {os<<ui.N;return os;}; |
---|
155 | |
---|
156 | template<class T> |
---|
157 | std::istream &operator>> ( std::istream &is, const uivector<T> &ui ) {is>>ui.V;return is;}; |
---|
158 | |
---|
159 | template<class T> |
---|
160 | std::ostream &operator<< ( std::ostream &os, const uivector<T> &ui ) {os<<ui.V; return os;}; |
---|
161 | |
---|
162 | template<class T> |
---|
163 | std::istream &operator>> ( std::istream &is, const uimatrix<T> &ui ) {is>>ui.M;return is;}; |
---|
164 | |
---|
165 | template<class T> |
---|
166 | std::ostream &operator<< ( std::ostream &os, const uimatrix<T> &ui ) {os<<ui.M;return os;}; |
---|