00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef __VALUES_H__
00025 #define __VALUES_H__
00026
00027 #include "common.h"
00028 #include "util.h"
00029
00030 BEGIN_NAMESPACE( classad )
00031
00032 class Literal;
00033 class ExprList;
00034 class ClassAd;
00035
00037 class Value
00038 {
00039 public:
00041 enum ValueType {
00042 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
00053 };
00054
00056 enum NumberFactor { NO_FACTOR= 0, B_FACTOR = 1, K_FACTOR = 2, M_FACTOR = 3, G_FACTOR = 4, T_FACTOR = 5
00063 };
00064
00065
00067 static const double ScaleFactor[];
00068
00070 Value();
00071
00073 Value(const Value &value);
00074
00076 ~Value();
00077
00079 Value& operator=(const Value &value);
00080
00082 void Clear (void);
00083
00087 void CopyFrom( const Value &v );
00088
00092 void SetBooleanValue(bool b);
00093
00097 void SetRealValue(double r);
00098
00102 void SetIntegerValue(int i);
00103
00106 void SetUndefinedValue(void);
00107
00110 void SetErrorValue(void);
00111
00116 void SetListValue(ExprList* l);
00117
00122 void SetClassAdValue(ClassAd* c);
00123
00127 void SetStringValue( const std::string &str );
00128
00133 void SetStringValue( const char *str );
00134
00139 void SetAbsoluteTimeValue( abstime_t secs );
00140
00144 void SetRelativeTimeValue( time_t secs );
00145 void SetRelativeTimeValue( double secs );
00146
00151 inline ValueType GetType() const { return valueType; }
00152
00157 inline bool IsBooleanValue(bool& b) const;
00161 inline bool IsBooleanValue() const;
00166 inline bool IsIntegerValue(int &i) const;
00170 inline bool IsIntegerValue() const;
00175 inline bool IsRealValue(double &r) const;
00179 inline bool IsRealValue() const;
00185 bool IsStringValue( std::string &str ) const;
00192 bool IsStringValue( const char *&str ) const;
00198 bool IsStringValue( char *str, int len ) const;
00202 inline bool IsStringValue() const;
00207 inline bool IsListValue(const ExprList*& l) const;
00214 inline bool IsListValue(ExprList*& l);
00220 inline bool IsListValue() const;
00225 inline bool IsClassAdValue(const ClassAd *&c) const;
00232 inline bool IsClassAdValue(ClassAd *&c);
00236 inline bool IsClassAdValue() const;
00240 inline bool IsUndefinedValue() const;
00244 inline bool IsErrorValue() const;
00248 inline bool IsExceptional() const;
00252 bool IsNumber () const;
00258 bool IsNumber (int &i) const;
00264 bool IsNumber (double &r) const;
00268 bool IsAbsoluteTimeValue( ) const;
00273 bool IsAbsoluteTimeValue( abstime_t& secs ) const;
00277 bool IsRelativeTimeValue( ) const;
00282 bool IsRelativeTimeValue( double& secs ) const;
00283 bool IsRelativeTimeValue( time_t& secs ) const;
00284
00285 bool SameAs(const Value &otherValue) const;
00286
00287 friend bool operator==(const Value &value1, const Value &value2);
00288
00289 friend std::ostream& operator<<(std::ostream &stream, Value &value);
00290
00291 private:
00292 friend class Literal;
00293 friend class ClassAd;
00294 friend class ExprTree;
00295
00296 ValueType valueType;
00297
00298
00299 union {
00300 bool booleanValue;
00301 int integerValue;
00302 double realValue;
00303 ExprList *listValue;
00304 ClassAd *classadValue;
00305 double relTimeValueSecs;
00306 abstime_t absTimeValueSecs;
00307
00308 };
00309 std::string strValue;
00310 };
00311
00312 bool convertValueToRealValue(const Value value, Value &realValue);
00313 bool convertValueToIntegerValue(const Value value, Value &integerValue);
00314 bool convertValueToStringValue(const Value value, Value &stringValue);
00315
00316
00317 inline bool Value::
00318 IsBooleanValue( bool& b ) const
00319 {
00320 b = booleanValue;
00321 return( valueType == BOOLEAN_VALUE );
00322 }
00323
00324 inline bool Value::
00325 IsBooleanValue() const
00326 {
00327 return( valueType == BOOLEAN_VALUE );
00328 }
00329
00330 inline bool Value::
00331 IsIntegerValue (int &i) const
00332 {
00333 i = integerValue;
00334 return (valueType == INTEGER_VALUE);
00335 }
00336
00337 inline bool Value::
00338 IsIntegerValue () const
00339 {
00340 return (valueType == INTEGER_VALUE);
00341 }
00342
00343 inline bool Value::
00344 IsRealValue (double &r) const
00345 {
00346 r = realValue;
00347 return (valueType == REAL_VALUE);
00348 }
00349
00350 inline bool Value::
00351 IsRealValue () const
00352 {
00353 return (valueType == REAL_VALUE);
00354 }
00355
00356 inline bool Value::
00357 IsListValue( const ExprList *&l) const
00358 {
00359 if (valueType == LIST_VALUE) {
00360 l = listValue;
00361 return true;
00362 } else {
00363 return false;
00364 }
00365 }
00366
00367 inline bool Value::
00368 IsListValue( ExprList *&l)
00369 {
00370 if (valueType == LIST_VALUE) {
00371 l = listValue;
00372 return true;
00373 } else {
00374 return false;
00375 }
00376 }
00377
00378 inline bool Value::
00379 IsListValue () const
00380 {
00381 return (valueType == LIST_VALUE);
00382 }
00383
00384
00385 inline bool Value::
00386 IsStringValue() const
00387 {
00388 return (valueType == STRING_VALUE);
00389 }
00390
00391
00392 inline bool Value::
00393 IsStringValue( const char *&s ) const
00394 {
00395
00396
00397
00398
00399 if (valueType == STRING_VALUE) {
00400 s = strValue.c_str( );
00401 return true;
00402 } else {
00403 return false;
00404 }
00405 }
00406
00407 inline bool Value::
00408 IsStringValue( char *s, int len ) const
00409 {
00410 if( valueType == STRING_VALUE ) {
00411 strncpy( s, strValue.c_str( ), len );
00412 return( true );
00413 }
00414 return( false );
00415 }
00416
00417 inline bool Value::
00418 IsStringValue( std::string &s ) const
00419 {
00420 if ( valueType == STRING_VALUE ) {
00421 s = strValue;
00422 return true;
00423 } else {
00424 return false;
00425 }
00426 }
00427
00428 inline bool Value::
00429 IsClassAdValue(const ClassAd *&ad) const
00430 {
00431 if ( valueType == CLASSAD_VALUE ) {
00432 ad = classadValue;
00433 return true;
00434 } else {
00435 return false;
00436 }
00437 }
00438
00439 inline bool Value::
00440 IsClassAdValue(ClassAd *&ad)
00441 {
00442 if ( valueType == CLASSAD_VALUE ) {
00443 ad = classadValue;
00444 return true;
00445 } else {
00446 return false;
00447 }
00448 }
00449
00450 inline bool Value::
00451 IsClassAdValue() const
00452 {
00453 return( valueType == CLASSAD_VALUE );
00454 }
00455
00456 inline bool Value::
00457 IsUndefinedValue (void) const
00458 {
00459 return (valueType == UNDEFINED_VALUE);
00460 }
00461
00462 inline bool Value::
00463 IsErrorValue(void) const
00464 {
00465 return (valueType == ERROR_VALUE);
00466 }
00467
00468 inline bool Value::
00469 IsExceptional(void) const
00470 {
00471 return( valueType == UNDEFINED_VALUE || valueType == ERROR_VALUE );
00472 }
00473
00474 inline bool Value::
00475 IsAbsoluteTimeValue( ) const
00476 {
00477 return( valueType == ABSOLUTE_TIME_VALUE );
00478 }
00479
00480 inline bool Value::
00481 IsAbsoluteTimeValue( abstime_t &secs ) const
00482 {
00483 secs = absTimeValueSecs;
00484 return( valueType == ABSOLUTE_TIME_VALUE );
00485 }
00486
00487 inline bool Value::
00488 IsRelativeTimeValue( ) const
00489 {
00490 return( valueType == RELATIVE_TIME_VALUE );
00491 }
00492
00493 inline bool Value::
00494 IsRelativeTimeValue( double &secs ) const
00495 {
00496 secs = relTimeValueSecs;
00497 return( valueType == RELATIVE_TIME_VALUE );
00498 }
00499 inline bool Value::
00500 IsRelativeTimeValue( time_t &secs ) const
00501 {
00502 secs = (int) relTimeValueSecs;
00503 return( valueType == RELATIVE_TIME_VALUE );
00504 }
00505 inline bool Value::
00506 IsNumber( ) const
00507 {
00508 return( valueType==INTEGER_VALUE || valueType==REAL_VALUE );
00509 }
00510 END_NAMESPACE
00511
00512 #endif//__VALUES_H__