Cmd implicit constructor
This commit is contained in:
+4
-3
@@ -2,10 +2,11 @@
|
||||
set -e
|
||||
|
||||
# Test cppipe functions
|
||||
OKs=$(test/functions_test.cppipe 2>/dev/null | grep OK | wc -l)
|
||||
if ! [ $OKs = 13 ]
|
||||
OKs=$(test/functions_test.cppipe | grep OK | wc -l)
|
||||
EXPECTED=14
|
||||
if ! [ $OKs = $EXPECTED ]
|
||||
then
|
||||
echo "Functions test failed: EXPECTED 13 OKs, got $OKs"
|
||||
echo "Functions test failed: EXPECTED $EXPECTED OKs, got $OKs"
|
||||
exit 1
|
||||
fi
|
||||
echo Functions test OK!
|
||||
|
||||
+10
-9
@@ -18,11 +18,11 @@ class Cmd
|
||||
public:
|
||||
/* all args must be const char* */
|
||||
template<typename... Args>
|
||||
explicit Cmd(Args... args)
|
||||
: argv({args...})
|
||||
{
|
||||
argv.push_back(nullptr);
|
||||
}
|
||||
explicit Cmd(Args...);
|
||||
|
||||
/* Implicitly convert from { "command", "param" }
|
||||
* e.g: exec({ "cmd", "arg" }); */
|
||||
Cmd(std::initializer_list<const char*> args);
|
||||
|
||||
/* Execute the command, if arguments are not given use stdin,out,err
|
||||
else use the given file desciptors. this can also be used
|
||||
@@ -45,8 +45,9 @@ public:
|
||||
class PendingCmd
|
||||
{
|
||||
public:
|
||||
PendingCmd(std::initializer_list<const char*> cmd_args);
|
||||
/* 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 */
|
||||
~PendingCmd();
|
||||
|
||||
@@ -60,10 +61,10 @@ public:
|
||||
/* Prevent a pending command from being executed on destruction */
|
||||
void cancel();
|
||||
|
||||
const Cmd& cmd;
|
||||
fd_t in, out, err;
|
||||
Cmd cmd;
|
||||
fd_t in=0, out=1, err=2;
|
||||
private:
|
||||
bool execed_;
|
||||
bool execed_ = false;
|
||||
|
||||
friend Proc detach(const PendingCmd&);
|
||||
friend Proc detachRedirIn(const PendingCmd&);
|
||||
|
||||
+20
-3
@@ -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
|
||||
{
|
||||
Proc p = createProcess(argv.data(), in, out, err);
|
||||
@@ -58,13 +70,18 @@ inline Cmd& Cmd::operator+=(const char* arg)
|
||||
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)
|
||||
: cmd(origin)
|
||||
inline PendingCmd::PendingCmd(Cmd origin, fd_t in, fd_t out, fd_t err)
|
||||
: cmd(std::move(origin))
|
||||
, in(in)
|
||||
, out(out)
|
||||
, err(err)
|
||||
, execed_(false)
|
||||
{}
|
||||
|
||||
inline PendingCmd::~PendingCmd()
|
||||
|
||||
+17
-15
@@ -25,7 +25,7 @@ int main(int argc, char* argv[])
|
||||
|
||||
string out1 = $(ll + "src" | grep + "inl" | grep + "child");
|
||||
if(out1.find("childProcess.inl") != string::npos)
|
||||
cout << "OK 0/12" << endl;
|
||||
cout << "OK 0/13" << endl;
|
||||
|
||||
string out2 = $(echo + "abc" + "def");
|
||||
// "abc def" = 7 chars, trailing newlines are stripped by $()
|
||||
@@ -35,8 +35,8 @@ int main(int argc, char* argv[])
|
||||
exit(1);
|
||||
}
|
||||
|
||||
Cmd success("echo", "OK 1/12");
|
||||
Cmd fail("mkdir", ".");
|
||||
Cmd success("echo", "OK 1/13");
|
||||
Cmd fail("false");
|
||||
Cmd unexpected("echo", "FAILURE");
|
||||
success &&
|
||||
fail &&
|
||||
@@ -47,34 +47,36 @@ int main(int argc, char* argv[])
|
||||
// unexpected &&
|
||||
// unexpected;
|
||||
|
||||
Cmd write_file("echo", "Existing ", " ", "file. OK 2/12");
|
||||
Cmd write_file("echo", "Existing ", " ", "file. OK 2/13");
|
||||
|
||||
write_file > "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" &&
|
||||
rm + "file.txt" &&
|
||||
fail ||
|
||||
Cmd("echo", "OK 4/12");
|
||||
Cmd("echo", "OK 4/13");
|
||||
|
||||
echo + "OK 5/12" &&
|
||||
echo + "OK 6/12",
|
||||
Cmd("echo", "OK 7/12");
|
||||
echo + "OK 5/13" &&
|
||||
echo + "OK 6/13",
|
||||
Cmd("echo", "OK 7/13");
|
||||
|
||||
echo + "OK 8/12" &
|
||||
echo + "OK 9/12" &&
|
||||
echo + "OK 10/12";
|
||||
echo + "OK 8/13" &
|
||||
echo + "OK 9/13" &&
|
||||
echo + "OK 10/13";
|
||||
|
||||
// wait for all detached
|
||||
while(wait(nullptr) != -1);
|
||||
|
||||
Cmd run_OK = echo;
|
||||
run_OK.append_args({ "OK 11/12" });
|
||||
run_OK.append_args({ "OK 11/13" });
|
||||
run( run_OK );
|
||||
|
||||
exec( echo + "OK 12/12" );
|
||||
run({ "echo", "OK 12/13" });
|
||||
|
||||
exec({ "echo", "OK 13/13" });
|
||||
|
||||
// todo
|
||||
// exec( echo + $(echo + "OK 11/12") );
|
||||
// exec( echo + $(echo + "OK 11/13") );
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user