CStringList Class Reference

An adapter for copying a list of C++ strings into a list of C-style strings for passing to C code that expects argc/argv parameters. More...

Public Member Functions

constexpr CStringList () noexcept
 Default construct, adding content later (via reset). More...
 
template<class StringType >
 CStringList (const UList< StringType > &input)
 Copy construct from a list of strings. More...
 
template<class StringType >
 CStringList (const SubStrings< StringType > &input)
 Copy construct from a list of sub-string references. More...
 
 ~CStringList ()
 Destructor. Invokes clear() to free memory. More...
 
bool empty () const noexcept
 True if the size (ie, argc) is zero. More...
 
int size () const noexcept
 Return the number of C-strings (ie, argc) More...
 
const char * cdata_bytes () const noexcept
 The flattened character content, with interspersed nul-chars. More...
 
size_t size_bytes () const noexcept
 
const char * data () const noexcept
 Same as cdata_bytes() More...
 
size_t length () const noexcept
 Same as size_bytes() More...
 
const char * get (int i) const
 Return string element at the given index. No bounds checking. More...
 
char ** strings () const noexcept
 Return the list of C-strings (ie, argv) More...
 
char ** strings (int start) const
 
void clear ()
 Clear contents and free memory. More...
 
template<class StringType >
int reset (const UList< StringType > &input)
 Copy the input list of strings. More...
 
template<class StringType >
int reset (const SubStrings< StringType > &input)
 Copy the input list of strings. More...
 
const char * operator[] (int i) const
 Return element at the given index. No bounds checking. More...
 
template<class StringType >
Foam::List< StringType > asList (int argc, const char *const argv[])
 
template<class StringType >
Foam::List< StringType > asList (const char *const argv[])
 

Static Public Member Functions

static int count (const char *const argv[])
 Count the number of parameters until the first nullptr. More...
 
template<class StringType >
static List< StringType > asList (int argc, const char *const argv[])
 Create a list from argc/argv parameters. More...
 
template<class StringType >
static List< StringType > asList (const char *const argv[])
 Create a list from a nullptr-terminated list of argv parameters. More...
 

Detailed Description

An adapter for copying a list of C++ strings into a list of C-style strings for passing to C code that expects argc/argv parameters.

In addition to providing a C-compatible list of C-strings, the string lists are flattened into a single string of data that can be also be passed en mass.

Example use:

wordList myStrings; ...
CStringList cstr(myStrings);
// pass as argc, argv:
someMain(cstr.size(), cstr.strings());
// access the raw characters:
os.write(cstr.data(), cstr.length());
virtual Ostream & write(const char c)
Write character.
Definition: OBJstream.C:78
OBJstream os(runTime.globalPath()/outputName)

Definition at line 69 of file CStringList.H.

Constructor & Destructor Documentation

◆ CStringList() [1/3]

constexpr CStringList ( )
inlineconstexprnoexcept

Default construct, adding content later (via reset).

Definition at line 63 of file CStringListI.H.

◆ CStringList() [2/3]

CStringList ( const UList< StringType > &  input)
inlineexplicit

Copy construct from a list of strings.

Copies the input characters.

Definition at line 73 of file CStringListI.H.

References Foam::input(), and CStringList::reset().

Here is the call graph for this function:

◆ CStringList() [3/3]

CStringList ( const SubStrings< StringType > &  input)
inlineexplicit

Copy construct from a list of sub-string references.

Copies the input characters.

Definition at line 82 of file CStringListI.H.

References Foam::input(), and CStringList::reset().

Here is the call graph for this function:

◆ ~CStringList()

~CStringList ( )
inline

Destructor. Invokes clear() to free memory.

Definition at line 92 of file CStringListI.H.

References clear().

Here is the call graph for this function:

Member Function Documentation

◆ count()

int count ( const char *const  argv[])
inlinestatic

Count the number of parameters until the first nullptr.

Return 0 if argv is nullptr.

Definition at line 47 of file CStringListI.H.

References n.

◆ empty()

bool empty ( ) const
inlinenoexcept

True if the size (ie, argc) is zero.

Definition at line 118 of file CStringListI.H.

Referenced by Foam::system().

Here is the caller graph for this function:

◆ size()

int size ( ) const
inlinenoexcept

Return the number of C-strings (ie, argc)

Definition at line 124 of file CStringListI.H.

Referenced by Foam::operator<<(), and Foam::system().

Here is the caller graph for this function:

◆ cdata_bytes()

const char * cdata_bytes ( ) const
inlinenoexcept

The flattened character content, with interspersed nul-chars.

Definition at line 146 of file CStringList.H.

◆ size_bytes()

size_t size_bytes ( ) const
inlinenoexcept

Overall length of the flattened character (data) content including interspersed nul-chars but not the trailing nul-char

Definition at line 150 of file CStringList.H.

◆ data()

const char * data ( ) const
inlinenoexcept

Same as cdata_bytes()

Definition at line 153 of file CStringList.H.

◆ length()

size_t length ( ) const
inlinenoexcept

Same as size_bytes()

Definition at line 156 of file CStringList.H.

◆ get()

const char * get ( int  i) const
inline

Return string element at the given index. No bounds checking.

Definition at line 158 of file CStringListI.H.

Referenced by Foam::operator<<().

Here is the caller graph for this function:

◆ strings() [1/2]

char ** strings ( ) const
inlinenoexcept

Return the list of C-strings (ie, argv)

The position at argc is a nullptr

Definition at line 130 of file CStringListI.H.

◆ strings() [2/2]

char ** strings ( int  start) const
inline

Return the sublist of C-strings (ie, argv) starting at the specified offset.

Parameters
startthe offset, must be less than argc

Definition at line 136 of file CStringListI.H.

◆ clear()

void clear ( )
inline

Clear contents and free memory.

Definition at line 100 of file CStringListI.H.

◆ reset() [1/2]

int reset ( const UList< StringType > &  input)
inline

Copy the input list of strings.

Returns
number of arguments (argc)

Definition at line 143 of file CStringListI.H.

References Foam::input().

Referenced by CStringList::CStringList().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reset() [2/2]

int reset ( const SubStrings< StringType > &  input)
inline

Copy the input list of strings.

Returns
number of arguments (argc)

Definition at line 150 of file CStringListI.H.

References Foam::input().

Here is the call graph for this function:

◆ asList() [1/4]

static List< StringType > asList ( int  argc,
const char *const  argv[] 
)
static

Create a list from argc/argv parameters.

A null pointer for argv is permissible when argc is zero.

◆ asList() [2/4]

static List< StringType > asList ( const char *const  argv[])
inlinestatic

Create a list from a nullptr-terminated list of argv parameters.

Using a nullptr for argv is permissible.

◆ operator[]()

const char * operator[] ( int  i) const
inline

Return element at the given index. No bounds checking.

Definition at line 164 of file CStringListI.H.

◆ asList() [3/4]

Foam::List< StringType > asList ( int  argc,
const char *const  argv[] 
)

Definition at line 73 of file CStringListTemplates.C.

◆ asList() [4/4]

Foam::List< StringType > asList ( const char *const  argv[])

Definition at line 88 of file CStringListTemplates.C.


The documentation for this class was generated from the following files: