- Cleaned up some operator implementations.
- Added Date, Duration formatting.
This commit is contained in:
Paul Beckingham 2011-07-04 13:51:36 -04:00
parent fc793e7b1d
commit b70f4e8528
2 changed files with 53 additions and 44 deletions

View file

@ -24,13 +24,18 @@
// USA // USA
// //
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
#include <iostream> #include <iostream>
#include <math.h> #include <math.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <text.h> #include <text.h>
#include <i18n.h>
#include <Context.h>
#include <Variant.h> #include <Variant.h>
extern Context context;
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
Variant::Variant () Variant::Variant ()
: _type (v_unknown) : _type (v_unknown)
@ -146,34 +151,34 @@ bool Variant::operator<= (Variant& other)
switch (_type) switch (_type)
{ {
case v_boolean: case v_boolean:
throw std::string ("Cannot perform relative comparison on bool types"); throw std::string (STRING_VARIANT_REL_BOOL);
break; break;
case v_integer: case v_integer:
result = _integer <= other._integer ? true : false; result = _integer <= other._integer;
break; break;
case v_double: case v_double:
result = _double <= other._double ? true : false; result = _double <= other._double;
break; break;
case v_string: case v_string:
{ {
int collating = strcmp (_string.c_str (), other._string.c_str ()); int collating = strcmp (_string.c_str (), other._string.c_str ());
result = collating <= 0 ? true : false; result = collating <= 0;
} }
break; break;
case v_date: case v_date:
result = _date <= other._date ? true : false; result = _date <= other._date;
break; break;
case v_duration: case v_duration:
result = (time_t)_duration <= (time_t)other._duration ? true : false; result = _duration <= other._duration;
break; break;
case v_unknown: case v_unknown:
throw std::string ("Cannot perform relative comparison on unknown types"); throw std::string (STRING_VARIANT_REL_UNKNOWN);
break; break;
} }
@ -189,34 +194,34 @@ bool Variant::operator>= (Variant& other)
switch (_type) switch (_type)
{ {
case v_boolean: case v_boolean:
throw std::string ("Cannot perform relative comparison on bool types"); throw std::string (STRING_VARIANT_REL_BOOL);
break; break;
case v_integer: case v_integer:
result = _integer >= other._integer ? true : false; result = _integer >= other._integer;
break; break;
case v_double: case v_double:
result = _double >= other._double ? true : false; result = _double >= other._double;
break; break;
case v_string: case v_string:
{ {
int collating = strcmp (_string.c_str (), other._string.c_str ()); int collating = strcmp (_string.c_str (), other._string.c_str ());
result = collating >= 0 ? true : false; result = collating >= 0;
} }
break; break;
case v_date: case v_date:
result = _date >= other._date ? true : false; result = _date >= other._date;
break; break;
case v_duration: case v_duration:
result = (time_t)_duration >= (time_t)other._duration ? true : false; result = _duration >= other._duration;
break; break;
case v_unknown: case v_unknown:
throw std::string ("Cannot perform relative comparison on unknown types"); throw std::string (STRING_VARIANT_REL_UNKNOWN);
break; break;
} }
@ -232,34 +237,34 @@ bool Variant::operator== (Variant& other)
switch (_type) switch (_type)
{ {
case v_boolean: case v_boolean:
result = _bool == other._bool ? true : false; result = _bool == other._bool;
break; break;
case v_integer: case v_integer:
result = _integer == other._integer ? true : false; result = _integer == other._integer;
break; break;
case v_double: case v_double:
result = _double == other._double ? true : false; result = _double == other._double;
break; break;
case v_string: case v_string:
{ {
int collating = strcmp (_string.c_str (), other._string.c_str ()); int collating = strcmp (_string.c_str (), other._string.c_str ());
result = collating == 0 ? true : false; result = collating == 0;
} }
break; break;
case v_date: case v_date:
result = _date == other._date ? true : false; result = _date == other._date;
break; break;
case v_duration: case v_duration:
result = _duration == other._duration ? true : false; result = _duration == other._duration;
break; break;
case v_unknown: case v_unknown:
throw std::string ("Cannot perform relative comparison on unknown types"); throw std::string (STRING_VARIANT_REL_UNKNOWN);
break; break;
} }
@ -275,34 +280,34 @@ bool Variant::operator!= (Variant& other)
switch (_type) switch (_type)
{ {
case v_boolean: case v_boolean:
result = _bool != other._bool ? true : false; result = _bool != other._bool;
break; break;
case v_integer: case v_integer:
result = _integer != other._integer ? true : false; result = _integer != other._integer;
break; break;
case v_double: case v_double:
result = _double != other._double ? true : false; result = _double != other._double;
break; break;
case v_string: case v_string:
{ {
int collating = strcmp (_string.c_str (), other._string.c_str ()); int collating = strcmp (_string.c_str (), other._string.c_str ());
result = collating != 0 ? true : false; result = collating != 0;
} }
break; break;
case v_date: case v_date:
result = _date != other._date ? true : false; result = _date != other._date;
break; break;
case v_duration: case v_duration:
result = _duration != other._duration ? true : false; result = _duration != other._duration;
break; break;
case v_unknown: case v_unknown:
throw std::string ("Cannot perform relative comparison on unknown types"); throw std::string (STRING_VARIANT_REL_UNKNOWN);
break; break;
} }
@ -485,34 +490,34 @@ bool Variant::operator< (Variant& other)
switch (_type) switch (_type)
{ {
case v_boolean: case v_boolean:
throw std::string ("Cannot perform relational compare Boolean types"); throw std::string (STRING_VARIANT_REL_BOOL);
break; break;
case v_integer: case v_integer:
result = _integer < other._integer ? true : false; result = _integer < other._integer;
break; break;
case v_double: case v_double:
result = _double < other._double ? true : false; result = _double < other._double;
break; break;
case v_string: case v_string:
{ {
int collating = strcmp (_string.c_str (), other._string.c_str ()); int collating = strcmp (_string.c_str (), other._string.c_str ());
result = collating < 0 ? true : false; result = collating < 0;
} }
break; break;
case v_date: case v_date:
result = _date < other._date ? true : false; result = _date < other._date;
break; break;
case v_duration: case v_duration:
result = _duration < other._duration ? true : false; result = _duration < other._duration;
break; break;
case v_unknown: case v_unknown:
throw std::string ("Cannot perform relative comparisons on unknown types"); throw std::string (STRING_VARIANT_REL_UNKNOWN);
break; break;
} }
@ -528,34 +533,34 @@ bool Variant::operator> (Variant& other)
switch (_type) switch (_type)
{ {
case v_boolean: case v_boolean:
throw std::string ("Cannot perform relational compare Boolean types"); throw std::string (STRING_VARIANT_REL_BOOL);
break; break;
case v_integer: case v_integer:
result = _integer > other._integer ? true : false; result = _integer > other._integer;
break; break;
case v_double: case v_double:
result = _double > other._double ? true : false; result = _double > other._double;
break; break;
case v_string: case v_string:
{ {
int collating = strcmp (_string.c_str (), other._string.c_str ()); int collating = strcmp (_string.c_str (), other._string.c_str ());
result = collating > 0 ? true : false; result = collating > 0;
} }
break; break;
case v_date: case v_date:
result = _date > other._date ? true : false; result = _date > other._date;
break; break;
case v_duration: case v_duration:
result = _duration > other._duration ? true : false; result = _duration > other._duration;
break; break;
case v_unknown: case v_unknown:
throw std::string ("Cannot perform relative comparisons on unknown types"); throw std::string (STRING_VARIANT_REL_UNKNOWN);
break; break;
} }
@ -648,11 +653,11 @@ std::string Variant::format ()
break; break;
case v_date: case v_date:
// TODO Format _date. output = _date.toString (context.config.get ("dateformat"));
break; break;
case v_duration: case v_duration:
// TODO Format _duration. output = _duration.formatCompact ();
break; break;
case v_unknown: case v_unknown:

View file

@ -415,5 +415,9 @@
#define STRING_UTIL_KIBIBYTES "KiB" #define STRING_UTIL_KIBIBYTES "KiB"
#define STRING_UTIL_BYTES "B" #define STRING_UTIL_BYTES "B"
// Variant
#define STRING_VARIANT_REL_BOOL "Cannot perform relational comparison on Boolean types."
#define STRING_VARIANT_REL_UNKNOWN "Cannot perform relational comparison on unknown types."
#endif #endif