Cmd implicit constructor

This commit is contained in:
vrd
2025-06-06 18:00:38 +03:00
parent 9923192ec9
commit 0a5af1f757
5 changed files with 52 additions and 30 deletions
+4 -3
View File
@@ -2,10 +2,11 @@
set -e set -e
# Test cppipe functions # Test cppipe functions
OKs=$(test/functions_test.cppipe 2>/dev/null | grep OK | wc -l) OKs=$(test/functions_test.cppipe | grep OK | wc -l)
if ! [ $OKs = 13 ] EXPECTED=14
if ! [ $OKs = $EXPECTED ]
then then
echo "Functions test failed: EXPECTED 13 OKs, got $OKs" echo "Functions test failed: EXPECTED $EXPECTED OKs, got $OKs"
exit 1 exit 1
fi fi
echo Functions test OK! echo Functions test OK!
+10 -9
View File
@@ -18,11 +18,11 @@ class Cmd
public: public:
/* all args must be const char* */ /* all args must be const char* */
template<typename... Args> template<typename... Args>
explicit Cmd(Args... args) explicit Cmd(Args...);
: argv({args...})
{ /* Implicitly convert from { "command", "param" }
argv.push_back(nullptr); * e.g: exec({ "cmd", "arg" }); */
} Cmd(std::initializer_list<const char*> args);
/* Execute the command, if arguments are not given use stdin,out,err /* Execute the command, if arguments are not given use stdin,out,err
else use the given file desciptors. this can also be used else use the given file desciptors. this can also be used
@@ -45,8 +45,9 @@ public:
class PendingCmd class PendingCmd
{ {
public: public:
PendingCmd(std::initializer_list<const char*> cmd_args);
/* Can be implicitly created from a command */ /* Can be implicitly created from a command */
PendingCmd(const Cmd&, fd_t in=0, fd_t out=1, fd_t err=2); PendingCmd(Cmd, fd_t in=0, fd_t out=1, fd_t err=2);
/* Execute the command on destruction */ /* Execute the command on destruction */
~PendingCmd(); ~PendingCmd();
@@ -60,10 +61,10 @@ public:
/* Prevent a pending command from being executed on destruction */ /* Prevent a pending command from being executed on destruction */
void cancel(); void cancel();
const Cmd& cmd; Cmd cmd;
fd_t in, out, err; fd_t in=0, out=1, err=2;
private: private:
bool execed_; bool execed_ = false;
friend Proc detach(const PendingCmd&); friend Proc detach(const PendingCmd&);
friend Proc detachRedirIn(const PendingCmd&); friend Proc detachRedirIn(const PendingCmd&);
+20 -3
View File
@@ -28,6 +28,18 @@ namespace _cppipe
} }
} }
template<typename... Args>
inline Cmd::Cmd(Args... args)
: Cmd({ args... })
{
}
inline Cmd::Cmd(std::initializer_list<const char*> args)
: argv(args)
{
argv.push_back(nullptr);
}
inline DeadProc Cmd::operator()(fd_t in, fd_t out, fd_t err) const inline DeadProc Cmd::operator()(fd_t in, fd_t out, fd_t err) const
{ {
Proc p = createProcess(argv.data(), in, out, err); Proc p = createProcess(argv.data(), in, out, err);
@@ -58,13 +70,18 @@ inline Cmd& Cmd::operator+=(const char* arg)
return *this; return *this;
} }
inline PendingCmd::PendingCmd(std::initializer_list<const char*> cmd_args)
: cmd(cmd_args)
, in(0)
, out(1)
, err(2)
{}
inline PendingCmd::PendingCmd(const Cmd& origin, fd_t in, fd_t out, fd_t err) inline PendingCmd::PendingCmd(Cmd origin, fd_t in, fd_t out, fd_t err)
: cmd(origin) : cmd(std::move(origin))
, in(in) , in(in)
, out(out) , out(out)
, err(err) , err(err)
, execed_(false)
{} {}
inline PendingCmd::~PendingCmd() inline PendingCmd::~PendingCmd()
+17 -15
View File
@@ -25,7 +25,7 @@ int main(int argc, char* argv[])
string out1 = $(ll + "src" | grep + "inl" | grep + "child"); string out1 = $(ll + "src" | grep + "inl" | grep + "child");
if(out1.find("childProcess.inl") != string::npos) if(out1.find("childProcess.inl") != string::npos)
cout << "OK 0/12" << endl; cout << "OK 0/13" << endl;
string out2 = $(echo + "abc" + "def"); string out2 = $(echo + "abc" + "def");
// "abc def" = 7 chars, trailing newlines are stripped by $() // "abc def" = 7 chars, trailing newlines are stripped by $()
@@ -35,8 +35,8 @@ int main(int argc, char* argv[])
exit(1); exit(1);
} }
Cmd success("echo", "OK 1/12"); Cmd success("echo", "OK 1/13");
Cmd fail("mkdir", "."); Cmd fail("false");
Cmd unexpected("echo", "FAILURE"); Cmd unexpected("echo", "FAILURE");
success && success &&
fail && fail &&
@@ -47,34 +47,36 @@ int main(int argc, char* argv[])
// unexpected && // unexpected &&
// unexpected; // unexpected;
Cmd write_file("echo", "Existing ", " ", "file. OK 2/12"); Cmd write_file("echo", "Existing ", " ", "file. OK 2/13");
write_file > "file.txt"; write_file > "file.txt";
grep + "Existing" < "file.txt"; grep + "Existing" < "file.txt";
echo + "Appended to file OK 3/12" >> "file.txt"; echo + "Appended to file OK 3/13" >> "file.txt";
grep + "Appended" < "file.txt" && grep + "Appended" < "file.txt" &&
rm + "file.txt" && rm + "file.txt" &&
fail || fail ||
Cmd("echo", "OK 4/12"); Cmd("echo", "OK 4/13");
echo + "OK 5/12" && echo + "OK 5/13" &&
echo + "OK 6/12", echo + "OK 6/13",
Cmd("echo", "OK 7/12"); Cmd("echo", "OK 7/13");
echo + "OK 8/12" & echo + "OK 8/13" &
echo + "OK 9/12" && echo + "OK 9/13" &&
echo + "OK 10/12"; echo + "OK 10/13";
// wait for all detached // wait for all detached
while(wait(nullptr) != -1); while(wait(nullptr) != -1);
Cmd run_OK = echo; Cmd run_OK = echo;
run_OK.append_args({ "OK 11/12" }); run_OK.append_args({ "OK 11/13" });
run( run_OK ); run( run_OK );
exec( echo + "OK 12/12" ); run({ "echo", "OK 12/13" });
exec({ "echo", "OK 13/13" });
// todo // todo
// exec( echo + $(echo + "OK 11/12") ); // exec( echo + $(echo + "OK 11/13") );
} }
+1
View File
@@ -1,3 +1,4 @@
fix debug
example #!/usr/bin/cppipe example #!/usr/bin/cppipe
description - advantages over manual compile description - advantages over manual compile
man man