Changeset 90
- Timestamp:
- 04/30/08 15:14:32 (17 years ago)
- Files:
-
- 3 modified
Legend:
- Unmodified
- Added
- Removed
-
bdm/userinfo.cpp
r78 r90 2 2 // C++ Implementation: itpp_ext 3 3 // 4 // Description: 4 // Description: 5 5 // 6 6 // … … 13 13 #include <itpp/itbase.h> 14 14 #include "userinfo.h" 15 16 17 std::istream &operator>> ( std::istream &is, const uistring &ui ) {is>>ui.S;return is;};18 std::ostream &operator<< ( std::ostream &os, const uistring &ui ) {os<<ui.S;return os;}; -
bdm/userinfo.h
r78 r90 15 15 using std::cout; 16 16 using std::endl; 17 using std::string; 17 18 18 19 /*! … … 23 24 class uibase { 24 25 protected: 25 std::string comment; 26 std::string help; 26 //! String identifier of a field 27 string name; 28 //! Explanation for a user what the field means 29 string help; 30 //! Possible parent of the userinfo 27 31 uibase* parent; 28 32 //! Indentation level, i.e. number of parents 33 int ilevel; 29 34 public: 30 35 //!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 ) {} 36 uibase ( string com = "Abstract class, please ignore!", uibase* par=NULL ) :name ( com ),help ( "" ),parent ( par ) { 37 if ( parent!=NULL ) {ilevel=parent->get_level() +1;} 38 else {ilevel =0;} 39 } 34 40 35 41 //! returns a summary of its contents (used in dialogs) … … 37 43 38 44 //! interaction with the user 39 virtual void askuser() {};45 virtual void askuser() {}; 40 46 41 47 //! test if the info is valid 42 48 virtual bool isvalid() {return true;} 43 49 //! for future use 44 virtual ~uibase(){}; 50 virtual ~uibase() {}; 51 52 virtual void save ( std::ostream &os ) { 53 os.width(ilevel); 54 os.fill(' '); 55 os<<"#"<<help<<endl; 56 os.width(ilevel); 57 os.fill(' '); 58 os<<name<<" = "; 59 }; 60 virtual void load ( std::istream &is ) { 61 char tmp[200]; 62 is.ignore ( ilevel+1,'#' ); // +1 is for # 63 is.getline ( tmp,200 );help=tmp; 64 is.ignore ( ilevel,'\0' ); 65 is.getline ( tmp,200,'=' ); name=tmp; 66 }; 67 68 //!access function 69 int get_level(){return ilevel;} 45 70 }; 46 71 … … 52 77 public: 53 78 //!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 );}; 79 uiscalar ( std::string com, uibase* par =NULL) :uibase ( com,par ) {N=T ( 0 );}; 56 80 57 81 void getsummary ( std::string &S ) {S="Scalar";}; 58 82 59 void askuser () {};83 void askuser () {}; 60 84 61 //! saving the info 62 template<class T2> 63 friend std::ostream &operator<< ( std::ostream &os, const uiscalar<T2> &ui ); 85 void save ( std::ostream &os ) {uibase::save ( os );os<<N<<endl;} 86 void load ( std::istream &is ) {uibase::load ( is );is>>N;} 87 //! for future use 88 ~uiscalar() {}; 64 89 65 //! saving the info 66 template<class T2> 67 friend std::istream &operator>> ( std::istream &is, const uiscalar<T2> &ui ); 68 ~uiscalar(){}; 90 //! access function 91 void set_value ( T N0 ) {N=N0;} 69 92 }; 70 93 … … 77 100 78 101 //!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 ); 102 uistring ( std::string com , uibase* par=NULL) :uibase ( com,par ) {} 84 103 85 //! saving the info 86 friend std::istream &operator>> ( std::istream &is, const uistring &ui ); 104 void askuser () {}; 87 105 88 ~uistring(){}; 106 void save ( std::ostream &os ) {uibase::save ( os );os<<S<<endl;} 107 void load ( std::istream &is ) {uibase::load ( is );is>>S;} 108 109 ~uistring() {}; 110 //! access function 111 void set_value ( std::string S0 ) {S=S0;} 89 112 }; 90 113 … … 92 115 template<class T> 93 116 class uivector : public uibase { 94 protected: 117 protected: 95 118 itpp::Vec<T> V; 96 119 public: … … 98 121 99 122 //!Default constructor 100 uivector ( std::string com ) :uibase ( com) {};123 uivector ( std::string com, uibase* par=NULL ) :uibase ( com,par ) {}; 101 124 102 void askuser (){}; 103 //! saving the info 104 template<class T2> 105 friend std::ostream &operator<< ( std::ostream &os, const uivector<T2> &ui ); 125 void askuser () {}; 106 126 107 //! saving the info 108 template<class T2> 109 friend std::istream &operator>> ( std::istream &is, const uivector<T2> &ui ); 127 void save ( std::ostream &os ) {uibase::save ( os );os<<V<<endl;;} 128 void load ( std::istream &is ) {uibase::load ( is );is>>V;} 129 130 //! access function 131 void set_value ( itpp::Vec<T> V0 ) {V=V0;} 110 132 111 133 }; … … 114 136 template<class T> 115 137 class uimatrix : public uibase { 138 protected: 116 139 itpp::Mat<T> M; 117 140 141 public: 118 142 //!Default constructor 119 uimatrix ( std::string com ) :uibase ( com) {}143 uimatrix ( std::string com, uibase* par=NULL ) :uibase ( com,par ) {} 120 144 121 void getsummary ( std::string &S ) { sprintf ( S,"Matrix %dx%d",M.rows(),M.cols() );};145 void getsummary ( std::string &S ) { S="Matrix ";}; 122 146 123 void askuser (){}; 124 //! saving the info 125 template<class T2> 126 friend std::ostream &operator<< ( std::ostream &os, const uimatrix<T2> &ui ); 147 void askuser () {}; 127 148 128 //! saving the info 129 template<class T2> 130 friend std::istream &operator>> ( std::istream &is, const uimatrix<T2> &ui ); 149 void save ( std::ostream &os ) {uibase::save ( os );os<<M<<endl;} 150 void load ( std::istream &is ) {uibase::load ( is );is>>M;} 131 151 152 //! access function 153 void set_value ( itpp::Mat<T> M0 ) {M=M0;} 132 154 }; 133 155 134 //!Compound user info135 class uicompound: public uibase {136 protected:137 uibase** elems;138 public:139 //!Default constructor140 uicompound ( const int n0 ) : elems(new uibase*[n0]) {};141 ~uicompound(){delete elems;}142 };143 156 144 157 typedef uimatrix<double> uimat; … … 148 161 typedef uivector<int> uiivec; 149 162 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;};167 -
tests/testUI.cpp
r82 r90 1 1 #include <itpp/itbase.h> 2 #include <fstream> 2 3 #include "userinfo.h" 3 4 4 5 //These lines are needed for use of cout and endl 5 using std::cout; 6 using std::endl; 6 using namespace std; 7 7 8 8 int main() 9 9 { 10 10 uiscalar<double> uisc("Sc"); 11 uiscalar<double> uisc2(""); 11 12 uivec uiv("VEc"); 13 uivec uiv2("V"); 12 14 uistring uist("Str"); 13 uicompound uic(2); 15 uistring uist2("S"); 16 uimat uim("Mat"); 17 uimat uim2("M"); 14 18 15 cout << uisc <<endl; 16 cout << uiv <<endl; 17 cout << uist <<endl; 18 19 //SET values 20 21 uisc.set_value(0.5); 22 uiv.set_value(itpp::vec_2(1.3,1.7)); 23 uist.set_value("Wow this is cool!"); 24 uim.set_value(itpp::mat_2x2(1.1,1.2,1.3,1.4)); 25 26 ofstream OF; 27 OF.open("testUI.exb"); 28 uisc.save(OF); 29 uiv.save(OF); 30 uist.save(OF); 31 uim.save(OF); 32 OF.close(); 33 34 ifstream IF; 35 IF.open("testUI.exb"); 36 uisc2.load(IF); 37 uiv2.load(IF); 38 uist2.load(IF); 39 uim2.load(IF); 40 IF.close(); 41 19 42 //Exit program: 20 getchar();21 43 return 0; 22 44