aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/jinja2cpp/src/value_helpers.h
blob: d5ac829fc4f52bd89690e54785e445e5df9b5aa0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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