From bdfb254e4d4b7700dd8dcf689abf9bbb27fa1c7d Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 23 Sep 2017 12:29:41 +0100 Subject: Merge improvements from tidy-arrays branch --- gentoobrowse-api/domain/converters.cpp | 79 ++------------------- gentoobrowse-api/domain/converters.h | 26 ------- gentoobrowse-api/domain/converters.impl.h | 81 ++++++++++++++++++++++ gentoobrowse-api/domain/unpackPqTextArray.h | 6 +- gentoobrowse-api/domain/unpackPqTextArray.ll | 14 ++-- .../service/maintenanceGitOperations.cpp | 2 +- 6 files changed, 98 insertions(+), 110 deletions(-) create mode 100644 gentoobrowse-api/domain/converters.impl.h diff --git a/gentoobrowse-api/domain/converters.cpp b/gentoobrowse-api/domain/converters.cpp index 073c545..b4748cd 100644 --- a/gentoobrowse-api/domain/converters.cpp +++ b/gentoobrowse-api/domain/converters.cpp @@ -1,51 +1,24 @@ #include #include "converters.h" +#include "converters.impl.h" #include "unpackPqTextArray.h" #include #include #include namespace Slicer { - static - void - unpackPqTextArray(const std::string & s, ::Gentoo::StringList & list) - { - std::stringstream ss(s); - UnpackPqTextArray l(ss, list); - l.yylex(); - } - template PqArray::PqArray(std::vector * l) : ModelPartForSequence>(l) { } - PqArray::PqArray(::Gentoo::StringList * sl) : - ModelPartForSequence<::Gentoo::StringList>(sl) - { - } - template void PqArray::SetValue(ValueSourcePtr s) { - this->Model->clear(); - std::string v; - s->set(v); - if (v.size() < 3) return; - typedef boost::algorithm::split_iterator string_split_iterator; - string_split_iterator sfi(++v.begin(), --v.end(), boost::algorithm::first_finder(",", boost::algorithm::is_iequal())); - for(auto it = sfi; it != string_split_iterator(); ++it) { - this->Model->push_back(boost::lexical_cast(*it)); - } - } - - void PqArray::SetValue(ValueSourcePtr s) - { - Model->clear(); - std::string v; - s->set(v); - unpackPqTextArray(v, *Model); + std::string in; + s->set(in); + *this->Model = unpackPqArray(in); } template @@ -56,50 +29,8 @@ namespace Slicer { return true; } - bool - PqArray::GetValue(ValueTargetPtr s) - { - s->get(packPqArray(*this->Model)); - return true; - } - template class PqArray; - - ::Gentoo::StringList - unpackPqTextArray(const std::string & s) - { - ::Gentoo::StringList list; - unpackPqTextArray(s, list); - return list; - } - - template - std::string - packPqArray(const T & l) - { - if (l.empty()) return "{}"; - std::vector l2; - l2.reserve(l.size()); - for(const auto & i : l) { - l2.push_back(packPqVar(i)); - } - return "{" + boost::algorithm::join(l2, ",") + "}"; - } - template std::string packPqArray>(const std::set &); - - template - std::string - packPqVar(const T & l) - { - return boost::lexical_cast(l); - } - - template<> - std::string - packPqVar(const std::string & l) - { - return "\"" + boost::algorithm::replace_all_copy(l, "\"", "\\\"") + "\""; - } + template class PqArray; boost::posix_time::ptime shortDateToPosixTime(const std::string & s) diff --git a/gentoobrowse-api/domain/converters.h b/gentoobrowse-api/domain/converters.h index 8449a65..404b9a1 100644 --- a/gentoobrowse-api/domain/converters.h +++ b/gentoobrowse-api/domain/converters.h @@ -16,32 +16,6 @@ namespace Slicer { bool GetValue(ValueTargetPtr) override; }; - template<> - class DLL_PUBLIC PqArray : public ModelPartForSequence<::Gentoo::StringList> { - public: - PqArray(::Gentoo::StringList *); - - void SetValue(ValueSourcePtr) override; - bool GetValue(ValueTargetPtr) override; - }; - - DLL_PUBLIC - ::Gentoo::StringList - unpackPqTextArray(const std::string & s); - - template - DLL_PUBLIC - std::string - packPqArray(const T & l); - - template - std::string - packPqVar(const T & l); - - template<> - std::string - packPqVar(const std::string & l); - DLL_PUBLIC boost::posix_time::ptime shortDateToPosixTime(const std::string & s); diff --git a/gentoobrowse-api/domain/converters.impl.h b/gentoobrowse-api/domain/converters.impl.h new file mode 100644 index 0000000..25f9266 --- /dev/null +++ b/gentoobrowse-api/domain/converters.impl.h @@ -0,0 +1,81 @@ +#ifndef GENTOOBROWSE_API_DOMAIN_CONVERTERS_IMPL_H +#define GENTOOBROWSE_API_DOMAIN_CONVERTERS_IMPL_H + +#include "unpackPqTextArray.h" + +namespace Slicer { + template + class UnpackPqTextArrayInto : public UnpackPqTextArray { + public: + UnpackPqTextArrayInto(std::istream & s, std::vector & l) : + UnpackPqTextArray(s), + list(l) + { + } + + virtual void consume(const std::string & s) override + { + list.push_back(boost::lexical_cast(s)); + } + + private: + std::vector & list; + }; + + template + typename std::enable_if::value>::type + packPqVar(std::ostream & s, const T & l) + { + s << l; + } + + std::string + escapePqChar(char c) + { + if (c == '"') { + return std::string("\\\"", 2); + } + return std::string(1, c); + } + + void + packPqVar(std::ostream & s, const std::string & l) + { + s << "\""; + std::transform(l.begin(), l.end(), std::ostream_iterator(s), escapePqChar); + s << "\""; + } + + template + std::vector + unpackPqArray(const std::string & s) + { + std::vector rtn; + std::stringstream ss(s); + UnpackPqTextArrayInto u(ss, rtn); + u.yylex(); + return rtn; + } + + template + std::string + packPqArray(const T & l) + { + std::stringstream ss; + ss << "{"; + if (!l.empty()) { + auto i = l.cbegin(); + packPqVar(ss, *i); + i++; + while (i != l.cend()) { + ss << ","; + packPqVar(ss, *i++); + } + } + ss << "}"; + return ss.str(); + } +} + +#endif + diff --git a/gentoobrowse-api/domain/unpackPqTextArray.h b/gentoobrowse-api/domain/unpackPqTextArray.h index aefaddd..5f639b4 100644 --- a/gentoobrowse-api/domain/unpackPqTextArray.h +++ b/gentoobrowse-api/domain/unpackPqTextArray.h @@ -10,13 +10,15 @@ namespace Slicer { class UnpackPqTextArray : public yyFlexLexer { public: - UnpackPqTextArray(std::istream &, ::Gentoo::StringList & sl); + UnpackPqTextArray(std::istream &); + virtual ~UnpackPqTextArray() = default; int yylex() override; void LexerError(const char * msg) override; + virtual void consume(const std::string &) = 0; private: - ::Gentoo::StringList & list; + std::string buffer; }; } diff --git a/gentoobrowse-api/domain/unpackPqTextArray.ll b/gentoobrowse-api/domain/unpackPqTextArray.ll index f8cce5f..224ffc0 100644 --- a/gentoobrowse-api/domain/unpackPqTextArray.ll +++ b/gentoobrowse-api/domain/unpackPqTextArray.ll @@ -35,32 +35,32 @@ char [^"] yy_pop_state(); } {unquoted} { - list.push_back(YYText()); + consume(YYText()); BEGIN(AFTER); } {quote} { - list.push_back(std::string()); + buffer.clear(); BEGIN(QUOTED); } {comma} { BEGIN(NEXT); } {quote} { + consume(buffer); BEGIN(AFTER); } {esc} { - list.back().push_back(*(YYText() + 1)); + buffer.push_back(*(YYText() + 1)); } {char} { - list.back().append(YYText()); + buffer.append(YYText()); } %% namespace Slicer { - UnpackPqTextArray::UnpackPqTextArray(std::istream & f, ::Gentoo::StringList & sl) : - yyFlexLexer(&f, NULL), - list(sl) + UnpackPqTextArray::UnpackPqTextArray(std::istream & f) : + yyFlexLexer(&f, NULL) { } diff --git a/gentoobrowse-api/service/maintenanceGitOperations.cpp b/gentoobrowse-api/service/maintenanceGitOperations.cpp index 59470c6..d532926 100644 --- a/gentoobrowse-api/service/maintenanceGitOperations.cpp +++ b/gentoobrowse-api/service/maintenanceGitOperations.cpp @@ -11,7 +11,7 @@ #include #include #include "utils/git.h" -#include "converters.h" +#include "converters.impl.h" #include namespace Gentoo { -- cgit v1.2.3