]> git.tdb.fi Git - poefilter.git/blob - source/category.cpp
Use implicit instead of explicit category ordering
[poefilter.git] / source / category.cpp
1 #include "category.h"
2 #include "choicecondition.h"
3 #include "filter.h"
4 #include "poefilter.h"
5 #include "rangecondition.h"
6 #include "theme.h"
7
8 using namespace std;
9 using namespace Msp;
10
11 Category::Category():
12         condition(0),
13         order(0)
14 { }
15
16 Category::Category(const string &n, unsigned o):
17         name(n),
18         condition(0),
19         order(o)
20 { }
21
22 Category::Category(const Category &other):
23         name(other.name),
24         condition(other.condition ? other.condition->clone() : 0),
25         order(other.order),
26         appearance(other.appearance)
27 { }
28
29 Category &Category::operator=(const Category &other)
30 {
31         delete condition;
32         name = other.name;
33         condition = (other.condition ? other.condition->clone() : 0);
34         order = other.order;
35         appearance = other.appearance;
36         return *this;
37 }
38
39 Category::~Category()
40 {
41         delete condition;
42 }
43
44 void Category::create_statements(list<FilterStatement> &st) const
45 {
46         if(st.empty())
47                 st.push_back(FilterStatement());
48
49         if(condition)
50                 condition->add_lines(st);
51 }
52
53
54 Category::Loader::Loader(Category &c, const PoeFilter &p, CompoundCondition *n):
55         DataFile::ObjectLoader<Category>(c),
56         poe(p),
57         compound(n),
58         app_loader(c.appearance)
59 {
60         add("and", &Loader::and_);
61         add("base_type", &Loader::condition<BaseTypeCondition>);
62         add("class", &Loader::condition<ClassCondition>);
63         add_range<DropLevelCondition>("drop_level");
64         add_range<HeightCondition>("height");
65         add_range<ItemLevelCondition>("item_level");
66         add("linked_colors", &Loader::linked_colors);
67         add_range<LinkedSocketsCondition>("linked_sockets");
68         add_range<SocketsCondition>("sockets");
69         add("or", &Loader::or_);
70         add_range<QualityCondition>("quality");
71         add_range<RarityCondition>("rarity");
72         add_range<WidthCondition>("width");
73
74         add_auxiliary_loader(app_loader);
75 }
76
77 template<typename T>
78 void Category::Loader::add_range(const string &keyword)
79 {
80         add(keyword, &Loader::condition<T>);
81         add(keyword, &Loader::condition_range<T>);
82         add("min_"+keyword, &Loader::condition_min<T>);
83         add("max_"+keyword, &Loader::condition_max<T>);
84 }
85
86 void Category::Loader::add_condition(Condition *cond)
87 {
88         if(compound)
89                 compound->add(cond);
90         else if(!obj.condition)
91                 obj.condition = cond;
92         else
93         {
94                 compound = new AndCondition;
95                 compound->add(obj.condition);
96                 compound->add(cond);
97                 obj.condition = compound;
98         }
99 }
100
101 void Category::Loader::and_()
102 {
103         RefPtr<AndCondition> cond = new AndCondition;
104         Loader sub_ldr(obj, poe, cond.get());
105         load_sub_with(sub_ldr);
106         add_condition(cond.release());
107 }
108
109 template<typename T>
110 void Category::Loader::condition(typename T::Type value)
111 {
112         add_condition(new T(value));
113 }
114
115 template<typename T>
116 void Category::Loader::condition_max(typename T::Type max)
117 {
118         add_condition(new T(T::Traits::get_min(), max));
119 }
120
121 template<typename T>
122 void Category::Loader::condition_min(typename T::Type min)
123 {
124         add_condition(new T(min, T::Traits::get_max()));
125 }
126
127 template<typename T>
128 void Category::Loader::condition_range(typename T::Type min, typename T::Type max)
129 {
130         add_condition(new T(min, max));
131 }
132
133 void Category::Loader::linked_colors(const LinkedColorsCondition::Colors &colors)
134 {
135         add_condition(new LinkedColorsCondition(colors));
136 }
137
138 void Category::Loader::or_()
139 {
140         RefPtr<OrCondition> cond = new OrCondition;
141         Loader sub_ldr(obj, poe, cond.get());
142         load_sub_with(sub_ldr);
143         add_condition(cond.release());
144 }