PLearn 0.1
Classes | Public Member Functions | Private Attributes | Friends
PLearn::FieldValue Class Reference

#include <SimpleDB.h>

Collaboration diagram for PLearn::FieldValue:
Collaboration graph
[legend]

List of all members.

Classes

struct  DateVal_t

Public Member Functions

 FieldValue ()
 Default construction and copy construction.
 FieldValue (const FieldValue &fv)
 ~FieldValue ()
 FieldValue (const char *)
 Constructors from concrete values.
 FieldValue (unsigned char)
 FieldValue (signed char)
 FieldValue (short)
 FieldValue (int)
 FieldValue (float)
 FieldValue (double)
 FieldValue (const PDate &)
bool isMissing () const
 Missing value handling.
void setMissing ()
string toString () const
 Conversion to strings and doubles.
double toDouble () const
PDate toDate () const
 operator double () const
 operator float () const
 operator int () const
 operator string () const
 operator PDate () const
FieldValueoperator= (FieldValue)
 Assignment.
bool operator== (const FieldValue &) const
 Relational (relational operations don't always make sense)
bool operator< (const FieldValue &) const
bool operator!= (const FieldValue &rhs) const
bool operator<= (const FieldValue &rhs) const
bool operator> (const FieldValue &rhs) const
bool operator>= (const FieldValue &rhs) const
FieldValue operator+ (const FieldValue &) const
 Arithmetic (again, these don't always make sense)
FieldValue operator- (const FieldValue &) const
FieldValue operator* (const FieldValue &) const
FieldValue operator/ (const FieldValue &) const
void swap (FieldValue &)
 Swap the guts of this with someone else.
bool isString () const
 Some useful type-checking predicates.
bool isIntegral () const
bool isFloating () const
bool isDate () const

Private Attributes

FieldType field_type_
int precision_
union {
   char *   string_val_
   long   long_val_
   double   double_val_
   struct DateVal_t   date_val_
}; 

Friends

class FieldRowRef

Detailed Description

A FieldValue is intended to encapsulate all polyphormic operations (copy, comparison, conversion to/from string, double, etc.) on the values of a SimpleDB field.

Definition at line 231 of file SimpleDB.h.


Constructor & Destructor Documentation

PLearn::FieldValue::FieldValue ( )

Default construction and copy construction.

Definition at line 101 of file SimpleDB.cc.

Referenced by operator*(), operator+(), operator-(), and operator/().

Here is the caller graph for this function:

PLearn::FieldValue::FieldValue ( const FieldValue fv)

Definition at line 105 of file SimpleDB.cc.

References PLearn::CharacterType, date_val_, PLearn::DateType, double_val_, PLearn::DoubleType, field_type_, PLearn::FloatType, PLearn::IntType, long_val_, PLERROR, precision_, PLearn::ShortType, PLearn::SignedCharType, string_val_, PLearn::StringType, PLearn::strlen(), and PLearn::Unknown.

    : field_type_(fv.field_type_), precision_(fv.precision_)
{
    switch (field_type_) {
    case Unknown:
        break;

    case StringType:
    {
        int stringlen = (int)strlen(fv.string_val_);
        if (stringlen+1 != precision_)
            PLERROR("Strings in a database field must contain a terminating null");
        string_val_ = new char[precision_];
        strcpy(string_val_, fv.string_val_);
    }
    break;

    case CharacterType:
    case SignedCharType:
    case ShortType:
    case IntType:
        long_val_ = fv.long_val_;
        break;

    case FloatType:
    case DoubleType:
        double_val_ = fv.double_val_;
        break;

    case DateType:
        date_val_ = fv.date_val_;
        break;
    }
}

Here is the call graph for this function:

PLearn::FieldValue::~FieldValue ( )
PLearn::FieldValue::FieldValue ( const char *  str) [explicit]

Constructors from concrete values.

Definition at line 158 of file SimpleDB.cc.

References precision_, and string_val_.

    : field_type_(StringType), precision_(strlen(str)+1)
{
    string_val_ = new char[precision_];
    strcpy(string_val_, str);
}
PLearn::FieldValue::FieldValue ( unsigned char  x) [explicit]

Definition at line 165 of file SimpleDB.cc.

PLearn::FieldValue::FieldValue ( signed char  x) [explicit]

Definition at line 171 of file SimpleDB.cc.

PLearn::FieldValue::FieldValue ( short  x) [explicit]

Definition at line 177 of file SimpleDB.cc.

    : field_type_(ShortType),
      precision_(Field("",ShortType).precision),
      long_val_(x)
{}
PLearn::FieldValue::FieldValue ( int  x) [explicit]

Definition at line 183 of file SimpleDB.cc.

    : field_type_(IntType),
      precision_(Field("",IntType).precision),
      long_val_(x)
{}
PLearn::FieldValue::FieldValue ( float  x) [explicit]

Definition at line 189 of file SimpleDB.cc.

    : field_type_(FloatType),
      precision_(Field("",FloatType).precision),
      double_val_(x)
{}
PLearn::FieldValue::FieldValue ( double  x) [explicit]

Definition at line 195 of file SimpleDB.cc.

    : field_type_(DoubleType),
      precision_(Field("",DoubleType).precision),
      double_val_(x)
{}
PLearn::FieldValue::FieldValue ( const PDate x) [explicit]

Member Function Documentation

bool PLearn::FieldValue::isDate ( ) const [inline]

Definition at line 315 of file SimpleDB.h.

References PLearn::DateType.

Referenced by PLearn::SDBVMFieldDateDiff::convertField(), and PLearn::FieldRowRef::operator=().

                        {
        return field_type_ == DateType;
    }

Here is the caller graph for this function:

bool PLearn::FieldValue::isFloating ( ) const [inline]

Definition at line 311 of file SimpleDB.h.

References PLearn::DoubleType, and PLearn::FloatType.

Referenced by operator*(), operator+(), operator-(), operator/(), operator<(), PLearn::FieldRowRef::operator=(), and operator==().

Here is the caller graph for this function:

bool PLearn::FieldValue::isIntegral ( ) const [inline]
bool PLearn::FieldValue::isMissing ( ) const

Missing value handling.

Definition at line 210 of file SimpleDB.cc.

References PLearn::CharacterType, date_val_, PLearn::DateType, PLearn::PDate::day, PLearn::FieldValue::DateVal_t::day, double_val_, PLearn::DoubleType, field_type_, PLearn::FloatType, PLearn::IntType, long_val_, PLearn::MissingCharacter, PLearn::MissingDate, PLearn::MissingInt, PLearn::MissingShort, PLearn::MissingSignedChar, PLearn::MissingString, PLearn::FieldValue::DateVal_t::month, PLearn::PDate::month, PLearn::ShortType, PLearn::SignedCharType, string_val_, PLearn::StringType, PLearn::Unknown, PLearn::FieldValue::DateVal_t::year, and PLearn::PDate::year.

Referenced by PLearn::SDBVMFieldDateDiff::convertField(), PLearn::SDBVMFieldCodeAsIs::getDiscreteValue(), PLearn::SDBVMFieldRemapStrings::getDiscreteValue(), PLearn::SDBVMFieldDateGreater::getDiscreteValue(), PLearn::SDBVMFieldRemapReals::getDiscreteValue(), PLearn::SDBVMFieldRemapIntervals::getDiscreteValue(), toDouble(), and toString().

{
    switch (field_type_) {
    case Unknown:       return true;
    case StringType:    return !string_val_ || string_val_[0] == MissingString;
    case CharacterType: return (unsigned char)long_val_ == MissingCharacter;
    case SignedCharType:return (signed char)  long_val_ == MissingSignedChar;
    case ShortType:     return (short)        long_val_ == MissingShort;
    case IntType:       return (int)          long_val_ == MissingInt;
    case FloatType:
    case DoubleType:    return isnan(double_val_);
    case DateType:
        return date_val_.year == MissingDate.year &&
            date_val_.month == MissingDate.month &&
            date_val_.day   == MissingDate.day;
    }
    return false;
}   

Here is the caller graph for this function:

bool PLearn::FieldValue::isString ( ) const [inline]

Some useful type-checking predicates.

Definition at line 301 of file SimpleDB.h.

References PLearn::StringType.

                          {
        return field_type_ == StringType;
    }
PLearn::FieldValue::operator double ( ) const [inline]

Definition at line 260 of file SimpleDB.h.

{ return toDouble(); }
PLearn::FieldValue::operator float ( ) const [inline]

Definition at line 261 of file SimpleDB.h.

{ return float(toDouble()); }
PLearn::FieldValue::operator int ( ) const [inline]

Definition at line 262 of file SimpleDB.h.

{ return int(toDouble()); }
PLearn::FieldValue::operator PDate ( ) const [inline]

Definition at line 264 of file SimpleDB.h.

{ return toDate(); }
PLearn::FieldValue::operator string ( ) const [inline]

Definition at line 263 of file SimpleDB.h.

{ return toString(); }
bool PLearn::FieldValue::operator!= ( const FieldValue rhs) const [inline]

Definition at line 275 of file SimpleDB.h.

                                                 {
        return !(*this == rhs);
    }
FieldValue PLearn::FieldValue::operator* ( const FieldValue rhs) const

Definition at line 468 of file SimpleDB.cc.

References PLearn::CharacterType, PLearn::DateType, double_val_, field_type_, FieldValue(), isFloating(), isIntegral(), long_val_, PLERROR, and PLearn::StringType.

{
    const FieldValue& lhs = *this;
    const FieldType& lhs_type = field_type_;
    const FieldType& rhs_type = rhs.field_type_;

    // Arithmetic addition is not defined for strings or dates or characters
    if (lhs_type == StringType || rhs_type == StringType)
        PLERROR("Strings cannot be multiplied");
    else if (lhs_type == CharacterType || rhs_type == CharacterType)
        PLERROR("Characters cannot be multiplied");
    else if (lhs_type == DateType || rhs_type == DateType)
        PLERROR("Dates cannot be multiplied");

    // Twice the same type
    else if (lhs.isIntegral() && rhs.isIntegral())
        return FieldValue(int(lhs.long_val_ * rhs.long_val_));
    else if (lhs.isFloating() && rhs.isFloating())
        return FieldValue(double(lhs.double_val_ * rhs.double_val_));

    // Cross-numeric : convert to double
    else if (lhs.isIntegral() && rhs.isFloating())
        return FieldValue(double(lhs.long_val_ * rhs.double_val_));
    else if (lhs.isFloating() && rhs.isIntegral())
        return FieldValue(double(lhs.double_val_ * rhs.long_val_));

    // Otherwise, PLERROR(should not happen)
    else
        PLERROR("Unrecognized case in multiplication between FieldValues");

    return FieldValue();
}

Here is the call graph for this function:

FieldValue PLearn::FieldValue::operator+ ( const FieldValue rhs) const

Arithmetic (again, these don't always make sense)

Definition at line 396 of file SimpleDB.cc.

References PLearn::CharacterType, PLearn::DateType, double_val_, field_type_, FieldValue(), isFloating(), isIntegral(), long_val_, PLERROR, and PLearn::StringType.

{
    const FieldValue& lhs = *this;
    const FieldType& lhs_type = field_type_;
    const FieldType& rhs_type = rhs.field_type_;

    // Arithmetic addition is not defined for strings or dates or characters
    if (lhs_type == StringType || rhs_type == StringType)
        PLERROR("Strings cannot be added");
    else if (lhs_type == CharacterType || rhs_type == CharacterType)
        PLERROR("Characters cannot be added");
    else if (lhs_type == DateType || rhs_type == DateType)
        PLERROR("Dates cannot be added");

    // Twice the same type
    else if (lhs.isIntegral() && rhs.isIntegral())
        return FieldValue(int(lhs.long_val_ + rhs.long_val_));
    else if (lhs.isFloating() && rhs.isFloating())
        return FieldValue(double(lhs.double_val_ + rhs.double_val_));

    // Cross-numeric : convert to double
    else if (lhs.isIntegral() && rhs.isFloating())
        return FieldValue(double(lhs.long_val_ + rhs.double_val_));
    else if (lhs.isFloating() && rhs.isIntegral())
        return FieldValue(double(lhs.double_val_ + rhs.long_val_));

    // Otherwise, PLERROR(should not happen)
    else
        PLERROR("Unrecognized case in addition between FieldValues");

    return FieldValue();
}

Here is the call graph for this function:

FieldValue PLearn::FieldValue::operator- ( const FieldValue rhs) const

Definition at line 429 of file SimpleDB.cc.

References PLearn::CharacterType, date_val_, PLearn::DateType, PLearn::FieldValue::DateVal_t::day, double_val_, field_type_, FieldValue(), isFloating(), isIntegral(), long_val_, PLearn::FieldValue::DateVal_t::month, PLERROR, PLearn::StringType, and PLearn::FieldValue::DateVal_t::year.

{
    const FieldValue& lhs = *this;
    const FieldType& lhs_type = field_type_;
    const FieldType& rhs_type = rhs.field_type_;

    // Arithmetic subtraction is not defined for strings or characters
    if (lhs_type == StringType || rhs_type == StringType)
        PLERROR("Strings cannot be subtracted");
    else if (lhs_type == CharacterType || rhs_type == CharacterType)
        PLERROR("Characters cannot be subtracted");

    // For dates, return the number of days between two dates
    else if (lhs_type == DateType && rhs_type == DateType)
        return FieldValue(int(
                              PDate(lhs.date_val_.year, lhs.date_val_.month, lhs.date_val_.day) -
                              PDate(rhs.date_val_.year, rhs.date_val_.month, rhs.date_val_.day)));
    else if (lhs_type == DateType || rhs_type == DateType)
        PLERROR("A date and a non-date cannot be subtracted");

    // Twice the same type
    else if (lhs.isIntegral() && rhs.isIntegral())
        return FieldValue(int(lhs.long_val_ - rhs.long_val_));
    else if (lhs.isFloating() && rhs.isFloating())
        return FieldValue(double(lhs.double_val_ - rhs.double_val_));

    // Cross-numeric : convert to double
    else if (lhs.isIntegral() && rhs.isFloating())
        return FieldValue(double(lhs.long_val_ - rhs.double_val_));
    else if (lhs.isFloating() && rhs.isIntegral())
        return FieldValue(double(lhs.double_val_ - rhs.long_val_));

    // Otherwise, PLERROR(should not happen)
    else
        PLERROR("Unrecognized case in subtraction between FieldValues");

    return FieldValue();
}

Here is the call graph for this function:

FieldValue PLearn::FieldValue::operator/ ( const FieldValue rhs) const

Definition at line 501 of file SimpleDB.cc.

References PLearn::CharacterType, PLearn::DateType, double_val_, field_type_, FieldValue(), isFloating(), isIntegral(), long_val_, PLERROR, and PLearn::StringType.

{
    const FieldValue& lhs = *this;
    const FieldType& lhs_type = field_type_;
    const FieldType& rhs_type = rhs.field_type_;

    // Arithmetic addition is not defined for strings or dates or characters
    if (lhs_type == StringType || rhs_type == StringType)
        PLERROR("Strings cannot be divided");
    else if (lhs_type == CharacterType || rhs_type == CharacterType)
        PLERROR("Characters cannot be divided");
    else if (lhs_type == DateType || rhs_type == DateType)
        PLERROR("Dates cannot be divided");

    // Twice the same type
    else if (lhs.isIntegral() && rhs.isIntegral())
        return FieldValue(int(lhs.long_val_ / rhs.long_val_));
    else if (lhs.isFloating() && rhs.isFloating())
        return FieldValue(double(lhs.double_val_ / rhs.double_val_));

    // Cross-numeric : convert to double
    else if (lhs.isIntegral() && rhs.isFloating())
        return FieldValue(double(lhs.long_val_ / rhs.double_val_));
    else if (lhs.isFloating() && rhs.isIntegral())
        return FieldValue(double(lhs.double_val_ / rhs.long_val_));

    // Otherwise, PLERROR(should not happen)
    else
        PLERROR("Unrecognized case in division between FieldValues");

    return FieldValue();
}

Here is the call graph for this function:

bool PLearn::FieldValue::operator< ( const FieldValue rhs) const

Definition at line 355 of file SimpleDB.cc.

References date_val_, PLearn::DateType, PLearn::FieldValue::DateVal_t::day, double_val_, field_type_, if(), isFloating(), isIntegral(), long_val_, PLearn::FieldValue::DateVal_t::month, PLERROR, string_val_, PLearn::StringType, and PLearn::FieldValue::DateVal_t::year.

{
    const FieldValue& lhs = *this;
    const FieldType& lhs_type = field_type_;
    const FieldType& rhs_type = rhs.field_type_;

    // Strings
    if (lhs_type == StringType && rhs_type == StringType)
        return strcmp(lhs.string_val_, rhs.string_val_) < 0;
    else if (lhs_type == StringType || rhs_type == StringType)
        PLERROR("A string can be relationally compared only with another string");

    // Dates
    else if (lhs_type == DateType && rhs_type == DateType)
        return
            PDate(lhs.date_val_.year, lhs.date_val_.month, lhs.date_val_.day) <
            PDate(rhs.date_val_.year, rhs.date_val_.month, rhs.date_val_.day);
    else if (lhs_type == DateType || rhs_type == DateType)
        PLERROR("A date can be relationally compared only with another date");

    // Two integrals
    else if (lhs.isIntegral() && rhs.isIntegral())
        return lhs.long_val_ < rhs.long_val_;

    // Two floating
    else if (lhs.isFloating() && rhs.isFloating())
        return lhs.double_val_ < rhs.double_val_;

    // Cross-numeric
    else if (lhs.isIntegral() && rhs.isFloating())
        return lhs.long_val_ < rhs.double_val_;
    else if (lhs.isFloating() && lhs.isIntegral())
        return lhs.double_val_ < rhs.long_val_;

    // Otherwise, PLERROR(should not happen)
    else
        PLERROR("Unrecognized case in relational testing between FieldValues");

    return false;                            // shut up the compiler
}

Here is the call graph for this function:

bool PLearn::FieldValue::operator<= ( const FieldValue rhs) const [inline]

Definition at line 279 of file SimpleDB.h.

                                                 {
        return (*this == rhs) || (*this < rhs);
    }
FieldValue & PLearn::FieldValue::operator= ( FieldValue  rhs)

Assignment.

--- Pseudo-polymorphic operations

Definition at line 308 of file SimpleDB.cc.

References swap().

{
    swap(rhs);
    return *this;
}

Here is the call graph for this function:

bool PLearn::FieldValue::operator== ( const FieldValue rhs) const

Relational (relational operations don't always make sense)

Definition at line 314 of file SimpleDB.cc.

References date_val_, PLearn::DateType, PLearn::FieldValue::DateVal_t::day, double_val_, field_type_, if(), isFloating(), isIntegral(), long_val_, PLearn::FieldValue::DateVal_t::month, PLERROR, string_val_, PLearn::StringType, and PLearn::FieldValue::DateVal_t::year.

{
    const FieldValue& lhs = *this;
    const FieldType& lhs_type = field_type_;
    const FieldType& rhs_type = rhs.field_type_;

    // Strings
    if (lhs_type == StringType && rhs_type == StringType)
        return !strcmp(lhs.string_val_, rhs.string_val_);
    else if (lhs_type == StringType || rhs_type == StringType)
        PLERROR("A string can be compared for equality only with another string");

    // Dates
    else if (lhs_type == DateType && rhs_type == DateType)
        return
            PDate(lhs.date_val_.year, lhs.date_val_.month, lhs.date_val_.day) ==
            PDate(rhs.date_val_.year, rhs.date_val_.month, rhs.date_val_.day);
    else if (lhs_type == DateType || rhs_type == DateType)
        PLERROR("A date can be compared for equality only with another date");

    // Two integrals
    else if (lhs.isIntegral() && rhs.isIntegral())
        return lhs.long_val_ == rhs.long_val_;

    // Two floating
    else if (lhs.isFloating() && rhs.isFloating())
        return lhs.double_val_ == rhs.double_val_;

    // Cross-numeric
    else if (lhs.isIntegral() && rhs.isFloating())
        return lhs.long_val_ == rhs.double_val_;
    else if (lhs.isFloating() && lhs.isIntegral())
        return lhs.double_val_ == rhs.long_val_;

    // Otherwise, PLERROR(should not happen)
    else
        PLERROR("Unrecognized case in equality testing between FieldValues");

    return false;                            // shut up the compiler
}

Here is the call graph for this function:

bool PLearn::FieldValue::operator> ( const FieldValue rhs) const [inline]

Definition at line 283 of file SimpleDB.h.

                                                 {
        return !(*this <= rhs);
    }
bool PLearn::FieldValue::operator>= ( const FieldValue rhs) const [inline]

Definition at line 287 of file SimpleDB.h.

                                                 {
        return !(*this < rhs);
    }
void PLearn::FieldValue::setMissing ( )
void PLearn::FieldValue::swap ( FieldValue rhs)

Swap the guts of this with someone else.

Definition at line 534 of file SimpleDB.cc.

References PLearn::CharacterType, date_val_, PLearn::DateType, double_val_, PLearn::DoubleType, field_type_, PLearn::FloatType, PLearn::IntType, long_val_, precision_, PLearn::ShortType, PLearn::SignedCharType, string_val_, PLearn::StringType, and PLearn::Unknown.

Referenced by operator=().

{
    std::swap(field_type_, rhs.field_type_);
    std::swap(precision_,  rhs.precision_);
    switch(field_type_) {
    case Unknown:       break;
    case StringType:    std::swap(string_val_, rhs.string_val_); break;
    case CharacterType:
    case SignedCharType:
    case ShortType:
    case IntType:       std::swap(long_val_, rhs.long_val_); break;
    case FloatType:
    case DoubleType:    std::swap(double_val_, rhs.double_val_); break;
    case DateType:      std::swap(date_val_, rhs.date_val_); break;
    }
}

Here is the caller graph for this function:

PDate PLearn::FieldValue::toDate ( ) const

Definition at line 296 of file SimpleDB.cc.

References date_val_, PLearn::DateType, PLearn::FieldValue::DateVal_t::day, field_type_, PLearn::FieldValue::DateVal_t::month, PLERROR, and PLearn::FieldValue::DateVal_t::year.

Referenced by PLearn::SDBVMFieldICBCTargets::convertField(), PLearn::SDBVMFieldDateDiff::convertField(), and PLearn::SDBVMFieldDateGreater::getDiscreteValue().

{
    switch(field_type_) {
    case DateType:
        return PDate(date_val_.year, date_val_.month, date_val_.day);

    default:
        PLERROR("Cannot convert non-date field type to a date");
    }
    return PDate();
}  

Here is the caller graph for this function:

double PLearn::FieldValue::toDouble ( ) const
string PLearn::FieldValue::toString ( ) const

Friends And Related Function Documentation

friend class FieldRowRef [friend]

Definition at line 233 of file SimpleDB.h.


Member Data Documentation

union { ... } [private]

Definition at line 321 of file SimpleDB.h.

Referenced by FieldValue(), and swap().


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines