summaryrefslogtreecommitdiff
path: root/gentoobrowse-api/domain/converters.impl.h
blob: 25f9266cfcc1964b76ef1b5d0d8df72051a2da79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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