1 | //
|
---|
2 | // keyvaltest.cc
|
---|
3 | //
|
---|
4 | // Copyright (C) 1996 Limit Point Systems, Inc.
|
---|
5 | //
|
---|
6 | // Author: Curtis Janssen <cljanss@limitpt.com>
|
---|
7 | // Maintainer: LPS
|
---|
8 | //
|
---|
9 | // This file is part of the SC Toolkit.
|
---|
10 | //
|
---|
11 | // The SC Toolkit is free software; you can redistribute it and/or modify
|
---|
12 | // it under the terms of the GNU Library General Public License as published by
|
---|
13 | // the Free Software Foundation; either version 2, or (at your option)
|
---|
14 | // any later version.
|
---|
15 | //
|
---|
16 | // The SC Toolkit is distributed in the hope that it will be useful,
|
---|
17 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
19 | // GNU Library General Public License for more details.
|
---|
20 | //
|
---|
21 | // You should have received a copy of the GNU Library General Public License
|
---|
22 | // along with the SC Toolkit; see the file COPYING.LIB. If not, write to
|
---|
23 | // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
24 | //
|
---|
25 | // The U.S. Government is granted a limited license as per AL 91-7.
|
---|
26 | //
|
---|
27 |
|
---|
28 | // a simple program to test the class stuff
|
---|
29 |
|
---|
30 | #include <iostream>
|
---|
31 | #include <fstream>
|
---|
32 |
|
---|
33 | #include <util/misc/formio.h>
|
---|
34 | #include <util/class/class.h>
|
---|
35 | #include <util/keyval/ipv2.h>
|
---|
36 | #include <util/keyval/keyval.h>
|
---|
37 |
|
---|
38 | using namespace std;
|
---|
39 | using namespace sc;
|
---|
40 |
|
---|
41 | class A: virtual public DescribedClass {
|
---|
42 | private:
|
---|
43 | int i;
|
---|
44 | public:
|
---|
45 | A();
|
---|
46 | A(const Ref<KeyVal>&keyval);
|
---|
47 | inline const int& a() const { return i; };
|
---|
48 | virtual void print (ostream&s = cout) const
|
---|
49 | {
|
---|
50 | s << "A::a = " << a() << '\n';
|
---|
51 | }
|
---|
52 | };
|
---|
53 |
|
---|
54 | A::A():
|
---|
55 | i(1)
|
---|
56 | {
|
---|
57 | }
|
---|
58 | A::A(const Ref<KeyVal>& keyval):
|
---|
59 | i(keyval->intvalue("a"))
|
---|
60 | {
|
---|
61 | }
|
---|
62 |
|
---|
63 | static ClassDesc A_cd(typeid(A),"A",1,"virtual public DescribedClass",
|
---|
64 | create<A>,create<A>,0);
|
---|
65 |
|
---|
66 | class B: public A {
|
---|
67 | private:
|
---|
68 | int b_;
|
---|
69 | public:
|
---|
70 | B();
|
---|
71 | B(const Ref<KeyVal>&keyval);
|
---|
72 | inline const int& b() const { return b_; };
|
---|
73 | virtual void print (ostream&s = cout) const
|
---|
74 | {
|
---|
75 | A::print(s);
|
---|
76 | s << "B::b = " << b() << '\n';
|
---|
77 | }
|
---|
78 | };
|
---|
79 |
|
---|
80 | B::B():
|
---|
81 | b_(2)
|
---|
82 | {
|
---|
83 | }
|
---|
84 | B::B(const Ref<KeyVal>&keyval):
|
---|
85 | A(new PrefixKeyVal(keyval,"A")),
|
---|
86 | b_(keyval->intvalue("b"))
|
---|
87 | {
|
---|
88 | }
|
---|
89 |
|
---|
90 | class ClassDesc B_cd(typeid(B),"B",1,"public A",create<B>,create<B>);
|
---|
91 |
|
---|
92 | class C: virtual public DescribedClass {
|
---|
93 | private:
|
---|
94 | int i;
|
---|
95 | public:
|
---|
96 | C();
|
---|
97 | C(const Ref<KeyVal>&keyval);
|
---|
98 | inline const int& c() const { return i; };
|
---|
99 | virtual void print (ostream&s = cout) const
|
---|
100 | {
|
---|
101 | s << "C::c = " << c() << '\n';
|
---|
102 | }
|
---|
103 | };
|
---|
104 |
|
---|
105 | C::C():
|
---|
106 | i(3)
|
---|
107 | {
|
---|
108 | }
|
---|
109 | C::C(const Ref<KeyVal>&keyval):
|
---|
110 | i(keyval->intvalue("c"))
|
---|
111 | {
|
---|
112 | }
|
---|
113 |
|
---|
114 | static ClassDesc C_cd(typeid(C),"C",1,"virtual public DescribedClass",
|
---|
115 | create<C>, create<C>);
|
---|
116 |
|
---|
117 | class D: public B, public C {
|
---|
118 | private:
|
---|
119 | int d_;
|
---|
120 | Ref<A> d_a_;
|
---|
121 | Ref<B> d_b_;
|
---|
122 | public:
|
---|
123 | D();
|
---|
124 | D(const Ref<KeyVal>&keyval);
|
---|
125 | inline const int& d() const { return d_; }
|
---|
126 | inline Ref<A> da() const { return d_a_; }
|
---|
127 | inline Ref<B> db() const { return d_b_; }
|
---|
128 | virtual void print (ostream&s = cout) const
|
---|
129 | {
|
---|
130 | B::print(s);
|
---|
131 | C::print(s);
|
---|
132 | s << "D::a:\n"; da()->print(s);
|
---|
133 | if ( (A*)d_a_.pointer() == dynamic_cast<A*>(db().pointer()) ) {
|
---|
134 | cout << "a == b\n";
|
---|
135 | }
|
---|
136 | else {
|
---|
137 | s << "D::b:\n"; db()->print(s);
|
---|
138 | }
|
---|
139 | s << "D::d = " << d() << '\n';
|
---|
140 | }
|
---|
141 | };
|
---|
142 |
|
---|
143 | D::D():
|
---|
144 | d_(4)
|
---|
145 | {
|
---|
146 | }
|
---|
147 | D::D(const Ref<KeyVal>&keyval):
|
---|
148 | B(new PrefixKeyVal(keyval,"B")),
|
---|
149 | C(new PrefixKeyVal(keyval,"C")),
|
---|
150 | d_(keyval->intvalue("d")),
|
---|
151 | d_a_(dynamic_cast<A*>(keyval->describedclassvalue("a").pointer())),
|
---|
152 | d_b_(dynamic_cast<B*>(keyval->describedclassvalue("b").pointer()))
|
---|
153 | {
|
---|
154 | }
|
---|
155 |
|
---|
156 | static ClassDesc D_cd(typeid(D),"D",1,"public B, public C",
|
---|
157 | create<D>,create<D>);
|
---|
158 |
|
---|
159 | main()
|
---|
160 | {
|
---|
161 | ClassDesc::list_all_classes();
|
---|
162 |
|
---|
163 | // test IPV2
|
---|
164 | IPV2::Status err;
|
---|
165 | ifstream in(SRCDIR "/keyvaltest.in",ios::in);
|
---|
166 | if (in.bad()) {
|
---|
167 | cout << "couldn't open " << SRCDIR << "/keyvaltest.in" << endl;
|
---|
168 | abort();
|
---|
169 | }
|
---|
170 | IPV2 *ipv2 = new IPV2();
|
---|
171 | ipv2->read(in,cout);
|
---|
172 | ipv2->print_tree(cout);
|
---|
173 | const char* test = 0;
|
---|
174 | ipv2->value_v(":forref:nest:x",&test,0,0);
|
---|
175 | cout << "test = \"" << test << "\"" << endl;
|
---|
176 | err = ipv2->truekeyword_v(":forref:a",&test,0,0);
|
---|
177 | cout << "test = \"" << test << "\" (" << ipv2->error_message(err) << ")"
|
---|
178 | << endl;
|
---|
179 | err = ipv2->truekeyword_v(":forref:nest:x",&test,0,0);
|
---|
180 | cout << "test = \"" << test << "\" (" << ipv2->error_message(err) << ")"
|
---|
181 | << endl;
|
---|
182 | err = ipv2->truekeyword_v(":forref:x",&test,0,0);
|
---|
183 | cout << "test = \"" << test << "\" (" << ipv2->error_message(err) << ")"
|
---|
184 | << endl;
|
---|
185 | delete ipv2;
|
---|
186 | ipv2 = 0;
|
---|
187 |
|
---|
188 | // test the test classes
|
---|
189 |
|
---|
190 | A a;
|
---|
191 | cout << "A name:" << a.class_name() << '\n';
|
---|
192 |
|
---|
193 | D d;
|
---|
194 | cout << "D name:" << d.class_name() << '\n';
|
---|
195 |
|
---|
196 | cout << "&d = " << (void*) &d << '\n';
|
---|
197 | cout << "dynamic_cast<D*>(&d) = " << (void*) dynamic_cast<D*>(&d) << '\n';
|
---|
198 | cout << "dynamic_cast<B*>(&d) = " << (void*) dynamic_cast<B*>(&d) << '\n';
|
---|
199 | cout << "dynamic_cast<A*>(&d) = " << (void*) dynamic_cast<A*>(&d) << '\n';
|
---|
200 | cout << "dynamic_cast<C*>(&d) = " << (void*) dynamic_cast<C*>(&d) << '\n';
|
---|
201 | cout << "dynamic_cast<DescribedClass*>(&d) = "
|
---|
202 | << (void*) dynamic_cast<DescribedClass*>(&d) << '\n';
|
---|
203 |
|
---|
204 |
|
---|
205 | Ref<AssignedKeyVal> akv = new AssignedKeyVal;
|
---|
206 |
|
---|
207 | akv->assign(":x",1);
|
---|
208 | akv->assign(":y",3.0);
|
---|
209 |
|
---|
210 | #define stringize(arg) # arg
|
---|
211 | #define show( arg ) do{cout<<" " stringize(arg) "="<<(arg);}while(0)
|
---|
212 |
|
---|
213 | show( akv->exists(":x") ); show( akv->errormsg() ); cout << '\n';
|
---|
214 | show( akv->exists(":z") ); show (akv->errormsg() ); cout << '\n';
|
---|
215 | show( akv->intvalue(":y") ); show( akv->errormsg() ); cout << '\n';
|
---|
216 | show( akv->doublevalue(":x") ); show( akv->errormsg() ); cout << '\n';
|
---|
217 | show( akv->intvalue(":x") ); show (akv->errormsg() ); cout << '\n';
|
---|
218 | show( akv->intvalue("x") ); show (akv->errormsg() ); cout << '\n';
|
---|
219 | show( akv->intvalue(":z") ); show (akv->errormsg() ); cout << '\n';
|
---|
220 |
|
---|
221 | Ref<KeyVal> pkv = new ParsedKeyVal(SRCDIR "/keyvaltest.in");
|
---|
222 | pkv->verbose(1);
|
---|
223 |
|
---|
224 | cout << "Initial unseen keywords:" << endl;
|
---|
225 | cout << incindent;
|
---|
226 | pkv->print_unseen(cout);
|
---|
227 | cout << decindent;
|
---|
228 | cout << "done" << endl;
|
---|
229 |
|
---|
230 | // size tests
|
---|
231 | for (int i=0; i < pkv->count("memory"); i++) {
|
---|
232 | cout << "memory:" << i << " = " << pkv->sizevalue("memory",i) << endl;
|
---|
233 | }
|
---|
234 |
|
---|
235 | show( pkv->exists(":s1") ); show( pkv->errormsg() ); cout << '\n';
|
---|
236 | show( pkv->exists(":s2") ); show( pkv->errormsg() ); cout << '\n';
|
---|
237 | show( pkv->stringvalue(":s1") ); show( pkv->errormsg() ); cout << '\n';
|
---|
238 | show( pkv->stringvalue(":s2") ); show( pkv->errormsg() ); cout << '\n';
|
---|
239 | char *tmp;
|
---|
240 | show( tmp=pkv->pcharvalue(":s1") ); show( pkv->errormsg() ); cout << '\n';
|
---|
241 | delete[] tmp;
|
---|
242 | show( tmp=pkv->pcharvalue(":s2") ); show( pkv->errormsg() ); cout << '\n';
|
---|
243 | delete[] tmp;
|
---|
244 |
|
---|
245 | show( pkv->exists(":x") ); show( pkv->errormsg() ); cout << '\n';
|
---|
246 | show( pkv->exists(":z") ); show (pkv->errormsg() ); cout << '\n';
|
---|
247 | show( pkv->intvalue(":y") ); show( pkv->errormsg() ); cout << '\n';
|
---|
248 | show( pkv->doublevalue(":x") ); show( pkv->errormsg() ); cout << '\n';
|
---|
249 | show( pkv->intvalue(":x") ); show (pkv->errormsg() ); cout << '\n';
|
---|
250 | show( pkv->intvalue("x") ); show (pkv->errormsg() ); cout << '\n';
|
---|
251 | show( pkv->intvalue(":z") ); show (pkv->errormsg() ); cout << '\n';
|
---|
252 |
|
---|
253 | show ( pkv->exists("test:object_d") ); show(pkv->errormsg()); cout << '\n';
|
---|
254 |
|
---|
255 | Ref<DescribedClass> rdc = pkv->describedclassvalue("test:object");
|
---|
256 | show (pkv->errormsg() ); cout << '\n';
|
---|
257 | show( rdc.pointer() ); cout << '\n';
|
---|
258 | Ref<A> ra; ra << rdc;
|
---|
259 | show( ra.pointer() ); cout << '\n';
|
---|
260 |
|
---|
261 | show( pkv->intvalue(":test:object:d") ); cout << '\n';
|
---|
262 |
|
---|
263 | pkv->dump();
|
---|
264 |
|
---|
265 | cout << "Final unseen keywords:" << endl;
|
---|
266 | pkv->exists("forref");
|
---|
267 | pkv->exists("testintco");
|
---|
268 | cout << incindent;
|
---|
269 | pkv->print_unseen(cout);
|
---|
270 | cout << decindent;
|
---|
271 | cout << "done" << endl;
|
---|
272 |
|
---|
273 | show( ra.pointer() ); cout << '\n';
|
---|
274 | if (ra.nonnull()) { ra->print(); cout << '\n'; }
|
---|
275 |
|
---|
276 | cout << "Testing string keyvals" << endl;
|
---|
277 | Ref<ParsedKeyVal> strkv = new ParsedKeyVal();
|
---|
278 | cout << " parsing" << endl;
|
---|
279 | strkv->parse_string("<B>:(b=123456)");
|
---|
280 | cout << " reading" << endl;
|
---|
281 | Ref<B> strb; strb << strkv->describedclassvalue();
|
---|
282 | if (strb.nonnull()) {
|
---|
283 | cout << " printing" << endl;
|
---|
284 | strb->print(); cout << endl;
|
---|
285 | }
|
---|
286 |
|
---|
287 | cout << "Testing parsed keyvals TOP keyword" << endl;
|
---|
288 | strkv = new ParsedKeyVal();
|
---|
289 | cout << " parsing" << endl;
|
---|
290 | strkv->parse_string("TOP=24");
|
---|
291 | cout << " reading" << endl;
|
---|
292 | int strkvint = strkv->intvalue();
|
---|
293 | cout << "strkvint = " << strkvint << endl;
|
---|
294 |
|
---|
295 | return 0;
|
---|
296 | }
|
---|
297 |
|
---|
298 | /////////////////////////////////////////////////////////////////////////////
|
---|
299 |
|
---|
300 | // Local Variables:
|
---|
301 | // mode: c++
|
---|
302 | // c-file-style: "CLJ"
|
---|
303 | // End:
|
---|