summaryrefslogtreecommitdiff
path: root/project2/common/variableConvert.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'project2/common/variableConvert.cpp')
-rw-r--r--project2/common/variableConvert.cpp33
1 files changed, 32 insertions, 1 deletions
diff --git a/project2/common/variableConvert.cpp b/project2/common/variableConvert.cpp
index f0e42c9..7eb16cf 100644
--- a/project2/common/variableConvert.cpp
+++ b/project2/common/variableConvert.cpp
@@ -121,7 +121,7 @@ class ConvertVisitorUCharStar : public boost::static_visitor<const unsigned char
private:
const VariableType * var;
};
-// Convert to generic type
+// Convert to numeric type
template <typename DestType>
class ConvertVisitor : public boost::static_visitor<DestType> {
public:
@@ -136,6 +136,9 @@ class ConvertVisitor : public boost::static_visitor<DestType> {
DestType operator()(const Null &) const {
throw NullVariable();
}
+ DestType operator()(const Boolean & b) const {
+ return b.value ? 1 : 0;
+ }
template <typename T>
DestType operator()(const T & r) const {
return boost::numeric_cast<DestType>(r);
@@ -164,6 +167,30 @@ class ConvertVisitorDateTime : public boost::static_visitor<const boost::posix_t
private:
const VariableType * var;
};
+// Convert to boolean
+class ConvertVisitorBool : public boost::static_visitor<bool> {
+ public:
+ ConvertVisitorBool(const VariableType * v) : var(v) {
+ }
+ bool operator()(const Glib::ustring & s) const {
+ const char * str = s.c_str();
+ if (strcasecmp(str, "true") == 0 || strcasecmp(str, "yes") == 0 || strcasecmp(str, "on") == 0) return true;
+ if (strcasecmp(str, "false") == 0 || strcasecmp(str, "no") == 0 || strcasecmp(str, "off") == 0) return false;
+ throw InvalidConversionTo("bool");
+ }
+ bool operator()(const boost::posix_time::ptime &) const {
+ throw InvalidConversionTo("bool");
+ }
+ bool operator()(const Null &) const {
+ throw NullVariable();
+ }
+ template <typename T>
+ bool operator()(const T & t) const {
+ return (t != 0);
+ }
+ private:
+ const VariableType * var;
+};
VariableType::operator const Glib::ustring &() const
{
return boost::apply_visitor(ConvertVisitorGlibUstring(this), *this);
@@ -196,3 +223,7 @@ VariableType::operator const boost::posix_time::ptime &() const
{
return boost::apply_visitor(ConvertVisitorDateTime(this), *this);
}
+VariableType::operator bool() const
+{
+ return boost::apply_visitor(ConvertVisitorBool(this), *this);
+}