From a9998984ed6d582c6b865ad1e7fa7b035a778c01 Mon Sep 17 00:00:00 2001 From: randomdan Date: Fri, 18 Mar 2011 16:11:17 +0000 Subject: Add native conversion of VariableTypes to int32_t Templatize the numeric conversions for VariableTypes --- project2/variableConvert.cpp | 64 +++++++++++++++----------------------------- project2/variables.h | 1 + 2 files changed, 22 insertions(+), 43 deletions(-) diff --git a/project2/variableConvert.cpp b/project2/variableConvert.cpp index 8797ef7..ea6ddef 100644 --- a/project2/variableConvert.cpp +++ b/project2/variableConvert.cpp @@ -145,56 +145,30 @@ class ConvertVisitorUCharStar : public boost::static_visitor { +// Convert to generic type +template +class ConvertVisitor : public boost::static_visitor { public: - ConvertVisitorInt(const VariableType * v) : var(v) { + ConvertVisitor(const VariableType * v) : var(v) { } - int64_t operator()(const Glib::ustring & r) const { - return boost::lexical_cast(r); + DestType operator()(const Glib::ustring & r) const { + return boost::lexical_cast(r); } - int64_t operator()(const boost::shared_ptr & r) const { - return boost::lexical_cast(*r); + DestType operator()(const boost::shared_ptr & r) const { + return boost::lexical_cast(*r); } - int64_t operator()(const boost::posix_time::ptime &) const { - throw InvalidConversionTo("Integer"); + DestType operator()(const boost::posix_time::ptime &) const { + throw InvalidConversionTo(typeid(DestType).name()); } - int64_t operator()(const boost::shared_ptr &) const { - throw InvalidConversionTo("Integer"); + DestType operator()(const boost::shared_ptr &) const { + throw InvalidConversionTo(typeid(DestType).name()); } - int64_t operator()(const Null &) const { + DestType operator()(const Null &) const { throw NullVariable(); } template - int64_t operator()(const T & r) const { - return boost::numeric_cast(r); - } - private: - const VariableType * var; -}; -// Convert to double -class ConvertVisitorDouble : public boost::static_visitor { - public: - ConvertVisitorDouble(const VariableType * v) : var(v) { - } - double operator()(const Glib::ustring & r) const { - return boost::lexical_cast(r); - } - double operator()(const boost::shared_ptr & r) const { - return boost::lexical_cast(*r); - } - double operator()(const boost::posix_time::ptime &) const { - throw InvalidConversionTo("Floating point"); - } - double operator()(const boost::shared_ptr &) const { - throw InvalidConversionTo("Floating point"); - } - double operator()(const Null &) const { - throw NullVariable(); - } - template - double operator()(const T & r) const { - return boost::numeric_cast(r); + DestType operator()(const T & r) const { + return boost::numeric_cast(r); } private: const VariableType * var; @@ -242,13 +216,17 @@ VariableType::operator const unsigned char *() const { return boost::apply_visitor(ConvertVisitorUCharStar(this), *this); } +VariableType::operator int32_t() const +{ + return boost::apply_visitor(ConvertVisitor(this), *this); +} VariableType::operator int64_t() const { - return boost::apply_visitor(ConvertVisitorInt(this), *this); + return boost::apply_visitor(ConvertVisitor(this), *this); } VariableType::operator double() const { - return boost::apply_visitor(ConvertVisitorDouble(this), *this); + return boost::apply_visitor(ConvertVisitor(this), *this); } VariableType::operator const boost::posix_time::ptime &() const { diff --git a/project2/variables.h b/project2/variables.h index 863355c..5e29547 100644 --- a/project2/variables.h +++ b/project2/variables.h @@ -50,6 +50,7 @@ class VariableType : public _VT { operator const char *() const; operator const unsigned char *() const; operator int64_t() const; + operator int32_t() const; operator double() const; operator const boost::posix_time::ptime &() const; -- cgit v1.2.3