TF++, Miscellaneous classes and modules in C++:
handletest.cc
Go to the documentation of this file.
1 
34 #define HANDLETEST_VERSION \
35  "HANDLETEST V1.0 test handle containers"
36 
37 #include <iostream>
38 #include <string>
39 #include <tfxx/commandline.h>
40 #include <tfxx/handle.h>
41 #include <tfxx/range.h>
42 
43 using std::cout;
44 using std::cerr;
45 using std::endl;
46 
48 #define CODE(line) cout.width(50); cout << endl << #line << "; --> "; line
49 
51 #define ILLEGAL(line) cout.width(50); cout << #line << \
52  "; !!! does not compile!" << endl;
53 
55 
56 std::ostream& operator << (std::ostream& os, const Tirange& r)
57 { os << r.first() << "-" << r.last(); }
58 
59 std::ostream& operator << (std::ostream& os,
61 { os << r->first() << "-" << r->last(); }
62 
63 class Any {
64  public:
65  virtual ~Any() { }
66  virtual void print() const { cout << "ERROR: called base!" << endl; }
67 }; // class Any
68 
69 class Int: public Any {
70  public:
71  Int(const int& v): Mv(v) { }
72  void print() const { cout << Mv << endl; }
73  private:
74  int Mv;
75 }; // class Int
76 
77 class String: public Any {
78  public:
79  String(const std::string& v): Mv(v) { }
80  void print() const { cout << Mv << endl; }
81  private:
82  std::string Mv;
83 }; // class Int
84 
85 int main(int iargc, char* argv[])
86 {
87 
88  // define usage information
89  char usage_text[]=
90  {
92  "usage: handletest" "\n"
93  " or: handletest --help|-h" "\n"
94  };
95 
96  // define full help text
97  char help_text[]=
98  {
99  HANDLETEST_CVSID
100  };
101 
102  // define commandline options
103  using namespace tfxx::cmdline;
104  static Declare options[]=
105  {
106  // 0: print help
107  {"help",arg_no,"-"},
108  // 1: verbose mode
109  {"v",arg_no,"-"},
110  {NULL}
111  };
112 
113  // no arguments? print usage...
114  if (iargc<1)
115  {
116  cerr << usage_text << endl;
117  exit(0);
118  }
119 
120  // collect options from commandline
121  Commandline cmdline(iargc, argv, options);
122 
123  // help requested? print full help text...
124  if (cmdline.optset(0))
125  {
126  cerr << usage_text << endl;
127  cerr << help_text << endl;
128  exit(0);
129  }
130 
131  CODE( Tirange r1(4,8) );
132  CODE( cout << r1 << endl );
133  CODE( typedef tfxx::Handle<Tirange> Tirhandle );
134  CODE( Tirhandle h1(r1) );
135  CODE( cout << *h1 << endl );
136  CODE( Tirhandle h2(h1) );
137  CODE( cout << *h2 << endl );
138  CODE( *h1=Tirange(12,34) );
139  CODE( cout << *h2 << endl );
140  CODE( h2->shift(-12) );
141  CODE( cout << *h1 << endl );
142  CODE( Tirhandle::Tcoc ch1(h1) );
143  CODE( cout << *ch1 << endl );
144  CODE( h2->shift(10) );
145  CODE( cout << *ch1 << endl );
146  ILLEGAL( ch1->shift(10) );
147  CODE( cout << *h1 << ", " << h1 << endl );
148  CODE( cout << *ch1 << ", " << ch1 << endl );
149 
150  CODE( String s1("Hi there") );
151  CODE( s1.print() );
152  CODE( Int i1(13) );
153  CODE( i1.print() );
154  CODE( typedef tfxx::Handle<Any> Tanyhandle );
155  CODE( Tanyhandle ah1(s1) );
156  CODE( ah1->print() );
157  CODE( Tanyhandle ah2(new String("ein string")) );
158  CODE( ah2->print() );
159  CODE( Tanyhandle ah3(ah2) );
160  CODE( ah3->print() );
161  CODE( Tanyhandle ah4(new Int(4321)) );
162  CODE( ah4->print() );
163  CODE( ah3=ah4 );
164  CODE( ah3->print() );
165 }
166 
167 /* ----- END OF handletest.cc ----- */
Tvalue & last()
access end of range
Definition: range.h:65
template #tfxx::THandle<class TObj>#:
Definition: handle.h:82
#define HANDLETEST_VERSION
Definition: handletest.cc:34
#define CODE(line)
Show code along with output of executed code.
Definition: handletest.cc:48
std::ostream & operator<<(std::ostream &os, const Tirange &r)
Definition: handletest.cc:56
bool optset(const int &iopt) const
true if option # iopt was set on commandline
Definition: commandline.h:213
int Mv
Definition: handletest.cc:74
Tvalue & first()
access start of range
Definition: range.h:63
String(const std::string &v)
Definition: handletest.cc:79
void print() const
Definition: handletest.cc:72
Int(const int &v)
Definition: handletest.cc:71
std::string Mv
Definition: handletest.cc:82
A class to deal with numerical ranges.
Definition: range.h:50
Namespace containing all components of module commandline. ,.
Definition: commandline.cc:41
Evaluates commandline by calling long_getopt. ,You may instantiate a Commandline object by passing th...
Definition: commandline.h:201
void print() const
Definition: handletest.cc:80
virtual void print() const
Definition: handletest.cc:66
#define ILLEGAL(line)
Show code that does not compile.
Definition: handletest.cc:51
tfxx::Range< int > Tirange
Definition: handletest.cc:54
virtual ~Any()
Definition: handletest.cc:65
struct to define options ,This struct is used to define a list of options. An example is: ...
Definition: commandline.h:136
option has no argument
Definition: commandline.h:100
int main(int iargc, char *argv[])
Definition: handletest.cc:85