1 #ifndef ZSERIO_WALKER_H_INC 2 #define ZSERIO_WALKER_H_INC 21 template <
typename ALLOC>
28 template <
typename ALLOC = std::allocator<u
int8_t>>
86 template <
typename ALLOC = std::allocator<u
int8_t>>
133 template <
typename ALLOC = std::allocator<u
int8_t>>
198 template <
typename ALLOC = std::allocator<u
int8_t>>
243 bool enterDepthLevel();
244 bool leaveDepthLevel();
259 template <
typename ALLOC = std::allocator<u
int8_t>>
311 std::regex m_pathRegex;
318 template <
typename ALLOC = std::allocator<u
int8_t>>
363 bool filterArrayElement(
size_t elementIndex);
365 size_t m_maxArrayLength;
374 template <
typename ALLOC = std::allocator<u
int8_t>>
422 template <
typename FILTER_FUNC,
typename ...ARGS>
423 bool applyFilters(FILTER_FUNC filterFunc, ARGS... args)
427 result &= (walkFilter.*filterFunc)(args...);
445 template <
typename ALLOC>
447 m_walkObserver(walkObserver), m_walkFilter(m_defaultWalkFilter)
450 template <
typename ALLOC>
452 m_walkObserver(walkObserver), m_walkFilter(walkFilter)
455 template <
typename ALLOC>
465 "' is not a compound type!";
468 m_walkObserver.beginRoot(compound);
469 walkFields(compound, typeInfo);
470 m_walkObserver.endRoot(compound);
473 template <
typename ALLOC>
479 StringView compoundChoice = compound->getChoice();
480 if (!compoundChoice.
empty())
483 auto fieldsIt = std::find_if(fields.
begin(), fields.
end(),
485 return fieldInfo.schemaName == compoundChoice; });
486 if (fieldsIt != fields.
end())
488 walkField(compound->getField(compoundChoice), *fieldsIt);
497 if (!walkField(compound->getField(fieldInfo.schemaName), fieldInfo))
503 template <
typename ALLOC>
507 if (reflectable && fieldInfo.
isArray)
509 if (m_walkFilter.beforeArray(reflectable, fieldInfo))
511 m_walkObserver.beginArray(reflectable, fieldInfo);
512 for (
size_t i = 0; i < reflectable->size(); ++i)
514 if (!walkFieldValue(reflectable->at(i), fieldInfo, i))
517 m_walkObserver.endArray(reflectable, fieldInfo);
520 return m_walkFilter.afterArray(reflectable, fieldInfo);
524 return walkFieldValue(reflectable, fieldInfo);
528 template <
typename ALLOC>
535 if (m_walkFilter.beforeCompound(reflectable, fieldInfo, elementIndex))
537 m_walkObserver.beginCompound(reflectable, fieldInfo, elementIndex);
538 walkFields(reflectable, typeInfo);
539 m_walkObserver.endCompound(reflectable, fieldInfo, elementIndex);
541 return m_walkFilter.afterCompound(reflectable, fieldInfo, elementIndex);
545 if (m_walkFilter.beforeValue(reflectable, fieldInfo, elementIndex))
546 m_walkObserver.visitValue(reflectable, fieldInfo, elementIndex);
547 return m_walkFilter.afterValue(reflectable, fieldInfo, elementIndex);
551 template <
typename ALLOC>
553 m_maxDepth(maxDepth), m_depth(1)
556 template <
typename ALLOC>
560 return enterDepthLevel();
563 template <
typename ALLOC>
567 return leaveDepthLevel();
570 template <
typename ALLOC>
574 return enterDepthLevel();
577 template <
typename ALLOC>
581 return leaveDepthLevel();
584 template <
typename ALLOC>
588 return m_depth <= m_maxDepth;
591 template <
typename ALLOC>
598 template <
typename ALLOC>
601 const bool enter = (m_depth <= m_maxDepth);
606 template <
typename ALLOC>
616 template <
typename ALLOC>
619 const ALLOC& allocator)
622 for (
auto it = currentPath.begin(); it != currentPath.end(); ++it)
624 if (!currentPathStr.empty())
625 currentPathStr +=
".";
626 currentPathStr += *it;
628 return currentPathStr;
631 template <
typename ALLOC>
635 if (elementIndex == WALKER_NOT_ELEMENT)
642 "[" +
toString(elementIndex, allocator) +
"]";
646 template <
typename ALLOC>
650 if (elementIndex == WALKER_NOT_ELEMENT)
651 currentPath.pop_back();
656 template <
typename ALLOC>
661 const std::regex& pathRegex,
const ALLOC& allocator) :
662 m_currentPath(currentPath), m_pathRegex(pathRegex), m_allocator(allocator)
674 m_matches = std::regex_match(getCurrentPath(), m_pathRegex);
682 m_currentPath.pop_back();
689 appendPath(fieldInfo, elementIndex);
690 m_matches = std::regex_match(getCurrentPath(), m_pathRegex);
697 size_t elementIndex)
override 699 popPath(fieldInfo, elementIndex);
704 size_t elementIndex)
override 706 appendPath(fieldInfo, elementIndex);
707 m_matches = std::regex_match(getCurrentPath(), m_pathRegex);
713 size_t elementIndex)
override 715 popPath(fieldInfo, elementIndex);
722 return detail::getCurrentPathImpl(m_currentPath, m_allocator);
727 detail::appendPathImpl(m_currentPath, fieldInfo, elementIndex, m_allocator);
732 detail::popPathImpl(m_currentPath, fieldInfo, elementIndex, m_allocator);
736 std::regex m_pathRegex;
738 bool m_matches =
false;
743 template <
typename ALLOC>
745 m_pathRegex(pathRegex), m_allocator(allocator)
748 template <
typename ALLOC>
752 m_currentPath.emplace_back(fieldInfo.
schemaName.data(), fieldInfo.
schemaName.size(), m_allocator);
754 if (std::regex_match(getCurrentPath(), m_pathRegex))
757 for (
size_t i = 0; i < array->size(); ++i)
760 "[" +
toString(i, m_allocator) +
"]";
762 if (matchSubtree(array->at(i), fieldInfo))
771 template <
typename ALLOC>
775 m_currentPath.pop_back();
779 template <
typename ALLOC>
783 appendPath(fieldInfo, elementIndex);
784 if (std::regex_match(getCurrentPath(), m_pathRegex))
787 return matchSubtree(compound, fieldInfo);
790 template <
typename ALLOC>
794 popPath(fieldInfo, elementIndex);
798 template <
typename ALLOC>
802 appendPath(fieldInfo, elementIndex);
803 return matchSubtree(value, fieldInfo);
806 template <
typename ALLOC>
810 popPath(fieldInfo, elementIndex);
814 template <
typename ALLOC>
817 detail::appendPathImpl(m_currentPath, fieldInfo, elementIndex, m_allocator);
820 template <
typename ALLOC>
823 detail::popPathImpl(m_currentPath, fieldInfo, elementIndex, m_allocator);
826 template <
typename ALLOC>
829 return detail::getCurrentPathImpl(m_currentPath, m_allocator);
832 template <
typename ALLOC>
840 detail::SubtreeRegexWalkFilter<ALLOC> subtreeFilter(m_currentPath, m_pathRegex, m_allocator);
843 return subtreeFilter.matches();
848 return std::regex_match(getCurrentPath(), m_pathRegex);
852 template <
typename ALLOC>
854 m_maxArrayLength(maxArrayLength)
857 template <
typename ALLOC>
864 template <
typename ALLOC>
871 template <
typename ALLOC>
875 return filterArrayElement(elementIndex);
878 template <
typename ALLOC>
882 return filterArrayElement(elementIndex);
885 template <
typename ALLOC>
889 return filterArrayElement(elementIndex);
892 template <
typename ALLOC>
896 return filterArrayElement(elementIndex);
899 template <
typename ALLOC>
902 return elementIndex == WALKER_NOT_ELEMENT ?
true : elementIndex < m_maxArrayLength;
905 template <
typename ALLOC>
907 m_walkFilters(walkFilters)
910 template <
typename ALLOC>
917 template <
typename ALLOC>
924 template <
typename ALLOC>
932 template <
typename ALLOC>
940 template <
typename ALLOC>
947 template <
typename ALLOC>
956 #endif // ZSERIO_WALKER_H_INC typename IBasicReflectable< ALLOC >::ConstPtr IBasicReflectableConstPtr
void walk(const IBasicReflectableConstPtr< ALLOC > &compound)
std::reference_wrapper< IBasicWalkFilter< ALLOC >> WalkFilterRef
BasicWalker & operator=(const BasicWalker &other)=delete
bool beforeCompound(const IBasicReflectableConstPtr< ALLOC > &compound, const BasicFieldInfo< ALLOC > &fieldInfo, size_t elementIndex) override
bool afterCompound(const IBasicReflectableConstPtr< ALLOC > &compound, const BasicFieldInfo< ALLOC > &fieldInfo, size_t elementIndex) override
bool afterArray(const IBasicReflectableConstPtr< ALLOC > &array, const BasicFieldInfo< ALLOC > &fieldInfo) override
BasicDepthWalkFilter(size_t maxDepth)
bool beforeArray(const IBasicReflectableConstPtr< ALLOC > &array, const BasicFieldInfo< ALLOC > &fieldInfo) override
bool afterCompound(const IBasicReflectableConstPtr< ALLOC > &compound, const BasicFieldInfo< ALLOC > &fieldInfo, size_t elementIndex) override
bool beforeArray(const IBasicReflectableConstPtr< ALLOC > &array, const BasicFieldInfo< ALLOC > &fieldInfo) override
string< ALLOC > toString(T value, const ALLOC &allocator=ALLOC())
bool beforeCompound(const IBasicReflectableConstPtr< ALLOC > &compound, const BasicFieldInfo< ALLOC > &fieldInfo, size_t elementIndex) override
void endArray(const IBasicReflectableConstPtr< ALLOC > &, const BasicFieldInfo< ALLOC > &) override
virtual Span< const BasicFieldInfo< ALLOC > > getFields() const =0
BasicArrayLengthWalkFilter(size_t maxArrayLength)
bool afterArray(const IBasicReflectableConstPtr< ALLOC > &, const BasicFieldInfo< ALLOC > &) override
BasicRegexWalkFilter(const char *pathRegex, const ALLOC &allocator=ALLOC())
bool beforeCompound(const IBasicReflectableConstPtr< ALLOC > &compound, const BasicFieldInfo< ALLOC > &fieldInfo, size_t elementIndex) override
bool beforeArray(const IBasicReflectableConstPtr< ALLOC > &, const BasicFieldInfo< ALLOC > &) override
void beginCompound(const IBasicReflectableConstPtr< ALLOC > &, const BasicFieldInfo< ALLOC > &, size_t) override
vector< WalkFilterRef, ALLOC > WalkFilters
bool afterValue(const IBasicReflectableConstPtr< ALLOC > &value, const BasicFieldInfo< ALLOC > &fieldInfo, size_t elementIndex) override
void endCompound(const IBasicReflectableConstPtr< ALLOC > &, const BasicFieldInfo< ALLOC > &, size_t) override
void beginArray(const IBasicReflectableConstPtr< ALLOC > &, const BasicFieldInfo< ALLOC > &) override
virtual StringView getSchemaName() const =0
bool afterArray(const IBasicReflectableConstPtr< ALLOC > &array, const BasicFieldInfo< ALLOC > &fieldInfo) override
static bool isCompound(SchemaType schemaType)
bool afterCompound(const IBasicReflectableConstPtr< ALLOC > &compound, const BasicFieldInfo< ALLOC > &fieldInfo, size_t elementIndex) override
void beginRoot(const IBasicReflectableConstPtr< ALLOC > &) override
virtual SchemaType getSchemaType() const =0
BasicWalker(IBasicWalkObserver< ALLOC > &walkObserver)
bool afterValue(const IBasicReflectableConstPtr< ALLOC > &, const BasicFieldInfo< ALLOC > &, size_t) override
bool afterValue(const IBasicReflectableConstPtr< ALLOC > &value, const BasicFieldInfo< ALLOC > &fieldInfo, size_t elementIndex) override
std::vector< T, RebindAlloc< ALLOC, T >> vector
void endRoot(const IBasicReflectableConstPtr< ALLOC > &) override
static bool hasChoice(SchemaType schemaType)
bool beforeValue(const IBasicReflectableConstPtr< ALLOC > &value, const BasicFieldInfo< ALLOC > &fieldInfo, size_t elementIndex) override
bool afterArray(const IBasicReflectableConstPtr< ALLOC > &array, const BasicFieldInfo< ALLOC > &fieldInfo) override
bool beforeCompound(const IBasicReflectableConstPtr< ALLOC > &compound, const BasicFieldInfo< ALLOC > &fieldInfo, size_t elementIndex) override
bool afterCompound(const IBasicReflectableConstPtr< ALLOC > &, const BasicFieldInfo< ALLOC > &, size_t) override
bool beforeCompound(const IBasicReflectableConstPtr< ALLOC > &, const BasicFieldInfo< ALLOC > &, size_t) override
void visitValue(const IBasicReflectableConstPtr< ALLOC > &, const BasicFieldInfo< ALLOC > &, size_t) override
constexpr iterator begin() const noexcept
BasicAndWalkFilter(const WalkFilters &walkFilters)
bool beforeValue(const IBasicReflectableConstPtr< ALLOC > &value, const BasicFieldInfo< ALLOC > &fieldInfo, size_t elementIndex) override
std::basic_string< char, std::char_traits< char >, RebindAlloc< ALLOC, char >> string
bool beforeValue(const IBasicReflectableConstPtr< ALLOC > &, const BasicFieldInfo< ALLOC > &, size_t) override
bool beforeValue(const IBasicReflectableConstPtr< ALLOC > &value, const BasicFieldInfo< ALLOC > &fieldInfo, size_t elementIndex) override
constexpr bool empty() const noexcept
constexpr iterator end() const noexcept
bool beforeArray(const IBasicReflectableConstPtr< ALLOC > &array, const BasicFieldInfo< ALLOC > &fieldInfo) override
bool beforeArray(const IBasicReflectableConstPtr< ALLOC > &array, const BasicFieldInfo< ALLOC > &fieldInfo) override
bool afterValue(const IBasicReflectableConstPtr< ALLOC > &value, const BasicFieldInfo< ALLOC > &fieldInfo, size_t elementIndex) override
const IBasicTypeInfo< ALLOC > & typeInfo
bool afterCompound(const IBasicReflectableConstPtr< ALLOC > &compound, const BasicFieldInfo< ALLOC > &fieldInfo, size_t elementIndex) override
bool afterValue(const IBasicReflectableConstPtr< ALLOC > &value, const BasicFieldInfo< ALLOC > &fieldInfo, size_t elementIndex) override
bool beforeValue(const IBasicReflectableConstPtr< ALLOC > &value, const BasicFieldInfo< ALLOC > &fieldInfo, size_t elementIndex) override
bool afterArray(const IBasicReflectableConstPtr< ALLOC > &array, const BasicFieldInfo< ALLOC > &fieldInfo) override