misc::cstring Class Reference

A smart pointer class for C strings. More...

#include <string.h>

List of all members.

Public Member Functions

 cstring ()
 Default onstructor.
 cstring (char const *str)
 Constructor initializing the encapsulated string.
 cstring (char const *str, size_t len)
 Constructor setting the encapsulated string to a portion of a string.
 cstring (cstring const &cs)
 Copy constructor.
cstringoperator= (cstring const &right)
 Assignment operator (for cstring r-values).
cstringoperator= (char const *right)
 Assigns a new string.
bool operator== (char const *right) const
 Checks two strings for equality, byte-per-byte.
bool operator!= (char const *right) const
 Checks two strings for difference, byte-per-byte.
 operator char * ()
 Conversion operator (to char *).
 operator char const * () const
 Conversion operator (to const char *).
cstringassign (char const *str)
 Assigns a new string.
cstringassign (char const *str, size_t len)
 Assigns a limited portion of a string.
int compare (char const *str) const
 Compares to a string, byte-per-byte.
int compare (char const *str, size_t len) const
 Compares to a limited portion of a string, byte-per-byte.
char * reserve (size_t size)
 Reserves a minimum size of writable memory.
void swap (misc::cstring &str)
 Swaps encapsulated strings of two cstring objects.
char * str ()
 Returns The encapsulated string.
char const * str () const
 Returns the encapsulated string (const version).


Detailed Description

This smart pointer class encapsulates a C null-terminated string, makes sure it is freed when the class is destructed and automatically duplicates strings passed to it through operator=.

This class is suitable for small memory requirements.


Constructor & Destructor Documentation

misc::cstring::cstring ( char const *  str  )  [inline, explicit]

This constructor initializes the encapsulated string to a duplicate of the given string.

Parameters:
[in] str The string to copy.

misc::cstring::cstring ( char const *  str,
size_t  len 
) [inline]

This constructor initializes the encapsulated string to a duplicate of a limited portion of the given string.

Parameters:
[in] str The string to copy. If it is NULL, then the encapsulated string is set to NULL and len is ignored.
[in] len The maximum number of characters to copy.


Member Function Documentation

cstring& misc::cstring::operator= ( char const *  right  )  [inline]

Assigns a new string to the encapsulated string. The old string is automatically freed.

Parameters:
[in] right The string to copy.
Returns:
The cstring object.

bool misc::cstring::operator== ( char const *  right  )  const

Does a byte-per-byte comparison of the encapsulated string and the given one.

Parameters:
[in] right The string to compare to the encapsulated string.
Returns:
true if the two strings are the same or are both NULL, false if they're different or if only one of the strings is NULL.

bool misc::cstring::operator!= ( char const *  right  )  const [inline]

Does a byte-per-byte comparison of the encapsulated string and the given one.

Parameters:
[in] right The string to compare to the encapsulated string.
Returns:
true if the two strings are different or if at least one of the strings is NULL, false otherwise.

cstring& misc::cstring::assign ( char const *  str  )  [inline]

Assigns a new string to the encapsulated string. The old string is automatically freed.

Parameters:
[in] str The string to copy. May be NULL.
Returns:
The cstring object.

cstring& misc::cstring::assign ( char const *  str,
size_t  len 
) [inline]

Assigns a limited portion of a string to the encapsulated string. The old string is automatically freed.

Parameters:
[in] str The string to copy. If it is NULL, then the encapsulated string is set to NULL and len is ignored.
[in] len The maximum number of characters to copy.
Returns:
The cstring object

int misc::cstring::compare ( char const *  str  )  const

Compares a string with the encapsulated string, byte-per-byte.

Parameters:
[in] str The string to compare.
Returns:
< 0, 0 or > 0 if the encapsulated string is respectively lexographically less than, equal to or greater than the given string. If the encapsulated string is NULL, then -1 is returned, if the given string is NULL, then 1 is returned and if both strings are NULL, then 0 is returned.

int misc::cstring::compare ( char const *  str,
size_t  len 
) const

Compares a limited portion of a string with the encapsulated string, byte-per-byte.

Parameters:
[in] str The string to compare.
[in] len The maximum number of characters to compare in the strings.
Returns:
< 0, 0 or > 0 if the encapsulated string is respectively lexographically less than, equal to or greater than the given string. If one or both strings are false, then -1 is returned.

char* misc::cstring::reserve ( size_t  size  ) 

Reserves a minimum size of writable memory for the enclosed string (this includes the NULL termination character). The contents of the string currently assigned is lost.

Parameters:
[in] size The size of the memory to reserve. Note that if this parameter has the value 0, then this function is an no-op.
Returns:
a pointer to the enclosed string.

void misc::cstring::swap ( misc::cstring str  ) 

Swaps encapsulated strings of two cstring objects. This is more optimized than normal swapping since no reallocation is done, the encapsulated pointers are simply exchanged.

Parameters:
[in] str The string to swap.


The documentation for this class was generated from the following file:
Generated on Wed Aug 15 00:37:23 2007 for Epona API by  doxygen 1.5.2