]> git.tdb.fi Git - poefilter.git/blob - source/choicecondition.h
424dca6a8fce12be193e6a9fb02f8048db309a7a
[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         std::vector<Type> values;
16
17 public:
18         ChoiceCondition(Type);
19         ChoiceCondition(const std::vector<Type> &);
20
21         virtual ChoiceCondition<T> *clone() const;
22         virtual bool can_merge(const Condition &, const CompoundCondition &) const;
23         virtual ChoiceCondition<T> *merge(const std::vector<Condition *> &, const CompoundCondition &) const;
24         virtual void add_lines(std::list<FilterStatement> &) const;
25 };
26
27
28 struct ClassTraits
29 {
30         typedef std::string Type;
31         static const char *get_keyword() { return "Class"; }
32 };
33
34 typedef ChoiceCondition<ClassTraits> ClassCondition;
35
36
37 struct BaseTypeTraits
38 {
39         typedef std::string Type;
40         static const char *get_keyword() { return "BaseType"; }
41 };
42
43 typedef ChoiceCondition<BaseTypeTraits> BaseTypeCondition;
44
45
46 template<typename T>
47 ChoiceCondition<T>::ChoiceCondition(Type v)
48 {
49         values.push_back(v);
50 }
51
52 template<typename T>
53 ChoiceCondition<T>::ChoiceCondition(const std::vector<Type> &v):
54         values(v)
55 { }
56
57 template<typename T>
58 ChoiceCondition<T> *ChoiceCondition<T>::clone() const
59 {
60         return new ChoiceCondition<T>(values);
61 }
62
63 template<typename T>
64 bool ChoiceCondition<T>::can_merge(const Condition &other, const CompoundCondition &parent) const
65 {
66         return dynamic_cast<const ChoiceCondition<T> *>(&other) && dynamic_cast<const OrCondition *>(&parent);
67 }
68
69 template<typename T>
70 ChoiceCondition<T> *ChoiceCondition<T>::merge(const std::vector<Condition *> &conditions, const CompoundCondition &parent) const
71 {
72         if(dynamic_cast<const OrCondition *>(&parent) && !conditions.empty())
73         {
74                 ChoiceCondition<T> *result = 0;
75                 for(std::vector<Condition *>::const_iterator i=conditions.begin(); ++i!=conditions.end(); )
76                 {
77                         const std::vector<Type> &v = static_cast<ChoiceCondition<T> *>(*i)->values;
78                         if(!result)
79                                 result = new ChoiceCondition<T>(v);
80                         else
81                                 result->values.insert(result->values.end(), v.begin(), v.end());
82                 }
83                 return result;
84         }
85         else
86                 return 0;
87 }
88
89 template<typename T>
90 void ChoiceCondition<T>::add_lines(std::list<FilterStatement> &st) const
91 {
92         std::string line = Traits::get_keyword();
93         for(typename std::vector<Type>::const_iterator i=values.begin(); i!=values.end(); ++i)
94                 line += Msp::format(" \"%s\"", *i);
95         FilterStatement::add_line(st, line);
96 }
97
98 #endif