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 53 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 62 of file CppCommandLine.hpp.

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

References Setup().

◆ ~CppCommandLine()

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

Destructor.

Definition at line 70 of file CppCommandLine.hpp.

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 }
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 92 of file CppCommandLine.hpp.

93 {
94 return m_argc;
95 }
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 84 of file CppCommandLine.hpp.

85 {
86 return m_argv;
87 }

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 98 of file CppCommandLine.hpp.

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 }
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 132 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 130 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 134 of file CppCommandLine.hpp.

Referenced by Setup().