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