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 <string>
36#include <vector>
37
38#ifndef NEKTAR_LIBUTILITIES_BASICUTILS_CPPCOMMANDLINE_HPP
39#define NEKTAR_LIBUTILITIES_BASICUTILS_CPPCOMMANDLINE_HPP
40
42{
43
44/**
45 * @brief Helper structure to construct C++ command line `argc` and `argv`
46 * variables from a C++ vector.
47 *
48 * This is a useful class when setting up a call to
49 * SessionReader::CreateInstance, since arguments must be constructed to the
50 * right memory addresses, otherwise MPI (specifically OpenMPI) often tends to
51 * segfault.
52 */
54{
55 CppCommandLine() = default;
56
57 /**
58 * @brief Constructor.
59 *
60 * @param argv List of command line arguments.
61 */
62 CppCommandLine(std::vector<std::string> argv)
63 {
64 Setup(argv);
65 }
66
67 /**
68 * @brief Destructor.
69 */
71 {
72 if (m_argv == nullptr)
73 {
74 return;
75 }
76
77 // Only single pointer delete is required since storage is in m_buf.
78 delete[] m_argv;
79 }
80
81 /**
82 * @brief Returns the constructed `argv`.
83 */
84 char **GetArgv()
85 {
86 return m_argv;
87 }
88
89 /**
90 * @brief Returns the constructed `argc`.
91 */
92 int GetArgc()
93 {
94 return m_argc;
95 }
96
97protected:
98 void Setup(std::vector<std::string> &argv)
99 {
100 int i = 0;
101 size_t bufSize = 0;
102 char *p;
103
104 m_argc = argv.size();
105 m_argv = new char *[m_argc + 1];
106
107 // Create argc, argv to give to the session reader. Note that this needs
108 // to be a contiguous block in memory, otherwise MPI (specifically
109 // OpenMPI) will likely segfault.
110 for (i = 0; i < m_argc; ++i)
111 {
112 bufSize += argv[i].size() + 1;
113 }
114
115 m_buf.resize(bufSize);
116 for (i = 0, p = &m_buf[0]; i < m_argc; ++i)
117 {
118 std::string tmp = argv[i];
119 std::copy(tmp.begin(), tmp.end(), p);
120 p[tmp.size()] = '\0';
121 m_argv[i] = p;
122 p += tmp.size() + 1;
123 }
124
125 m_argv[m_argc] = nullptr;
126 }
127
128private:
129 /// Pointers for strings `argv`.
130 char **m_argv = nullptr;
131 /// Number of arguments `argc`.
132 int m_argc = 0;
133 /// Buffer for storage of the argument strings.
134 std::vector<char> m_buf;
135};
136
137} // namespace Nektar::LibUtilities
138
139#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.