summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrandomdan <randomdan@localhost>2013-03-29 21:37:22 +0000
committerrandomdan <randomdan@localhost>2013-03-29 21:37:22 +0000
commit3f47498cc4a4e394108306119bb9a78007bd79ba (patch)
tree3c4c7fceb294b5cd97b9a3324d137d43d163b9ac
parentMore aggregates (diff)
downloadproject2-3f47498cc4a4e394108306119bb9a78007bd79ba.tar.bz2
project2-3f47498cc4a4e394108306119bb9a78007bd79ba.tar.xz
project2-3f47498cc4a4e394108306119bb9a78007bd79ba.zip
Split variable and variabletype code into separate files
Use variabletype in populating options data Add code for guessing the type of some input data
-rw-r--r--project2/cgi/cgiEnvironment.cpp2
-rw-r--r--project2/cgi/cgiEnvironment.h2
-rw-r--r--project2/common/options.cpp9
-rw-r--r--project2/common/options.h36
-rw-r--r--project2/common/pch.hpp1
-rw-r--r--project2/common/variableConvert.cpp4
-rw-r--r--project2/common/variableType.cpp258
-rw-r--r--project2/common/variableType.h105
-rw-r--r--project2/common/variables.cpp244
-rw-r--r--project2/common/variables.h97
-rw-r--r--project2/common/variables/config.cpp4
-rw-r--r--project2/console/consoleEnvironment.cpp2
-rw-r--r--project2/streams/streamNvpRows.cpp3
13 files changed, 401 insertions, 366 deletions
diff --git a/project2/cgi/cgiEnvironment.cpp b/project2/cgi/cgiEnvironment.cpp
index 6462f5c..048f7d7 100644
--- a/project2/cgi/cgiEnvironment.cpp
+++ b/project2/cgi/cgiEnvironment.cpp
@@ -193,7 +193,7 @@ HostnamePlatformIdentifier::paramRequired() const
}
void
-HostnamePlatformIdentifier::consume(const Glib::ustring & p, const Glib::ustring & r) const
+HostnamePlatformIdentifier::consume(const Glib::ustring & p, const VariableType & r) const
{
if (!platform && Glib::Regex::create(r, Glib::REGEX_CASELESS | Glib::REGEX_DOTALL)->match(Environment::getCurrent()->getServerName())) {
platform = new Glib::ustring(p);
diff --git a/project2/cgi/cgiEnvironment.h b/project2/cgi/cgiEnvironment.h
index 176ca8d..c09f5a9 100644
--- a/project2/cgi/cgiEnvironment.h
+++ b/project2/cgi/cgiEnvironment.h
@@ -21,7 +21,7 @@ class HostnamePlatformIdentifier : public Options::Target {
virtual ~HostnamePlatformIdentifier();
void reset() const;
bool paramRequired() const;
- void consume(const Glib::ustring &, const Glib::ustring &) const;
+ void consume(const Glib::ustring &, const VariableType &) const;
const Glib::ustring & derivedPlatform() const;
private:
mutable Glib::ustring * platform;
diff --git a/project2/common/options.cpp b/project2/common/options.cpp
index 94ae861..60fd7cd 100644
--- a/project2/common/options.cpp
+++ b/project2/common/options.cpp
@@ -11,7 +11,7 @@ class NamedOption : public Options::Option {
desc(d)
{
}
- void consume(const Glib::ustring & n, const Glib::ustring & p, const Glib::ustring & v) const {
+ void consume(const Glib::ustring & n, const Glib::ustring & p, const VariableType & v) const {
if (n == id) {
target->consume(p, v);
}
@@ -44,7 +44,7 @@ class OptionAlias : public Options::Option {
target(t)
{
}
- void consume(const Glib::ustring & a, const Glib::ustring & p, const Glib::ustring & v) const {
+ void consume(const Glib::ustring & a, const Glib::ustring & p, const VariableType & v) const {
if (a == alias) {
target->target->consume(p, v);
}
@@ -108,12 +108,13 @@ Options::reset() const
}
void
-Options::consume(const Glib::ustring & n, const Glib::ustring & p, const Glib::ustring & v) const
+Options::consume(const Glib::ustring & n, const Glib::ustring & p, const VariableType & v) const
{
BOOST_FOREACH(const OptionPtr & o, options) {
o->consume(n, p, v);
}
}
+
const Options::Option *
Options::find(const Glib::ustring & n) const {
BOOST_FOREACH(const OptionPtr & o, options) {
@@ -130,7 +131,7 @@ Options::InstanceTarget::InstanceTarget() :
}
void
-Options::InstanceTarget::consume(const Glib::ustring & p, const Glib::ustring & v) const
+Options::InstanceTarget::consume(const Glib::ustring & p, const VariableType & v) const
{
if (ts != Platform && p.empty()) {
assign(v);
diff --git a/project2/common/options.h b/project2/common/options.h
index 93c6aa5..8b9e9a0 100644
--- a/project2/common/options.h
+++ b/project2/common/options.h
@@ -7,6 +7,7 @@
#include <list>
#include <boost/lexical_cast.hpp>
#include <boost/utility/enable_if.hpp>
+#include "variableType.h"
#if !__GNUC_PREREQ(4,7)
// If gcc_version >= 4.0
@@ -41,7 +42,7 @@ class Options {
public:
virtual void reset() const = 0;
virtual bool paramRequired() const = 0;
- virtual void consume(const Glib::ustring & platform, const Glib::ustring & value) const = 0;
+ virtual void consume(const Glib::ustring & platform, const VariableType & value) const = 0;
};
typedef boost::intrusive_ptr<Target> TargetPtr;
@@ -51,9 +52,9 @@ class Options {
bool paramRequired() const {
return true;
}
- void consume(const Glib::ustring & platform, const Glib::ustring & value) const;
+ void consume(const Glib::ustring & platform, const VariableType & value) const;
protected:
- virtual void assign(const Glib::ustring & value) const = 0;
+ virtual void assign(const VariableType & value) const = 0;
mutable TargetState ts;
};
@@ -71,18 +72,27 @@ class Options {
ts = Default;
*target = defValue;
}
- void assign(const Glib::ustring & value) const {
+ void assign(const VariableType & value) const {
doAssign(value);
}
protected:
T * target;
private:
template <typename dummy = int>
- void doAssign(const Glib::ustring & value, typename boost::disable_if<std::is_assignable<T, Glib::ustring>, dummy>::type = 0) const {
- *target = boost::lexical_cast<T>(value);
+ void doAssign(const VariableType & value,
+ typename boost::disable_if<std::is_assignable<T, VariableType>, dummy>::type = 0,
+ typename boost::disable_if<std::is_convertible<VariableType, T>, dummy>::type = 0) const {
+ *target = boost::lexical_cast<T>(value.as<Glib::ustring>());
}
template <typename dummy = int>
- void doAssign(const Glib::ustring & value, typename boost::enable_if<std::is_assignable<T, Glib::ustring>, dummy>::type = 0) const {
+ void doAssign(const VariableType & value,
+ typename boost::enable_if<std::is_convertible<VariableType, T>, dummy>::type = 0) const {
+ *target = value.as<T>();
+ }
+ template <typename dummy = int>
+ void doAssign(const VariableType & value,
+ typename boost::disable_if<std::is_convertible<VariableType, T>, dummy>::type = 0,
+ typename boost::enable_if<std::is_assignable<T, VariableType>, dummy>::type = 0) const {
*target = value;
}
D defValue;
@@ -98,18 +108,18 @@ class Options {
ts = Default;
target->clear();
}
- void assign(const Glib::ustring & value) const {
+ void assign(const VariableType & value) const {
doAssign(value);
}
protected:
VofT * target;
private:
template <typename dummy = int>
- void doAssign(const Glib::ustring & value, typename boost::disable_if<std::is_constructible<T, Glib::ustring>, dummy>::type = 0) const {
- target->push_back(boost::lexical_cast<T>(value));
+ void doAssign(const VariableType & value, typename boost::disable_if<std::is_constructible<T, VariableType>, dummy>::type = 0) const {
+ target->push_back(boost::lexical_cast<T>(value.as<Glib::ustring>()));
}
template <typename dummy = int>
- void doAssign(const Glib::ustring & value, typename boost::enable_if<std::is_constructible<T, Glib::ustring>, dummy>::type = 0) const {
+ void doAssign(const VariableType & value, typename boost::enable_if<std::is_constructible<T, VariableType>, dummy>::type = 0) const {
target->push_back(T(value));
}
};
@@ -121,7 +131,7 @@ class Options {
virtual Glib::ustring name() const = 0;
virtual Glib::ustring description() const = 0;
virtual bool accepts(const Glib::ustring & name) const = 0;
- virtual void consume(const Glib::ustring & name, const Glib::ustring & platform, const Glib::ustring & value) const = 0;
+ virtual void consume(const Glib::ustring & name, const Glib::ustring & platform, const VariableType & value) const = 0;
};
typedef boost::intrusive_ptr<Option> OptionPtr;
@@ -147,7 +157,7 @@ class Options {
}
void reset() const;
- void consume(const Glib::ustring & name, const Glib::ustring & platform, const Glib::ustring & value) const;
+ void consume(const Glib::ustring & name, const Glib::ustring & platform, const VariableType & value) const;
const Option * find(const Glib::ustring & name) const;
const Glib::ustring name;
diff --git a/project2/common/pch.hpp b/project2/common/pch.hpp
index 4150755..da12580 100644
--- a/project2/common/pch.hpp
+++ b/project2/common/pch.hpp
@@ -26,6 +26,7 @@
#include "scriptStorage.h"
#include "scriptLoader.h"
#include "variables.h"
+#include "variableType.h"
#include "sourceObject.h"
#include "scripts.h"
diff --git a/project2/common/variableConvert.cpp b/project2/common/variableConvert.cpp
index 6237c26..d0363f5 100644
--- a/project2/common/variableConvert.cpp
+++ b/project2/common/variableConvert.cpp
@@ -151,8 +151,8 @@ class ConvertVisitorBool : public boost::static_visitor<bool> {
private:
static bool fromStr(const Glib::ustring & s) {
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;
+ if (strcasecmp(str, "true") == 0 || strcasecmp(str, "yes") == 0 || strcasecmp(str, "on") == 0 || strcasecmp(str, "1") == 0) return true;
+ if (strcasecmp(str, "false") == 0 || strcasecmp(str, "no") == 0 || strcasecmp(str, "off") == 0 || strcasecmp(str, "0") == 0) return false;
throw InvalidConversionTo("bool", s);
}
const VariableType * var;
diff --git a/project2/common/variableType.cpp b/project2/common/variableType.cpp
new file mode 100644
index 0000000..710296a
--- /dev/null
+++ b/project2/common/variableType.cpp
@@ -0,0 +1,258 @@
+#include <pch.hpp>
+#include "variableType.h"
+#include "logger.h"
+#include <boost/date_time/posix_time/posix_time.hpp>
+#include <boost/algorithm/string/predicate.hpp>
+
+SimpleMessageException(UnknownVariableType);
+
+Boolean::Boolean(bool v) : value(v)
+{
+}
+
+bool
+Boolean::operator<(const Boolean & b) const
+{
+ return ((this->value == false) && (b.value == true));
+}
+
+Boolean::operator bool() const
+{
+ return this->value;
+}
+
+std::basic_ostream<char> &
+operator<<(std::basic_ostream<char> & os, const Boolean & b)
+{
+ os << (b.value ? "true" : "false");
+ return os;
+}
+
+std::basic_ostream<unsigned char> &
+operator<<(std::basic_ostream<unsigned char> & os, const Boolean & b)
+{
+ os << (b.value ? "true" : "false");
+ return os;
+}
+
+VT_typeID
+VariableType::getTypeFromName(const std::string & src)
+{
+ if (src.empty()) return String;
+ if (src == "string") return String;
+ if (src == "bool") return Bool;
+ if (src == "int") return Int;
+ if (src == "float") return Float;
+ if (src == "datetime") return DateTime;
+ if (src == "null") return Nul;
+ throw UnknownVariableType(src);
+}
+
+VariableType
+VariableType::make(const Glib::ustring & src, const VT_typeID format)
+{
+ VariableType vt(src);
+ vt.convertCache = src;
+ switch (format) {
+ default:
+ case DefaultType:
+ case String:
+ return vt;
+ case Nul:
+ return Null();
+ case Bool:
+ return vt.as<bool>();
+ case Int:
+ return vt.as<int64_t>();
+ case Float:
+ return vt.as<double>();
+ case DateTime:
+ return vt.as<boost::posix_time::ptime>();
+ }
+}
+
+VariableType
+VariableType::make(const Glib::ustring & src)
+{
+ if (src.empty()) {
+ return Null();
+ }
+ else if (boost::algorithm::all(src, g_unichar_isdigit)) {
+ return VariableType(src).as<int64_t>();
+ }
+ else if (boost::algorithm::all(src, [](gunichar c){ return c == '.' || g_unichar_isdigit(c); })) {
+ return VariableType(src).as<double>();
+ }
+ else {
+ try {
+ return VariableType(src).as<bool>();
+ }
+ catch (...) {
+ try {
+ return VariableType(src).as<boost::posix_time::ptime>();
+ }
+ catch (...) {
+ return src;
+ }
+ }
+ }
+}
+
+VariableType::VariableType()
+{
+}
+
+#define VTCONS(Source) \
+VariableType::VariableType(const Source & t) : \
+ _VT(t) \
+{ \
+}
+#define VTCONSD(Source, Store) \
+VariableType::VariableType(const Source & t) : \
+ _VT(Store(t)) \
+{ \
+}
+#define VTCONSC(Source, Store, Cast) \
+VariableType::VariableType(const Source & t) : \
+ _VT(Store((Cast)t)) \
+{ \
+}
+#define VTCONSN(Source, Store) \
+VariableType::VariableType(const Source & t) : \
+ _VT(boost::numeric_cast<Store>(t)) \
+{ \
+}
+
+VTCONS(Null);
+VTCONS(boost::posix_time::ptime);
+VTCONS(Glib::ustring);
+VTCONSD(bool, Boolean);
+VTCONSN(float, double);
+VTCONSN(double, double);
+VTCONSN(int, int64_t);
+VTCONSN(unsigned int, int64_t);
+VTCONSN(long int, int64_t);
+VTCONSN(long unsigned int, int64_t);
+VTCONSN(long long int, int64_t);
+VTCONSN(long long unsigned int, int64_t);
+VTCONSD(std::string, Glib::ustring);
+VTCONSD(char * const, Glib::ustring);
+VTCONSC(unsigned char * const, Glib::ustring, const char * const);
+
+VariableType::VariableType(const struct tm & vt) :
+ _VT(boost::posix_time::ptime_from_tm(vt))
+{
+}
+
+VariableType::VariableType(const VariableType & vt) :
+ _VT(*((const _VT *)&vt)),
+ convertCache(vt.convertCache)
+{
+}
+
+template <class S, typename Op>
+class compi : public boost::static_visitor<bool> {
+ public:
+ compi(const S & s) : _s(s) { }
+ bool operator()(const S & t) const
+ {
+ return Op()(t, _s);
+ }
+ template <class T>
+ bool operator()(const T &) const
+ {
+ // should never be called
+ throw std::logic_error("Shouldn't ever be comparing variables of different type");
+ }
+ private:
+ const S & _s;
+};
+
+template <template <class T> class Op>
+class comp : public boost::static_visitor<bool> {
+ public:
+ comp(const VariableType & a) : _a(a) { }
+ template <class T>
+ bool operator()(const T & t) const
+ {
+ return boost::apply_visitor(compi<T, Op<T>>(t), _a);
+ }
+ private:
+ const VariableType & _a;
+};
+
+bool
+VariableType::operator<(const VariableType & b) const
+{
+ if (this->which() < b.which()) {
+ return true;
+ }
+ if (this->which() == b.which()) {
+ return boost::apply_visitor(comp<std::less>(*this), b);
+ }
+ return false;
+}
+
+bool
+VariableType::operator==(const VariableType & b) const
+{
+ if (this->which() == b.which()) {
+ return boost::apply_visitor(comp<std::equal_to>(*this), b);
+ }
+ return false;
+}
+
+bool
+VariableType::operator<=(const VariableType & b) const
+{
+ if (this->which() < b.which()) {
+ return true;
+ }
+ if (this->which() == b.which()) {
+ return boost::apply_visitor(comp<std::less_equal>(*this), b);
+ }
+ return false;
+}
+
+bool
+VariableType::operator>(const VariableType & b) const
+{
+ if (this->which() > b.which()) {
+ return true;
+ }
+ if (this->which() == b.which()) {
+ return boost::apply_visitor(comp<std::greater>(*this), b);
+ }
+ return false;
+}
+
+bool
+VariableType::operator>=(const VariableType & b) const
+{
+ if (this->which() > b.which()) {
+ return true;
+ }
+ if (this->which() == b.which()) {
+ return boost::apply_visitor(comp<std::greater_equal>(*this), b);
+ }
+ return false;
+}
+
+bool
+VariableType::operator!=(const VariableType & b) const
+{
+ if (this->which() != b.which()) {
+ return true;
+ }
+ if (this->which() == b.which()) {
+ return boost::apply_visitor(comp<std::not_equal_to>(*this), b);
+ }
+ return false;
+}
+
+bool
+VariableType::isNull() const
+{
+ return (get<Null>());
+}
+
diff --git a/project2/common/variableType.h b/project2/common/variableType.h
new file mode 100644
index 0000000..21c2c39
--- /dev/null
+++ b/project2/common/variableType.h
@@ -0,0 +1,105 @@
+#ifndef VARIABLETYPE_H
+#define VARIABLETYPE_H
+
+#include <boost/any.hpp>
+#include <boost/date_time/posix_time/posix_time_types.hpp>
+#include <glibmm/ustring.h>
+#include <boost/variant.hpp>
+#include <boost/optional.hpp>
+
+enum VT_typeID {
+ DefaultType,
+ Nul,
+ String,
+ Bool,
+ Int,
+ Float,
+ DateTime,
+};
+
+class Null {
+ public:
+ bool operator<(const Null &) const { return false; }
+ bool operator<=(const Null &) const { return false; }
+ bool operator>(const Null &) const { return false; }
+ bool operator>=(const Null &) const { return false; }
+ bool operator!=(const Null &) const { return false; }
+ bool operator==(const Null &) const { return true; }
+};
+
+class Boolean {
+ public:
+ Boolean(bool v);
+ bool operator<(const Boolean &) const;
+ operator bool() const;
+ bool value;
+};
+
+std::basic_ostream<char> & operator<<(std::basic_ostream<char> &, const Boolean &);
+std::basic_ostream<unsigned char> & operator<<(std::basic_ostream<unsigned char> &, const Boolean &);
+
+typedef boost::variant<
+ // Other
+ Null,
+ Boolean,
+ // Strings
+ Glib::ustring,
+ // Numbers
+ int64_t,
+ double,
+ // Date and Times
+ boost::posix_time::ptime
+ > _VT;
+
+class VariableType : public _VT {
+ public:
+ VariableType(const unsigned char * const & t);
+ VariableType(const char * const & t);
+ VariableType(const std::string &);
+ VariableType(const Glib::ustring &);
+ VariableType(const struct tm &);
+ VariableType(const boost::posix_time::ptime &);
+ VariableType(const Null &);
+ VariableType(const bool &);
+ VariableType(const int &);
+ VariableType(const unsigned int &);
+ VariableType(const long int &);
+ VariableType(const long unsigned int &);
+ VariableType(const long long int &);
+ VariableType(const long long unsigned int &);
+ VariableType(const float &);
+ VariableType(const double &);
+ VariableType();
+ VariableType(const VariableType &);
+
+ static VariableType make(const Glib::ustring & src, VT_typeID format);
+ static VariableType make(const Glib::ustring & src);
+ static VT_typeID getTypeFromName(const std::string & src);
+
+ bool operator<(const VariableType &) const;
+ bool operator<=(const VariableType &) const;
+ bool operator>(const VariableType &) const;
+ bool operator>=(const VariableType &) const;
+ bool operator==(const VariableType &) const;
+ bool operator!=(const VariableType &) const;
+
+ operator const Glib::ustring &() const;
+ operator const std::string &() const;
+ 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;
+ template <typename T> operator boost::optional<T>() const { return isNull() ? boost::optional<T>() : as<T>(); }
+ operator bool() const;
+ bool isNull() const;
+ template <typename T> T as() const { return *this; }
+ template <typename T> const T * get() const { return boost::get<T>(this); }
+
+ typedef boost::any ConvertCache;
+ mutable ConvertCache convertCache;
+};
+
+#endif
+
diff --git a/project2/common/variables.cpp b/project2/common/variables.cpp
index 7dcf243..6f62dc1 100644
--- a/project2/common/variables.cpp
+++ b/project2/common/variables.cpp
@@ -14,250 +14,6 @@
#include <boost/algorithm/string/predicate.hpp>
#include <boost/function.hpp>
#include <boost/bind.hpp>
-#include <boost/date_time/posix_time/posix_time.hpp>
-
-SimpleMessageException(UnknownVariableType);
-
-Boolean::Boolean(bool v) : value(v)
-{
-}
-
-bool
-Boolean::operator<(const Boolean & b) const
-{
- return ((this->value == false) && (b.value == true));
-}
-
-Boolean::operator bool() const
-{
- return this->value;
-}
-
-std::basic_ostream<char> &
-operator<<(std::basic_ostream<char> & os, const Boolean & b)
-{
- os << (b.value ? "true" : "false");
- return os;
-}
-
-std::basic_ostream<unsigned char> &
-operator<<(std::basic_ostream<unsigned char> & os, const Boolean & b)
-{
- os << (b.value ? "true" : "false");
- return os;
-}
-
-VT_typeID
-VariableType::getTypeFromName(const std::string & src)
-{
- if (src.empty()) return String;
- if (src == "string") return String;
- if (src == "bool") return Bool;
- if (src == "int") return Int;
- if (src == "float") return Float;
- if (src == "datetime") return DateTime;
- if (src == "null") return Nul;
- throw UnknownVariableType(src);
-}
-
-SimpleMessageException(DateParseError);
-VariableType
-VariableType::make(const Glib::ustring & src, const VT_typeID format)
-{
- VariableType vt;
- switch (format) {
- default:
- case DefaultType:
- case String:
- vt = src;
- return vt;
- case Nul:
- return Null();
- case Bool:
- vt = boost::lexical_cast<bool>(src);
- break;
- case Int:
- vt = boost::lexical_cast<int64_t>(src);
- break;
- case Float:
- vt = boost::lexical_cast<double>(src);
- break;
- case DateTime:
- {
- struct tm tm;
- memset(&tm, 0, sizeof(struct tm));
- mktime(&tm);
- const char * e = strptime(src.c_str(), "%FT%T", &tm);
- if (!e || *e) {
- Logger()->messagef(LOG_ERR, "%s: check failed (parse) for '%s' against '%s' (remaining chars='%s')",
- __PRETTY_FUNCTION__, src.c_str(), "%FT%T", e);
- throw DateParseError(src);
- }
- vt = boost::posix_time::ptime_from_tm(tm);
- }
- break;
- }
- vt.convertCache = src;
- return vt;
-}
-
-VariableType::VariableType()
-{
-}
-
-#define VTCONS(Source) \
-VariableType::VariableType(const Source & t) : \
- _VT(t) \
-{ \
-}
-#define VTCONSD(Source, Store) \
-VariableType::VariableType(const Source & t) : \
- _VT(Store(t)) \
-{ \
-}
-#define VTCONSC(Source, Store, Cast) \
-VariableType::VariableType(const Source & t) : \
- _VT(Store((Cast)t)) \
-{ \
-}
-#define VTCONSN(Source, Store) \
-VariableType::VariableType(const Source & t) : \
- _VT(boost::numeric_cast<Store>(t)) \
-{ \
-}
-
-VTCONS(Null);
-VTCONS(boost::posix_time::ptime);
-VTCONS(Glib::ustring);
-VTCONSD(bool, Boolean);
-VTCONSN(float, double);
-VTCONSN(double, double);
-VTCONSN(int, int64_t);
-VTCONSN(unsigned int, int64_t);
-VTCONSN(long int, int64_t);
-VTCONSN(long unsigned int, int64_t);
-VTCONSN(long long int, int64_t);
-VTCONSN(long long unsigned int, int64_t);
-VTCONSD(std::string, Glib::ustring);
-VTCONSD(char * const, Glib::ustring);
-VTCONSC(unsigned char * const, Glib::ustring, const char * const);
-
-VariableType::VariableType(const struct tm & vt) :
- _VT(boost::posix_time::ptime_from_tm(vt))
-{
-}
-
-VariableType::VariableType(const VariableType & vt) :
- _VT(*((const _VT *)&vt)),
- convertCache(vt.convertCache)
-{
-}
-
-template <class S, typename Op>
-class compi : public boost::static_visitor<bool> {
- public:
- compi(const S & s) : _s(s) { }
- bool operator()(const S & t) const
- {
- return Op()(t, _s);
- }
- template <class T>
- bool operator()(const T &) const
- {
- // should never be called
- throw std::logic_error("Shouldn't ever be comparing variables of different type");
- }
- private:
- const S & _s;
-};
-
-template <template <class T> class Op>
-class comp : public boost::static_visitor<bool> {
- public:
- comp(const VariableType & a) : _a(a) { }
- template <class T>
- bool operator()(const T & t) const
- {
- return boost::apply_visitor(compi<T, Op<T>>(t), _a);
- }
- private:
- const VariableType & _a;
-};
-
-bool
-VariableType::operator<(const VariableType & b) const
-{
- if (this->which() < b.which()) {
- return true;
- }
- if (this->which() == b.which()) {
- return boost::apply_visitor(comp<std::less>(*this), b);
- }
- return false;
-}
-
-bool
-VariableType::operator==(const VariableType & b) const
-{
- if (this->which() == b.which()) {
- return boost::apply_visitor(comp<std::equal_to>(*this), b);
- }
- return false;
-}
-
-bool
-VariableType::operator<=(const VariableType & b) const
-{
- if (this->which() < b.which()) {
- return true;
- }
- if (this->which() == b.which()) {
- return boost::apply_visitor(comp<std::less_equal>(*this), b);
- }
- return false;
-}
-
-bool
-VariableType::operator>(const VariableType & b) const
-{
- if (this->which() > b.which()) {
- return true;
- }
- if (this->which() == b.which()) {
- return boost::apply_visitor(comp<std::greater>(*this), b);
- }
- return false;
-}
-
-bool
-VariableType::operator>=(const VariableType & b) const
-{
- if (this->which() > b.which()) {
- return true;
- }
- if (this->which() == b.which()) {
- return boost::apply_visitor(comp<std::greater_equal>(*this), b);
- }
- return false;
-}
-
-bool
-VariableType::operator!=(const VariableType & b) const
-{
- if (this->which() != b.which()) {
- return true;
- }
- if (this->which() == b.which()) {
- return boost::apply_visitor(comp<std::not_equal_to>(*this), b);
- }
- return false;
-}
-
-bool
-VariableType::isNull() const
-{
- return (get<Null>());
-}
VariableImplDyn::VariableImplDyn(ScriptNodePtr e)
{
diff --git a/project2/common/variables.h b/project2/common/variables.h
index 68923b5..dbb4c34 100644
--- a/project2/common/variables.h
+++ b/project2/common/variables.h
@@ -3,109 +3,14 @@
#include <boost/intrusive_ptr.hpp>
#include <boost/optional.hpp>
-#include <boost/any.hpp>
-#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <stdint.h>
-#include <glibmm/ustring.h>
#include "intrusivePtrBase.h"
#include "scriptLoader.h"
-#include <boost/variant.hpp>
+#include "variableType.h"
#include <boost/shared_ptr.hpp>
SimpleMessageException(UnknownVariableSource);
-enum VT_typeID {
- DefaultType,
- Nul,
- String,
- Bool,
- Int,
- Float,
- DateTime,
-};
-
-class Null {
- public:
- bool operator<(const Null &) const { return false; }
- bool operator<=(const Null &) const { return false; }
- bool operator>(const Null &) const { return false; }
- bool operator>=(const Null &) const { return false; }
- bool operator!=(const Null &) const { return false; }
- bool operator==(const Null &) const { return true; }
-};
-
-class Boolean {
- public:
- Boolean(bool v);
- bool operator<(const Boolean &) const;
- operator bool() const;
- bool value;
-};
-
-std::basic_ostream<char> & operator<<(std::basic_ostream<char> &, const Boolean &);
-std::basic_ostream<unsigned char> & operator<<(std::basic_ostream<unsigned char> &, const Boolean &);
-
-typedef boost::variant<
- // Other
- Null,
- Boolean,
- // Strings
- Glib::ustring,
- // Numbers
- int64_t,
- double,
- // Date and Times
- boost::posix_time::ptime
- > _VT;
-
-class VariableType : public _VT {
- public:
- VariableType(const unsigned char * const & t);
- VariableType(const char * const & t);
- VariableType(const std::string &);
- VariableType(const Glib::ustring &);
- VariableType(const struct tm &);
- VariableType(const boost::posix_time::ptime &);
- VariableType(const Null &);
- VariableType(const bool &);
- VariableType(const int &);
- VariableType(const unsigned int &);
- VariableType(const long int &);
- VariableType(const long unsigned int &);
- VariableType(const long long int &);
- VariableType(const long long unsigned int &);
- VariableType(const float &);
- VariableType(const double &);
- VariableType();
- VariableType(const VariableType &);
-
- static VariableType make(const Glib::ustring & src, const VT_typeID format = DefaultType);
- static VT_typeID getTypeFromName(const std::string & src);
-
- bool operator<(const VariableType &) const;
- bool operator<=(const VariableType &) const;
- bool operator>(const VariableType &) const;
- bool operator>=(const VariableType &) const;
- bool operator==(const VariableType &) const;
- bool operator!=(const VariableType &) const;
-
- operator const Glib::ustring &() const;
- operator const std::string &() const;
- 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;
- operator bool() const;
- bool isNull() const;
- template <typename T> T as() const { return *this; }
- template <typename T> const T * get() const { return boost::get<T>(this); }
-
- typedef boost::any ConvertCache;
- mutable ConvertCache convertCache;
-};
-
/// Base class for Project2 variable accessors
class VariableImpl : public IntrusivePtrBase {
public:
diff --git a/project2/common/variables/config.cpp b/project2/common/variables/config.cpp
index 67014c4..51759d5 100644
--- a/project2/common/variables/config.cpp
+++ b/project2/common/variables/config.cpp
@@ -5,7 +5,7 @@
#include <boost/algorithm/string/predicate.hpp>
#include "../appEngine.h"
-typedef std::map<Glib::ustring, Glib::ustring> ConfigOptions;
+typedef std::map<Glib::ustring, VariableType> ConfigOptions;
static ConfigOptions cfgOpts;
SimpleMessageException(NoSuchConfigurationValue);
@@ -40,7 +40,7 @@ class VariableConfigLoader : public VariableLoader::For<VariableConfig> {
void reset() const {
cfgOpts.clear();
}
- void consume(const Glib::ustring & n, const Glib::ustring & p, const Glib::ustring & v) const {
+ void consume(const Glib::ustring & n, const Glib::ustring & p, const VariableType & v) const {
if (boost::algorithm::starts_with(n, "application.")) {
Glib::ustring k(n.substr(12));
const ConfigOptions::iterator i = cfgOpts.find(k);
diff --git a/project2/console/consoleEnvironment.cpp b/project2/console/consoleEnvironment.cpp
index 09f4572..3fcaf7d 100644
--- a/project2/console/consoleEnvironment.cpp
+++ b/project2/console/consoleEnvironment.cpp
@@ -32,7 +32,7 @@ class ShowHelpTrigger : public Options::Target {
bool paramRequired() const {
return false;
}
- void consume(const Glib::ustring &, const Glib::ustring &) const {
+ void consume(const Glib::ustring &, const VariableType &) const {
fprintf(stdout, "Help\n");
exit(1);
}
diff --git a/project2/streams/streamNvpRows.cpp b/project2/streams/streamNvpRows.cpp
index 895826a..2577ffa 100644
--- a/project2/streams/streamNvpRows.cpp
+++ b/project2/streams/streamNvpRows.cpp
@@ -115,8 +115,7 @@ class StreamNvpRows : public RowSet {
}
bool newValue(size_t start) {
- fields.back() =
- boost::algorithm::trim_copy_if(tok.substr(0, start), g_unichar_isspace);
+ fields.back() = VariableType::make(boost::algorithm::trim_copy_if(tok.substr(0, start), g_unichar_isspace));
inValue = false;
return true;
}