diff options
author | Maxim Yurchuk <maxim-yurchuk@ydb.tech> | 2024-10-18 20:31:38 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-10-18 20:31:38 +0300 |
commit | 2a74bac2d2d3bccb4e10120f1ead805640ec9dd0 (patch) | |
tree | 047e4818ced5aaf73f58517629e5260b5291f9f0 /contrib/libs/jinja2cpp/src/value_helpers.h | |
parent | 2d9656823e9521d8c29ea4c9a1d0eab78391abfc (diff) | |
parent | 3d834a1923bbf9403cd4a448e7f32b670aa4124f (diff) | |
download | ydb-2a74bac2d2d3bccb4e10120f1ead805640ec9dd0.tar.gz |
Merge pull request #10502 from ydb-platform/mergelibs-241016-1210
Library import 241016-1210
Diffstat (limited to 'contrib/libs/jinja2cpp/src/value_helpers.h')
-rw-r--r-- | contrib/libs/jinja2cpp/src/value_helpers.h | 167 |
1 files changed, 167 insertions, 0 deletions
diff --git a/contrib/libs/jinja2cpp/src/value_helpers.h b/contrib/libs/jinja2cpp/src/value_helpers.h new file mode 100644 index 0000000000..d5ac829fc4 --- /dev/null +++ b/contrib/libs/jinja2cpp/src/value_helpers.h @@ -0,0 +1,167 @@ +#ifndef JINJA2CPP_SRC_VALUE_HELPERS_H +#define JINJA2CPP_SRC_VALUE_HELPERS_H + +#include <jinja2cpp/value.h> + +#include <boost/iterator/iterator_facade.hpp> + +namespace jinja2 +{ +#if 0 +class GenericListIterator + : public boost::iterator_facade< + GenericListIterator, + const InternalValue, + boost::random_access_traversal_tag> +{ +public: + GenericListIterator() + : m_current(0) + , m_list(nullptr) + {} + + explicit GenericListIterator(GenericList& list) + : m_current(0) + , m_list(&list) + {} + +private: + friend class boost::iterator_core_access; + + void increment() + { + ++ m_current; + m_valueIdx = m_current; + m_currentVal = m_current == m_list->GetSize() ? InternalValue() : m_list->GetValueByIndex(static_cast<int64_t>(m_current)); + } + + int distance_to(const GenericListIterator& other) const + { + if (m_list == nullptr) + return other.m_list == nullptr ? 0 : -other.distance_to(*this); + + if (other.m_list == nullptr) + return m_list->GetSize() - m_current; + + return other.m_current - m_current; + } + + void advance(int distance) + { + m_current += distance; + if (distance != 0) + { + m_valueIdx = m_current; + m_currentVal = m_current == m_list->GetSize() ? InternalValue() : m_list->GetValueByIndex(static_cast<int64_t>(m_current)); + + } + } + + bool equal(const GenericListIterator& other) const + { + if (m_list == nullptr) + return other.m_list == nullptr ? true : other.equal(*this); + + if (other.m_list == nullptr) + return m_current == m_list->GetSize(); + + return this->m_list == other.m_list && this->m_current == other.m_current; + } + + const InternalValue& dereference() const + { + if (m_current != m_valueIdx) + m_currentVal = m_current == m_list->GetSize() ? InternalValue() : m_list->GetValueByIndex(static_cast<int64_t>(m_current)); + return m_currentVal; + } + + int64_t m_current = 0; + mutable int64_t m_valueIdx = -1; + mutable InternalValue m_currentVal; + GenericList* m_list; +}; + +class ConstGenericListIterator + : public boost::iterator_facade< + GenericListIterator, + const InternalValue, + boost::random_access_traversal_tag> +{ +public: + ConstGenericListIterator() + : m_current(0) + , m_list(nullptr) + {} + + explicit ConstGenericListIterator(const GenericList& list) + : m_current(0) + , m_list(&list) + {} + +private: + friend class boost::iterator_core_access; + + void increment() + { + ++ m_current; + } + + int distance_to(const ConstGenericListIterator& other) const + { + if (m_list == nullptr) + return other.m_list == nullptr ? 0 : -other.distance_to(*this); + + if (other.m_list == nullptr) + return m_list->GetSize() - m_current; + + return other.m_current - m_current; + } + + void advance(int distance) + { + m_current += distance; + } + + bool equal(const ConstGenericListIterator& other) const + { + if (m_list == nullptr) + return other.m_list == nullptr ? true : other.equal(*this); + + if (other.m_list == nullptr) + return m_current == m_list->GetSize(); + + return this->m_list == other.m_list && this->m_current == other.m_current; + } + + const InternalValue& dereference() const + { + return m_list->GetValueByIndex(static_cast<int64_t>(m_current)); + } + + size_t m_current; + const GenericList* m_list; +}; + +inline auto begin(GenericList& list) +{ + return GenericListIterator(list); +} + +inline auto end(GenericList& list) +{ + return GenericListIterator(); +} + +inline auto begin(const GenericList& list) +{ + return ConstGenericListIterator(list); +} + +inline auto end(const GenericList& list) +{ + return ConstGenericListIterator(); +} +#endif +} // namespace jinja2 + +#endif // JINJA2CPP_SRC_VALUE_HELPERS_H |