Changeset 90

Show
Ignore:
Timestamp:
04/30/08 15:14:32 (16 years ago)
Author:
smidl
Message:

basic userinfo + test

Files:
3 modified

Legend:

Unmodified
Added
Removed
  • bdm/userinfo.cpp

    r78 r90  
    22// C++ Implementation: itpp_ext 
    33// 
    4 // Description:  
     4// Description: 
    55// 
    66// 
     
    1313#include <itpp/itbase.h> 
    1414#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  
    1515using std::cout; 
    1616using std::endl; 
     17using std::string; 
    1718 
    1819/*! 
     
    2324class uibase { 
    2425protected: 
    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 
    2731        uibase* parent; 
    28  
     32        //! Indentation level, i.e. number of parents 
     33        int ilevel; 
    2934public: 
    3035//!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        } 
    3440 
    3541//! returns a summary of its contents (used in dialogs) 
     
    3743 
    3844//! interaction with the user 
    39         virtual void askuser(){}; 
     45        virtual void askuser() {}; 
    4046 
    4147//! test if the info is valid 
    4248        virtual bool isvalid() {return true;} 
    4349        //! 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;} 
    4570}; 
    4671 
     
    5277public: 
    5378//!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 );}; 
    5680 
    5781        void getsummary ( std::string &S ) {S="Scalar";}; 
    5882 
    59         void askuser (){}; 
     83        void askuser () {}; 
    6084 
    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() {}; 
    6489 
    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;} 
    6992}; 
    7093 
     
    77100 
    78101//!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 ) {} 
    84103 
    85 //! saving the info 
    86         friend std::istream &operator>> ( std::istream &is, const uistring &ui ); 
     104        void askuser () {}; 
    87105 
    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;} 
    89112}; 
    90113 
     
    92115template<class T> 
    93116class uivector : public uibase { 
    94 protected:       
     117protected: 
    95118        itpp::Vec<T> V; 
    96119public: 
     
    98121 
    99122//!Default constructor 
    100         uivector ( std::string com ) :uibase ( com ) {}; 
     123        uivector ( std::string com, uibase* par=NULL ) :uibase ( com,par ) {}; 
    101124 
    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 () {}; 
    106126 
    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;} 
    110132 
    111133}; 
     
    114136template<class T> 
    115137class uimatrix : public uibase { 
     138protected: 
    116139        itpp::Mat<T> M; 
    117          
     140 
     141public: 
    118142        //!Default constructor 
    119         uimatrix ( std::string com ) :uibase ( com ) {} 
     143        uimatrix ( std::string com, uibase* par=NULL ) :uibase ( com,par ) {} 
    120144 
    121         void getsummary ( std::string &S ) {sprintf ( S,"Matrix %dx%d",M.rows(),M.cols() );}; 
     145        void getsummary ( std::string &S ) { S="Matrix ";}; 
    122146 
    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 () {}; 
    127148 
    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;} 
    131151 
     152        //! access function 
     153        void set_value ( itpp::Mat<T> M0 ) {M=M0;} 
    132154}; 
    133155 
    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 }; 
    143156 
    144157typedef uimatrix<double> uimat; 
     
    148161typedef uivector<int> uiivec; 
    149162 
    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  
    11#include <itpp/itbase.h> 
     2#include <fstream> 
    23#include "userinfo.h" 
    34 
    45//These lines are needed for use of cout and endl 
    5 using std::cout; 
    6 using std::endl; 
     6using namespace std; 
    77 
    88int main() 
    99{ 
    1010        uiscalar<double> uisc("Sc");     
     11        uiscalar<double> uisc2("");      
    1112        uivec uiv("VEc"); 
     13        uivec uiv2("V"); 
    1214        uistring uist("Str"); 
    13         uicompound uic(2); 
     15        uistring uist2("S"); 
     16        uimat uim("Mat"); 
     17        uimat uim2("M"); 
    1418         
    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         
    1942        //Exit program: 
    20         getchar(); 
    2143        return 0; 
    2244