You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by ji...@apache.org on 2016/07/04 17:01:25 UTC
[04/15] mesos git commit: Added an abstraction os::raw::Argv in stout.
Added an abstraction os::raw::Argv in stout.
Review: https://reviews.apache.org/r/49424
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/7e63b808
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/7e63b808
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/7e63b808
Branch: refs/heads/master
Commit: 7e63b80804d0d1d2156f28eacc2f6ccbd6ff6c31
Parents: 72a4752
Author: Jie Yu <yu...@gmail.com>
Authored: Wed Jun 29 21:47:10 2016 -0700
Committer: Jie Yu <yu...@gmail.com>
Committed: Mon Jul 4 10:01:13 2016 -0700
----------------------------------------------------------------------
3rdparty/stout/include/Makefile.am | 1 +
3rdparty/stout/include/stout/os.hpp | 3 +
3rdparty/stout/include/stout/os/raw/argv.hpp | 78 +++++++++++++++++++++++
3rdparty/stout/tests/os_tests.cpp | 12 ++++
4 files changed, 94 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mesos/blob/7e63b808/3rdparty/stout/include/Makefile.am
----------------------------------------------------------------------
diff --git a/3rdparty/stout/include/Makefile.am b/3rdparty/stout/include/Makefile.am
index f10c836..4768064 100644
--- a/3rdparty/stout/include/Makefile.am
+++ b/3rdparty/stout/include/Makefile.am
@@ -129,6 +129,7 @@ nobase_include_HEADERS = \
stout/os/posix/stat.hpp \
stout/os/posix/su.hpp \
stout/os/posix/write.hpp \
+ stout/os/raw/argv.hpp \
stout/os/raw/environment.hpp \
stout/os/windows/bootid.hpp \
stout/os/windows/chroot.hpp \
http://git-wip-us.apache.org/repos/asf/mesos/blob/7e63b808/3rdparty/stout/include/stout/os.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/include/stout/os.hpp b/3rdparty/stout/include/stout/os.hpp
index 53b0093..facf183 100644
--- a/3rdparty/stout/include/stout/os.hpp
+++ b/3rdparty/stout/include/stout/os.hpp
@@ -64,6 +64,9 @@
#include <stout/os/touch.hpp>
#include <stout/os/utime.hpp>
+#include <stout/os/raw/argv.hpp>
+#include <stout/os/raw/environment.hpp>
+
// For readability, we minimize the number of #ifdef blocks in the code by
// splitting platform specifc system calls into separate directories.
#ifdef __WINDOWS__
http://git-wip-us.apache.org/repos/asf/mesos/blob/7e63b808/3rdparty/stout/include/stout/os/raw/argv.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/include/stout/os/raw/argv.hpp b/3rdparty/stout/include/stout/os/raw/argv.hpp
new file mode 100644
index 0000000..3ac6216
--- /dev/null
+++ b/3rdparty/stout/include/stout/os/raw/argv.hpp
@@ -0,0 +1,78 @@
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef __STOUT_OS_RAW_ARGV_HPP__
+#define __STOUT_OS_RAW_ARGV_HPP__
+
+#include <string>
+#include <vector>
+
+#include <stout/foreach.hpp>
+
+namespace os {
+namespace raw {
+
+/**
+ * Represent the argument list expected by `execv` routines. The
+ * argument list is an array of pointers that point to null-terminated
+ * strings. The array of pointers must be terminated by a nullptr. To
+ * use this abstraction, see the following example:
+ *
+ * vector<string> args = {"arg0", "arg1"};
+ * os::raw::Argv argv(args);
+ * execvp("my_binary", argv);
+ */
+class Argv
+{
+public:
+ template <typename Iterable>
+ explicit Argv(const Iterable& iterable)
+ {
+ std::vector<char*> _argv;
+ foreach (const std::string& arg, iterable) {
+ char* _arg = new char[arg.size() + 1];
+
+ // NOTE: strcpy will fill the terminating `\0' character.
+ strcpy(_arg, arg.c_str());
+ _argv.emplace_back(_arg);
+ }
+
+ size = _argv.size();
+ argv = new char*[size + 1];
+ for (size_t i = 0; i < size; i++) {
+ argv[i] = _argv[i];
+ }
+ argv[size] = nullptr;
+ }
+
+ ~Argv()
+ {
+ for (size_t i = 0; i < size; i++) {
+ delete[] argv[i];
+ }
+ delete[] argv;
+ }
+
+ operator char**()
+ {
+ return argv;
+ }
+
+private:
+ char** argv;
+ size_t size;
+};
+
+} // namespace raw {
+} // namespace os {
+
+#endif // __STOUT_OS_RAW_ARGV_HPP__
http://git-wip-us.apache.org/repos/asf/mesos/blob/7e63b808/3rdparty/stout/tests/os_tests.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/tests/os_tests.cpp b/3rdparty/stout/tests/os_tests.cpp
index 1dedff2..e674440 100644
--- a/3rdparty/stout/tests/os_tests.cpp
+++ b/3rdparty/stout/tests/os_tests.cpp
@@ -102,6 +102,18 @@ TEST_F(OsTest, Environment)
}
+TEST_F(OsTest, Argv)
+{
+ vector<string> args = {"arg0", "arg1", "arg2"};
+
+ os::raw::Argv _argv(args);
+ char** argv = _argv;
+ for (size_t i = 0; i < args.size(); i++) {
+ EXPECT_EQ(args[i], argv[i]);
+ }
+}
+
+
TEST_F(OsTest, System)
{
EXPECT_EQ(0, os::system("exit 0"));