diff options
Diffstat (limited to 'gentoobrowse-api/domain/converters.impl.h')
-rw-r--r-- | gentoobrowse-api/domain/converters.impl.h | 81 |
1 files changed, 81 insertions, 0 deletions
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 <typename T> + class UnpackPqTextArrayInto : public UnpackPqTextArray { + public: + UnpackPqTextArrayInto(std::istream & s, std::vector<T> & l) : + UnpackPqTextArray(s), + list(l) + { + } + + virtual void consume(const std::string & s) override + { + list.push_back(boost::lexical_cast<T>(s)); + } + + private: + std::vector<T> & list; + }; + + template<typename T> + typename std::enable_if<std::is_arithmetic<T>::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<std::string>(s), escapePqChar); + s << "\""; + } + + template <typename T> + std::vector<T> + unpackPqArray(const std::string & s) + { + std::vector<T> rtn; + std::stringstream ss(s); + UnpackPqTextArrayInto<T> u(ss, rtn); + u.yylex(); + return rtn; + } + + template<typename T> + 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 + |