Apache Mesos
argv.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 
13 #ifndef __STOUT_OS_RAW_ARGV_HPP__
14 #define __STOUT_OS_RAW_ARGV_HPP__
15 
16 #include <string.h>
17 
18 #include <string>
19 #include <vector>
20 
21 #include <stout/foreach.hpp>
22 
23 namespace os {
24 namespace raw {
25 
36 class Argv
37 {
38 public:
39  Argv(const Argv&) = delete;
40  Argv& operator=(const Argv&) = delete;
41 
42  template <typename Iterable>
43  explicit Argv(const Iterable& iterable)
44  {
45  foreach (const std::string& arg, iterable) {
46  args.emplace_back(arg);
47  }
48 
49  argv = new char*[args.size() + 1];
50  for (size_t i = 0; i < args.size(); i++) {
51  argv[i] = const_cast<char*>(args[i].c_str());
52  }
53 
54  argv[args.size()] = nullptr;
55  }
56 
58  {
59  delete[] argv;
60  }
61 
62  operator char**() const
63  {
64  return argv;
65  }
66 
67  operator std::vector<std::string>() const
68  {
69  return args;
70  }
71 
72 private:
73  std::vector<std::string> args;
74 
75  // NOTE: This points to strings in the vector `args`.
76  char** argv;
77 };
78 
79 } // namespace raw {
80 } // namespace os {
81 
82 #endif // __STOUT_OS_RAW_ARGV_HPP__
Argv(const Iterable &iterable)
Definition: argv.hpp:43
Definition: posix_signalhandler.hpp:23
Represent the argument list expected by execv routines.
Definition: argv.hpp:36
Argv(const Argv &)=delete
Argv & operator=(const Argv &)=delete
~Argv()
Definition: argv.hpp:57