Classes | Public Types | Public Member Functions | Public Attributes | Private Member Functions | Private Attributes | List of all members
GenericPointer< ValueType, Allocator > Class Template Reference

Represents a JSON Pointer. Use Pointer for UTF8 encoding and default allocator. More...

#include <fwd.h>

Classes

class  PercentDecodeStream
 A helper stream for decoding a percent-encoded sequence into code unit. More...
 
class  PercentEncodeStream
 A helper stream to encode character (UTF-8 code unit) into percent-encoded sequence. More...
 
struct  Token
 A token is the basic units of internal representation. More...
 

Public Types

typedef ValueType::EncodingType EncodingType
 Encoding type from Value. More...
 
typedef ValueType::Ch Ch
 Character type from Value. More...
 

Public Member Functions

Allocator stackAllocator RAPIDJSON_DISABLEIF_RETURN ((internal::OrExpr< internal::IsPointer< T >, internal::IsGenericValue< T > >),(ValueType &)) GetWithDefault(GenericDocument< EncodingType
 
bool Erase (ValueType &root) const
 Erase a value in a subtree. More...
 
Constructors and destructor.
 GenericPointer (Allocator *allocator=0)
 Default constructor. More...
 
 GenericPointer (const Ch *source, Allocator *allocator=0)
 Constructor that parses a string or URI fragment representation. More...
 
 GenericPointer (const Ch *source, size_t length, Allocator *allocator=0)
 Constructor that parses a string or URI fragment representation, with length of the source string. More...
 
 GenericPointer (const Token *tokens, size_t tokenCount)
 Constructor with user-supplied tokens. More...
 
 GenericPointer (const GenericPointer &rhs)
 Copy constructor. More...
 
 GenericPointer (const GenericPointer &rhs, Allocator *allocator)
 Copy constructor. More...
 
 ~GenericPointer ()
 Destructor. More...
 
GenericPointeroperator= (const GenericPointer &rhs)
 Assignment operator. More...
 
Append token
GenericPointer Append (const Token &token, Allocator *allocator=0) const
 Append a token and return a new Pointer. More...
 
GenericPointer Append (const Ch *name, SizeType length, Allocator *allocator=0) const
 Append a name token with length, and return a new Pointer. More...
 
template<typename T >
 RAPIDJSON_DISABLEIF_RETURN ((internal::NotExpr< internal::IsSame< typename internal::RemoveConst< T >::Type, Ch > >),(GenericPointer)) Append(T *name
 Append a name token without length, and return a new Pointer. More...
 
Swap a value
ValueType & Swap (ValueType &root, ValueType &value, typename ValueType::AllocatorType &allocator) const
 Swap a value with a value in a subtree. More...
 
template<typename stackAllocator >
ValueType & Swap (GenericDocument< EncodingType, typename ValueType::AllocatorType, stackAllocator > &document, ValueType &value) const
 Swap a value with a value in a document. More...
 

Public Attributes

Allocator * allocator
 
Allocator stackAllocator stackAllocator & document
 
Allocator stackAllocator stackAllocator T defaultValue const
 

Private Member Functions

ChCopyFromRaw (const GenericPointer &rhs, size_t extraToken=0, size_t extraNameBufferSize=0)
 Clone the content from rhs to this. More...
 
bool NeedPercentEncode (Ch c) const
 Check whether a character should be percent-encoded. More...
 
void Parse (const Ch *source, size_t length)
 Parse a JSON String or its URI fragment representation into tokens. More...
 
template<bool uriFragment, typename OutputStream >
bool Stringify (OutputStream &os) const
 Stringify to string or URI fragment representation. More...
 

Private Attributes

Allocator * allocator_
 The current allocator. It is either user-supplied or equal to ownAllocator_. More...
 
Allocator * ownAllocator_
 Allocator owned by this Pointer. More...
 
ChnameBuffer_
 A buffer containing all names in tokens. More...
 
Tokentokens_
 A list of tokens. More...
 
size_t tokenCount_
 Number of tokens in tokens_. More...
 
size_t parseErrorOffset_
 Offset in code unit when parsing fail. More...
 
PointerParseErrorCode parseErrorCode_
 Parsing error code. More...
 

Set a value

value
 
T ValueType::AllocatorType &allocator const
 
stackAllocator & document
 
stackAllocator T value const
 
ValueType & Set (ValueType &root, ValueType &value, typename ValueType::AllocatorType &allocator) const
 Set a value in a subtree, with move semantics. More...
 
ValueType & Set (ValueType &root, const ValueType &value, typename ValueType::AllocatorType &allocator) const
 Set a value in a subtree, with copy semantics. More...
 
ValueType & Set (ValueType &root, const Ch *value, typename ValueType::AllocatorType &allocator) const
 Set a null-terminated string in a subtree. More...
 
template<typename T >
 RAPIDJSON_DISABLEIF_RETURN ((internal::OrExpr< internal::IsPointer< T >, internal::IsGenericValue< T > >),(ValueType &)) Set(ValueType &root
 Set a primitive value in a subtree. More...
 
template<typename stackAllocator >
ValueType & Set (GenericDocument< EncodingType, typename ValueType::AllocatorType, stackAllocator > &document, ValueType &value) const
 Set a value in a document, with move semantics. More...
 
template<typename stackAllocator >
ValueType & Set (GenericDocument< EncodingType, typename ValueType::AllocatorType, stackAllocator > &document, const ValueType &value) const
 Set a value in a document, with copy semantics. More...
 
template<typename stackAllocator >
ValueType & Set (GenericDocument< EncodingType, typename ValueType::AllocatorType, stackAllocator > &document, const Ch *value) const
 Set a null-terminated string in a document. More...
 
template<typename T , typename stackAllocator >
 RAPIDJSON_DISABLEIF_RETURN ((internal::OrExpr< internal::IsPointer< T >, internal::IsGenericValue< T > >),(ValueType &)) Set(GenericDocument< EncodingType
 Set a primitive value in a document. More...
 

Detailed Description

template<typename ValueType, typename Allocator = CrtAllocator>
class GenericPointer< ValueType, Allocator >

Represents a JSON Pointer. Use Pointer for UTF8 encoding and default allocator.

This class implements RFC 6901 "JavaScript Object Notation (JSON) Pointer" (https://tools.ietf.org/html/rfc6901).

A JSON pointer is for identifying a specific value in a JSON document (GenericDocument). It can simplify coding of DOM tree manipulation, because it can access multiple-level depth of DOM tree with single API call.

After it parses a string representation (e.g. "/foo/0" or URI fragment representation (e.g. "#/foo/0") into its internal representation (tokens), it can be used to resolve a specific value in multiple documents, or sub-tree of documents.

Contrary to GenericValue, Pointer can be copy constructed and copy assigned. Apart from assignment, a Pointer cannot be modified after construction.

Although Pointer is very convenient, please aware that constructing Pointer involves parsing and dynamic memory allocation. A special constructor with user- supplied tokens eliminates these.

GenericPointer depends on GenericDocument and GenericValue.

Template Parameters
ValueTypeThe value type of the DOM tree. E.g. GenericValue<UTF8<> >
AllocatorThe allocator type for allocating memory for internal representation.
Note
GenericPointer uses same encoding of ValueType. However, Allocator of GenericPointer is independent of Allocator of Value.

Definition at line 126 of file fwd.h.

Member Typedef Documentation

template<typename ValueType, typename Allocator = CrtAllocator>
typedef ValueType::Ch GenericPointer< ValueType, Allocator >::Ch

Character type from Value.

Definition at line 82 of file pointer.h.

template<typename ValueType, typename Allocator = CrtAllocator>
typedef ValueType::EncodingType GenericPointer< ValueType, Allocator >::EncodingType

Encoding type from Value.

Definition at line 81 of file pointer.h.

Constructor & Destructor Documentation

template<typename ValueType, typename Allocator = CrtAllocator>
GenericPointer< ValueType, Allocator >::GenericPointer ( Allocator *  allocator = 0)
inline

Default constructor.

Definition at line 107 of file pointer.h.

Ch * nameBuffer_
A buffer containing all names in tokens.
Definition: pointer.h:1044
PointerParseErrorCode parseErrorCode_
Parsing error code.
Definition: pointer.h:1048
Token * tokens_
A list of tokens.
Definition: pointer.h:1045
size_t parseErrorOffset_
Offset in code unit when parsing fail.
Definition: pointer.h:1047
size_t tokenCount_
Number of tokens in tokens_.
Definition: pointer.h:1046
Allocator * allocator
Definition: pointer.h:245
The parse is successful.
Definition: pointer.h:38
Allocator * allocator_
The current allocator. It is either user-supplied or equal to ownAllocator_.
Definition: pointer.h:1042
Allocator * ownAllocator_
Allocator owned by this Pointer.
Definition: pointer.h:1043
template<typename ValueType, typename Allocator = CrtAllocator>
GenericPointer< ValueType, Allocator >::GenericPointer ( const Ch source,
Allocator *  allocator = 0 
)
inlineexplicit

Constructor that parses a string or URI fragment representation.

Parameters
sourceA null-terminated, string or URI fragment representation of JSON pointer.
allocatorUser supplied allocator for this pointer. If no allocator is provided, it creates a self-owned one.

Definition at line 114 of file pointer.h.

116  }
const CharType(& source)[N]
Definition: pointer.h:1147
void Parse(const Ch *source, size_t length)
Parse a JSON String or its URI fragment representation into tokens.
Definition: pointer.h:805
Ch * nameBuffer_
A buffer containing all names in tokens.
Definition: pointer.h:1044
PointerParseErrorCode parseErrorCode_
Parsing error code.
Definition: pointer.h:1048
Token * tokens_
A list of tokens.
Definition: pointer.h:1045
size_t parseErrorOffset_
Offset in code unit when parsing fail.
Definition: pointer.h:1047
size_t tokenCount_
Number of tokens in tokens_.
Definition: pointer.h:1046
Allocator * allocator
Definition: pointer.h:245
SizeType StrLen(const Ch *s)
Custom strlen() which works on different character types.
Definition: strfunc.h:31
The parse is successful.
Definition: pointer.h:38
Allocator * allocator_
The current allocator. It is either user-supplied or equal to ownAllocator_.
Definition: pointer.h:1042
Allocator * ownAllocator_
Allocator owned by this Pointer.
Definition: pointer.h:1043
template<typename ValueType, typename Allocator = CrtAllocator>
GenericPointer< ValueType, Allocator >::GenericPointer ( const Ch source,
size_t  length,
Allocator *  allocator = 0 
)
inline

Constructor that parses a string or URI fragment representation, with length of the source string.

Parameters
sourceA string or URI fragment representation of JSON pointer.
lengthLength of source.
allocatorUser supplied allocator for this pointer. If no allocator is provided, it creates a self-owned one.
Note
Slightly faster than the overload without length.

Definition at line 137 of file pointer.h.

138  Parse(source, length);
139  }
const CharType(& source)[N]
Definition: pointer.h:1147
void Parse(const Ch *source, size_t length)
Parse a JSON String or its URI fragment representation into tokens.
Definition: pointer.h:805
Ch * nameBuffer_
A buffer containing all names in tokens.
Definition: pointer.h:1044
PointerParseErrorCode parseErrorCode_
Parsing error code.
Definition: pointer.h:1048
Token * tokens_
A list of tokens.
Definition: pointer.h:1045
size_t parseErrorOffset_
Offset in code unit when parsing fail.
Definition: pointer.h:1047
size_t tokenCount_
Number of tokens in tokens_.
Definition: pointer.h:1046
Allocator * allocator
Definition: pointer.h:245
The parse is successful.
Definition: pointer.h:38
Allocator * allocator_
The current allocator. It is either user-supplied or equal to ownAllocator_.
Definition: pointer.h:1042
Allocator * ownAllocator_
Allocator owned by this Pointer.
Definition: pointer.h:1043
template<typename ValueType, typename Allocator = CrtAllocator>
GenericPointer< ValueType, Allocator >::GenericPointer ( const Token tokens,
size_t  tokenCount 
)
inline

Constructor with user-supplied tokens.

This constructor let user supplies const array of tokens. This prevents the parsing process and eliminates allocation. This is preferred for memory constrained environments.

Parameters
tokensAn constant array of tokens representing the JSON pointer.
tokenCountNumber of tokens.

Example

#define NAME(s) { s, sizeof(s) / sizeof(s[0]) - 1, kPointerInvalidIndex }
#define INDEX(i) { #i, sizeof(#i) - 1, i }
static const Pointer::Token kTokens[] = { NAME("foo"), INDEX(123) };
static const Pointer p(kTokens, sizeof(kTokens) / sizeof(kTokens[0]));
// Equivalent to static const Pointer p("/foo/123");
#undef NAME
#undef INDEX

Definition at line 163 of file pointer.h.

163 : allocator_(), ownAllocator_(), nameBuffer_(), tokens_(const_cast<Token*>(tokens)), tokenCount_(tokenCount), parseErrorOffset_(), parseErrorCode_(kPointerParseErrorNone) {}
Ch * nameBuffer_
A buffer containing all names in tokens.
Definition: pointer.h:1044
PointerParseErrorCode parseErrorCode_
Parsing error code.
Definition: pointer.h:1048
Token * tokens_
A list of tokens.
Definition: pointer.h:1045
size_t parseErrorOffset_
Offset in code unit when parsing fail.
Definition: pointer.h:1047
size_t tokenCount_
Number of tokens in tokens_.
Definition: pointer.h:1046
The parse is successful.
Definition: pointer.h:38
Allocator * allocator_
The current allocator. It is either user-supplied or equal to ownAllocator_.
Definition: pointer.h:1042
Allocator * ownAllocator_
Allocator owned by this Pointer.
Definition: pointer.h:1043
template<typename ValueType, typename Allocator = CrtAllocator>
GenericPointer< ValueType, Allocator >::GenericPointer ( const GenericPointer< ValueType, Allocator > &  rhs)
inline

Copy constructor.

Definition at line 166 of file pointer.h.

167  *this = rhs;
168  }
Ch * nameBuffer_
A buffer containing all names in tokens.
Definition: pointer.h:1044
PointerParseErrorCode parseErrorCode_
Parsing error code.
Definition: pointer.h:1048
Token * tokens_
A list of tokens.
Definition: pointer.h:1045
size_t parseErrorOffset_
Offset in code unit when parsing fail.
Definition: pointer.h:1047
size_t tokenCount_
Number of tokens in tokens_.
Definition: pointer.h:1046
The parse is successful.
Definition: pointer.h:38
Allocator * allocator_
The current allocator. It is either user-supplied or equal to ownAllocator_.
Definition: pointer.h:1042
Allocator * ownAllocator_
Allocator owned by this Pointer.
Definition: pointer.h:1043
template<typename ValueType, typename Allocator = CrtAllocator>
GenericPointer< ValueType, Allocator >::GenericPointer ( const GenericPointer< ValueType, Allocator > &  rhs,
Allocator *  allocator 
)
inline

Copy constructor.

Definition at line 171 of file pointer.h.

172  *this = rhs;
173  }
Ch * nameBuffer_
A buffer containing all names in tokens.
Definition: pointer.h:1044
PointerParseErrorCode parseErrorCode_
Parsing error code.
Definition: pointer.h:1048
Token * tokens_
A list of tokens.
Definition: pointer.h:1045
size_t parseErrorOffset_
Offset in code unit when parsing fail.
Definition: pointer.h:1047
size_t tokenCount_
Number of tokens in tokens_.
Definition: pointer.h:1046
Allocator * allocator
Definition: pointer.h:245
The parse is successful.
Definition: pointer.h:38
Allocator * allocator_
The current allocator. It is either user-supplied or equal to ownAllocator_.
Definition: pointer.h:1042
Allocator * ownAllocator_
Allocator owned by this Pointer.
Definition: pointer.h:1043
template<typename ValueType, typename Allocator = CrtAllocator>
GenericPointer< ValueType, Allocator >::~GenericPointer ( )
inline

Destructor.

Definition at line 176 of file pointer.h.

176  {
177  if (nameBuffer_) // If user-supplied tokens constructor is used, nameBuffer_ is nullptr and tokens_ are not deallocated.
178  Allocator::Free(tokens_);
180  }
Ch * nameBuffer_
A buffer containing all names in tokens.
Definition: pointer.h:1044
Token * tokens_
A list of tokens.
Definition: pointer.h:1045
#define RAPIDJSON_DELETE(x)
! customization point for global delete
Definition: rapidjson.h:605
Allocator * ownAllocator_
Allocator owned by this Pointer.
Definition: pointer.h:1043

Member Function Documentation

template<typename ValueType, typename Allocator = CrtAllocator>
GenericPointer GenericPointer< ValueType, Allocator >::Append ( const Token token,
Allocator *  allocator = 0 
) const
inline

Append a token and return a new Pointer.

Parameters
tokenToken to be appended.
allocatorAllocator for the newly return Pointer.
Returns
A new Pointer with appended token.

Definition at line 214 of file pointer.h.

214  {
216  r.allocator_ = allocator;
217  Ch *p = r.CopyFromRaw(*this, 1, token.length + 1);
218  std::memcpy(p, token.name, (token.length + 1) * sizeof(Ch));
219  r.tokens_[tokenCount_].name = p;
220  r.tokens_[tokenCount_].length = token.length;
221  r.tokens_[tokenCount_].index = token.index;
222  return r;
223  }
Ch * CopyFromRaw(const GenericPointer &rhs, size_t extraToken=0, size_t extraNameBufferSize=0)
Clone the content from rhs to this.
Definition: pointer.h:762
Token * tokens_
A list of tokens.
Definition: pointer.h:1045
SizeType index
A valid array index, if it is not equal to kPointerInvalidIndex.
Definition: pointer.h:100
SizeType length
Length of the name.
Definition: pointer.h:99
Represents a JSON Pointer. Use Pointer for UTF8 encoding and default allocator.
Definition: fwd.h:126
size_t tokenCount_
Number of tokens in tokens_.
Definition: pointer.h:1046
Allocator * allocator
Definition: pointer.h:245
ValueType::Ch Ch
Character type from Value.
Definition: pointer.h:82
p
Definition: test.py:223
const Ch * name
Name of the token. It has null character at the end but it can contain null character.
Definition: pointer.h:98
Allocator * allocator_
The current allocator. It is either user-supplied or equal to ownAllocator_.
Definition: pointer.h:1042
template<typename ValueType, typename Allocator = CrtAllocator>
GenericPointer GenericPointer< ValueType, Allocator >::Append ( const Ch name,
SizeType  length,
Allocator *  allocator = 0 
) const
inline

Append a name token with length, and return a new Pointer.

Parameters
nameName to be appended.
lengthLength of name.
allocatorAllocator for the newly return Pointer.
Returns
A new Pointer with appended token.

Definition at line 232 of file pointer.h.

232  {
233  Token token = { name, length, kPointerInvalidIndex };
234  return Append(token, allocator);
235  }
static QCString name
Definition: declinfo.cpp:673
static RAPIDJSON_NAMESPACE_BEGIN const SizeType kPointerInvalidIndex
Represents an invalid index in GenericPointer::Token.
Definition: pointer.h:31
Allocator * allocator
Definition: pointer.h:245
GenericPointer Append(const Token &token, Allocator *allocator=0) const
Append a token and return a new Pointer.
Definition: pointer.h:214
template<typename ValueType, typename Allocator = CrtAllocator>
Ch* GenericPointer< ValueType, Allocator >::CopyFromRaw ( const GenericPointer< ValueType, Allocator > &  rhs,
size_t  extraToken = 0,
size_t  extraNameBufferSize = 0 
)
inlineprivate

Clone the content from rhs to this.

Parameters
rhsSource pointer.
extraTokenExtra tokens to be allocated.
extraNameBufferSizeExtra name buffer size (in number of Ch) to be allocated.
Returns
Start of non-occupied name buffer, for storing extra names.

Definition at line 762 of file pointer.h.

762  {
763  if (!allocator_) // allocator is independently owned.
764  ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator)();
765 
766  size_t nameBufferSize = rhs.tokenCount_; // null terminators for tokens
767  for (Token *t = rhs.tokens_; t != rhs.tokens_ + rhs.tokenCount_; ++t)
768  nameBufferSize += t->length;
769 
770  tokenCount_ = rhs.tokenCount_ + extraToken;
771  tokens_ = static_cast<Token *>(allocator_->Malloc(tokenCount_ * sizeof(Token) + (nameBufferSize + extraNameBufferSize) * sizeof(Ch)));
772  nameBuffer_ = reinterpret_cast<Ch *>(tokens_ + tokenCount_);
773  if (rhs.tokenCount_ > 0) {
774  std::memcpy(tokens_, rhs.tokens_, rhs.tokenCount_ * sizeof(Token));
775  }
776  if (nameBufferSize > 0) {
777  std::memcpy(nameBuffer_, rhs.nameBuffer_, nameBufferSize * sizeof(Ch));
778  }
779 
780  // Adjust pointers to name buffer
781  std::ptrdiff_t diff = nameBuffer_ - rhs.nameBuffer_;
782  for (Token *t = tokens_; t != tokens_ + rhs.tokenCount_; ++t)
783  t->name += diff;
784 
785  return nameBuffer_ + nameBufferSize;
786  }
Ch * nameBuffer_
A buffer containing all names in tokens.
Definition: pointer.h:1044
Token * tokens_
A list of tokens.
Definition: pointer.h:1045
#define RAPIDJSON_NEW(TypeName)
! customization point for global new
Definition: rapidjson.h:601
size_t tokenCount_
Number of tokens in tokens_.
Definition: pointer.h:1046
ValueType::Ch Ch
Character type from Value.
Definition: pointer.h:82
Allocator * allocator_
The current allocator. It is either user-supplied or equal to ownAllocator_.
Definition: pointer.h:1042
Allocator * ownAllocator_
Allocator owned by this Pointer.
Definition: pointer.h:1043
template<typename ValueType, typename Allocator = CrtAllocator>
bool GenericPointer< ValueType, Allocator >::Erase ( ValueType &  root) const
inline

Erase a value in a subtree.

Parameters
rootRoot value of a DOM sub-tree to be resolved. It can be any value other than document root.
Returns
Whether the resolved value is found and erased.
Note
Erasing with an empty pointer Pointer(""), i.e. the root, always fail and return false.

Definition at line 714 of file pointer.h.

714  {
715  RAPIDJSON_ASSERT(IsValid());
716  if (tokenCount_ == 0) // Cannot erase the root
717  return false;
718 
719  ValueType* v = &root;
720  const Token* last = tokens_ + (tokenCount_ - 1);
721  for (const Token *t = tokens_; t != last; ++t) {
722  switch (v->GetType()) {
723  case kObjectType:
724  {
725  typename ValueType::MemberIterator m = v->FindMember(GenericStringRef<Ch>(t->name, t->length));
726  if (m == v->MemberEnd())
727  return false;
728  v = &m->value;
729  }
730  break;
731  case kArrayType:
732  if (t->index == kPointerInvalidIndex || t->index >= v->Size())
733  return false;
734  v = &((*v)[t->index]);
735  break;
736  default:
737  return false;
738  }
739  }
740 
741  switch (v->GetType()) {
742  case kObjectType:
743  return v->EraseMember(GenericStringRef<Ch>(last->name, last->length));
744  case kArrayType:
745  if (last->index == kPointerInvalidIndex || last->index >= v->Size())
746  return false;
747  v->Erase(v->Begin() + last->index);
748  return true;
749  default:
750  return false;
751  }
752  }
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:406
object
Definition: rapidjson.h:622
array
Definition: rapidjson.h:623
Token * tokens_
A list of tokens.
Definition: pointer.h:1045
static RAPIDJSON_NAMESPACE_BEGIN const SizeType kPointerInvalidIndex
Represents an invalid index in GenericPointer::Token.
Definition: pointer.h:31
size_t tokenCount_
Number of tokens in tokens_.
Definition: pointer.h:1046
Reference to a constant string (not taking a copy)
Definition: document.h:253
template<typename ValueType, typename Allocator = CrtAllocator>
bool GenericPointer< ValueType, Allocator >::NeedPercentEncode ( Ch  c) const
inlineprivate

Check whether a character should be percent-encoded.

According to RFC 3986 2.3 Unreserved Characters.

Parameters
cThe character (code unit) to be tested.

Definition at line 793 of file pointer.h.

793  {
794  return !((c >= '0' && c <= '9') || (c >= 'A' && c <='Z') || (c >= 'a' && c <= 'z') || c == '-' || c == '.' || c == '_' || c =='~');
795  }
template<typename ValueType, typename Allocator = CrtAllocator>
GenericPointer& GenericPointer< ValueType, Allocator >::operator= ( const GenericPointer< ValueType, Allocator > &  rhs)
inline

Assignment operator.

Definition at line 183 of file pointer.h.

183  {
184  if (this != &rhs) {
185  // Do not delete ownAllcator
186  if (nameBuffer_)
187  Allocator::Free(tokens_);
188 
189  tokenCount_ = rhs.tokenCount_;
192 
193  if (rhs.nameBuffer_)
194  CopyFromRaw(rhs); // Normally parsed tokens.
195  else {
196  tokens_ = rhs.tokens_; // User supplied const tokens.
197  nameBuffer_ = 0;
198  }
199  }
200  return *this;
201  }
Ch * CopyFromRaw(const GenericPointer &rhs, size_t extraToken=0, size_t extraNameBufferSize=0)
Clone the content from rhs to this.
Definition: pointer.h:762
Ch * nameBuffer_
A buffer containing all names in tokens.
Definition: pointer.h:1044
PointerParseErrorCode parseErrorCode_
Parsing error code.
Definition: pointer.h:1048
Token * tokens_
A list of tokens.
Definition: pointer.h:1045
size_t parseErrorOffset_
Offset in code unit when parsing fail.
Definition: pointer.h:1047
size_t tokenCount_
Number of tokens in tokens_.
Definition: pointer.h:1046
template<typename ValueType, typename Allocator = CrtAllocator>
void GenericPointer< ValueType, Allocator >::Parse ( const Ch source,
size_t  length 
)
inlineprivate

Parse a JSON String or its URI fragment representation into tokens.

Parameters
sourceEither a JSON Pointer string, or its URI fragment representation. Not need to be null terminated.
lengthLength of the source string.
Note
Source cannot be JSON String Representation of JSON Pointer, e.g. In "/\u0000", will not be unescaped.

Definition at line 805 of file pointer.h.

805  {
806  RAPIDJSON_ASSERT(source != NULL);
809 
810  // Create own allocator if user did not supply.
811  if (!allocator_)
812  ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator)();
813 
814  // Count number of '/' as tokenCount
815  tokenCount_ = 0;
816  for (const Ch* s = source; s != source + length; s++)
817  if (*s == '/')
818  tokenCount_++;
819 
820  Token* token = tokens_ = static_cast<Token *>(allocator_->Malloc(tokenCount_ * sizeof(Token) + length * sizeof(Ch)));
821  Ch* name = nameBuffer_ = reinterpret_cast<Ch *>(tokens_ + tokenCount_);
822  size_t i = 0;
823 
824  // Detect if it is a URI fragment
825  bool uriFragment = false;
826  if (source[i] == '#') {
827  uriFragment = true;
828  i++;
829  }
830 
831  if (i != length && source[i] != '/') {
833  goto error;
834  }
835 
836  while (i < length) {
837  RAPIDJSON_ASSERT(source[i] == '/');
838  i++; // consumes '/'
839 
840  token->name = name;
841  bool isNumber = true;
842 
843  while (i < length && source[i] != '/') {
844  Ch c = source[i];
845  if (uriFragment) {
846  // Decoding percent-encoding for URI fragment
847  if (c == '%') {
848  PercentDecodeStream is(&source[i], source + length);
850  Ch* begin = os.PutBegin();
851  if (!Transcoder<UTF8<>, EncodingType>().Validate(is, os) || !is.IsValid()) {
853  goto error;
854  }
855  size_t len = os.PutEnd(begin);
856  i += is.Tell() - 1;
857  if (len == 1)
858  c = *name;
859  else {
860  name += len;
861  isNumber = false;
862  i++;
863  continue;
864  }
865  }
866  else if (NeedPercentEncode(c)) {
868  goto error;
869  }
870  }
871 
872  i++;
873 
874  // Escaping "~0" -> '~', "~1" -> '/'
875  if (c == '~') {
876  if (i < length) {
877  c = source[i];
878  if (c == '0') c = '~';
879  else if (c == '1') c = '/';
880  else {
882  goto error;
883  }
884  i++;
885  }
886  else {
888  goto error;
889  }
890  }
891 
892  // First check for index: all of characters are digit
893  if (c < '0' || c > '9')
894  isNumber = false;
895 
896  *name++ = c;
897  }
898  token->length = static_cast<SizeType>(name - token->name);
899  if (token->length == 0)
900  isNumber = false;
901  *name++ = '\0'; // Null terminator
902 
903  // Second check for index: more than one digit cannot have leading zero
904  if (isNumber && token->length > 1 && token->name[0] == '0')
905  isNumber = false;
906 
907  // String to SizeType conversion
908  SizeType n = 0;
909  if (isNumber) {
910  for (size_t j = 0; j < token->length; j++) {
911  SizeType m = n * 10 + static_cast<SizeType>(token->name[j] - '0');
912  if (m < n) { // overflow detection
913  isNumber = false;
914  break;
915  }
916  n = m;
917  }
918  }
919 
920  token->index = isNumber ? n : kPointerInvalidIndex;
921  token++;
922  }
923 
924  RAPIDJSON_ASSERT(name <= nameBuffer_ + length); // Should not overflow buffer
926  return;
927 
928  error:
929  Allocator::Free(tokens_);
930  nameBuffer_ = 0;
931  tokens_ = 0;
932  tokenCount_ = 0;
933  parseErrorOffset_ = i;
934  return;
935  }
static QCString name
Definition: declinfo.cpp:673
Encoding conversion.
Definition: encodings.h:658
const CharType(& source)[N]
Definition: pointer.h:1147
RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.)
Definition: rapidjson.h:384
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:406
Ch * nameBuffer_
A buffer containing all names in tokens.
Definition: pointer.h:1044
ValueType::EncodingType EncodingType
Encoding type from Value.
Definition: pointer.h:81
Invalid percent encoding in URI fragment.
Definition: pointer.h:42
PointerParseErrorCode parseErrorCode_
Parsing error code.
Definition: pointer.h:1048
error
Definition: include.cc:26
Token * tokens_
A list of tokens.
Definition: pointer.h:1045
static RAPIDJSON_NAMESPACE_BEGIN const SizeType kPointerInvalidIndex
Represents an invalid index in GenericPointer::Token.
Definition: pointer.h:31
A token must begin with a &#39;/&#39;.
Definition: pointer.h:40
A read-write string stream.
Definition: fwd.h:52
size_t parseErrorOffset_
Offset in code unit when parsing fail.
Definition: pointer.h:1047
UTF-8 encoding.
Definition: encodings.h:96
#define RAPIDJSON_NEW(TypeName)
! customization point for global new
Definition: rapidjson.h:601
std::void_t< T > n
size_t tokenCount_
Number of tokens in tokens_.
Definition: pointer.h:1046
ValueType::Ch Ch
Character type from Value.
Definition: pointer.h:82
bool NeedPercentEncode(Ch c) const
Check whether a character should be percent-encoded.
Definition: pointer.h:793
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:72
The parse is successful.
Definition: pointer.h:38
Allocator * allocator_
The current allocator. It is either user-supplied or equal to ownAllocator_.
Definition: pointer.h:1042
A character must percent encoded in URI fragment.
Definition: pointer.h:43
Allocator * ownAllocator_
Allocator owned by this Pointer.
Definition: pointer.h:1043
static QCString * s
Definition: config.cpp:1042
template<typename ValueType, typename Allocator = CrtAllocator>
template<typename T >
GenericPointer< ValueType, Allocator >::RAPIDJSON_DISABLEIF_RETURN ( (internal::NotExpr< internal::IsSame< typename internal::RemoveConst< T >::Type, Ch > >)  ,
(GenericPointer< ValueType, Allocator >)   
)

Append a name token without length, and return a new Pointer.

Parameters
nameName (const Ch*) to be appended.
allocatorAllocator for the newly return Pointer.
Returns
A new Pointer with appended token.
template<typename ValueType, typename Allocator = CrtAllocator>
Allocator stackAllocator GenericPointer< ValueType, Allocator >::RAPIDJSON_DISABLEIF_RETURN ( (internal::OrExpr< internal::IsPointer< T >, internal::IsGenericValue< T > >)  ,
(ValueType &)   
)
template<typename ValueType, typename Allocator = CrtAllocator>
template<typename T >
GenericPointer< ValueType, Allocator >::RAPIDJSON_DISABLEIF_RETURN ( (internal::OrExpr< internal::IsPointer< T >, internal::IsGenericValue< T > >)  ,
(ValueType &)   
)

Set a primitive value in a subtree.

Template Parameters
TEither Type, int, unsigned, int64_t, uint64_t, bool
template<typename ValueType, typename Allocator = CrtAllocator>
template<typename T , typename stackAllocator >
GenericPointer< ValueType, Allocator >::RAPIDJSON_DISABLEIF_RETURN ( (internal::OrExpr< internal::IsPointer< T >, internal::IsGenericValue< T > >)  ,
(ValueType &)   
)

Set a primitive value in a document.

Template Parameters
TEither Type, int, unsigned, int64_t, uint64_t, bool
template<typename ValueType, typename Allocator = CrtAllocator>
ValueType& GenericPointer< ValueType, Allocator >::Set ( ValueType &  root,
ValueType &  value,
typename ValueType::AllocatorType &  allocator 
) const
inline

Set a value in a subtree, with move semantics.

It creates all parents if they are not exist or types are different to the tokens. So this function always succeeds but potentially remove existing values.

Parameters
rootRoot value of a DOM sub-tree to be resolved. It can be any value other than document root.
valueValue to be set.
allocatorAllocator for creating the values if the specified value or its parents are not exist.
See also
Create()

Definition at line 613 of file pointer.h.

613  {
614  return Create(root, allocator) = value;
615  }
Allocator * allocator
Definition: pointer.h:245
template<typename ValueType, typename Allocator = CrtAllocator>
ValueType& GenericPointer< ValueType, Allocator >::Set ( ValueType &  root,
const ValueType &  value,
typename ValueType::AllocatorType &  allocator 
) const
inline

Set a value in a subtree, with copy semantics.

Definition at line 618 of file pointer.h.

618  {
619  return Create(root, allocator).CopyFrom(value, allocator);
620  }
Allocator * allocator
Definition: pointer.h:245
template<typename ValueType, typename Allocator = CrtAllocator>
ValueType& GenericPointer< ValueType, Allocator >::Set ( ValueType &  root,
const Ch value,
typename ValueType::AllocatorType &  allocator 
) const
inline

Set a null-terminated string in a subtree.

Definition at line 623 of file pointer.h.

623  {
624  return Create(root, allocator) = ValueType(value, allocator).Move();
625  }
Allocator * allocator
Definition: pointer.h:245
template<typename ValueType, typename Allocator = CrtAllocator>
template<typename stackAllocator >
ValueType& GenericPointer< ValueType, Allocator >::Set ( GenericDocument< EncodingType, typename ValueType::AllocatorType, stackAllocator > &  document,
ValueType &  value 
) const
inline

Set a value in a document, with move semantics.

Definition at line 646 of file pointer.h.

646  {
647  return Create(document) = value;
648  }
template<typename ValueType, typename Allocator = CrtAllocator>
template<typename stackAllocator >
ValueType& GenericPointer< ValueType, Allocator >::Set ( GenericDocument< EncodingType, typename ValueType::AllocatorType, stackAllocator > &  document,
const ValueType &  value 
) const
inline

Set a value in a document, with copy semantics.

Definition at line 652 of file pointer.h.

652  {
653  return Create(document).CopyFrom(value, document.GetAllocator());
654  }
Allocator & GetAllocator()
Get the allocator of this document.
Definition: document.h:2408
template<typename ValueType, typename Allocator = CrtAllocator>
template<typename stackAllocator >
ValueType& GenericPointer< ValueType, Allocator >::Set ( GenericDocument< EncodingType, typename ValueType::AllocatorType, stackAllocator > &  document,
const Ch value 
) const
inline

Set a null-terminated string in a document.

Definition at line 658 of file pointer.h.

658  {
659  return Create(document) = ValueType(value, document.GetAllocator()).Move();
660  }
Allocator & GetAllocator()
Get the allocator of this document.
Definition: document.h:2408
template<typename ValueType, typename Allocator = CrtAllocator>
template<bool uriFragment, typename OutputStream >
bool GenericPointer< ValueType, Allocator >::Stringify ( OutputStream &  os) const
inlineprivate

Stringify to string or URI fragment representation.

Template Parameters
uriFragmentTrue for stringifying to URI fragment representation. False for string representation.
OutputStreamtype of output stream.
Parameters
osThe output stream.

Definition at line 944 of file pointer.h.

944  {
945  RAPIDJSON_ASSERT(IsValid());
946 
947  if (uriFragment)
948  os.Put('#');
949 
950  for (Token *t = tokens_; t != tokens_ + tokenCount_; ++t) {
951  os.Put('/');
952  for (size_t j = 0; j < t->length; j++) {
953  Ch c = t->name[j];
954  if (c == '~') {
955  os.Put('~');
956  os.Put('0');
957  }
958  else if (c == '/') {
959  os.Put('~');
960  os.Put('1');
961  }
962  else if (uriFragment && NeedPercentEncode(c)) {
963  // Transcode to UTF8 sequence
965  PercentEncodeStream<OutputStream> target(os);
966  if (!Transcoder<EncodingType, UTF8<> >().Validate(source, target))
967  return false;
968  j += source.Tell() - 1;
969  }
970  else
971  os.Put(c);
972  }
973  }
974  return true;
975  }
Encoding conversion.
Definition: encodings.h:658
const CharType(& source)[N]
Definition: pointer.h:1147
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:406
Read-only string stream.
Definition: fwd.h:47
ValueType::EncodingType EncodingType
Encoding type from Value.
Definition: pointer.h:81
Token * tokens_
A list of tokens.
Definition: pointer.h:1045
UTF-8 encoding.
Definition: encodings.h:96
size_t tokenCount_
Number of tokens in tokens_.
Definition: pointer.h:1046
ValueType::Ch Ch
Character type from Value.
Definition: pointer.h:82
bool NeedPercentEncode(Ch c) const
Check whether a character should be percent-encoded.
Definition: pointer.h:793
template<typename ValueType, typename Allocator = CrtAllocator>
ValueType& GenericPointer< ValueType, Allocator >::Swap ( ValueType &  root,
ValueType &  value,
typename ValueType::AllocatorType &  allocator 
) const
inline

Swap a value with a value in a subtree.

It creates all parents if they are not exist or types are different to the tokens. So this function always succeeds but potentially remove existing values.

Parameters
rootRoot value of a DOM sub-tree to be resolved. It can be any value other than document root.
valueValue to be swapped.
allocatorAllocator for creating the values if the specified value or its parents are not exist.
See also
Create()

Definition at line 695 of file pointer.h.

695  {
696  return Create(root, allocator).Swap(value);
697  }
Allocator * allocator
Definition: pointer.h:245
template<typename ValueType, typename Allocator = CrtAllocator>
template<typename stackAllocator >
ValueType& GenericPointer< ValueType, Allocator >::Swap ( GenericDocument< EncodingType, typename ValueType::AllocatorType, stackAllocator > &  document,
ValueType &  value 
) const
inline

Swap a value with a value in a document.

Definition at line 701 of file pointer.h.

701  {
702  return Create(document).Swap(value);
703  }

Member Data Documentation

template<typename ValueType, typename Allocator = CrtAllocator>
Allocator* GenericPointer< ValueType, Allocator >::allocator

Definition at line 245 of file pointer.h.

template<typename ValueType, typename Allocator = CrtAllocator>
Allocator* GenericPointer< ValueType, Allocator >::allocator_
private

The current allocator. It is either user-supplied or equal to ownAllocator_.

Definition at line 1042 of file pointer.h.

template<typename ValueType, typename Allocator = CrtAllocator>
Allocator stackAllocator stackAllocator T defaultValue GenericPointer< ValueType, Allocator >::const
Initial value:
{
return GetWithDefault(document, defaultValue, document.GetAllocator())

Definition at line 594 of file pointer.h.

template<typename ValueType, typename Allocator = CrtAllocator>
T ValueType::AllocatorType& allocator GenericPointer< ValueType, Allocator >::const
Initial value:
{
return Create(root, allocator) = ValueType(value).Move()

Definition at line 640 of file pointer.h.

template<typename ValueType, typename Allocator = CrtAllocator>
stackAllocator T value GenericPointer< ValueType, Allocator >::const
Initial value:
{
return Create(document) = value

Definition at line 676 of file pointer.h.

template<typename ValueType, typename Allocator = CrtAllocator>
Allocator stackAllocator stackAllocator& GenericPointer< ValueType, Allocator >::document

Definition at line 594 of file pointer.h.

template<typename ValueType, typename Allocator = CrtAllocator>
stackAllocator& GenericPointer< ValueType, Allocator >::document

Definition at line 676 of file pointer.h.

template<typename ValueType, typename Allocator = CrtAllocator>
Ch* GenericPointer< ValueType, Allocator >::nameBuffer_
private

A buffer containing all names in tokens.

Definition at line 1044 of file pointer.h.

template<typename ValueType, typename Allocator = CrtAllocator>
Allocator* GenericPointer< ValueType, Allocator >::ownAllocator_
private

Allocator owned by this Pointer.

Definition at line 1043 of file pointer.h.

template<typename ValueType, typename Allocator = CrtAllocator>
PointerParseErrorCode GenericPointer< ValueType, Allocator >::parseErrorCode_
private

Parsing error code.

Definition at line 1048 of file pointer.h.

template<typename ValueType, typename Allocator = CrtAllocator>
size_t GenericPointer< ValueType, Allocator >::parseErrorOffset_
private

Offset in code unit when parsing fail.

Definition at line 1047 of file pointer.h.

template<typename ValueType, typename Allocator = CrtAllocator>
size_t GenericPointer< ValueType, Allocator >::tokenCount_
private

Number of tokens in tokens_.

Definition at line 1046 of file pointer.h.

template<typename ValueType, typename Allocator = CrtAllocator>
Token* GenericPointer< ValueType, Allocator >::tokens_
private

A list of tokens.

Definition at line 1045 of file pointer.h.

template<typename ValueType, typename Allocator = CrtAllocator>
T GenericPointer< ValueType, Allocator >::value

Definition at line 640 of file pointer.h.


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