From b8b7dfedd192d666da5c64ab941e8819e3091fe9 Mon Sep 17 00:00:00 2001 From: Milo Yip Date: Sat, 30 Jan 2016 23:07:51 +0800 Subject: [PATCH] Fix partial specialization issue --- include/rapidjson/schema.h | 59 ++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/include/rapidjson/schema.h b/include/rapidjson/schema.h index 7ff55d1..34dc318 100644 --- a/include/rapidjson/schema.h +++ b/include/rapidjson/schema.h @@ -1249,6 +1249,36 @@ private: bool exclusiveMaximum_; }; +template +struct TokenHelper { + RAPIDJSON_FORCEINLINE static void AppendIndexToken(Stack& documentStack, SizeType index) { + *documentStack.template Push() = '/'; + char buffer[21]; + size_t length = static_cast((sizeof(SizeType) == 4 ? u32toa(index, buffer) : u64toa(index, buffer)) - buffer); + for (size_t i = 0; i < length; i++) + *documentStack.template Push() = buffer[i]; + } +}; + +// Partial specialized version for char to prevent buffer copying. +template +struct TokenHelper { + RAPIDJSON_FORCEINLINE static void AppendIndexToken(Stack& documentStack, SizeType index) { + if (sizeof(SizeType) == 4) { + char *buffer = documentStack.template Push(1 + 10); // '/' + uint + *buffer++ = '/'; + const char* end = internal::u32toa(index, buffer); + documentStack.template Pop(static_cast(10 - (end - buffer))); + } + else { + char *buffer = documentStack.template Push(1 + 20); // '/' + uint64 + *buffer++ = '/'; + const char* end = internal::u64toa(index, buffer); + documentStack.template Pop(static_cast(20 - (end - buffer))); + } + } +}; + } // namespace internal /////////////////////////////////////////////////////////////////////////////// @@ -1709,7 +1739,7 @@ private: PushSchema(root_); else { if (CurrentContext().inArray) - AppendToken(CurrentContext().arrayElementIndex); + internal::TokenHelper, Ch>::AppendIndexToken(documentStack_, CurrentContext().arrayElementIndex); if (!CurrentSchema().BeginValue(CurrentContext())) return false; @@ -1789,33 +1819,6 @@ private: } } - template - void AppendToken(SizeType index) { - *documentStack_.template Push() = '/'; - char buffer[21]; - size_t length = static_cast((sizeof(SizeType) == 4 ? internal::u32toa(index, buffer) : internal::u64toa(index, buffer)) - buffer); - for (size_t i = 0; i < length; i++) - *documentStack_.template Push() = buffer[i]; - } - - // Specialized version for char to prevent buffer copying. - template <> - void AppendToken(SizeType index) { - if (sizeof(SizeType) == 4) { - char *buffer = documentStack_.template Push(1 + 10); // '/' + uint - *buffer++ = '/'; - const char* end = internal::u32toa(index, buffer); - documentStack_.template Pop(static_cast(10 - (end - buffer))); - } - else { - char *buffer = documentStack_.template Push(1 + 20); // '/' + uint64 - *buffer++ = '/'; - const char* end = internal::u64toa(index, buffer); - documentStack_.template Pop(static_cast(20 - (end - buffer))); - } - } - - RAPIDJSON_FORCEINLINE void PushSchema(const SchemaType& schema) { new (schemaStack_.template Push()) Context(*this, &schema); } RAPIDJSON_FORCEINLINE void PopSchema() {