aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/jinja2cpp/src/value.cpp
blob: 3139b29ac259af8769e21ff7049e78cbd1827d93 (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
#if 0
#include "jinja2cpp/value.h"
#include <sstream>

namespace jinja2
{
template<typename T>
std::string toString(T val)
{
    std::ostringstream os;
    os << val;
    return os.str();
}

namespace
{
struct ValueRenderer : boost::static_visitor<std::string>
{
    std::string operator() (bool val) const
    {
        return val ? "True" : "False";
    }
    std::string operator() (const EmptyValue&) const
    {
        return std::string();
    }
    std::string operator() (const std::wstring&) const
    {
        return std::string();
    }

    std::string operator() (const ValuesList& vals) const
    {
        std::string result = "{";
        bool isFirst = true;
        for (auto& val : vals)
        {
            if (isFirst)
                isFirst = false;
            else
                result += ", ";

            result += boost::apply_visitor(ValueRenderer(), val.data());
        }
        result += "}";
        return result;
    }

    std::string operator() (const ValuesMap& vals) const
    {
        std::string result = "{";
        bool isFirst = true;
        for (auto& val : vals)
        {
            if (isFirst)
                isFirst = false;
            else
                result += ", ";

            result += "{\"" + val.first + "\",";
            result += boost::apply_visitor(ValueRenderer(), val.second.data());
            result += "}";
        }
        result += "}";
        return result;
    }

    std::string operator() (const GenericMap& /*val*/) const
    {
        return "";
    }

    std::string operator() (const GenericList& /*val*/) const
    {
        return "";
    }

    template<typename T>
    std::string operator() (const T& val) const
    {
        return toString(val);
    }
};

struct SubscriptionVisitor : public boost::static_visitor<InternalValue>
{
    InternalValue operator() (const ValuesMap& values, const std::string& field) const
    {
        auto p = values.find(field);
        if (p == values.end())
            return InternalValue();

        return p->second;
    }

    InternalValue operator() (const GenericMap& values, const std::string& field) const
    {
        if (!values.HasValue(field))
            return InternalValue();

        return values.GetValueByName(field);
    }

    InternalValue operator() (const GenericMap& values, const int64_t index) const
    {
        if (index < 0 || static_cast<size_t>(index) >= values.GetSize())
            return InternalValue();

        return values.GetValueByIndex(index);
    }

    InternalValue operator() (const ValuesList& values, int64_t index) const
    {
        if (index < 0 || static_cast<size_t>(index) >= values.size())
            return InternalValue();

        return values[static_cast<size_t>(index)];
    }

    InternalValue operator() (const GenericList& values, const int64_t index) const
    {
        if (index < 0 || static_cast<size_t>(index) >= values.GetSize())
            return InternalValue();

        return values.GetValueByIndex(index);
    }

    template<typename T, typename U>
    InternalValue operator() (T&& /*first*/, U&& /*second*/) const
    {
        return InternalValue();
    }
};

} //

InternalValue InternalValue::subscript(const InternalValue& index) const
{
    return boost::apply_visitor(SubscriptionVisitor(), m_data, index.m_data);
}

} // jinja2
#endif