Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
GenericPointer< ValueType, Allocator > Class Template Reference

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

#include <pointer.h>

Collaboration diagram for GenericPointer< ValueType, Allocator >:

Classes

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.
 
typedef ValueType::Ch Ch
 Character type from Value.
 

Append token

Allocatorallocator
 
Allocatorallocator_
 The current allocator. It is either user-supplied or equal to ownAllocator_.
 
AllocatorownAllocator_
 Allocator owned by this Pointer.
 
ChnameBuffer_
 A buffer containing all names in tokens.
 
Tokentokens_
 A list of tokens.
 
size_t tokenCount_
 Number of tokens in tokens_.
 
size_t parseErrorOffset_
 Offset in code unit when parsing fail.
 
PointerParseErrorCode parseErrorCode_
 Parsing error code.
 
GenericPointer Append (const Token &token, Allocator *allocator=0) const
 Append a token and return a new Pointer.
 
GenericPointer Append (const Ch *name, SizeType length, Allocator *allocator=0) const
 Append a name token with length, and return a new Pointer.
 
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.
 

Constructors and destructor.

 GenericPointer (Allocator *allocator=0)
 Default constructor.
 
 GenericPointer (const Ch *source, Allocator *allocator=0)
 Constructor that parses a string or URI fragment representation.
 
 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.
 
 GenericPointer (const Token *tokens, size_t tokenCount)
 Constructor with user-supplied tokens.
 
 GenericPointer (const GenericPointer &rhs)
 Copy constructor.
 
 GenericPointer (const GenericPointer &rhs, Allocator *allocator)
 Copy constructor.
 
 ~GenericPointer ()
 Destructor.
 
GenericPointeroperator= (const GenericPointer &rhs)
 Assignment operator.
 
GenericPointerSwap (GenericPointer &other) RAPIDJSON_NOEXCEPT
 Swap the content of this pointer with an other.
 
void swap (GenericPointer &a, GenericPointer &b) RAPIDJSON_NOEXCEPT
 free-standing swap function helper
 

Detailed Description

template<typename ValueType, typename Allocator = CrtAllocator>
class GenericPointer< ValueType, 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 79 of file pointer.h.

Member Typedef Documentation

◆ Ch

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

Definition at line 82 of file pointer.h.

◆ EncodingType

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

Definition at line 81 of file pointer.h.

Constructor & Destructor Documentation

◆ GenericPointer() [1/6]

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

Definition at line 107 of file pointer.h.

Ch * nameBuffer_
A buffer containing all names in tokens.
Definition pointer.h:1101
Allocator * allocator_
The current allocator. It is either user-supplied or equal to ownAllocator_.
Definition pointer.h:1099
size_t tokenCount_
Number of tokens in tokens_.
Definition pointer.h:1103
PointerParseErrorCode parseErrorCode_
Parsing error code.
Definition pointer.h:1105
Token * tokens_
A list of tokens.
Definition pointer.h:1102
Allocator * ownAllocator_
Allocator owned by this Pointer.
Definition pointer.h:1100
size_t parseErrorOffset_
Offset in code unit when parsing fail.
Definition pointer.h:1104
Allocator * allocator
Definition pointer.h:275
@ kPointerParseErrorNone
The parse is successful.
Definition pointer.h:38

◆ GenericPointer() [2/6]

template<typename ValueType , typename Allocator = CrtAllocator>
GenericPointer< ValueType, Allocator >::GenericPointer ( const Ch * source,
Allocator * allocator = 0 )
inlineexplicit
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 }
SizeType StrLen(const Ch *s)
Custom strlen() which works on different character types.
Definition strfunc.h:31
const CharType(& source)[N]
Definition pointer.h:1204
Here is the call graph for this function:

◆ GenericPointer() [3/6]

template<typename ValueType , typename Allocator = CrtAllocator>
GenericPointer< ValueType, Allocator >::GenericPointer ( const Ch * source,
size_t length,
Allocator * allocator = 0 )
inline
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.

◆ GenericPointer() [4/6]

template<typename ValueType , typename Allocator = CrtAllocator>
GenericPointer< ValueType, Allocator >::GenericPointer ( const Token * tokens,
size_t tokenCount )
inline

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
const mie::Vuint & p
Definition bn.cpp:27
Represents a JSON Pointer. Use Pointer for UTF8 encoding and default allocator.
Definition pointer.h:79
#define NAME(s)
#define INDEX(i)
A token is the basic units of internal representation.
Definition pointer.h:97

Definition at line 163 of file pointer.h.

163: allocator_(), ownAllocator_(), nameBuffer_(), tokens_(const_cast<Token*>(tokens)), tokenCount_(tokenCount), parseErrorOffset_(), parseErrorCode_(kPointerParseErrorNone) {}

◆ GenericPointer() [5/6]

template<typename ValueType , typename Allocator = CrtAllocator>
GenericPointer< ValueType, Allocator >::GenericPointer ( const GenericPointer< ValueType, Allocator > & rhs)
inline

Definition at line 166 of file pointer.h.

◆ GenericPointer() [6/6]

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

Definition at line 171 of file pointer.h.

◆ ~GenericPointer()

template<typename ValueType , typename Allocator = CrtAllocator>
GenericPointer< ValueType, Allocator >::~GenericPointer ( )
inline

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 }
#define RAPIDJSON_DELETE(x)
! customization point for global delete
Definition rapidjson.h:631

Member Function Documentation

◆ Append() [1/2]

template<typename ValueType , typename Allocator = CrtAllocator>
GenericPointer GenericPointer< ValueType, Allocator >::Append ( const Ch * name,
SizeType length,
Allocator * allocator = 0 ) const
inline
Parameters
nameName to be appended.
lengthLength of name.
allocatorAllocator for the newly return Pointer.
Returns
A new Pointer with appended token.

Definition at line 262 of file pointer.h.

262 {
263 Token token = { name, length, kPointerInvalidIndex };
264 return Append(token, allocator);
265 }
std::string name
GenericPointer Append(const Token &token, Allocator *allocator=0) const
Append a token and return a new Pointer.
Definition pointer.h:244
Here is the call graph for this function:

◆ Append() [2/2]

template<typename ValueType , typename Allocator = CrtAllocator>
GenericPointer GenericPointer< ValueType, Allocator >::Append ( const Token & token,
Allocator * allocator = 0 ) const
inline
Parameters
tokenToken to be appended.
allocatorAllocator for the newly return Pointer.
Returns
A new Pointer with appended token.

Definition at line 244 of file pointer.h.

244 {
246 r.allocator_ = allocator;
247 Ch *p = r.CopyFromRaw(*this, 1, token.length + 1);
248 std::memcpy(p, token.name, (token.length + 1) * sizeof(Ch));
249 r.tokens_[tokenCount_].name = p;
250 r.tokens_[tokenCount_].length = token.length;
251 r.tokens_[tokenCount_].index = token.index;
252 return r;
253 }
const mie::Vuint & r
Definition bn.cpp:28
ValueType::Ch Ch
Character type from Value.
Definition pointer.h:82
Here is the caller graph for this function:

◆ operator=()

template<typename ValueType , typename Allocator = CrtAllocator>
GenericPointer & GenericPointer< ValueType, Allocator >::operator= ( const GenericPointer< ValueType, Allocator > & rhs)
inline

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
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 }

◆ RAPIDJSON_DISABLEIF_RETURN()

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 >)  )
Parameters
nameName (const Ch*) to be appended.
allocatorAllocator for the newly return Pointer.
Returns
A new Pointer with appended token.

◆ Swap()

template<typename ValueType , typename Allocator = CrtAllocator>
GenericPointer & GenericPointer< ValueType, Allocator >::Swap ( GenericPointer< ValueType, Allocator > & other)
inline
Parameters
otherThe pointer to swap with.
Note
Constant complexity.

Definition at line 208 of file pointer.h.

208 {
209 internal::Swap(allocator_, other.allocator_);
210 internal::Swap(ownAllocator_, other.ownAllocator_);
211 internal::Swap(nameBuffer_, other.nameBuffer_);
212 internal::Swap(tokens_, other.tokens_);
213 internal::Swap(tokenCount_, other.tokenCount_);
214 internal::Swap(parseErrorOffset_, other.parseErrorOffset_);
215 internal::Swap(parseErrorCode_, other.parseErrorCode_);
216 return *this;
217 }
void Swap(T &a, T &b) RAPIDJSON_NOEXCEPT
Custom swap() to avoid dependency on C++ <algorithm> header.
Definition swap.h:33
Here is the call graph for this function:
Here is the caller graph for this function:

Friends And Related Symbol Documentation

◆ swap

template<typename ValueType , typename Allocator = CrtAllocator>
void swap ( GenericPointer< ValueType, Allocator > & a,
GenericPointer< ValueType, Allocator > & b )
friend

Helper function to enable support for common swap implementation pattern based on std::swap:

void swap(MyClass& a, MyClass& b) {
using std::swap;
swap(a.pointer, b.pointer);
// ...
}
friend void swap(GenericPointer &a, GenericPointer &b) RAPIDJSON_NOEXCEPT
free-standing swap function helper
Definition pointer.h:231
void swap(picojson::value &x, picojson::value &y)
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1181
See also
Swap()

Definition at line 231 of file pointer.h.

231{ a.Swap(b); }

Member Data Documentation

◆ allocator

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

Definition at line 275 of file pointer.h.

275 {
277 }
278
279#if RAPIDJSON_HAS_STDSTRING
281
286 GenericPointer Append(const std::basic_string<Ch>& name, Allocator* allocator = 0) const {
287 return Append(name.c_str(), static_cast<SizeType>(name.size()), allocator);
288 }
289#endif
290
292
298 char buffer[21];
299 char* end = sizeof(SizeType) == 4 ? internal::u32toa(index, buffer) : internal::u64toa(index, buffer);
300 SizeType length = static_cast<SizeType>(end - buffer);
301 buffer[length] = '\0';
302
303 if (sizeof(Ch) == 1) {
304 Token token = { reinterpret_cast<Ch*>(buffer), length, index };
305 return Append(token, allocator);
306 }
307 else {
308 Ch name[21];
309 for (size_t i = 0; i <= length; i++)
310 name[i] = static_cast<Ch>(buffer[i]);
311 Token token = { name, length, index };
312 return Append(token, allocator);
313 }
314 }
315
317
322 GenericPointer Append(const ValueType& token, Allocator* allocator = 0) const {
323 if (token.IsString())
324 return Append(token.GetString(), token.GetStringLength(), allocator);
325 else {
326 RAPIDJSON_ASSERT(token.IsUint64());
327 RAPIDJSON_ASSERT(token.GetUint64() <= SizeType(~0));
328 return Append(static_cast<SizeType>(token.GetUint64()), allocator);
329 }
330 }
331
333
334
336 bool IsValid() const { return parseErrorCode_ == kPointerParseErrorNone; }
337
339 size_t GetParseErrorOffset() const { return parseErrorOffset_; }
340
342 PointerParseErrorCode GetParseErrorCode() const { return parseErrorCode_; }
343
345
347 Allocator& GetAllocator() { return *allocator_; }
348
350
351
353 const Token* GetTokens() const { return tokens_; }
354
356 size_t GetTokenCount() const { return tokenCount_; }
357
359
361
362
364
367 bool operator==(const GenericPointer& rhs) const {
368 if (!IsValid() || !rhs.IsValid() || tokenCount_ != rhs.tokenCount_)
369 return false;
370
371 for (size_t i = 0; i < tokenCount_; i++) {
372 if (tokens_[i].index != rhs.tokens_[i].index ||
373 tokens_[i].length != rhs.tokens_[i].length ||
374 (tokens_[i].length != 0 && std::memcmp(tokens_[i].name, rhs.tokens_[i].name, sizeof(Ch)* tokens_[i].length) != 0))
375 {
376 return false;
377 }
378 }
379
380 return true;
381 }
382
384
387 bool operator!=(const GenericPointer& rhs) const { return !(*this == rhs); }
388
390
393 bool operator<(const GenericPointer& rhs) const {
394 if (!IsValid())
395 return false;
396 if (!rhs.IsValid())
397 return true;
398
399 if (tokenCount_ != rhs.tokenCount_)
400 return tokenCount_ < rhs.tokenCount_;
401
402 for (size_t i = 0; i < tokenCount_; i++) {
403 if (tokens_[i].index != rhs.tokens_[i].index)
404 return tokens_[i].index < rhs.tokens_[i].index;
405
406 if (tokens_[i].length != rhs.tokens_[i].length)
407 return tokens_[i].length < rhs.tokens_[i].length;
408
409 if (int cmp = std::memcmp(tokens_[i].name, rhs.tokens_[i].name, sizeof(Ch) * tokens_[i].length))
410 return cmp < 0;
411 }
412
413 return false;
414 }
415
417
419
420
422
426 template<typename OutputStream>
427 bool Stringify(OutputStream& os) const {
428 return Stringify<false, OutputStream>(os);
429 }
430
432
436 template<typename OutputStream>
437 bool StringifyUriFragment(OutputStream& os) const {
438 return Stringify<true, OutputStream>(os);
439 }
440
442
444
445
447
461 ValueType& Create(ValueType& root, typename ValueType::AllocatorType& allocator, bool* alreadyExist = 0) const {
462 RAPIDJSON_ASSERT(IsValid());
463 ValueType* v = &root;
464 bool exist = true;
465 for (const Token *t = tokens_; t != tokens_ + tokenCount_; ++t) {
466 if (v->IsArray() && t->name[0] == '-' && t->length == 1) {
467 v->PushBack(ValueType().Move(), allocator);
468 v = &((*v)[v->Size() - 1]);
469 exist = false;
470 }
471 else {
472 if (t->index == kPointerInvalidIndex) { // must be object name
473 if (!v->IsObject())
474 v->SetObject(); // Change to Object
475 }
476 else { // object name or array index
477 if (!v->IsArray() && !v->IsObject())
478 v->SetArray(); // Change to Array
479 }
480
481 if (v->IsArray()) {
482 if (t->index >= v->Size()) {
483 v->Reserve(t->index + 1, allocator);
484 while (t->index >= v->Size())
485 v->PushBack(ValueType().Move(), allocator);
486 exist = false;
487 }
488 v = &((*v)[t->index]);
489 }
490 else {
491 typename ValueType::MemberIterator m = v->FindMember(GenericStringRef<Ch>(t->name, t->length));
492 if (m == v->MemberEnd()) {
493 v->AddMember(ValueType(t->name, t->length, allocator).Move(), ValueType().Move(), allocator);
494 v = &(--v->MemberEnd())->value; // Assumes AddMember() appends at the end
495 exist = false;
496 }
497 else
498 v = &m->value;
499 }
500 }
501 }
502
503 if (alreadyExist)
504 *alreadyExist = exist;
505
506 return *v;
507 }
508
510
515 template <typename stackAllocator>
517 return Create(document, document.GetAllocator(), alreadyExist);
518 }
519
521
523
524
526
539 ValueType* Get(ValueType& root, size_t* unresolvedTokenIndex = 0) const {
540 RAPIDJSON_ASSERT(IsValid());
541 ValueType* v = &root;
542 for (const Token *t = tokens_; t != tokens_ + tokenCount_; ++t) {
543 switch (v->GetType()) {
544 case kObjectType:
545 {
546 typename ValueType::MemberIterator m = v->FindMember(GenericStringRef<Ch>(t->name, t->length));
547 if (m == v->MemberEnd())
548 break;
549 v = &m->value;
550 }
551 continue;
552 case kArrayType:
553 if (t->index == kPointerInvalidIndex || t->index >= v->Size())
554 break;
555 v = &((*v)[t->index]);
556 continue;
557 default:
558 break;
559 }
560
561 // Error: unresolved token
562 if (unresolvedTokenIndex)
563 *unresolvedTokenIndex = static_cast<size_t>(t - tokens_);
564 return 0;
565 }
566 return v;
567 }
568
570
574 const ValueType* Get(const ValueType& root, size_t* unresolvedTokenIndex = 0) const {
575 return Get(const_cast<ValueType&>(root), unresolvedTokenIndex);
576 }
577
579
581
582
584
593 ValueType& GetWithDefault(ValueType& root, const ValueType& defaultValue, typename ValueType::AllocatorType& allocator) const {
594 bool alreadyExist;
595 ValueType& v = Create(root, allocator, &alreadyExist);
596 return alreadyExist ? v : v.CopyFrom(defaultValue, allocator);
597 }
598
600 ValueType& GetWithDefault(ValueType& root, const Ch* defaultValue, typename ValueType::AllocatorType& allocator) const {
601 bool alreadyExist;
602 ValueType& v = Create(root, allocator, &alreadyExist);
603 return alreadyExist ? v : v.SetString(defaultValue, allocator);
604 }
605
606#if RAPIDJSON_HAS_STDSTRING
608 ValueType& GetWithDefault(ValueType& root, const std::basic_string<Ch>& defaultValue, typename ValueType::AllocatorType& allocator) const {
609 bool alreadyExist;
610 ValueType& v = Create(root, allocator, &alreadyExist);
611 return alreadyExist ? v : v.SetString(defaultValue, allocator);
612 }
613#endif
614
616
619 template <typename T>
620 RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr<internal::IsPointer<T>, internal::IsGenericValue<T> >), (ValueType&))
621 GetWithDefault(ValueType& root, T defaultValue, typename ValueType::AllocatorType& allocator) const {
622 return GetWithDefault(root, ValueType(defaultValue).Move(), allocator);
623 }
624
626 template <typename stackAllocator>
628 return GetWithDefault(document, defaultValue, document.GetAllocator());
629 }
630
632 template <typename stackAllocator>
634 return GetWithDefault(document, defaultValue, document.GetAllocator());
635 }
636
637#if RAPIDJSON_HAS_STDSTRING
639 template <typename stackAllocator>
640 ValueType& GetWithDefault(GenericDocument<EncodingType, typename ValueType::AllocatorType, stackAllocator>& document, const std::basic_string<Ch>& defaultValue) const {
641 return GetWithDefault(document, defaultValue, document.GetAllocator());
642 }
643#endif
644
646
649 template <typename T, typename stackAllocator>
650 RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr<internal::IsPointer<T>, internal::IsGenericValue<T> >), (ValueType&))
652 return GetWithDefault(document, defaultValue, document.GetAllocator());
653 }
654
656
658
659
661
670 ValueType& Set(ValueType& root, ValueType& value, typename ValueType::AllocatorType& allocator) const {
671 return Create(root, allocator) = value;
672 }
673
675 ValueType& Set(ValueType& root, const ValueType& value, typename ValueType::AllocatorType& allocator) const {
676 return Create(root, allocator).CopyFrom(value, allocator);
677 }
678
680 ValueType& Set(ValueType& root, const Ch* value, typename ValueType::AllocatorType& allocator) const {
681 return Create(root, allocator) = ValueType(value, allocator).Move();
682 }
683
684#if RAPIDJSON_HAS_STDSTRING
686 ValueType& Set(ValueType& root, const std::basic_string<Ch>& value, typename ValueType::AllocatorType& allocator) const {
687 return Create(root, allocator) = ValueType(value, allocator).Move();
688 }
689#endif
690
692
695 template <typename T>
696 RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr<internal::IsPointer<T>, internal::IsGenericValue<T> >), (ValueType&))
697 Set(ValueType& root, T value, typename ValueType::AllocatorType& allocator) const {
698 return Create(root, allocator) = ValueType(value).Move();
699 }
700
702 template <typename stackAllocator>
704 return Create(document) = value;
705 }
706
708 template <typename stackAllocator>
710 return Create(document).CopyFrom(value, document.GetAllocator());
711 }
712
714 template <typename stackAllocator>
716 return Create(document) = ValueType(value, document.GetAllocator()).Move();
717 }
718
719#if RAPIDJSON_HAS_STDSTRING
721 template <typename stackAllocator>
723 return Create(document) = ValueType(value, document.GetAllocator()).Move();
724 }
725#endif
726
728
731 template <typename T, typename stackAllocator>
732 RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr<internal::IsPointer<T>, internal::IsGenericValue<T> >), (ValueType&))
734 return Create(document) = value;
735 }
736
738
740
741
743
752 ValueType& Swap(ValueType& root, ValueType& value, typename ValueType::AllocatorType& allocator) const {
753 return Create(root, allocator).Swap(value);
754 }
755
757 template <typename stackAllocator>
759 return Create(document).Swap(value);
760 }
761
763
765
771 bool Erase(ValueType& root) const {
772 RAPIDJSON_ASSERT(IsValid());
773 if (tokenCount_ == 0) // Cannot erase the root
774 return false;
775
776 ValueType* v = &root;
777 const Token* last = tokens_ + (tokenCount_ - 1);
778 for (const Token *t = tokens_; t != last; ++t) {
779 switch (v->GetType()) {
780 case kObjectType:
781 {
782 typename ValueType::MemberIterator m = v->FindMember(GenericStringRef<Ch>(t->name, t->length));
783 if (m == v->MemberEnd())
784 return false;
785 v = &m->value;
786 }
787 break;
788 case kArrayType:
789 if (t->index == kPointerInvalidIndex || t->index >= v->Size())
790 return false;
791 v = &((*v)[t->index]);
792 break;
793 default:
794 return false;
795 }
796 }
797
798 switch (v->GetType()) {
799 case kObjectType:
800 return v->EraseMember(GenericStringRef<Ch>(last->name, last->length));
801 case kArrayType:
802 if (last->index == kPointerInvalidIndex || last->index >= v->Size())
803 return false;
804 v->Erase(v->Begin() + last->index);
805 return true;
806 default:
807 return false;
808 }
809 }
810
811private:
813
819 Ch* CopyFromRaw(const GenericPointer& rhs, size_t extraToken = 0, size_t extraNameBufferSize = 0) {
820 if (!allocator_) // allocator is independently owned.
822
823 size_t nameBufferSize = rhs.tokenCount_; // null terminators for tokens
824 for (Token *t = rhs.tokens_; t != rhs.tokens_ + rhs.tokenCount_; ++t)
825 nameBufferSize += t->length;
826
827 tokenCount_ = rhs.tokenCount_ + extraToken;
828 tokens_ = static_cast<Token *>(allocator_->Malloc(tokenCount_ * sizeof(Token) + (nameBufferSize + extraNameBufferSize) * sizeof(Ch)));
829 nameBuffer_ = reinterpret_cast<Ch *>(tokens_ + tokenCount_);
830 if (rhs.tokenCount_ > 0) {
831 std::memcpy(tokens_, rhs.tokens_, rhs.tokenCount_ * sizeof(Token));
832 }
833 if (nameBufferSize > 0) {
834 std::memcpy(nameBuffer_, rhs.nameBuffer_, nameBufferSize * sizeof(Ch));
835 }
836
837 // Adjust pointers to name buffer
838 std::ptrdiff_t diff = nameBuffer_ - rhs.nameBuffer_;
839 for (Token *t = tokens_; t != tokens_ + rhs.tokenCount_; ++t)
840 t->name += diff;
841
842 return nameBuffer_ + nameBufferSize;
843 }
844
846
850 bool NeedPercentEncode(Ch c) const {
851 return !((c >= '0' && c <= '9') || (c >= 'A' && c <='Z') || (c >= 'a' && c <= 'z') || c == '-' || c == '.' || c == '_' || c =='~');
852 }
853
855#ifndef __clang__ // -Wdocumentation
861#endif
862 void Parse(const Ch* source, size_t length) {
863 RAPIDJSON_ASSERT(source != NULL);
866
867 // Create own allocator if user did not supply.
868 if (!allocator_)
870
871 // Count number of '/' as tokenCount
872 tokenCount_ = 0;
873 for (const Ch* s = source; s != source + length; s++)
874 if (*s == '/')
875 tokenCount_++;
876
877 Token* token = tokens_ = static_cast<Token *>(allocator_->Malloc(tokenCount_ * sizeof(Token) + length * sizeof(Ch)));
878 Ch* name = nameBuffer_ = reinterpret_cast<Ch *>(tokens_ + tokenCount_);
879 size_t i = 0;
880
881 // Detect if it is a URI fragment
882 bool uriFragment = false;
883 if (source[i] == '#') {
884 uriFragment = true;
885 i++;
886 }
887
888 if (i != length && source[i] != '/') {
890 goto error;
891 }
892
893 while (i < length) {
894 RAPIDJSON_ASSERT(source[i] == '/');
895 i++; // consumes '/'
896
897 token->name = name;
898 bool isNumber = true;
899
900 while (i < length && source[i] != '/') {
901 Ch c = source[i];
902 if (uriFragment) {
903 // Decoding percent-encoding for URI fragment
904 if (c == '%') {
905 PercentDecodeStream is(&source[i], source + length);
907 Ch* begin = os.PutBegin();
908 if (!Transcoder<UTF8<>, EncodingType>().Validate(is, os) || !is.IsValid()) {
910 goto error;
911 }
912 size_t len = os.PutEnd(begin);
913 i += is.Tell() - 1;
914 if (len == 1)
915 c = *name;
916 else {
917 name += len;
918 isNumber = false;
919 i++;
920 continue;
921 }
922 }
923 else if (NeedPercentEncode(c)) {
925 goto error;
926 }
927 }
928
929 i++;
930
931 // Escaping "~0" -> '~', "~1" -> '/'
932 if (c == '~') {
933 if (i < length) {
934 c = source[i];
935 if (c == '0') c = '~';
936 else if (c == '1') c = '/';
937 else {
939 goto error;
940 }
941 i++;
942 }
943 else {
945 goto error;
946 }
947 }
948
949 // First check for index: all of characters are digit
950 if (c < '0' || c > '9')
951 isNumber = false;
952
953 *name++ = c;
954 }
955 token->length = static_cast<SizeType>(name - token->name);
956 if (token->length == 0)
957 isNumber = false;
958 *name++ = '\0'; // Null terminator
959
960 // Second check for index: more than one digit cannot have leading zero
961 if (isNumber && token->length > 1 && token->name[0] == '0')
962 isNumber = false;
963
964 // String to SizeType conversion
965 SizeType n = 0;
966 if (isNumber) {
967 for (size_t j = 0; j < token->length; j++) {
968 SizeType m = n * 10 + static_cast<SizeType>(token->name[j] - '0');
969 if (m < n) { // overflow detection
970 isNumber = false;
971 break;
972 }
973 n = m;
974 }
975 }
976
977 token->index = isNumber ? n : kPointerInvalidIndex;
978 token++;
979 }
980
981 RAPIDJSON_ASSERT(name <= nameBuffer_ + length); // Should not overflow buffer
983 return;
984
985 error:
986 Allocator::Free(tokens_);
987 nameBuffer_ = 0;
988 tokens_ = 0;
989 tokenCount_ = 0;
991 return;
992 }
993
995
1000 template<bool uriFragment, typename OutputStream>
1001 bool Stringify(OutputStream& os) const {
1002 RAPIDJSON_ASSERT(IsValid());
1003
1004 if (uriFragment)
1005 os.Put('#');
1006
1007 for (Token *t = tokens_; t != tokens_ + tokenCount_; ++t) {
1008 os.Put('/');
1009 for (size_t j = 0; j < t->length; j++) {
1010 Ch c = t->name[j];
1011 if (c == '~') {
1012 os.Put('~');
1013 os.Put('0');
1014 }
1015 else if (c == '/') {
1016 os.Put('~');
1017 os.Put('1');
1018 }
1019 else if (uriFragment && NeedPercentEncode(c)) {
1020 // Transcode to UTF8 sequence
1022 PercentEncodeStream<OutputStream> target(os);
1023 if (!Transcoder<EncodingType, UTF8<> >().Validate(source, target))
1024 return false;
1025 j += source.Tell() - 1;
1026 }
1027 else
1028 os.Put(c);
1029 }
1030 }
1031 return true;
1032 }
1033
1035
1040 class PercentDecodeStream {
1041 public:
1042 typedef typename ValueType::Ch Ch;
1043
1045
1049 PercentDecodeStream(const Ch* source, const Ch* end) : src_(source), head_(source), end_(end), valid_(true) {}
1050
1051 Ch Take() {
1052 if (*src_ != '%' || src_ + 3 > end_) { // %XY triplet
1053 valid_ = false;
1054 return 0;
1055 }
1056 src_++;
1057 Ch c = 0;
1058 for (int j = 0; j < 2; j++) {
1059 c = static_cast<Ch>(c << 4);
1060 Ch h = *src_;
1061 if (h >= '0' && h <= '9') c = static_cast<Ch>(c + h - '0');
1062 else if (h >= 'A' && h <= 'F') c = static_cast<Ch>(c + h - 'A' + 10);
1063 else if (h >= 'a' && h <= 'f') c = static_cast<Ch>(c + h - 'a' + 10);
1064 else {
1065 valid_ = false;
1066 return 0;
1067 }
1068 src_++;
1069 }
1070 return c;
1071 }
1072
1073 size_t Tell() const { return static_cast<size_t>(src_ - head_); }
1074 bool IsValid() const { return valid_; }
1075
1076 private:
1077 const Ch* src_;
1078 const Ch* head_;
1079 const Ch* end_;
1080 bool valid_;
1081 };
bool operator<(const CBigNum &a, const CBigNum &b)
Definition base58.cpp:498
A document for parsing JSON text as DOM.
Definition document.h:2124
Allocator & GetAllocator()
Get the allocator of this document.
Definition document.h:2412
ValueType::EncodingType EncodingType
Encoding type from Value.
Definition pointer.h:81
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.
GenericPointer & Swap(GenericPointer &other) RAPIDJSON_NOEXCEPT
Swap the content of this pointer with an other.
Definition pointer.h:208
Concept for allocating, resizing and freeing memory block.
bool operator!=(const environment &other)
os_t os
bool operator==(const environment &other)
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition rapidjson.h:406
PointerParseErrorCode
Error code of parsing.
Definition pointer.h:37
@ kPointerParseErrorInvalidEscape
Invalid escape.
Definition pointer.h:41
@ kPointerParseErrorTokenMustBeginWithSolidus
A token must begin with a '/'.
Definition pointer.h:40
@ kPointerParseErrorCharacterMustPercentEncode
A character must percent encoded in URI fragment.
Definition pointer.h:43
@ kPointerParseErrorInvalidPercentEncoding
Invalid percent encoding in URI fragment.
Definition pointer.h:42
#define Ch(x, y, z)
Definition hash_impl.h:17
void diff(const std::string &a, const std::string &b)
Definition jmp.cpp:18
ValueType
Definition Types.h:14
char * u64toa(uint64_t value, char *buffer)
Definition itoa.h:126
char * u32toa(uint32_t value, char *buffer)
Definition itoa.h:39
const GenericPointer< typename T::ValueType > T2 value
Definition pointer.h:1282
const GenericPointer< typename T::ValueType > T2 defaultValue
Definition pointer.h:1181
#define T(meth, val, expected)
@ kObjectType
object
Definition rapidjson.h:648
@ kArrayType
array
Definition rapidjson.h:649
RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.)
Definition rapidjson.h:384
#define RAPIDJSON_NEW(TypeName)
! customization point for global new
Definition rapidjson.h:627
A read-write string stream.
Definition stream.h:188
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
const Ch * name
Name of the token. It has null character at the end but it can contain null character.
Definition pointer.h:98
Reference to a constant string (not taking a copy)
Definition document.h:256
Read-only string stream.
Definition stream.h:154
Encoding conversion.
Definition encodings.h:658
UTF-8 encoding.
Definition encodings.h:96
void cmp(const Operand &op, uint32 imm)
char * s
uint16_t j
size_t len

◆ allocator_

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

Definition at line 1099 of file pointer.h.

◆ nameBuffer_

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

Definition at line 1101 of file pointer.h.

◆ ownAllocator_

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

Definition at line 1100 of file pointer.h.

◆ parseErrorCode_

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

Definition at line 1105 of file pointer.h.

◆ parseErrorOffset_

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

Definition at line 1104 of file pointer.h.

◆ tokenCount_

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

Definition at line 1103 of file pointer.h.

◆ tokens_

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

Definition at line 1102 of file pointer.h.


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