]> git.tdb.fi Git - poefilter.git/blob - source/rangecondition.h
Use covariant return type for Condition::clone
[poefilter.git] / source / rangecondition.h
1 #ifndef RANGECONDITION_H_
2 #define RANGECONDITION_H_
3
4 #include "condition.h"
5 #include "filter.h"
6
7 template<typename T>
8 class RangeCondition: public Condition
9 {
10 public:
11         typedef T Traits;
12         typedef typename Traits::Type Type;
13
14 protected:
15         Type min;
16         Type max;
17
18 public:
19         RangeCondition(Type);
20         RangeCondition(Type, Type);
21
22         virtual RangeCondition<T> *clone() const;
23         virtual void add_lines(std::list<FilterStatement> &) const;
24 };
25
26
27 struct RarityTraits
28 {
29         typedef Rarity Type;
30         static Rarity get_min() { return NORMAL; }
31         static Rarity get_max() { return UNIQUE; }
32         static const char *get_keyword() { return "Rarity"; }
33 };
34
35 typedef RangeCondition<RarityTraits> RarityCondition;
36
37
38 struct QualityTraits
39 {
40         typedef unsigned Type;
41         static unsigned get_min() { return 0; }
42         static unsigned get_max() { return 20; }
43         static const char *get_keyword() { return "Quality"; }
44 };
45
46 typedef RangeCondition<QualityTraits> QualityCondition;
47
48
49 struct ItemLevelTraits
50 {
51         typedef unsigned Type;
52         static unsigned get_min() { return 0; }
53         static unsigned get_max() { return 100; }
54         static const char *get_keyword() { return "ItemLevel"; }
55 };
56
57 typedef RangeCondition<ItemLevelTraits> ItemLevelCondition;
58
59
60 struct DropLevelTraits
61 {
62         typedef unsigned Type;
63         static unsigned get_min() { return 0; }
64         static unsigned get_max() { return 100; }
65         static const char *get_keyword() { return "DropLevel"; }
66 };
67
68 typedef RangeCondition<DropLevelTraits> DropLevelCondition;
69
70
71 struct WidthTraits
72 {
73         typedef unsigned Type;
74         static unsigned get_min() { return 1; }
75         static unsigned get_max() { return 2; }
76         static const char *get_keyword() { return "Width"; }
77 };
78
79 typedef RangeCondition<WidthTraits> WidthCondition;
80
81
82 struct HeightTraits
83 {
84         typedef unsigned Type;
85         static unsigned get_min() { return 1; }
86         static unsigned get_max() { return 4; }
87         static const char *get_keyword() { return "Height"; }
88 };
89
90 typedef RangeCondition<HeightTraits> HeightCondition;
91
92
93 struct SocketsTraits
94 {
95         typedef unsigned Type;
96         static unsigned get_min() { return 1; }
97         static unsigned get_max() { return 6; }
98         static const char *get_keyword() { return "Sockets"; }
99 };
100
101 typedef RangeCondition<SocketsTraits> SocketsCondition;
102
103
104 struct LinkedSocketsTraits
105 {
106         typedef unsigned Type;
107         static unsigned get_min() { return 1; }
108         static unsigned get_max() { return 6; }
109         static const char *get_keyword() { return "LinkedSockets"; }
110 };
111
112 typedef RangeCondition<LinkedSocketsTraits> LinkedSocketsCondition;
113
114
115 template<typename T>
116 RangeCondition<T>::RangeCondition(Type v):
117         min(v),
118         max(v)
119 { }
120
121 template<typename T>
122 RangeCondition<T>::RangeCondition(Type n, Type x):
123         min(n),
124         max(x)
125 { }
126
127 template<typename T>
128 RangeCondition<T> *RangeCondition<T>::clone() const
129 {
130         return new RangeCondition<T>(min, max);
131 }
132
133 template<typename T>
134 void RangeCondition<T>::add_lines(std::list<FilterStatement> &st) const
135 {
136         const char *keyword = Traits::get_keyword();
137         if(min==max)
138                 FilterStatement::add_line(st, Msp::format("%s %s", keyword, min));
139         else
140         {
141                 if(min!=Traits::get_min())
142                         FilterStatement::add_line(st, Msp::format("%s >= %s", keyword, min));
143                 if(max!=Traits::get_max())
144                         FilterStatement::add_line(st, Msp::format("%s <= %s", keyword, max));
145         }
146 }
147
148 #endif