1 /*
2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef PANDA_LIBPANDABASE_OS_EXEC_H_
17 #define PANDA_LIBPANDABASE_OS_EXEC_H_
18
19 #include "libpandabase/utils/expected.h"
20 #include "libpandabase/utils/span.h"
21 #include "libpandabase/os/error.h"
22 #include <array>
23
24 #if defined(PANDA_TARGET_UNIX)
25 #include "platforms/unix/libpandabase/exec.h"
26 #elif defined(PANDA_TARGET_WINDOWS)
27 #include "platforms/windows/libpandabase/exec.h"
28 #else
29 #error "Unsupported platform"
30 #endif
31
32 namespace ark::os::exec {
33
34 PANDA_PUBLIC_API Expected<int, Error> Exec(Span<const char *> args);
35 PANDA_PUBLIC_API Expected<int, Error> ExecNoWait(Span<const char *> args);
36 PANDA_PUBLIC_API Expected<int, Error> Wait(int64_t process, bool testStatus);
37
38 template <typename... Args>
decltype(auto)39 decltype(auto) Exec(Args... args)
40 {
41 std::array<const char *, sizeof...(args) + 1> arguments = {args..., nullptr};
42 return os::exec::Exec(Span(arguments));
43 }
44
45 template <typename... Args>
decltype(auto)46 decltype(auto) ExecNoWait(Args... args)
47 {
48 std::array<const char *, sizeof...(args) + 1> arguments = {args..., nullptr};
49 return os::exec::ExecNoWait(Span(arguments));
50 }
51
52 template <typename Callback, typename... Args>
decltype(auto)53 decltype(auto) ExecWithCallback(Callback callback, Args... args)
54 {
55 std::array<const char *, sizeof...(args) + 1> arguments = {args..., nullptr};
56 return os::exec::ExecWithCallback(callback, Span(arguments));
57 }
58
59 template <typename Callback, typename... Args>
decltype(auto)60 decltype(auto) ExecWithCallbackNoWait(Callback callback, Args... args)
61 {
62 std::array<const char *, sizeof...(args) + 1> arguments = {args..., nullptr};
63 return os::exec::ExecWithCallbackNoWait(callback, Span(arguments));
64 }
65
66 } // namespace ark::os::exec
67
68 #endif // PANDA_LIBPANDABASE_OS_EXEC_H_
69