forked from niftools/nifskope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nifexpr.h
142 lines (123 loc) · 4.39 KB
/
nifexpr.h
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
/***** BEGIN LICENSE BLOCK *****
BSD License
Copyright (c) 2005-2012, NIF File Format Library and Tools
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the NIF File Format Library and Tools project may not be
used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
***** END LICENCE BLOCK *****/
#ifndef NIFEXPR_H
#define NIFEXPR_H
#pragma once
#include <QVariant>
#include <QString>
class Expression
{
enum Operator {
e_nop, e_not_eq, e_eq, e_gte, e_lte, e_gt, e_lt, e_bit_and, e_bit_or,
e_add, e_sub, e_bool_and, e_bool_or, e_not,
};
QVariant lhs;
QVariant rhs;
Operator opcode;
public:
explicit Expression()
{
opcode = Expression::e_nop;
}
Expression(const QString & cond, int startpos, int endpos)
{
opcode = Expression::e_nop;
partition(cond.mid(startpos, endpos-startpos+1));
}
Expression(const QString & cond)
{
opcode = Expression::e_nop;
partition(cond);
}
QString toString() const;
public:
template <class F>
QVariant evaluateValue( const F& convert ) const
{
QVariant l = convertValue(lhs, convert);
QVariant r = convertValue(rhs, convert);
NormalizeVariants(l, r);
switch (opcode)
{
case Expression::e_not:
return QVariant::fromValue( !r.toBool() );
case Expression::e_not_eq:
return QVariant::fromValue(l != r);
case Expression::e_eq:
return QVariant::fromValue(l == r);
case Expression::e_gte:
return QVariant::fromValue(l.toUInt() >= r.toUInt());
case Expression::e_lte:
return QVariant::fromValue(l.toUInt() <= r.toUInt());
case Expression::e_gt:
return QVariant::fromValue(l.toUInt() > r.toUInt());
case Expression::e_lt:
return QVariant::fromValue(l.toUInt() < r.toUInt());
case Expression::e_bit_and:
return QVariant::fromValue(l.toUInt() & r.toUInt());
case Expression::e_bit_or:
return QVariant::fromValue(l.toUInt() | r.toUInt());
case Expression::e_add:
return QVariant::fromValue(l.toUInt() + r.toUInt());
case Expression::e_sub:
return QVariant::fromValue(l.toUInt() - r.toUInt());
case Expression::e_bool_and:
return QVariant::fromValue(l.toBool() && r.toBool());
case Expression::e_bool_or:
return QVariant::fromValue(l.toBool() || r.toBool());
case Expression::e_nop:
return l;
}
return l;
}
template <class F>
bool evaluateBool( const F& convert ) const
{
return evaluateValue(convert).toBool();
}
template <class F>
int evaluateUInt( const F& convert ) const
{
return evaluateValue(convert).toUInt();
}
private:
static Operator operatorFromString(const QString& str);
void partition(const QString & cond, int offset = 0);
void NormalizeVariants(QVariant &l, QVariant &r) const;
template <class F>
QVariant convertValue(const QVariant& v, const F& convert ) const
{
if (v.type() == QVariant::UserType)
{
if ( v.canConvert<Expression>() )
return v.value<Expression>().evaluateValue(convert);
}
return convert(v);
}
};
Q_DECLARE_METATYPE(Expression)
#endif