summaryrefslogtreecommitdiff
path: root/project2/variables.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'project2/variables.cpp')
-rw-r--r--project2/variables.cpp58
1 files changed, 23 insertions, 35 deletions
diff --git a/project2/variables.cpp b/project2/variables.cpp
index 5c4508f..c8b2571 100644
--- a/project2/variables.cpp
+++ b/project2/variables.cpp
@@ -3,37 +3,39 @@
#include "appEngine.h"
#include "session.h"
#include "rowUser.h"
+#include "genericVisitor.h"
#include <libxml++/nodes/textnode.h>
#include <stdexcept>
#include <syslog.h>
#include <boost/tokenizer.hpp>
#include <boost/foreach.hpp>
#include <boost/algorithm/string/predicate.hpp>
-#include <boost/lexical_cast.hpp>
#include <boost/function.hpp>
#include <boost/bind.hpp>
class VariableLiteral : public VariableImpl {
public:
- VariableLiteral(const Glib::ustring & src) : VariableImpl(src) { }
+ VariableLiteral(const Glib::ustring & src) : VariableImpl(src), val(src) { }
- virtual const Glib::ustring & value() const { return source; }
+ virtual const VariableType & value() const { return val; }
+ private:
+ VariableType val;
};
class VariableImplDyn : public VariableImpl {
public:
VariableImplDyn(const Glib::ustring & src) : VariableImpl(src), cacheValid(false) { }
- virtual const Glib::ustring & value() const = 0;
+ virtual const VariableType & value() const = 0;
protected:
- mutable Glib::ustring cache;
+ mutable VariableType cache;
mutable bool cacheValid;
};
class VariableSession : public VariableImplDyn {
public:
VariableSession(const Glib::ustring & src) : VariableImplDyn(src) { }
- const Glib::ustring & value() const
+ const VariableType & value() const
{
try {
cache = ApplicationEngine::getCurrent()->session()->GetValue(name);
@@ -51,7 +53,7 @@ class VariableSession : public VariableImplDyn {
class VariableParam : public VariableImplDyn {
public:
VariableParam(const Glib::ustring & src) : VariableImplDyn(src) { }
- const Glib::ustring & value() const
+ const VariableType & value() const
{
if (!cacheValid) {
try {
@@ -72,7 +74,7 @@ class VariableParam : public VariableImplDyn {
class VariableUri : public VariableImplDyn {
public:
VariableUri(const Glib::ustring & src) : VariableImplDyn(src) { }
- const Glib::ustring & value() const
+ const VariableType & value() const
{
if (!cacheValid) {
try {
@@ -90,18 +92,7 @@ class VariableUri : public VariableImplDyn {
}
};
-template <class T>
-static void assignHelper(Glib::ustring & dest, const T & src) {
- const Glib::ustring * srcp = boost::get<const Glib::ustring>(&src);
- if (srcp) {
- dest = *srcp;
- }
- else {
- dest = boost::lexical_cast<Glib::ustring>(src);
- }
-}
-template <>
-void assignHelper(Glib::ustring & dest, const Glib::ustring & src) {
+static void assignHelper(VariableType & dest, const VariableType & src) {
dest = src;
}
@@ -132,7 +123,7 @@ class VariableParent : public VariableImplDyn, public RowUser {
}
}
}
- const Glib::ustring & value() const
+ const VariableType & value() const
{
if (!cacheValid) {
try {
@@ -175,11 +166,11 @@ class VariableParent : public VariableImplDyn, public RowUser {
void bind() const
{
if (attr) {
- getValue = boost::bind(&assignHelper<VariableType>, _1, boost::bind(row->resolveAttr(name)));
+ getValue = boost::bind(&assignHelper, _1, boost::bind(row->resolveAttr(name)));
}
else {
- typedef const Glib::ustring & (RowSet::*gCV)(const Glib::ustring &) const;
- getValue = boost::bind(&assignHelper<Glib::ustring>, _1,
+ typedef VariableType (RowSet::*gCV)(const Glib::ustring &) const;
+ getValue = boost::bind(&assignHelper, _1,
boost::bind((gCV)&RowSet::getCurrentValue, _2, name));
}
}
@@ -187,7 +178,7 @@ class VariableParent : public VariableImplDyn, public RowUser {
const size_t depth;
const bool attr;
const RowUser * dep;
- mutable boost::function2<void, Glib::ustring &, ConstRowSetPtr> getValue;
+ mutable boost::function2<void, VariableType &, ConstRowSetPtr> getValue;
};
class VariableParse : public VariableImplDyn, public RowUser {
@@ -200,21 +191,18 @@ class VariableParse : public VariableImplDyn, public RowUser {
vars.push_back(Variable::create(t, this));
}
}
- const Glib::ustring & value() const
+ const VariableType & value() const
{
if (!cacheValid) {
- size_t len = 0;
- BOOST_FOREACH(Variable::VariableImplPtr vip, vars) {
- len += vip->value().length() + 1;
- }
- cache.clear();
- cache.reserve(len);
+ Glib::ustring newCache;
BOOST_FOREACH(Variable::VariableImplPtr v, vars) {
- if (cache.length()) {
- cache.append(" ");
+ if (newCache.length()) {
+ newCache.append(" ");
}
- cache.append(v->value());
+ typedef Glib::ustring & (Glib::ustring::*appender)(const Glib::ustring &);
+ LexicalCall<Glib::ustring, Glib::ustring &>(boost::bind((appender)&Glib::ustring::append, newCache, _1), v->value());
}
+ cache = newCache;
cacheValid = true;
}
return cache;