Public Types | Public Member Functions | Public Attributes | Private Member Functions | Static Private Attributes | Related Functions | List of all members
GenericStringRef< CharType > Struct Template Reference

Reference to a constant string (not taking a copy) More...

#include <document.h>

Public Types

typedef CharType Ch
 character type of the string More...
 

Public Member Functions

template<SizeType N>
 GenericStringRef (const CharType(&str)[N]) RAPIDJSON_NOEXCEPT
 Create string reference from const character array. More...
 
 GenericStringRef (const CharType *str)
 Explicitly create string reference from const character pointer. More...
 
 GenericStringRef (const CharType *str, SizeType len)
 Create constant string reference from pointer and length. More...
 
 GenericStringRef (const GenericStringRef &rhs)
 
 operator const Ch * () const
 implicit conversion to plain CharType pointer More...
 

Public Attributes

const Ch *const s
 plain CharType pointer More...
 
const SizeType length
 length of the string (excluding the trailing NULL terminator) More...
 

Private Member Functions

SizeType NotNullStrLen (const CharType *str)
 
template<SizeType N>
 GenericStringRef (CharType(&str)[N])
 Disallow construction from non-const array. More...
 
GenericStringRefoperator= (const GenericStringRef &rhs)
 Copy assignment operator not permitted - immutable type. More...
 

Static Private Attributes

static const Ch emptyString [] = { CharType() }
 Empty string - used when passing in a NULL pointer. More...
 

Related Functions

(Note that these are not member functions.)

template<typename CharType >
GenericStringRef< CharType > StringRef (const CharType *str)
 Mark a character pointer as constant string. More...
 
template<typename CharType >
GenericStringRef< CharType > StringRef (const CharType *str, size_t length)
 Mark a character pointer as constant string. More...
 

Detailed Description

template<typename CharType>
struct GenericStringRef< CharType >

Reference to a constant string (not taking a copy)

Template Parameters
CharTypecharacter type of the string

This helper class is used to automatically infer constant string references for string literals, especially from const (!) character arrays.

The main use is for creating JSON string values without copying the source string via an Allocator. This requires that the referenced string pointers have a sufficient lifetime, which exceeds the lifetime of the associated GenericValue.

Example

Value v("foo"); // ok, no need to copy & calculate length
const char foo[] = "foo";
v.SetString(foo); // ok
const char* bar = foo;
// Value x(bar); // not ok, can't rely on bar's lifetime
Value x(StringRef(bar)); // lifetime explicitly guaranteed by user
Value y(StringRef(bar, 3)); // ok, explicitly pass length
See also
StringRef, GenericValue::SetString

Definition at line 253 of file document.h.

Member Typedef Documentation

template<typename CharType>
typedef CharType GenericStringRef< CharType >::Ch

character type of the string

Definition at line 254 of file document.h.

Constructor & Destructor Documentation

template<typename CharType>
template<SizeType N>
GenericStringRef< CharType >::GenericStringRef ( const CharType(&)  str[N])
inline

Create string reference from const character array.

This constructor implicitly creates a constant string reference from a const character array. It has better performance than StringRef(const CharType*) by inferring the string length from the array length, and also supports strings containing null characters.

Template Parameters
Nlength of the string, automatically inferred
Parameters
strConstant character array, lifetime assumed to be longer than the use of the string in e.g. a GenericValue
Postcondition
s == str
Note
Constant complexity.
There is a hidden, private overload to disallow references to non-const character arrays to be created via this constructor. By this, e.g. function-scope arrays used to be filled via snprintf are excluded from consideration. In such cases, the referenced string should be copied to the GenericValue instead.

Definition at line 282 of file document.h.

283  : s(str), length(N-1) {}
const Ch *const s
plain CharType pointer
Definition: document.h:326
const SizeType length
length of the string (excluding the trailing NULL terminator)
Definition: document.h:327
static QCString str
template<typename CharType>
GenericStringRef< CharType >::GenericStringRef ( const CharType *  str)
inlineexplicit

Explicitly create string reference from const character pointer.

This constructor can be used to explicitly create a reference to a constant string pointer.

See also
StringRef(const CharType*)
Parameters
strConstant character pointer, lifetime assumed to be longer than the use of the string in e.g. a GenericValue
Postcondition
s == str
Note
There is a hidden, private overload to disallow references to non-const character arrays to be created via this constructor. By this, e.g. function-scope arrays used to be filled via snprintf are excluded from consideration. In such cases, the referenced string should be copied to the GenericValue instead.

Definition at line 306 of file document.h.

307  : s(str), length(NotNullStrLen(str)) {}
SizeType NotNullStrLen(const CharType *str)
Definition: document.h:330
const Ch *const s
plain CharType pointer
Definition: document.h:326
const SizeType length
length of the string (excluding the trailing NULL terminator)
Definition: document.h:327
static QCString str
template<typename CharType>
GenericStringRef< CharType >::GenericStringRef ( const CharType *  str,
SizeType  len 
)
inline

Create constant string reference from pointer and length.

Parameters
strconstant string, lifetime assumed to be longer than the use of the string in e.g. a GenericValue
lenlength of the string, excluding the trailing NULL terminator
Postcondition
s == str && length == len
Note
Constant complexity.

Definition at line 318 of file document.h.

319  : s(RAPIDJSON_LIKELY(str) ? str : emptyString), length(len) { RAPIDJSON_ASSERT(str != 0 || len == 0u); }
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:406
const Ch *const s
plain CharType pointer
Definition: document.h:326
const SizeType length
length of the string (excluding the trailing NULL terminator)
Definition: document.h:327
#define RAPIDJSON_LIKELY(x)
Compiler branching hint for expression with high probability to be true.
Definition: rapidjson.h:463
static const Ch emptyString[]
Empty string - used when passing in a NULL pointer.
Definition: document.h:336
static QCString str
template<typename CharType>
GenericStringRef< CharType >::GenericStringRef ( const GenericStringRef< CharType > &  rhs)
inline

Definition at line 321 of file document.h.

321 : s(rhs.s), length(rhs.length) {}
const Ch *const s
plain CharType pointer
Definition: document.h:326
const SizeType length
length of the string (excluding the trailing NULL terminator)
Definition: document.h:327
template<typename CharType>
template<SizeType N>
GenericStringRef< CharType >::GenericStringRef ( CharType(&)  str[N])
private

Disallow construction from non-const array.

Member Function Documentation

template<typename CharType>
SizeType GenericStringRef< CharType >::NotNullStrLen ( const CharType *  str)
inlineprivate

Definition at line 330 of file document.h.

330  {
331  RAPIDJSON_ASSERT(str != 0);
332  return internal::StrLen(str);
333  }
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:406
SizeType StrLen(const Ch *s)
Custom strlen() which works on different character types.
Definition: strfunc.h:31
static QCString str
template<typename CharType>
GenericStringRef< CharType >::operator const Ch * ( ) const
inline

implicit conversion to plain CharType pointer

Definition at line 324 of file document.h.

324 { return s; }
const Ch *const s
plain CharType pointer
Definition: document.h:326
template<typename CharType>
GenericStringRef& GenericStringRef< CharType >::operator= ( const GenericStringRef< CharType > &  rhs)
private

Copy assignment operator not permitted - immutable type.

Friends And Related Function Documentation

template<typename CharType >
GenericStringRef< CharType > StringRef ( const CharType *  str)
related

Mark a character pointer as constant string.

Mark a plain character pointer as a "string literal". This function can be used to avoid copying a character string to be referenced as a value in a JSON GenericValue object, if the string's lifetime is known to be valid long enough.

Template Parameters
CharTypeCharacter type of the string
Parameters
strConstant string, lifetime assumed to be longer than the use of the string in e.g. a GenericValue
Returns
GenericStringRef string reference object
See also
GenericValue::GenericValue(StringRefType), GenericValue::operator=(StringRefType), GenericValue::SetString(StringRefType), GenericValue::PushBack(StringRefType, Allocator&), GenericValue::AddMember

Definition at line 361 of file document.h.

361  {
363 }
Reference to a constant string (not taking a copy)
Definition: document.h:253
static QCString str
template<typename CharType >
GenericStringRef< CharType > StringRef ( const CharType *  str,
size_t  length 
)
related

Mark a character pointer as constant string.

Mark a plain character pointer as a "string literal". This function can be used to avoid copying a character string to be referenced as a value in a JSON GenericValue object, if the string's lifetime is known to be valid long enough.

This version has better performance with supplied length, and also supports string containing null characters.

Template Parameters
CharTypecharacter type of the string
Parameters
strConstant string, lifetime assumed to be longer than the use of the string in e.g. a GenericValue
lengthThe length of source string.
Returns
GenericStringRef string reference object

Definition at line 381 of file document.h.

381  {
383 }
RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.)
Definition: rapidjson.h:384
const SizeType length
length of the string (excluding the trailing NULL terminator)
Definition: document.h:327
Reference to a constant string (not taking a copy)
Definition: document.h:253
static QCString str

Member Data Documentation

template<typename CharType>
const CharType GenericStringRef< CharType >::emptyString = { CharType() }
staticprivate

Empty string - used when passing in a NULL pointer.

Definition at line 336 of file document.h.

template<typename CharType>
const SizeType GenericStringRef< CharType >::length

length of the string (excluding the trailing NULL terminator)

Definition at line 327 of file document.h.

template<typename CharType>
const Ch* const GenericStringRef< CharType >::s

plain CharType pointer

Definition at line 326 of file document.h.


The documentation for this struct was generated from the following file: