From 5f04b66d9592b18d37764874653adfaad47e2f19 Mon Sep 17 00:00:00 2001 From: randomdan Date: Fri, 22 Jul 2011 15:55:02 +0000 Subject: Remove the pointer versions of variable type members Add a new variable that looks up it's value in a map populated from a standard rowset --- project2/fileStrmVarWriter.cpp | 8 --- project2/fileStrmVarWriter.h | 2 - project2/sqlRows.cpp | 4 +- project2/sqlVariableBinder.cpp | 12 ----- project2/sqlVariableBinder.h | 2 - project2/streamRows.cpp | 28 +++++----- project2/streamRows.h | 3 +- project2/variableConvert.cpp | 37 ------------- project2/variables.cpp | 115 ++++++++++++++++++++++++++++++++++++++--- project2/variables.h | 4 +- project2/xslRows.cpp | 8 +-- 11 files changed, 129 insertions(+), 94 deletions(-) diff --git a/project2/fileStrmVarWriter.cpp b/project2/fileStrmVarWriter.cpp index 564c86a..1af99dc 100644 --- a/project2/fileStrmVarWriter.cpp +++ b/project2/fileStrmVarWriter.cpp @@ -53,15 +53,7 @@ void FileStreamVariableWriter::operator()(const Glib::ustring & i) const { // Care much? None of the others check. } } -void FileStreamVariableWriter::operator()(const boost::shared_ptr & i) const { - if (fwrite(i->c_str(), i->bytes(), 1, out) < 1) { - // Care much? None of the others check. - } -} void FileStreamVariableWriter::operator()(const boost::posix_time::ptime & i) const { fprintf(out, "[%s]", boost::posix_time::to_iso_extended_string(i).c_str()); } -void FileStreamVariableWriter::operator()(const boost::shared_ptr & i) const { - fprintf(out, "[%s]", boost::posix_time::to_iso_extended_string(*i).c_str()); -} diff --git a/project2/fileStrmVarWriter.h b/project2/fileStrmVarWriter.h index 6b0a258..b98691b 100644 --- a/project2/fileStrmVarWriter.h +++ b/project2/fileStrmVarWriter.h @@ -20,9 +20,7 @@ class FileStreamVariableWriter : public boost::static_visitor<> { void operator()(const float & i) const; void operator()(const double & i) const; void operator()(const Glib::ustring & i) const; - void operator()(const boost::shared_ptr & i) const; void operator()(const boost::posix_time::ptime & i) const; - void operator()(const boost::shared_ptr & i) const; private: FILE * out; diff --git a/project2/sqlRows.cpp b/project2/sqlRows.cpp index 920b5a6..2b8745f 100644 --- a/project2/sqlRows.cpp +++ b/project2/sqlRows.cpp @@ -48,7 +48,7 @@ class HandleAsVariableType : public DB::HandleField { variable = Null(); } void string(const char * c, size_t l) { - variable = boost::shared_ptr(new Glib::ustring(c, c + l)); + variable = Glib::ustring(c, c + l); } void integer(int64_t i) { variable = i; @@ -57,7 +57,7 @@ class HandleAsVariableType : public DB::HandleField { variable = d; } void timestamp(const struct tm & t) { - variable = boost::shared_ptr(new boost::posix_time::ptime(boost::posix_time::ptime_from_tm(t))); + variable = boost::posix_time::ptime(boost::posix_time::ptime_from_tm(t)); } VariableType variable; }; diff --git a/project2/sqlVariableBinder.cpp b/project2/sqlVariableBinder.cpp index 02528d3..23c24f2 100644 --- a/project2/sqlVariableBinder.cpp +++ b/project2/sqlVariableBinder.cpp @@ -19,12 +19,6 @@ SqlVariableBinder::operator()(const Glib::ustring & i) const cmd->bindParamS(idx, i); } void -SqlVariableBinder::operator()(const boost::shared_ptr & i) const -{ - if (!i) return cmd->bindNull(idx); - cmd->bindParamS(idx, *i); -} -void SqlVariableBinder::operator()(const long long unsigned int & i) const { cmd->bindParamI(idx, i); @@ -80,10 +74,4 @@ SqlVariableBinder::operator()(const boost::posix_time::ptime & i) const struct tm tm(boost::posix_time::to_tm(i)); cmd->bindParamT(idx, &tm); } -void -SqlVariableBinder::operator()(const boost::shared_ptr & i) const -{ - if (!i) return cmd->bindNull(idx); - (*this)(*i); -} diff --git a/project2/sqlVariableBinder.h b/project2/sqlVariableBinder.h index 42a4567..df3879a 100644 --- a/project2/sqlVariableBinder.h +++ b/project2/sqlVariableBinder.h @@ -15,7 +15,6 @@ class SqlVariableBinder : public boost::static_visitor<> { SqlVariableBinder(DB::Command * c, unsigned int i); void operator()(const Null & i) const; void operator()(const Glib::ustring & i) const; - void operator()(const boost::shared_ptr & i) const; void operator()(const long long unsigned int & i) const; void operator()(const long unsigned int & i) const; void operator()(const unsigned int & i) const; @@ -27,7 +26,6 @@ class SqlVariableBinder : public boost::static_visitor<> { void operator()(const double & i) const; void operator()(const float & i) const; void operator()(const boost::posix_time::ptime & i) const; - void operator()(const boost::shared_ptr & i) const; private: DB::Command * cmd; diff --git a/project2/streamRows.cpp b/project2/streamRows.cpp index 4cf29e6..825dc60 100644 --- a/project2/streamRows.cpp +++ b/project2/streamRows.cpp @@ -39,20 +39,20 @@ void StreamRows::begin() const { curCol = columns.get().begin(); - tok = StringPtr(new Glib::ustring()); + tok.clear(); } void StreamRows::pushChar(gunichar c, const RowProcessor * rp) const { - if ((!inQuotes) && (c == *newline.rbegin()) && (tok->compare(tok->length() - newlin.length(), newlin.length(), newlin) == 0)) { + if ((!inQuotes) && (c == *newline.rbegin()) && (tok.compare(tok.length() - newlin.length(), newlin.length(), newlin) == 0)) { if (skipheader) { skipheader -= 1; } else { - tok->erase(tok->length() - newlin.length()); + tok.erase(tok.length() - newlin.length()); if (!mkCols) { - if (!tok->empty()) { + if (!tok.empty()) { *curCol++ = VariableType(tok); } if (keepBlankRows || curCol != columns.get().begin()) { @@ -67,18 +67,18 @@ StreamRows::pushChar(gunichar c, const RowProcessor * rp) const } } else { - if (!tok->empty()) { - addColumn(*tok); + if (!tok.empty()) { + addColumn(tok); } mkCols = false; } curCol = columns.get().begin(); } - tok = StringPtr(new Glib::ustring()); + tok.clear(); } else if (c == quoteChar) { if (prevWasQuote) { - *tok += c; + tok += c; prevWasQuote = false; inQuotes = !inQuotes; } @@ -91,27 +91,27 @@ StreamRows::pushChar(gunichar c, const RowProcessor * rp) const prevWasQuote = false; if (skipheader == 0) { if (mkCols) { - addColumn(*tok); + addColumn(tok); } else { *curCol++ = VariableType(tok); } } - tok = StringPtr(new Glib::ustring()); + tok.clear(); } else { prevWasQuote = false; - *tok += c; + tok += c; } } void StreamRows::end(const RowProcessor * rp) const { - if (!tok->empty()) { + if (!tok.empty()) { if (skipheader == 0) { if (mkCols) { - addColumn(*tok); + addColumn(tok); } else { *curCol++ = VariableType(tok); @@ -129,6 +129,6 @@ StreamRows::end(const RowProcessor * rp) const rowNum += 1; } curCol = columns.get().begin(); - tok = StringPtr(new Glib::ustring()); + tok.clear(); } diff --git a/project2/streamRows.h b/project2/streamRows.h index 338d997..4dd9064 100644 --- a/project2/streamRows.h +++ b/project2/streamRows.h @@ -33,8 +33,7 @@ class StreamRows : public DefinedColumns { mutable bool mkCols; mutable bool inQuotes; mutable bool prevWasQuote; - typedef boost::shared_ptr StringPtr; - mutable StringPtr tok; + mutable Glib::ustring tok; mutable Columns::index::type::iterator curCol; }; diff --git a/project2/variableConvert.cpp b/project2/variableConvert.cpp index ea6ddef..0f69a52 100644 --- a/project2/variableConvert.cpp +++ b/project2/variableConvert.cpp @@ -43,15 +43,9 @@ class ConvertVisitorGlibUstring : public boost::static_visitor & r) const { - return *set(var, r.get(), NULL); - } const Glib::ustring & operator()(const boost::posix_time::ptime & r) const { return *set(var, new Glib::ustring(boost::posix_time::to_iso_extended_string(r))); } - const Glib::ustring & operator()(const boost::shared_ptr & r) const { - return *set(var, new Glib::ustring(boost::posix_time::to_iso_extended_string(*r))); - } const Glib::ustring & operator()(const Null &) const { throw NullVariable(); } @@ -70,15 +64,9 @@ class ConvertVisitorStdString : public boost::static_visitor & r) const { - return *set(var, new std::string(*r)); - } const std::string & operator()(const boost::posix_time::ptime & r) const { return *set(var, new std::string(boost::posix_time::to_iso_extended_string(r))); } - const std::string & operator()(const boost::shared_ptr & r) const { - return *set(var, new std::string(boost::posix_time::to_iso_extended_string(*r))); - } const std::string & operator()(const Null &) const { throw NullVariable(); } @@ -97,15 +85,9 @@ class ConvertVisitorCharStar : public boost::static_visitor { const char * operator()(const Glib::ustring & r) const { return set(var, &r, NULL)->c_str(); } - const char * operator()(const boost::shared_ptr & r) const { - return set(var, r.get(), NULL)->c_str(); - } const char * operator()(const boost::posix_time::ptime & r) const { return set(var, new std::string(boost::posix_time::to_iso_extended_string(r)))->c_str(); } - const char * operator()(const boost::shared_ptr & r) const { - return set(var, new std::string(boost::posix_time::to_iso_extended_string(*r)))->c_str(); - } const char * operator()(const Null &) const { return set(var, NULL, NULL); } @@ -124,17 +106,10 @@ class ConvertVisitorUCharStar : public boost::static_visitor(set(var, &r, NULL)->c_str()); } - const unsigned char * operator()(const boost::shared_ptr & r) const { - return reinterpret_cast(set(var, r.get(), NULL)->c_str()); - } const unsigned char * operator()(const boost::posix_time::ptime & r) const { return reinterpret_cast( set(var, new std::string(boost::posix_time::to_iso_extended_string(r)))->c_str()); } - const unsigned char * operator()(const boost::shared_ptr & r) const { - return reinterpret_cast( - set(var, new std::string(boost::posix_time::to_iso_extended_string(*r)))->c_str()); - } const unsigned char * operator()(const Null &) const { return set(var, NULL, NULL); } @@ -154,15 +129,9 @@ class ConvertVisitor : public boost::static_visitor { DestType operator()(const Glib::ustring & r) const { return boost::lexical_cast(r); } - DestType operator()(const boost::shared_ptr & r) const { - return boost::lexical_cast(*r); - } DestType operator()(const boost::posix_time::ptime &) const { throw InvalidConversionTo(typeid(DestType).name()); } - DestType operator()(const boost::shared_ptr &) const { - throw InvalidConversionTo(typeid(DestType).name()); - } DestType operator()(const Null &) const { throw NullVariable(); } @@ -181,15 +150,9 @@ class ConvertVisitorDateTime : public boost::static_visitor); } - const boost::posix_time::ptime & operator()(const boost::shared_ptr & r) const { - return *set(var, new boost::posix_time::ptime(boost::posix_time::time_from_string(*r)), deleter); - } const boost::posix_time::ptime & operator()(const boost::posix_time::ptime & r) const { return r; } - const boost::posix_time::ptime & operator()(const boost::shared_ptr & r) const { - return *r; - } const boost::posix_time::ptime & operator()(const Null &) const { throw NullVariable(); } diff --git a/project2/variables.cpp b/project2/variables.cpp index f633ccd..969c17f 100644 --- a/project2/variables.cpp +++ b/project2/variables.cpp @@ -1,10 +1,13 @@ #include "variables.h" +#include "logger.h" +#include "iHaveParameters.h" #include "xmlObjectLoader.h" #include "exceptions.h" #include "appEngine.h" #include "session.h" #include "rowUser.h" #include "rowSet.h" +#include "rowProcessor.h" #include #include #include @@ -20,7 +23,6 @@ SimpleMessageException(NoVariableDefinition); enum VT_typeID { DefaultType, - StringPtr, String, Int, UInt, @@ -31,7 +33,6 @@ enum VT_typeID { Float, Double, DateTime, - DateTimePtr, }; static @@ -40,7 +41,6 @@ getVariableTypeFromName(const std::string & src) { if (src.empty()) return String; if (src == "string") return String; - if (src == "stringptr") return StringPtr; if (src == "int") return Int; if (src == "uint") return UInt; if (src == "lint") return LInt; @@ -50,7 +50,6 @@ getVariableTypeFromName(const std::string & src) if (src == "float") return Float; if (src == "double") return Double; if (src == "datetime") return DateTime; - if (src == "datetimeptr") return DateTimePtr; throw UnknownVariableType(src); } static @@ -60,8 +59,6 @@ makeVariableType(const Glib::ustring & src, const VT_typeID format = DefaultType switch (format) { default: case DefaultType: - case StringPtr: - return boost::shared_ptr(new Glib::ustring(src)); case String: return src; case Int: @@ -82,8 +79,6 @@ makeVariableType(const Glib::ustring & src, const VT_typeID format = DefaultType return boost::lexical_cast(src); case DateTime: return boost::posix_time::time_from_string(src); - case DateTimePtr: - return boost::shared_ptr(new boost::posix_time::ptime(boost::posix_time::time_from_string(src))); } } @@ -364,6 +359,108 @@ class VariableConfig : public VariableImplDyn { const Glib::ustring name; }; +template +class compi : public boost::static_visitor { + public: + compi(const S & s) : _s(s) { } + bool operator()(const S & t) const + { + return _s < t; + } + template + bool operator()(const T &) const + { + // should never be called + return false; + } + private: + const S & _s; +}; + +class comp : public boost::static_visitor { + public: + comp(const VariableType & a) : _a(a) { } + template + bool operator()(const T & t) const + { + return boost::apply_visitor(compi(t), _a); + } + private: + const VariableType & _a; +}; + +bool +operator<(const VariableType & a, const VariableType & b) +{ + if (a.which() < b.which()) { + return true; + } + if (a.which() == b.which()) { + return boost::apply_visitor(comp(a), b); + } + return false; +} + +/// Variable implementation that looks up it's value in a map of key(s)/value pairs +class VariableLookup : public VariableImplDyn, public RowProcessor { + private: + typedef std::vector Key; + typedef std::map Map; + public: + class NotFound { }; + VariableLookup(const xmlpp::Element * e) : + VariableImplDyn(e), + RowProcessor(e), + name(e->get_attribute_value("name")) + { + LoaderBase loader(true); + loader.supportedStorers.insert(Storer::into(&rowSets)); + loader.collectAll(e, false); + } + const VariableType & value() const + { + if (map.empty()) { + fillCache(); + } + Key k; + k.reserve(parameters.size()); + BOOST_FOREACH(const Parameters::value_type & p, parameters) { + k.push_back(p.second->value()); + } + Map::const_iterator i = map.find(k); + if (i != map.end()) { + cache = i->second; + return cache; + } + throw NotFound(); + } + private: + void fillCache() const + { + BOOST_FOREACH(const RowSets::value_type & rs, rowSets) { + cs = rs; + rs->execute(this); + } + cs.reset(); + Logger()->messagef(LOG_DEBUG, "%s: %s has filled cached with %zu items", + __PRETTY_FUNCTION__, name.c_str(), map.size()); + } + void rowReady() const + { + Key k; + k.reserve(cs->columnCount() - 1); + BOOST_FOREACH(const Parameters::value_type & p, parameters) { + k.push_back(cs->getCurrentValue(p.first)); + } + map[k] = cs->getCurrentValue(name); + } + mutable Map map; + mutable RowSetPtr cs; + typedef ANONSTORAGEOF(RowSet) RowSets; + RowSets rowSets; + const Glib::ustring name; +}; + Variable::Variable(VariableType def) : var(new VariableFixed(def)) { @@ -391,6 +488,8 @@ Variable::Variable(const xmlpp::Element * e, const Glib::ustring & n, bool requi var = new VariableParam(c); else if (source == "config") var = new VariableConfig(c); + else if (source == "lookup") + var = new VariableLookup(c); else if (source == "literal" || source.empty()) { if (const xmlpp::Attribute * la = c->get_attribute("value")) { var = new VariableLiteral(la->get_value(), getVariableTypeFromName(c->get_attribute_value("type"))); diff --git a/project2/variables.h b/project2/variables.h index 37aba03..a6dfb05 100644 --- a/project2/variables.h +++ b/project2/variables.h @@ -18,7 +18,6 @@ typedef boost::variant< Null, // Strings Glib::ustring, - boost::shared_ptr, // Numbers long long unsigned int, long unsigned int, @@ -31,8 +30,7 @@ typedef boost::variant< double, float, // DateTimes - boost::posix_time::ptime, - boost::shared_ptr + boost::posix_time::ptime > _VT; class VariableType : public _VT { diff --git a/project2/xslRows.cpp b/project2/xslRows.cpp index b30d9b6..9c4ce98 100644 --- a/project2/xslRows.cpp +++ b/project2/xslRows.cpp @@ -110,19 +110,19 @@ XslRows::execute(const RowProcessor * rp) const values[xp.first] = xpathObjI->floatval; } else if (xpathObjI->stringval) { - values[xp.first] = boost::shared_ptr(new Glib::ustring((const char *)xpathObjI->stringval)); + values[xp.first] = Glib::ustring((const char *)xpathObjI->stringval); } else if (xpathObjI->nodesetval) { - boost::shared_ptr str = boost::shared_ptr(new Glib::ustring()); + Glib::ustring str; for (int i = 0; i < xpathObjI->nodesetval->nodeNr; i += 1) { xmlNodePtr n = xpathObjI->nodesetval->nodeTab[i]; if (n->content) { - *str += (const char *)n->content; + str += (const char *)n->content; } for (n = n->children; n; n = n->next) { xmlChar * val = n->content; if (val) { - *str += (const char *)val; + str += (const char *)val; } } } -- cgit v1.2.3