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

Revision 8789, 7.2 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
36 #ifndef _VJ_VARVALUE_H_
37 #define _VJ_VARVALUE_H_
38
39 #include <vjConfig.h>
40 #include <ctype.h>
41
42
43 typedef enum { T_INT, T_FLOAT, T_BOOL, T_STRING, T_DISTANCE,
44                T_CHUNK, T_EMBEDDEDCHUNK, T_INVALID } VarType;
45
46 typedef enum {U_Feet, U_Inches, U_Meters, U_Centimeters, U_BadUnit}
47               CfgUnit;
48
49 class vjConfigChunk;
50
51 /* note for myself as I'm adding T_DISTANCE - everything gets stored
52  * internally as feet.
53  */
54
55 //-------------------------------------------------
56 //: A vjVarValue is an object that knows its own type even if we don't.
57 //  More seriously, it's the value storage unit and value return type
58 //  for a ConfigChunk. <br>
59 //  Currently, vjVarValues can be of types int, FLOAT, boolean, string
60 //  (char*), distance(essentially FLOAT), as defined by the VarType
61 //  enumeration in vjVarValue.h. <br>
62 //  When you get a vjVarValue, you can do just a few things with it: <br>
63 //    1. assign it to a variable and then use it.  Note that there is
64 //       type checking here: if you try assigning a string vjVarValue to
65 //       an int, you'll get an error. <br>
66 //    2. Cast it to the right type and use it. <br>
67 //    3. print it - vjVarValues have overloaded << so you can print them
68 //       without having to cast to the right value. <br>
69 //  Note that it's generally incumbent upon the client to know what
70 //  kind of vjVarValue he's getting and what it can do.  Hey, you're
71 //  the one who queried the ConfigChunk, not me. <br>
72 //
73 // @author  Christopher Just
74 //
75 //!PUBLIC_API:
76 //--------------------------------------------------
77
78 class vjVarValue {
79
80 private:
81
82     VarType    type;
83
84     // these are the possible storage areas.
85     int          intval;
86     float        floatval;
87     std::string  strval;
88     bool         boolval;
89     vjConfigChunk *embeddedchunkval;
90     unsigned int validation;
91
92     static vjVarValue* invalid_instance;
93     static const std::string using_invalid_msg;
94
95 public:
96
97     //:Gets a reference to a global "invalid" vjVarValue
98     //!NOTE: This is mainly useful for returning an invalid VarValue in
99     //+      case of an error, and is used internally by some Config/*
100     //+      classes.
101     //!NOTE: There is a fairly harmless race condition where an extra
102     //+      invalid vjVarValue gets created & not deleted.  This is
103     //+      very unlikely, and would only result in losing a few bytes
104     //+      anyway.
105     static vjVarValue& getInvalidInstance ();
106
107
108     inline VarType getType () const {
109         return type;
110     }
111
112     //: Copy constructor.
113     vjVarValue (const vjVarValue &v);
114
115
116     //: Constructor - creates a T_EMBEDDEDCHUNK vjVarValue containing ch
117     //!NOTE: This is explicit for safety's sake.  I already encountered a bug
118     //+      where some chunkdb code was interpreting
119     //+          vjVarValue v1 = chunk.getProperty(blah)
120     //+      as casting the result of getProperty to a chunk* and then calling
121     //+      this constructor instead of using the vjVarValue copy constructor
122     //+      becuase getProperty returns a const vjVarValue and the copy const
123     //+      didn't expect a const (since fixed).
124     explicit vjVarValue (vjConfigChunk* ch);
125
126
127     //: Creates a new vjVarValue of type t.
128     //! NOTE: Note that once a vjVarValue object has been created, the type
129     //+ cannot be changed.
130     vjVarValue ( VarType t );
131
132
133
134     //: Destroys self and all associated memory.
135     ~vjVarValue();
136
137
138     #ifdef VJ_DEBUG
139     void assertValid () const;
140     #else
141     inline void assertValid () const {
142         ;
143     }
144     #endif
145
146
147
148     //: Assignment Operator
149     vjVarValue& operator= (const vjVarValue &v);
150
151
152
153     //: Equality Operator
154     bool operator == (const vjVarValue& v) const;
155     inline bool operator != (const vjVarValue& v) const {
156         return !(*this == v);
157     }
158
159
160     /*  Cast Operators
161      *  These operators are used whenever a vjVarValue is cast to another
162      *  type.  They do some amount of type checking and coercion,
163      *  eventually returning the data stored within the config itself.
164      *  Right now, in event of an error we only write a message to cerr
165      *  and return a "reasonable" value - 0, 0.0, false, "", NULL, etc.
166      */
167
168     //: Cast to int
169     //!RETURNS: i - integer value of self if T_INT, 0 or 1 if T_BOOL
170     //!RETURNS: 0 - if not T_INT or T_BOOL (this is bad)
171     operator int() const;
172    
173
174     //: cast to ConfigChunk
175     //!NOTE: Returns a copy of the contained chunk which must be
176     //+      freed.
177     operator vjConfigChunk*() const;
178
179
180     //: Cast to bool
181     operator bool() const;
182
183
184     //: Cast to float (for T_FLOAT or T_DISTANCE)
185     operator float () const;
186
187
188     //: Returns a string VarValue as a c-style string
189     //!NOTE: returns a freshly allocated char array that the caller is
190     //+      responsible for deleting.
191     char* cstring () const;
192
193
194     //: Cast to std::string
195     operator std::string () const;
196
197
198     //: Assignment overload
199     //!NOTE: type of a vjVarValue is immutable, so a type mismatch here
200     //+      can cause an error (in which case the assignment fails)
201     vjVarValue& operator = (int i);
202     vjVarValue& operator = (bool i);
203     vjVarValue& operator = (float i);
204     vjVarValue& operator = (const std::string& i);
205
206     //: Assignment overload
207     //!NOTE: type of a vjVarValue is immutable, so a type mismatch here
208     //+      can cause an error (in which case the assignment fails)
209     //!NOTE: the vjVarValue makes a copy of the string - you can do with
210     //+      the original as you please.
211     vjVarValue &operator = (const char *s);
212
213     vjVarValue &operator = (vjConfigChunk *s);
214
215
216
217     //: Writes the value of self to the stream out
218     //!NOTE: v knows what type it is, so it makes sure it's printed
219     //+      in a reasonable way.  ints & floats are printed as numbers,
220     //+      bools as the strings "true" and "false", strings and
221     //+      chunks as their string reps, etc.
222     friend std::ostream& operator << (std::ostream& out, const vjVarValue& v);
223    
224 };
225
226 #endif
Note: See TracBrowser for help on using the browser.