Nektar++
Loading...
Searching...
No Matches
CppCommandLine.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: CppCommandLine.hpp
4//
5// For more information, please see: http://www.nektar.info/
6//
7// The MIT License
8//
9// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10// Department of Aeronautics, Imperial College London (UK), and Scientific
11// Computing and Imaging Institute, University of Utah (USA).
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description: Utility class to define C++ command line arguments.
32//
33///////////////////////////////////////////////////////////////////////////////
34
35#include <vector>
36
37#ifndef NEKTAR_LIBUTILITIES_BASICUTILS_CPPCOMMANDLINE_HPP
38#define NEKTAR_LIBUTILITIES_BASICUTILS_CPPCOMMANDLINE_HPP
39
41{
42
43/**
44 * @brief Helper structure to construct C++ command line `argc` and `argv`
45 * variables from a C++ vector.
46 *
47 * This is a useful class when setting up a call to
48 * SessionReader::CreateInstance, since arguments must be constructed to the
49 * right memory addresses, otherwise MPI (specifically OpenMPI) often tends to
50 * segfault.
51 */
53{
54 CppCommandLine() = default;
55
56 /**
57 * @brief Constructor.
58 *
59 * @param argv List of command line arguments.
60 */
61 CppCommandLine(std::vector<std::string> argv)
62 {
63 Setup(argv);
64 }
65
66 /**
67 * @brief Destructor.
68 */
70 {
71 if (m_argv == nullptr)
72 {
73 return;
74 }
75
76 // Only single pointer delete is required since storage is in m_buf.
77 delete[] m_argv;
78 }
79
80 /**
81 * @brief Returns the constructed `argv`.
82 */
83 char **GetArgv()
84 {
85 return m_argv;
86 }
87
88 /**
89 * @brief Returns the constructed `argc`.
90 */
91 int GetArgc()
92 {
93 return m_argc;
94 }
95
96protected:
97 void Setup(std::vector<std::string> &argv)
98 {
99 int i = 0;
100 size_t bufSize = 0;
101 char *p;
102
103 m_argc = argv.size();
104 m_argv = new char *[m_argc + 1];
105
106 // Create argc, argv to give to the session reader. Note that this needs
107 // to be a contiguous block in memory, otherwise MPI (specifically
108 // OpenMPI) will likely segfault.
109 for (i = 0; i < m_argc; ++i)
110 {
111 bufSize += argv[i].size() + 1;
112 }
113
114 m_buf.resize(bufSize);
115 for (i = 0, p = &m_buf[0]; i < m_argc; ++i)
116 {
117 std::string tmp = argv[i];
118 std::copy(tmp.begin(), tmp.end(), p);
119 p[tmp.size()] = '\0';
120 m_argv[i] = p;
121 p += tmp.size() + 1;
122 }
123
124 m_argv[m_argc] = nullptr;
125 }
126
127private:
128 /// Pointers for strings `argv`.
129 char **m_argv = nullptr;
130 /// Number of arguments `argc`.
131 int m_argc = 0;
132 /// Buffer for storage of the argument strings.
133 std::vector<char> m_buf;
134};
135
136} // namespace Nektar::LibUtilities
137
138#endif
Helper structure to construct C++ command line argc and argv variables from a C++ vector.
void Setup(std::vector< std::string > &argv)
std::vector< char > m_buf
Buffer for storage of the argument strings.
int GetArgc()
Returns the constructed argc.
char ** m_argv
Pointers for strings argv.
int m_argc
Number of arguments argc.
CppCommandLine(std::vector< std::string > argv)
Constructor.
char ** GetArgv()
Returns the constructed argv.