root/juggler/branches/1.0/Config/vjVarValue.cpp

Revision 8789, 11.3 kB (checked in by patrickh, 7 years ago)

Copyright update.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /*************** <auto-copyright.pl BEGIN do not edit this line> **************
2  *
3  * VR Juggler is (C) Copyright 1998, 1999, 2000, 2001, 2002
4  *   by Iowa State University
5  *
6  * Original Authors:
7  *   Allen Bierbaum, Christopher Just,
8  *   Patrick Hartling, Kevin Meinert,
9  *   Carolina Cruz-Neira, Albert Baker
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Library General Public
13  * License as published by the Free Software Foundation; either
14  * version 2 of the License, or (at your option) any later version.
15  *
16  * This library 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 GNU
19  * Library General Public License for more details.
20  *
21  * You should have received a copy of the GNU Library General Public
22  * License along with this library; if not, write to the
23  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24  * Boston, MA 02111-1307, USA.
25  *
26  * -----------------------------------------------------------------
27  * File:          $RCSfile$
28  * Date modified: $Date$
29  * Version:       $Revision$
30  * -----------------------------------------------------------------
31  *
32  *************** <auto-copyright.pl END do not edit this line> ***************/
33
34
35 #include <vjConfig.h>
36 #include <ctype.h>
37 #include <Config/vjVarValue.h>
38 #include <Config/vjConfigChunk.h>
39 #include <Kernel/vjDebug.h>
40 #include <Config/vjParseUtil.h>
41 #include <Config/vjConfigTokens.h>
42
43 vjVarValue* vjVarValue::invalid_instance = NULL;
44 const std::string vjVarValue::using_invalid_msg = "Casting from T_INVALID VarValue - this may mean we're confused";
45
46
47 /*static*/ vjVarValue& vjVarValue::getInvalidInstance () {
48     if (invalid_instance == NULL)
49         invalid_instance = new vjVarValue (T_INVALID);
50     return *invalid_instance;
51 }
52
53
54 vjVarValue::vjVarValue (const vjVarValue &v)
55 {
56     validation = 1;
57
58     //strval = std::string("");
59     intval = 0;
60     floatval = 0.0;
61     embeddedchunkval = NULL;
62     boolval = false;
63     *this = v;
64 }
65
66
67 vjVarValue::vjVarValue (vjConfigChunk* ch)
68 {
69     validation = 1;
70
71     //strval = std::string("");
72     intval = 0;
73     floatval = 0.0;
74     embeddedchunkval = NULL;
75     boolval = false;
76     type = T_EMBEDDEDCHUNK;
77     if (ch)
78         embeddedchunkval = new vjConfigChunk(*ch);
79 }
80
81
82 vjVarValue::vjVarValue ( VarType t )
83 {
84     validation = 1;
85
86     //strval = std::string("");
87     type = t;
88     intval = 0;
89     floatval = 0.0;
90     embeddedchunkval = NULL;
91     boolval = false;
92 }
93
94
95
96 vjVarValue::~vjVarValue() {
97     validation = 0;
98
99 //     if (embeddedchunkval)
100 //    delete embeddedchunkval;
101 }
102
103
104 #ifdef VJ_DEBUG
105 void vjVarValue::assertValid () const {
106     assert (validation == 1 && "Trying to use deleted vjVarValue");
107     if ((type == T_EMBEDDEDCHUNK) && embeddedchunkval)
108         embeddedchunkval->assertValid();
109 }
110 #endif
111
112
113
114 vjVarValue& vjVarValue::operator= (const vjVarValue &v) {
115     assertValid();
116     v.assertValid();
117
118     if (&v == this)
119         return *this;
120
121     type = v.type;
122
123     if (embeddedchunkval) {
124         // delete embeddedchunkval; XXX
125         embeddedchunkval = NULL;
126     }
127     intval = v.intval;
128     floatval = v.floatval;
129     boolval = v.boolval;
130     strval = v.strval;
131     if (v.embeddedchunkval) {
132
133         embeddedchunkval = new vjConfigChunk (*v.embeddedchunkval);
134     }
135     return *this;
136 }
137
138
139
140 //: Equality Operator
141 bool vjVarValue::operator == (const vjVarValue& v) const {
142     assertValid();
143     v.assertValid();
144
145     if (type != v.type)
146         return false;
147     switch (type) {
148     case T_INT:
149         return (intval == v.intval);
150     case T_FLOAT:
151         return (floatval == v.floatval);
152     case T_STRING:
153     case T_CHUNK:
154         return (strval == v.strval);
155     case T_BOOL:
156         return (boolval == v.boolval);
157     case T_EMBEDDEDCHUNK:
158         if (embeddedchunkval) {
159             if (v.embeddedchunkval)
160                 return (*embeddedchunkval == *(v.embeddedchunkval));
161             else
162                 return false;
163         }
164         else
165             return (!v.embeddedchunkval);
166     default:
167         return false;
168     }
169 }
170
171
172
173
174 vjVarValue::operator int() const {
175     assertValid();
176
177     switch (type) {
178     case T_INT:
179         return intval;
180     case T_BOOL:
181         return boolval;
182     case T_FLOAT:
183         return (int)floatval;
184     case T_INVALID:
185         vjDEBUG(vjDBG_CONFIG,4) << using_invalid_msg.c_str() << 1
186                                 << std::endl << vjDEBUG_FLUSH;
187         return 0;
188     default:
189         vjDEBUG(vjDBG_ERROR,0) << "vjVarValue: type mismatch in cast to int.\n"
190                                << vjDEBUG_FLUSH;
191         return 0;
192     }
193 }
194
195
196
197 vjVarValue::operator vjConfigChunk*() const {
198     assertValid();
199
200     switch (type) {
201     case T_EMBEDDEDCHUNK:
202         // we need to make a copy because if the value is deleted, it deletes
203         // its embeddedchunk
204         if (embeddedchunkval)
205             return new vjConfigChunk (*embeddedchunkval);
206         else {
207             return NULL;
208         }
209     case T_INVALID:
210         vjDEBUG(vjDBG_CONFIG,4) << using_invalid_msg.c_str() << 2
211                  << std::endl << vjDEBUG_FLUSH;
212         return NULL;
213     default:
214         vjDEBUG(vjDBG_ERROR,0) << "vjVarValue: type mismatch in cast to vjConfigChunk* - real type is " << typeString(type) << ".\n" << vjDEBUG_FLUSH;
215         return NULL;
216     }
217 }
218
219
220
221 vjVarValue::operator bool() const {
222     assertValid();
223
224     if ((type == T_BOOL))
225         return boolval;
226     switch (type) {
227     case T_BOOL:
228         return boolval;
229     case T_INT:
230         return (bool)intval;
231     case T_FLOAT:
232         return (bool)floatval;
233     case T_INVALID:
234         vjDEBUG(vjDBG_CONFIG,4) << using_invalid_msg.c_str() << 3
235                                 << std::endl << vjDEBUG_FLUSH;
236         return false;
237     default:
238         vjDEBUG(vjDBG_ERROR,0) << "vjVarValue: type mismatch in cast to bool.\n"
239                                << vjDEBUG_FLUSH;
240    return false;
241     }
242 }
243
244
245
246 vjVarValue::operator float () const {
247     assertValid();
248
249     switch (type) {
250     case T_FLOAT:
251         return floatval;
252     case T_INT:
253         return (float)intval;
254     case T_BOOL:
255         return (float)boolval;
256     case T_INVALID:
257         vjDEBUG(vjDBG_CONFIG,4) <<  using_invalid_msg.c_str() << 4
258                                 << std::endl << vjDEBUG_FLUSH;
259         return 0.0f;
260     default:
261         vjDEBUG(vjDBG_ERROR,0) << "vjVarValue: type mismatch in cast to float.\n" << vjDEBUG_FLUSH;
262         return 0.0f;
263     }
264 }
265
266
267
268 char* vjVarValue::cstring () const {
269     assertValid();
270
271     switch (type) {
272     case T_STRING:
273     case T_CHUNK:
274         return strdup (strval.c_str());
275     case T_INVALID:
276         vjDEBUG(vjDBG_CONFIG,4) <<  using_invalid_msg.c_str() << 5
277                                 << std::endl << vjDEBUG_FLUSH;
278         return strdup("");
279     default:
280         vjDEBUG(vjDBG_ERROR,0) << "vjVarValue: type mismatch in cstring().\n" << vjDEBUG_FLUSH;
281         return strdup("");
282     }
283 }
284
285
286
287 vjVarValue::operator std::string () const {
288     assertValid();
289
290     switch (type) {
291     case T_STRING:
292     case T_CHUNK:
293         return strval;
294     case T_INVALID:
295         vjDEBUG(vjDBG_CONFIG,4) <<  using_invalid_msg.c_str() << 6
296                                 << std::endl << vjDEBUG_FLUSH;
297         return (std::string)"";
298     default:
299         vjDEBUG(vjDBG_ERROR,0) << "vjVarValue: type mismatch in cast to std::string.\n" << vjDEBUG_FLUSH;
300         return (std::string)"";
301     }
302 }
303
304
305
306 vjVarValue &vjVarValue::operator = (int i) {
307     assertValid();
308
309     switch (type) {
310     case T_INT:
311         intval = i;
312         break;
313     case T_FLOAT:
314         floatval = (float)i;
315         break;
316     case T_BOOL:
317         boolval = (bool)i;
318         break;
319     default:
320         vjDEBUG(vjDBG_ERROR,0) << "vjVarValue: type mismatch in assignment - vjVarValue(" << typeString(type) << ") = int.\n" << vjDEBUG_FLUSH;
321     }
322     return *this;
323 }
324
325
326
327 vjVarValue& vjVarValue::operator = (bool i) {
328     assertValid();
329
330     switch (type) {
331     case T_INT:
332         intval = (int)i;
333         break;
334     case T_BOOL:
335         boolval = i;
336         break;
337     default:
338         vjDEBUG(vjDBG_ERROR,0) << "vjVarValue: type mismatch in assignment - vjVarValue(" << typeString(type) << ") = bool.\n" << vjDEBUG_FLUSH;
339     }
340     return *this;
341 }
342
343
344
345 vjVarValue &vjVarValue::operator = (float i) {
346     assertValid();
347
348     switch (type) {
349     case T_FLOAT:
350     case T_DISTANCE:
351         floatval = i;
352         break;
353     default:
354         vjDEBUG(vjDBG_ERROR,0) << "vjVarValue: type mismatch in assignment - vjVarValue(" << typeString(type) << ") = float.\n" << vjDEBUG_FLUSH;
355     }
356     return *this;
357 }
358
359
360
361 vjVarValue &vjVarValue::operator = (const std::string& s) {
362     assertValid();
363
364     return *this = s.c_str();
365 }
366
367
368
369 vjVarValue &vjVarValue::operator = (const char *val) {
370     assertValid();
371
372     bool err = false;
373     char* endval;
374     int i;
375     float f;
376     bool b;
377
378     if (val == NULL) {
379         val = "";
380     }
381
382     switch (type) {
383     case T_STRING:
384     case T_CHUNK:
385         strval = val;
386         break;
387     case T_INT:
388         i = strtol (val, &endval, 0);
389         if (*endval == '\0')
390             intval = i;
391         else
392             err = true;
393         break;
394     case T_FLOAT:
395         f = (float)strtod (val, &endval);
396         if (*endval == '\0')
397             floatval = f;
398         else
399             err = true;
400         break;
401     case T_BOOL:
402         if (!strcasecmp (val, true_TOKEN))
403             boolval = true;
404         else if (!strcasecmp (val, false_TOKEN))
405             boolval = false;
406         else { // we'll try to accept a numeric value
407             b = strtol (val, &endval, 0);
408             if (*endval == '\0')
409                 boolval = b;
410             else
411                 err = true;
412         }
413         break;
414     default:
415         vjDEBUG(vjDBG_ERROR,0) << "vjVarValue: type mismatch in assignment - vjVarValue(" << typeString(type) << ") = string/char*.\n" << vjDEBUG_FLUSH;
416         break;
417     }
418     if (err)
419         vjDEBUG(vjDBG_ERROR,0) << "vjVarValue: couldn't assign string '"
420                                << val << "'.\n" << vjDEBUG_FLUSH;
421     return *this;
422 }
423
424
425 vjVarValue &vjVarValue::operator = (vjConfigChunk *s) {
426    assertValid();
427
428    switch (type)
429        {
430        case T_EMBEDDEDCHUNK:
431            /* XXX: Leave it hanging for now.
432               if (embeddedchunkval)
433               delete embeddedchunkval;
434            */
435            if (s)
436                embeddedchunkval = new vjConfigChunk (*s);
437            else
438                embeddedchunkval = NULL;
439            break;
440        default:
441            vjDEBUG(vjDBG_ERROR,0) << "vjVarValue: type mismatch in assignment - vjVarValue(" << typeString(type) << ") = vjConfigChunk*.\n" << vjDEBUG_FLUSH;
442        }
443    return *this;
444 }
445
446
447
448 std::ostream& operator << (std::ostream& out, const vjVarValue& v) {
449     v.assertValid();
450
451     //      vjDEBUG(vjDBG_ERROR,0) << "in << func" << vjDEBUG_FLUSH;
452
453     switch (v.type) {
454     case T_INT:
455         out << v.intval;
456         return out;
457     case T_FLOAT:
458     case T_DISTANCE:
459         out << v.floatval;
460         return out;
461     case T_BOOL:
462         out << ((v.boolval)?"true":"false");
463         return out;
464     case T_STRING:
465     case T_CHUNK:
466         out << v.strval.c_str();
467         return out;
468     case T_EMBEDDEDCHUNK:
469         if (v.embeddedchunkval)
470             out << *(v.embeddedchunkval);
471         return out;
472     default:
473         out << "[can't print value for type " << (int)v.type << " ]";
474         return out;
475     }
476 }
Note: See TracBrowser for help on using the browser.