aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/jinja2cpp/src/testers.h
blob: 9d29e6cbc1abb73d6e20772fb0e758ddbe87663b (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
#ifndef JINJA2CPP_SRC_TESTERS_H
#define JINJA2CPP_SRC_TESTERS_H

#include "expression_evaluator.h"
#include "function_base.h"
#include "jinja2cpp/value.h"
#include "render_context.h"

#include <memory>
#include <functional>

namespace jinja2
{
using TesterPtr = std::shared_ptr<IsExpression::ITester>;
using TesterParams = CallParamsInfo;

extern TesterPtr CreateTester(std::string testerName, CallParamsInfo params);

namespace testers
{

class TesterBase : public FunctionBase, public IsExpression::ITester
{
};

class Comparator : public TesterBase
{
public:
    Comparator(TesterParams params, BinaryExpression::Operation op);

    bool Test(const InternalValue& baseVal, RenderContext& context) override;
    bool IsEqual(const IComparable& other) const override
    {
        auto* val = dynamic_cast<const Comparator*>(&other);
        if (!val)
            return false;
        return m_op == val->m_op;
    }
private:
    BinaryExpression::Operation m_op;
};

class StartsWith : public IsExpression::ITester
{
public:
    StartsWith(TesterParams);

    bool Test(const InternalValue& baseVal, RenderContext& context) override;

    bool IsEqual(const IComparable& other) const override
    {
        auto* val = dynamic_cast<const StartsWith*>(&other);
        if (!val)
            return false;
        return m_stringEval == val->m_stringEval;
    }
private:
    ExpressionEvaluatorPtr<> m_stringEval;
};

class ValueTester : public TesterBase
{
public:
    enum Mode
    {
        IsDefinedMode,
        IsEvenMode,
        IsInMode,
        IsIterableMode,
        IsLowerMode,
        IsMappingMode,
        IsNumberMode,
        IsOddMode,
        IsSequenceMode,
        IsStringMode,
        IsUndefinedMode,
        IsUpperMode
    };

    ValueTester(TesterParams params, Mode mode);

    bool Test(const InternalValue& baseVal, RenderContext& context) override;

    bool IsEqual(const IComparable& other) const override
    {
        auto* val = dynamic_cast<const ValueTester*>(&other);
        if (!val)
            return false;
        return m_mode == val->m_mode;
    }
private:
    Mode m_mode;
};

class UserDefinedTester : public TesterBase
{
public:
    UserDefinedTester(std::string filterName, TesterParams params);

    bool Test(const InternalValue& baseVal, RenderContext& context) override;

    bool IsEqual(const IComparable& other) const override
    {
        auto* val = dynamic_cast<const UserDefinedTester*>(&other);
        if (!val)
            return false;
        return m_testerName == val->m_testerName && m_callParams == val->m_callParams;
    }
private:
    std::string m_testerName;
    TesterParams m_callParams;
};
} // namespace testers
} // namespace jinja2

#endif // JINJA2CPP_SRC_TESTERS_H