value.h

00001 /***************************************************************
00002  *
00003  * Copyright (C) 1990-2007, Condor Team, Computer Sciences Department,
00004  * University of Wisconsin-Madison, WI.
00005  * 
00006  * Licensed under the Apache License, Version 2.0 (the "License"); you
00007  * may not use this file except in compliance with the License.  You may
00008  * obtain a copy of the License at
00009  * 
00010  *    http://www.apache.org/licenses/LICENSE-2.0
00011  * 
00012  * Unless required by applicable law or agreed to in writing, software
00013  * distributed under the License is distributed on an "AS IS" BASIS,
00014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015  * See the License for the specific language governing permissions and
00016  * limitations under the License.
00017  *
00018  ***************************************************************/
00019 
00020 
00021 #ifndef __CLASSAD_VALUE_H__
00022 #define __CLASSAD_VALUE_H__
00023 
00024 #include "classad/common.h"
00025 #include "classad/util.h"
00026 
00027 BEGIN_NAMESPACE( classad )
00028 
00029 class Literal;
00030 class ExprList;
00031 class ClassAd;
00032 
00034 class Value 
00035 {
00036     public:
00038         enum ValueType {
00039                                                 NULL_VALUE          = 0,                    ERROR_VALUE         = 1<<0,                 UNDEFINED_VALUE     = 1<<1,     BOOLEAN_VALUE       = 1<<2,                 INTEGER_VALUE       = 1<<3,                     REAL_VALUE          = 1<<4,             RELATIVE_TIME_VALUE = 1<<5,             ABSOLUTE_TIME_VALUE = 1<<6,                     STRING_VALUE        = 1<<7,                     CLASSAD_VALUE       = 1<<8,         LIST_VALUE          = 1<<9
00050         };
00051 
00053         enum NumberFactor {  NO_FACTOR= 0,          B_FACTOR = 1,          K_FACTOR = 2,          M_FACTOR = 3,          G_FACTOR = 4,          T_FACTOR = 5
00060         };
00061 
00062  
00064         static const double ScaleFactor[];
00065 
00067         Value();
00068 
00070         Value(const Value &value);
00071 
00073         ~Value();
00074 
00076         Value& operator=(const Value &value);
00077 
00079         void Clear (void);
00080 
00084         void CopyFrom( const Value &v );
00085 
00089         void SetBooleanValue(bool b);
00090 
00094         void SetRealValue(double r);
00095 
00099         void SetIntegerValue(int i);
00100 
00103         void SetUndefinedValue(void);
00104 
00107         void SetErrorValue(void);
00108 
00113         void SetListValue(ExprList* l);
00114 
00119         void SetClassAdValue(ClassAd* c);   
00120 
00124         void SetStringValue( const std::string &str );
00125 
00130         void SetStringValue( const char *str );
00131 
00136         void SetAbsoluteTimeValue( abstime_t secs );
00137 
00141         void SetRelativeTimeValue( time_t secs );
00142         void SetRelativeTimeValue( double secs );
00143 
00148         inline ValueType GetType() const { return valueType; }
00149 
00154         inline bool IsBooleanValue(bool& b) const;
00158         inline bool IsBooleanValue() const;
00159 
00167         bool IsBooleanValueEquiv(bool &b) const;
00168 
00173         inline bool IsIntegerValue(int &i) const;   
00177         inline bool IsIntegerValue() const;
00182         inline bool IsRealValue(double &r) const;   
00186         inline bool IsRealValue() const;
00192         bool IsStringValue( std::string &str ) const;   
00199         bool IsStringValue( const char *&str ) const;   
00205         bool IsStringValue( char *str, int len ) const;     
00210         inline bool IsStringValue( int &size ) const;
00214         inline bool IsStringValue() const;
00219         inline bool IsListValue(const ExprList*& l) const;
00226         inline bool IsListValue(ExprList*& l);
00232         inline bool IsListValue() const;
00237         inline bool IsClassAdValue(const ClassAd *&c) const; 
00244         inline bool IsClassAdValue(ClassAd *&c); 
00248         inline bool IsClassAdValue() const;
00252         inline bool IsUndefinedValue() const;
00256         inline bool IsErrorValue() const;
00260         inline bool IsExceptional() const;
00264         bool IsNumber () const;
00270         bool IsNumber (int &i) const;
00276         bool IsNumber (double &r) const;
00280         bool IsAbsoluteTimeValue( ) const;
00285         bool IsAbsoluteTimeValue( abstime_t& secs ) const;
00289         bool IsRelativeTimeValue( ) const;
00294         bool IsRelativeTimeValue( double& secs ) const;
00295         bool IsRelativeTimeValue( time_t& secs ) const;
00296 
00297         bool SameAs(const Value &otherValue) const;
00298 
00299         friend bool operator==(const Value &value1, const Value &value2);
00300 
00301         friend std::ostream& operator<<(std::ostream &stream, Value &value);
00302 
00303     private:
00304         friend class Literal;
00305         friend class ClassAd;
00306         friend class ExprTree;
00307 
00308         ValueType       valueType;      // the type of the value
00309 
00310 
00311         union {
00312             bool            booleanValue;
00313             int             integerValue;
00314             double          realValue;
00315             ExprList        *listValue;
00316             ClassAd         *classadValue;
00317             double          relTimeValueSecs;
00318             abstime_t absTimeValueSecs;
00319           
00320         };
00321         std::string         strValue;       // has ctor/dtor cannot be in the union
00322 };
00323 
00324 bool convertValueToRealValue(const Value value, Value &realValue);
00325 bool convertValueToIntegerValue(const Value value, Value &integerValue);
00326 bool convertValueToStringValue(const Value value, Value &stringValue);
00327 
00328 // implementations of the inlined functions
00329 inline bool Value::
00330 IsBooleanValue( bool& b ) const
00331 {
00332     b = booleanValue;
00333     return( valueType == BOOLEAN_VALUE );
00334 }
00335 
00336 inline bool Value::
00337 IsBooleanValue() const
00338 {
00339     return( valueType == BOOLEAN_VALUE );
00340 }
00341 
00342 inline bool Value::
00343 IsIntegerValue (int &i) const
00344 {
00345     i = integerValue;
00346     return (valueType == INTEGER_VALUE);
00347 }  
00348 
00349 inline bool Value::
00350 IsIntegerValue () const
00351 {
00352     return (valueType == INTEGER_VALUE);
00353 }  
00354 
00355 inline bool Value::
00356 IsRealValue (double &r) const
00357 {
00358     r = realValue;
00359     return (valueType == REAL_VALUE);
00360 }  
00361 
00362 inline bool Value::
00363 IsRealValue () const
00364 {
00365     return (valueType == REAL_VALUE);
00366 }  
00367 
00368 inline bool Value::
00369 IsListValue( const ExprList *&l) const
00370 {
00371     if (valueType == LIST_VALUE) {
00372         l = listValue;
00373         return true;
00374     } else {
00375         return false;
00376     }
00377 }
00378 
00379 inline bool Value::
00380 IsListValue( ExprList *&l)
00381 {
00382     if (valueType == LIST_VALUE) {
00383         l = listValue;
00384         return true;
00385     } else {
00386         return false;
00387     }
00388 }
00389 
00390 inline bool Value::
00391 IsListValue () const
00392 {
00393     return (valueType == LIST_VALUE);
00394 }
00395 
00396 
00397 inline bool Value::
00398 IsStringValue() const
00399 {
00400     return (valueType == STRING_VALUE);
00401 }
00402 
00403 
00404 inline bool Value::
00405 IsStringValue( const char *&s ) const
00406 {
00407     // We want to be careful not to copy it in here. 
00408     // People may accumulate in the "s" after this call,
00409     // So it best to only touch it if it exists.
00410     // (Example: the strcat classad function)
00411     if (valueType == STRING_VALUE) {
00412         s = strValue.c_str( );
00413         return true;
00414     } else {
00415         return false;
00416     }
00417 }
00418 
00419 inline bool Value::
00420 IsStringValue( char *s, int len ) const
00421 {
00422     if( valueType == STRING_VALUE ) {
00423         strncpy( s, strValue.c_str( ), len );
00424         return( true );
00425     }
00426     return( false );
00427 }
00428 
00429 inline bool Value::
00430 IsStringValue( std::string &s ) const
00431 {
00432     if ( valueType == STRING_VALUE ) {
00433         s = strValue;
00434         return true;
00435     } else {
00436         return false;
00437     }
00438 }
00439 
00440 inline bool Value::
00441 IsStringValue( int &size ) const
00442 {
00443     if (valueType == STRING_VALUE) {
00444         size = strValue.size();
00445         return true;
00446     } else {
00447         size = -1;
00448         return false;
00449     }
00450 }
00451 
00452 inline bool Value::
00453 IsClassAdValue(const ClassAd *&ad) const
00454 {
00455     if ( valueType == CLASSAD_VALUE ) {
00456         ad = classadValue;
00457         return true;
00458     } else {
00459         return false;
00460     }
00461 }
00462 
00463 inline bool Value::
00464 IsClassAdValue(ClassAd *&ad)
00465 {
00466     if ( valueType == CLASSAD_VALUE ) {
00467         ad = classadValue;
00468         return true;
00469     } else {
00470         return false;
00471     }
00472 }
00473 
00474 inline bool Value:: 
00475 IsClassAdValue() const
00476 {
00477     return( valueType == CLASSAD_VALUE );   
00478 }
00479 
00480 inline bool Value::
00481 IsUndefinedValue (void) const
00482 { 
00483     return (valueType == UNDEFINED_VALUE);
00484 }
00485 
00486 inline bool Value::
00487 IsErrorValue(void) const
00488 { 
00489     return (valueType == ERROR_VALUE); 
00490 }
00491 
00492 inline bool Value::
00493 IsExceptional(void) const
00494 {
00495     return( valueType == UNDEFINED_VALUE || valueType == ERROR_VALUE );
00496 }
00497 
00498 inline bool Value::
00499 IsAbsoluteTimeValue( ) const
00500 {
00501     return( valueType == ABSOLUTE_TIME_VALUE );
00502 }
00503 
00504 inline bool Value::
00505 IsAbsoluteTimeValue( abstime_t &secs ) const
00506 {
00507     secs = absTimeValueSecs;
00508     return( valueType == ABSOLUTE_TIME_VALUE );
00509 }
00510 
00511 inline bool Value::
00512 IsRelativeTimeValue( ) const
00513 {
00514     return( valueType == RELATIVE_TIME_VALUE );
00515 }
00516 
00517 inline bool Value::
00518 IsRelativeTimeValue( double &secs ) const
00519 {
00520     secs = relTimeValueSecs;
00521     return( valueType == RELATIVE_TIME_VALUE );
00522 }
00523 inline bool Value::
00524 IsRelativeTimeValue( time_t &secs ) const
00525 {
00526     secs = (int) relTimeValueSecs;
00527     return( valueType == RELATIVE_TIME_VALUE );
00528 }
00529 inline bool Value::
00530 IsNumber( ) const
00531 {
00532     return( valueType==INTEGER_VALUE || valueType==REAL_VALUE );
00533 }
00534 END_NAMESPACE // classad
00535 
00536 #endif//__CLASSAD_VALUE_H__
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends