Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

Var.C

00001 /* $Id: Var.C,v 1.2 2000/11/03 22:03:28 rpav Exp $
00002  *
00003  * SFW - The Studio Framework
00004  * Copyright (C) 2000  Ryan Pavlik
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019  *
00020  */
00021 
00022 /*
00023  * Code for the 'var', the dynamic-type class, and related classes
00024  *
00025  */ 
00026 
00027 #include <cstdio>
00028 #include <SFW/Var.H>
00029 
00030 TYPE_INIT(SFW::var);
00031 TYPE_INIT(SFW::longvar);
00032 
00033 TYPE_INIT(SFW::VVector);
00034 TYPE_INIT(SFW::VDeque);
00035 TYPE_INIT(SFW::VList);
00036 TYPE_INIT(SFW::VSet);
00037 TYPE_INIT(SFW::VMultiSet);
00038 
00039 TYPE_INIT(SFW::LVVector);
00040 TYPE_INIT(SFW::LVDeque);
00041 TYPE_INIT(SFW::LVList);
00042 TYPE_INIT(SFW::LVSet);
00043 TYPE_INIT(SFW::LVMultiSet);
00044 
00045 TYPE_INIT(SFW::Map);
00046 TYPE_INIT(SFW::MultiMap);
00047 TYPE_INIT(SFW::LMap);
00048 TYPE_INIT(SFW::LMultiMap);
00049 
00050 TYPE_INIT(SFW::VMap);
00051 TYPE_INIT(SFW::VMultiMap);
00052 TYPE_INIT(SFW::LVMap);
00053 TYPE_INIT(SFW::LVMultiMap);
00054 
00055 TYPE_INIT(SFW::PVMap);
00056 TYPE_INIT(SFW::PVMultiMap);
00057 TYPE_INIT(SFW::PLVMap);
00058 TYPE_INIT(SFW::PLVMultiMap);
00059 
00060 using namespace SFW;
00061 
00062 var::var() {
00063     type = VAR_UNDEF;
00064 }
00065 
00066 var::var(const var &_v) {
00067     type = _v.type;
00068 
00069     if(type == VAR_STRING)
00070         u.s = new string(*_v.u.s);
00071     else
00072         u = _v.u;
00073 }
00074 
00075 var::var(string _s) {
00076     type = VAR_STRING;
00077     u.s = new string(_s);
00078 }
00079 
00080 var::var(char _c) {
00081     type = VAR_CHAR;
00082     u.c = _c;
00083 }
00084 
00085 var::var(bool _b) {
00086     type = VAR_BOOL;
00087     u.b = _b;
00088 }
00089 
00090 var::var(int _i) {
00091     type = VAR_INT;
00092     u.i = _i;
00093 }
00094 
00095 var::var(float _f) {
00096     type = VAR_FLOAT;
00097     u.f = _f;
00098 }
00099 
00100 var::~var() {
00101     if(type == VAR_STRING)
00102         delete u.s;
00103 }
00104 
00105 var::operator string() const {
00106     char buf[128];
00107     string ret = "";
00108     
00109     switch(type) {
00110         case VAR_STRING:
00111             ret = *u.s;
00112             break;
00113 
00114         case VAR_CHAR:
00115             ret += u.c;
00116             break;
00117 
00118         case VAR_BOOL:
00119             ret = (u.b ? "true" : "false");
00120             break;
00121 
00122         case VAR_INT:
00123             snprintf(buf, sizeof(buf), "%d", u.i);
00124             ret = buf;
00125             break;
00126 
00127         case VAR_FLOAT:
00128             snprintf(buf, sizeof(buf), "%.99f", u.f);
00129             ret = buf;
00130             break;
00131     }
00132 
00133     return ret;
00134 }
00135 
00136 var::operator char() const {
00137     char ret = 0;
00138     
00139     switch(type) {
00140         case VAR_STRING:
00141             if(u.s->size() > 0)
00142                 ret = u.s->at(0);
00143             break;
00144 
00145         case VAR_CHAR:
00146             ret = u.c;
00147             break;
00148 
00149         case VAR_BOOL:
00150             ret = (char)u.b;
00151             break;
00152 
00153         case VAR_INT:
00154             ret = (char)u.i;
00155             break;
00156 
00157         case VAR_FLOAT:
00158             ret = (char)u.f;
00159             break;
00160     }
00161     
00162     return ret;    
00163 }
00164 
00165 var::operator bool() const {
00166     return u.b;
00167 }
00168 
00169 var::operator int() const {
00170     int ret = 0;
00171     
00172     switch(type) {
00173         case VAR_STRING:
00174             sscanf((*u.s).c_str(), "%i", &ret);
00175             break;
00176 
00177         case VAR_CHAR:
00178             ret = (int)u.c;
00179             break;
00180 
00181         case VAR_BOOL:
00182             ret = (int)u.b;
00183             break;
00184 
00185         case VAR_INT:
00186             ret = u.i;
00187             break;
00188 
00189         case VAR_FLOAT:
00190             ret = (int)u.f;
00191             break;
00192     }
00193     
00194     return ret;
00195 }
00196 
00197 var::operator float() const {
00198     float ret = 0.0;
00199     
00200     switch(type) {
00201         case VAR_STRING:
00202             sscanf((*u.s).c_str(), "%f", &ret);
00203             break;
00204 
00205         case VAR_CHAR:
00206             ret = (float)u.c;
00207             break;
00208 
00209         case VAR_BOOL:
00210             ret = (float)u.b;
00211             break;
00212 
00213         case VAR_INT:
00214             ret = (float)u.i;
00215             break;
00216 
00217         case VAR_FLOAT:
00218             ret = u.f;
00219             break;
00220     }
00221     
00222     return ret;
00223 }
00224 
00225 var var::operator= (const var &_v) {
00226     if(type == VAR_STRING)
00227         delete u.s;
00228     
00229     type = _v.type;
00230 
00231     if(type == VAR_STRING)
00232         u.s = new string(*_v.u.s);
00233     else
00234         u = _v.u;
00235 
00236     return *this;
00237 }
00238 
00239 bool var::operator< (const var &_v) const {
00240     /* If their types aren't the same, do the
00241      * best we can.. compare as strings */
00242     if(type != _v.type)
00243         return (string)*this < (string)_v;
00244             
00245     switch(type) {
00246         case VAR_STRING:
00247             return *u.s < *_v.u.s;
00248 
00249         case VAR_CHAR:
00250             return u.c < _v.u.c;
00251 
00252         case VAR_BOOL:
00253             return u.b < _v.u.b;
00254 
00255         case VAR_INT:
00256             return u.i < _v.u.i;
00257 
00258         case VAR_FLOAT:
00259             return u.f < _v.u.f;
00260     }
00261 
00262     return false;
00263 }
00264 
00265 VarType var::varType() {
00266     return type;
00267 }
00268 
00269 /***
00270  *** LongVarType
00271  ***
00272  **/
00273 longvar::longvar() {
00274     type = VAR_LONG_UNDEF;
00275 }
00276 
00277 longvar::longvar(const longvar &_v) {
00278     type = _v.type;
00279     u    = _v.u;
00280 }
00281 
00282 longvar::longvar(long long _ll) {
00283     type = VAR_LONG_LONG;
00284     u.ll = _ll;
00285 }
00286 
00287 longvar::longvar(double _d) {
00288     type = VAR_LONG_DOUBLE;
00289     u.d = _d;
00290 }
00291 
00292 longvar::operator string() const {
00293     char buf[128];
00294     string ret = "";
00295     
00296     switch(type) {
00297         case VAR_LONG_LONG:
00298             snprintf(buf, sizeof(buf), "%ll", u.ll);
00299             ret = buf;
00300             break;
00301 
00302         case VAR_LONG_DOUBLE:
00303             snprintf(buf, sizeof(buf), "%.99g", u.d);
00304             ret = buf;
00305             break;
00306     }
00307 
00308     return ret;
00309 }
00310 
00311 longvar::operator long long() const {
00312     return u.ll;
00313 }
00314 
00315 longvar::operator double() const {
00316     return u.d;
00317 }
00318 
00319 longvar longvar::operator= (const longvar &_lv) {
00320     type = _lv.type;
00321     u    = _lv.u;
00322 
00323     return *this;
00324 }
00325 
00326 bool longvar::operator< (const longvar &_v) const {
00327     if(type != _v.type)
00328         return (string)*this < (string)_v;
00329             
00330     switch(type) {
00331         case VAR_LONG_LONG:
00332             return u.ll < _v.u.ll;
00333 
00334         case VAR_LONG_DOUBLE:
00335             return u.d < _v.u.d;
00336     }
00337 
00338     return false;
00339 }
00340 
00341 LongVarType longvar::varType() {
00342     return type;
00343 }

Generated at Tue Jan 2 15:38:34 2001 for SFW by doxygen1.2.4 written by Dimitri van Heesch, © 1997-2000