Clean water File(const String&) usage
[carla.git] / source / backend / utils / PipeClient.cpp
blob3f1fadf444882b6bc0988b500ce9409f28d6e381
1 /*
2 * Carla Plugin Host
3 * Copyright (C) 2011-2023 Filipe Coelho <falktx@falktx.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * For a full copy of the GNU General Public License see the doc/GPL.txt file.
18 #include "CarlaUtils.h"
20 #include "CarlaPipeUtils.hpp"
22 namespace CB = CARLA_BACKEND_NAMESPACE;
24 // --------------------------------------------------------------------------------------------------------------------
26 class ExposedCarlaPipeClient : public CarlaPipeClient
28 public:
29 ExposedCarlaPipeClient(const CarlaPipeCallbackFunc callbackFunc, void* const callbackPtr) noexcept
30 : CarlaPipeClient(),
31 fCallbackFunc(callbackFunc),
32 fCallbackPtr(callbackPtr),
33 fLastReadLine(nullptr)
35 CARLA_SAFE_ASSERT(fCallbackFunc != nullptr);
38 ~ExposedCarlaPipeClient() override
40 if (fLastReadLine != nullptr)
42 delete[] fLastReadLine;
43 fLastReadLine = nullptr;
47 const char* readlineblock(const uint timeout) noexcept
49 delete[] fLastReadLine;
50 fLastReadLine = CarlaPipeClient::_readlineblock(true, 0, timeout);
51 return fLastReadLine;
54 bool readlineblock_bool(const uint timeout) noexcept
56 if (const char* const line = CarlaPipeClient::_readlineblock(false, 0, timeout))
57 return std::strcmp(line, "true") == 0;
59 return false;
62 int readlineblock_int(const uint timeout) noexcept
64 if (const char* const line = CarlaPipeClient::_readlineblock(false, 0, timeout))
65 return std::atoi(line);
67 return 0;
70 double readlineblock_float(const uint timeout) noexcept
72 if (const char* const line = CarlaPipeClient::_readlineblock(false, 0, timeout))
73 return std::atof(line);
75 return 0.0;
78 bool msgReceived(const char* const msg) noexcept override
80 if (fCallbackFunc != nullptr)
82 try {
83 fCallbackFunc(fCallbackPtr, msg);
84 } CARLA_SAFE_EXCEPTION("msgReceived");
87 return true;
90 private:
91 const CarlaPipeCallbackFunc fCallbackFunc;
92 void* const fCallbackPtr;
93 const char* fLastReadLine;
95 CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ExposedCarlaPipeClient)
98 // --------------------------------------------------------------------------------------------------------------------
100 CarlaPipeClientHandle carla_pipe_client_new(const char* argv[], CarlaPipeCallbackFunc callbackFunc, void* callbackPtr)
102 carla_debug("carla_pipe_client_new(%p, %p, %p)", argv, callbackFunc, callbackPtr);
104 ExposedCarlaPipeClient* const pipe = new ExposedCarlaPipeClient(callbackFunc, callbackPtr);
106 if (! pipe->initPipeClient(argv))
108 delete pipe;
109 return nullptr;
112 return pipe;
115 void carla_pipe_client_idle(CarlaPipeClientHandle handle)
117 CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
119 ((ExposedCarlaPipeClient*)handle)->idlePipe();
122 bool carla_pipe_client_is_running(CarlaPipeClientHandle handle)
124 CARLA_SAFE_ASSERT_RETURN(handle != nullptr, false);
126 return ((ExposedCarlaPipeClient*)handle)->isPipeRunning();
129 void carla_pipe_client_lock(CarlaPipeClientHandle handle)
131 CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
133 return ((ExposedCarlaPipeClient*)handle)->lockPipe();
136 void carla_pipe_client_unlock(CarlaPipeClientHandle handle)
138 CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
140 return ((ExposedCarlaPipeClient*)handle)->unlockPipe();
143 const char* carla_pipe_client_readlineblock(CarlaPipeClientHandle handle, uint timeout)
145 CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
147 return ((ExposedCarlaPipeClient*)handle)->readlineblock(timeout);
150 bool carla_pipe_client_readlineblock_bool(CarlaPipeClientHandle handle, uint timeout)
152 CARLA_SAFE_ASSERT_RETURN(handle != nullptr, false);
154 return ((ExposedCarlaPipeClient*)handle)->readlineblock_bool(timeout);
157 int carla_pipe_client_readlineblock_int(CarlaPipeClientHandle handle, uint timeout)
159 CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 0);
161 return ((ExposedCarlaPipeClient*)handle)->readlineblock_int(timeout);
164 double carla_pipe_client_readlineblock_float(CarlaPipeClientHandle handle, uint timeout)
166 CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 0.0);
168 return ((ExposedCarlaPipeClient*)handle)->readlineblock_float(timeout);
171 bool carla_pipe_client_write_msg(CarlaPipeClientHandle handle, const char* msg)
173 CARLA_SAFE_ASSERT_RETURN(handle != nullptr, false);
175 return ((ExposedCarlaPipeClient*)handle)->writeMessage(msg);
178 bool carla_pipe_client_write_and_fix_msg(CarlaPipeClientHandle handle, const char* msg)
180 CARLA_SAFE_ASSERT_RETURN(handle != nullptr, false);
182 return ((ExposedCarlaPipeClient*)handle)->writeAndFixMessage(msg);
185 bool carla_pipe_client_sync(CarlaPipeClientHandle handle)
187 CARLA_SAFE_ASSERT_RETURN(handle != nullptr, false);
189 return ((ExposedCarlaPipeClient*)handle)->syncMessages();
192 bool carla_pipe_client_sync_and_unlock(CarlaPipeClientHandle handle)
194 CARLA_SAFE_ASSERT_RETURN(handle != nullptr, false);
196 ExposedCarlaPipeClient* const pipe = (ExposedCarlaPipeClient*)handle;
197 const bool ret = pipe->syncMessages();
198 pipe->unlockPipe();
199 return ret;
202 void carla_pipe_client_destroy(CarlaPipeClientHandle handle)
204 CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
205 carla_debug("carla_pipe_client_destroy(%p)", handle);
207 ExposedCarlaPipeClient* const pipe = (ExposedCarlaPipeClient*)handle;
208 pipe->closePipeClient();
209 delete pipe;
212 // --------------------------------------------------------------------------------------------------------------------
214 bool carla_pipe_client_flush(CarlaPipeClientHandle handle)
216 return carla_pipe_client_sync(handle);
219 bool carla_pipe_client_flush_and_unlock(CarlaPipeClientHandle handle)
221 return carla_pipe_client_sync_and_unlock(handle);
224 // --------------------------------------------------------------------------------------------------------------------
226 #ifndef CARLA_PLUGIN_BUILD
227 # include "CarlaPipeUtils.cpp"
228 #endif
230 // --------------------------------------------------------------------------------------------------------------------