summaryrefslogtreecommitdiff
path: root/gentoobrowse-api/domain/converters.impl.h
blob: 1d79e56f2d9f1de54166315a0c4fe1a2eb372ff8 (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
#pragma once

#include "unpackPqTextArray.h"
#include <boost/lexical_cast.hpp>

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>
	void
	packPqVar(std::ostream & s, const T & l)
	{
		if constexpr (std::is_arithmetic<T>::value) {
			s << l;
		}
		else {
			s << '\"';
			std::transform(l.begin(), l.end(), std::ostream_iterator<std::string_view>(s), [](const char & c) {
				if (c == '"') {
					return std::string_view(R"(\")");
				}
				return std::string_view(&c, 1);
			});
			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 std::move(ss).str();
	}
}