diff options
Diffstat (limited to 'project2/common/variableConvert.cpp')
-rw-r--r-- | project2/common/variableConvert.cpp | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/project2/common/variableConvert.cpp b/project2/common/variableConvert.cpp index 737a856..2c6e49a 100644 --- a/project2/common/variableConvert.cpp +++ b/project2/common/variableConvert.cpp @@ -29,6 +29,9 @@ class ConvertVisitorGlibUstring : public boost::static_visitor<const Glib::ustri const Glib::ustring & operator()(const Glib::ustring & r) const { return r; } + const Glib::ustring & operator()(const boost::posix_time::time_duration & r) const { + CONVERT(const Glib::ustring, boost::posix_time::to_simple_string); + } const Glib::ustring & operator()(const boost::posix_time::ptime & r) const { CONVERT(const Glib::ustring, boost::posix_time::to_iso_extended_string); } @@ -50,6 +53,9 @@ class ConvertVisitorStdString : public boost::static_visitor<const std::string & const std::string & operator()(const Glib::ustring & r) const { return r.raw(); } + const std::string & operator()(const boost::posix_time::time_duration & r) const { + CONVERTF(const Glib::ustring, boost::posix_time::to_simple_string, raw); + } const std::string & operator()(const boost::posix_time::ptime & r) const { CONVERTF(const Glib::ustring, boost::posix_time::to_iso_extended_string, raw); } @@ -71,6 +77,9 @@ class ConvertVisitorCharStar : public boost::static_visitor<const char *> { const char * operator()(const Glib::ustring & r) const { return r.c_str(); } + const char * operator()(const boost::posix_time::time_duration & r) const { + CONVERTF(const Glib::ustring, boost::posix_time::to_simple_string, c_str); + } const char * operator()(const boost::posix_time::ptime & r) const { CONVERTF(const Glib::ustring, boost::posix_time::to_iso_extended_string, c_str); } @@ -93,6 +102,9 @@ class ConvertVisitor : public boost::static_visitor<DestType> { DestType operator()(const Glib::ustring & r) const { CONVERT(DestType, boost::lexical_cast<DestType>); } + DestType operator()(const boost::posix_time::time_duration &) const { + throw InvalidConversionTo(typeid(DestType).name(), "<Time Duration>"); + } DestType operator()(const boost::posix_time::ptime &) const { throw InvalidConversionTo(typeid(DestType).name(), "<Date Time>"); } @@ -109,6 +121,27 @@ class ConvertVisitor : public boost::static_visitor<DestType> { private: const VariableType * var; }; +// Convert to time duration +class ConvertVisitorTimeDuration : public boost::static_visitor<const boost::posix_time::time_duration &> { + public: + ConvertVisitorTimeDuration(const VariableType * v) : var(v) { + } + const boost::posix_time::time_duration & operator()(const Glib::ustring & r) const { + CONVERT(const boost::posix_time::time_duration, boost::posix_time::duration_from_string); + } + const boost::posix_time::time_duration & operator()(const boost::posix_time::time_duration & t) const { + return t; + } + const boost::posix_time::time_duration & operator()(const Null &) const { + throw NullVariable(); + } + template <typename T> + const boost::posix_time::time_duration & operator()(const T &) const { + throw InvalidConversionTo("TimeDuration", typeid(T).name()); + } + private: + const VariableType * var; +}; // Convert to ptime class ConvertVisitorDateTime : public boost::static_visitor<const boost::posix_time::ptime &> { public: @@ -138,6 +171,9 @@ class ConvertVisitorBool : public boost::static_visitor<bool> { bool operator()(const Glib::ustring & r) const { CONVERT(bool, fromStr); } + bool operator()(const boost::posix_time::time_duration & t) const { + return (t.ticks() != 0); + } bool operator()(const boost::posix_time::ptime &) const { throw InvalidConversionTo("bool", "<Date time>"); } @@ -189,6 +225,10 @@ VariableType::operator double() const { return boost::apply_visitor(ConvertVisitor<double>(this), *this); } +VariableType::operator const boost::posix_time::time_duration &() const +{ + return boost::apply_visitor(ConvertVisitorTimeDuration(this), *this); +} VariableType::operator const boost::posix_time::ptime &() const { return boost::apply_visitor(ConvertVisitorDateTime(this), *this); |