summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrandomdan <randomdan@localhost>2011-03-18 16:11:17 +0000
committerrandomdan <randomdan@localhost>2011-03-18 16:11:17 +0000
commita9998984ed6d582c6b865ad1e7fa7b035a778c01 (patch)
tree9f4a7c688629005359f256b4b76820300163d9eb
parentProperly unregister components when their library is unloaded (diff)
downloadproject2-a9998984ed6d582c6b865ad1e7fa7b035a778c01.tar.bz2
project2-a9998984ed6d582c6b865ad1e7fa7b035a778c01.tar.xz
project2-a9998984ed6d582c6b865ad1e7fa7b035a778c01.zip
Add native conversion of VariableTypes to int32_t
Templatize the numeric conversions for VariableTypes
-rw-r--r--project2/variableConvert.cpp64
-rw-r--r--project2/variables.h1
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<const unsigned char
private:
const VariableType * var;
};
-// Convert to int64_t
-class ConvertVisitorInt : public boost::static_visitor<int64_t> {
+// Convert to generic type
+template <typename DestType>
+class ConvertVisitor : public boost::static_visitor<DestType> {
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<int64_t>(r);
+ DestType operator()(const Glib::ustring & r) const {
+ return boost::lexical_cast<DestType>(r);
}
- int64_t operator()(const boost::shared_ptr<const Glib::ustring> & r) const {
- return boost::lexical_cast<int64_t>(*r);
+ DestType operator()(const boost::shared_ptr<const Glib::ustring> & r) const {
+ return boost::lexical_cast<DestType>(*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 boost::posix_time::ptime> &) const {
- throw InvalidConversionTo("Integer");
+ DestType operator()(const boost::shared_ptr<const boost::posix_time::ptime> &) const {
+ throw InvalidConversionTo(typeid(DestType).name());
}
- int64_t operator()(const Null &) const {
+ DestType operator()(const Null &) const {
throw NullVariable();
}
template <typename T>
- int64_t operator()(const T & r) const {
- return boost::numeric_cast<int64_t>(r);
- }
- private:
- const VariableType * var;
-};
-// Convert to double
-class ConvertVisitorDouble : public boost::static_visitor<double> {
- public:
- ConvertVisitorDouble(const VariableType * v) : var(v) {
- }
- double operator()(const Glib::ustring & r) const {
- return boost::lexical_cast<double>(r);
- }
- double operator()(const boost::shared_ptr<const Glib::ustring> & r) const {
- return boost::lexical_cast<double>(*r);
- }
- double operator()(const boost::posix_time::ptime &) const {
- throw InvalidConversionTo("Floating point");
- }
- double operator()(const boost::shared_ptr<const boost::posix_time::ptime> &) const {
- throw InvalidConversionTo("Floating point");
- }
- double operator()(const Null &) const {
- throw NullVariable();
- }
- template <typename T>
- double operator()(const T & r) const {
- return boost::numeric_cast<double>(r);
+ DestType operator()(const T & r) const {
+ return boost::numeric_cast<DestType>(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<int32_t>(this), *this);
+}
VariableType::operator int64_t() const
{
- return boost::apply_visitor(ConvertVisitorInt(this), *this);
+ return boost::apply_visitor(ConvertVisitor<int64_t>(this), *this);
}
VariableType::operator double() const
{
- return boost::apply_visitor(ConvertVisitorDouble(this), *this);
+ return boost::apply_visitor(ConvertVisitor<double>(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;