summaryrefslogtreecommitdiff
path: root/include/llmr/style/value_comparison.hpp
blob: 4386fae595da2cab943ef503ea62229ebad18f97 (plain)
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
#ifndef LLMR_STYLE_VALUE_COMPARISON
#define LLMR_STYLE_VALUE_COMPARISON

#include "value.hpp"
#include <cstdlib>
#include <cerrno>

namespace llmr {

namespace util {

namespace detail {

template <typename Operator>
struct relaxed_operator_visitor {
    typedef bool result_type;

    inline bool operator()(bool lhs, bool rhs) const { return Operator()(lhs, rhs); }

    template <typename T, class = typename std::enable_if<std::is_arithmetic<T>::value>::type>
    inline bool operator()(bool lhs, T rhs) const { return Operator()(T(lhs), rhs); }

    template <typename T, class = typename std::enable_if<std::is_arithmetic<T>::value>::type>
    inline bool operator()(T lhs, bool rhs) const { return Operator()(lhs, T(rhs)); }

    inline bool operator()(int64_t lhs, uint64_t rhs) const {
        return lhs < 0 ? false : Operator()(uint64_t(lhs), rhs);
    }
    inline bool operator()(uint64_t lhs, int64_t rhs) const {
        return rhs < 0 ? false : Operator()(lhs, uint64_t(rhs));
    }

    template <typename T, class = typename std::enable_if<std::is_arithmetic<T>::value>::type>
    inline bool operator()(const std::string &lhs, T rhs) const {
        double value;
        return parseNumericString(lhs, value) ? Operator()(value, double(rhs)) : false;
    }

    template <typename T, class = typename std::enable_if<std::is_arithmetic<T>::value>::type>
    inline bool operator()(T lhs, const std::string &rhs) const {
        double value;
        return parseNumericString(rhs, value) ? Operator()(double(lhs), value) : false;
    }

    template <typename T0, typename T1>
    inline bool operator()(T0 lhs, T1 rhs) const { return Operator()(lhs, rhs); }
};

struct relaxed_equal_operator {
    template <typename T0, typename T1>
    inline bool operator()(T0 lhs, T1 rhs) const { return lhs == rhs; }
};

struct relaxed_not_equal_operator {
    template <typename T0, typename T1>
    inline bool operator()(T0 lhs, T1 rhs) const { return lhs != rhs; }
};

struct relaxed_greater_operator {
    template <typename T0, typename T1>
    inline bool operator()(T0 lhs, T1 rhs) const { return lhs > rhs; }
};

struct relaxed_greater_equal_operator {
    template <typename T0, typename T1>
    inline bool operator()(T0 lhs, T1 rhs) const { return lhs >= rhs; }
};

struct relaxed_less_operator {
    template <typename T0, typename T1>
    inline bool operator()(T0 lhs, T1 rhs) const { return lhs < rhs; }
};

struct relaxed_less_equal_operator {
    template <typename T0, typename T1>
    inline bool operator()(T0 lhs, T1 rhs) const { return lhs <= rhs; }
};

} // end namespace detail

inline bool relaxed_equal(Value const &lhs, Value const &rhs) {
    return apply_visitor(detail::relaxed_operator_visitor<detail::relaxed_equal_operator>(), lhs, rhs);
}

inline bool relaxed_not_equal(Value const &lhs, Value const &rhs) {
    return apply_visitor(detail::relaxed_operator_visitor<detail::relaxed_not_equal_operator>(), lhs, rhs);
}

inline bool relaxed_greater(Value const &lhs, Value const &rhs) {
    return apply_visitor(detail::relaxed_operator_visitor<detail::relaxed_greater_operator>(), lhs, rhs);
}

inline bool relaxed_greater_equal(Value const &lhs, Value const &rhs) {
    return apply_visitor(detail::relaxed_operator_visitor<detail::relaxed_greater_equal_operator>(), lhs, rhs);
}

inline bool relaxed_less(Value const &lhs, Value const &rhs) {
    return apply_visitor(detail::relaxed_operator_visitor<detail::relaxed_less_operator>(), lhs, rhs);
}

inline bool relaxed_less_equal(Value const &lhs, Value const &rhs) {
    return apply_visitor(detail::relaxed_operator_visitor<detail::relaxed_less_equal_operator>(), lhs, rhs);
}

}

}

#endif