Avoid potential negative array index access to cached text.
[LibreOffice.git] / vcl / unx / gtk3_kde5 / gtk3_kde5_filepicker_ipc.hxx
blob58ca3eb4527a6407f1fd36f563a84c4a36920ef3
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #pragma once
22 #include <osl/process.h>
23 #include <unotools/resmgr.hxx>
25 #include "filepicker_ipc_commands.hxx"
27 #include <functional>
28 #include <mutex>
29 #include <thread>
30 #include <sstream>
32 OUString getResString(TranslateId pResId);
34 class Gtk3KDE5FilePickerIpc
36 protected:
37 oslProcess m_process;
38 oslFileHandle m_inputWrite;
39 oslFileHandle m_outputRead;
40 // simple multiplexing: every command gets its own ID that can be used to
41 // read the corresponding response
42 uint64_t m_msgId = 1;
43 std::mutex m_mutex;
44 uint64_t m_incomingResponse = 0;
45 std::string m_responseBuffer;
46 std::stringstream m_responseStream;
48 public:
49 explicit Gtk3KDE5FilePickerIpc();
50 ~Gtk3KDE5FilePickerIpc();
52 sal_Int16 execute();
54 void writeResponseLine(const std::string& line);
56 template <typename... Args> uint64_t sendCommand(Commands command, const Args&... args)
58 auto id = m_msgId;
59 ++m_msgId;
60 std::stringstream stream;
61 sendIpcArgs(stream, id, command, args...);
62 writeResponseLine(stream.str());
63 return id;
66 std::string readResponseLine();
68 // workaround gcc <= 4.8 bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55914
69 template <int...> struct seq
72 template <int N, int... S> struct gens : gens<N - 1, N - 1, S...>
75 template <int... S> struct gens<0, S...>
77 typedef seq<S...> type;
79 template <typename... Args> struct ArgsReader
81 ArgsReader(Args&... args)
82 : m_args(args...)
86 void operator()(std::istream& stream)
88 callFunc(stream, typename gens<sizeof...(Args)>::type());
91 private:
92 template <int... S> void callFunc(std::istream& stream, seq<S...>)
94 readIpcArgs(stream, std::get<S>(m_args)...);
97 std::tuple<Args&...> m_args;
100 template <typename... Args> void readResponse(uint64_t id, Args&... args)
102 ArgsReader<Args...> argsReader(args...);
103 while (true)
105 // only let one thread read at any given time
106 std::scoped_lock<std::mutex> lock(m_mutex);
108 // check if we need to read (and potentially wait) a response ID
109 if (m_incomingResponse == 0)
111 m_responseStream.clear();
112 m_responseStream.str(readResponseLine());
113 readIpcArgs(m_responseStream, m_incomingResponse);
116 if (m_incomingResponse == id)
118 // the response we are waiting for came in
119 argsReader(m_responseStream);
120 m_incomingResponse = 0;
121 break;
123 else
125 // the next response answers some other request, yield
126 std::this_thread::yield();
131 private:
132 std::function<void()> blockMainWindow();
135 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */