Nektar++
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Private Attributes | List of all members
Nektar::LibUtilities::CppCommandLine Struct Reference

Helper structure to construct C++ command line argc and argv variables from a C++ vector. More...

#include <CppCommandLine.hpp>

Inheritance diagram for Nektar::LibUtilities::CppCommandLine:
[legend]

Public Member Functions

 CppCommandLine ()=default
 
 CppCommandLine (std::vector< std::string > argv)
 Constructor.
 
 ~CppCommandLine ()
 Destructor.
 
char ** GetArgv ()
 Returns the constructed argv.
 
int GetArgc ()
 Returns the constructed argc.
 

Protected Member Functions

void Setup (std::vector< std::string > &argv)
 

Private Attributes

char ** m_argv = nullptr
 Pointers for strings argv.
 
int m_argc = 0
 Number of arguments argc.
 
std::vector< char > m_buf
 Buffer for storage of the argument strings.
 

Detailed Description

Helper structure to construct C++ command line argc and argv variables from a C++ vector.

This is a useful class when setting up a call to SessionReader::CreateInstance, since arguments must be constructed to the right memory addresses, otherwise MPI (specifically OpenMPI) often tends to segfault.

Definition at line 52 of file CppCommandLine.hpp.

Constructor & Destructor Documentation

◆ CppCommandLine() [1/2]

Nektar::LibUtilities::CppCommandLine::CppCommandLine ( )
default

◆ CppCommandLine() [2/2]

Nektar::LibUtilities::CppCommandLine::CppCommandLine ( std::vector< std::string >  argv)
inline

Constructor.

Parameters
argvList of command line arguments.

Definition at line 61 of file CppCommandLine.hpp.

62 {
63 Setup(argv);
64 }
void Setup(std::vector< std::string > &argv)

References Setup().

◆ ~CppCommandLine()

Nektar::LibUtilities::CppCommandLine::~CppCommandLine ( )
inline

Destructor.

Definition at line 69 of file CppCommandLine.hpp.

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 }
char ** m_argv
Pointers for strings argv.

References m_argv.

Member Function Documentation

◆ GetArgc()

int Nektar::LibUtilities::CppCommandLine::GetArgc ( )
inline

Returns the constructed argc.

Definition at line 91 of file CppCommandLine.hpp.

92 {
93 return m_argc;
94 }
int m_argc
Number of arguments argc.

References m_argc.

Referenced by Field_Init(), NewPartition(), and SessionReader_CreateInstance().

◆ GetArgv()

char ** Nektar::LibUtilities::CppCommandLine::GetArgv ( )
inline

Returns the constructed argv.

Definition at line 83 of file CppCommandLine.hpp.

84 {
85 return m_argv;
86 }

References m_argv.

Referenced by Field_Init(), NewPartition(), and SessionReader_CreateInstance().

◆ Setup()

void Nektar::LibUtilities::CppCommandLine::Setup ( std::vector< std::string > &  argv)
inlineprotected

Definition at line 97 of file CppCommandLine.hpp.

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 }
std::vector< double > p(NPUPPER)
std::vector< char > m_buf
Buffer for storage of the argument strings.

References m_argc, m_argv, and m_buf.

Referenced by CppCommandLine(), and PyCppCommandLine::PyCppCommandLine().

Member Data Documentation

◆ m_argc

int Nektar::LibUtilities::CppCommandLine::m_argc = 0
private

Number of arguments argc.

Definition at line 131 of file CppCommandLine.hpp.

Referenced by GetArgc(), and Setup().

◆ m_argv

char** Nektar::LibUtilities::CppCommandLine::m_argv = nullptr
private

Pointers for strings argv.

Definition at line 129 of file CppCommandLine.hpp.

Referenced by GetArgv(), Setup(), and ~CppCommandLine().

◆ m_buf

std::vector<char> Nektar::LibUtilities::CppCommandLine::m_buf
private

Buffer for storage of the argument strings.

Definition at line 133 of file CppCommandLine.hpp.

Referenced by Setup().