]> git.tdb.fi Git - poefilter.git/blob - source/choicecondition.h
6e2954dbf04790ea6ebfb1a3530463880dcf2b93
[poefilter.git] / source / choicecondition.h
1 #ifndef CHOICECONDITION_H_
2 #define CHOICECONDITION_H_
3
4 #include "condition.h"
5 #include "filter.h"
6
7 template<typename T>
8 class ChoiceCondition: public Condition
9 {
10 public:
11         typedef T Traits;
12         typedef typename Traits::Type Type;
13
14 private:
15         Type value;
16
17 public:
18         ChoiceCondition(Type);
19
20         virtual Condition *clone() const;
21         virtual bool can_merge(const Condition &, const CompoundCondition &) const;
22         virtual void add_lines(std::list<FilterStatement> &) const;
23         virtual void add_merged_lines(const std::vector<Condition *> &, const CompoundCondition &, std::list<FilterStatement> &) const;
24 };
25
26
27 struct ClassTraits
28 {
29         typedef std::string Type;
30         static const char *get_keyword() { return "Class"; }
31 };
32
33 typedef ChoiceCondition<ClassTraits> ClassCondition;
34
35
36 struct BaseTypeTraits
37 {
38         typedef std::string Type;
39         static const char *get_keyword() { return "BaseType"; }
40 };
41
42 typedef ChoiceCondition<BaseTypeTraits> BaseTypeCondition;
43
44
45 template<typename T>
46 ChoiceCondition<T>::ChoiceCondition(Type v):
47         value(v)
48 { }
49
50 template<typename T>
51 Condition *ChoiceCondition<T>::clone() const
52 {
53         return new ChoiceCondition<T>(values);
54 }
55
56 template<typename T>
57 bool ChoiceCondition<T>::can_merge(const Condition &other, const CompoundCondition &parent) const
58 {
59         return dynamic_cast<const ChoiceCondition<T> *>(&other) && dynamic_cast<const OrCondition *>(&parent);
60 }
61
62 template<typename T>
63 void ChoiceCondition<T>::add_lines(std::list<FilterStatement> &st) const
64 {
65         const char *keyword = Traits::get_keyword();
66         FilterStatement::add_line(st, Msp::format("%s \"%s\"", keyword, value));
67 }
68
69 template<typename T>
70 void ChoiceCondition<T>::add_merged_lines(const std::vector<Condition *> &conditions, const CompoundCondition &parent, std::list<FilterStatement> &st) const
71 {
72         if(dynamic_cast<const OrCondition *>(&parent))
73         {
74                 std::string line = Traits::get_keyword();
75                 for(std::vector<Condition *>::const_iterator i=conditions.begin(); i!=conditions.end(); ++i)
76                         line += Msp::format(" \"%s\"", static_cast<const ChoiceCondition<Traits> *>(*i)->value);
77                 FilterStatement::add_line(st, line);
78         }
79 }
80
81 #endif