]> git.tdb.fi Git - poefilter.git/blob - source/condition.cpp
Use covariant return type for Condition::clone
[poefilter.git] / source / condition.cpp
1 #include "condition.h"
2 #include "filter.h"
3
4 using namespace std;
5 using namespace Msp;
6
7 CompoundCondition::~CompoundCondition()
8 {
9         for(vector<Condition *>::const_iterator i=conditions.begin(); i!=conditions.end(); ++i)
10                 delete *i;
11 }
12
13 void CompoundCondition::clone_to(CompoundCondition &other) const
14 {
15         for(vector<Condition *>::const_iterator i=conditions.begin(); i!=conditions.end(); ++i)
16                 other.add((*i)->clone());
17 }
18
19 void CompoundCondition::add(Condition *cond)
20 {
21         conditions.push_back(cond);
22 }
23
24
25 AndCondition *AndCondition::clone() const
26 {
27         AndCondition *result = new AndCondition;
28         clone_to(*result);
29         return result;
30 }
31
32 void AndCondition::add_lines(list<FilterStatement> &st) const
33 {
34         for(vector<Condition *>::const_iterator i=conditions.begin(); i!=conditions.end(); ++i)
35                 (*i)->add_lines(st);
36 }
37
38
39 OrCondition *OrCondition::clone() const
40 {
41         OrCondition *result = new OrCondition;
42         clone_to(*result);
43         return result;
44 }
45
46 void OrCondition::add_lines(list<FilterStatement> &st) const
47 {
48         bool merge = conditions.size()>1;
49         for(vector<Condition *>::const_iterator i=conditions.begin(); (merge && ++i!=conditions.end()); )
50                 merge = conditions.front()->can_merge(**i, *this);
51
52         if(merge)
53                 conditions.front()->add_merged_lines(conditions, *this, st);
54         else
55         {
56                 list<FilterStatement> result;
57                 for(vector<Condition *>::const_iterator i=conditions.begin(); i!=conditions.end(); ++i)
58                 {
59                         list<FilterStatement> sub_result = st;
60                         (*i)->add_lines(sub_result);
61                         result.splice(result.end(), sub_result);
62                 }
63                 swap(result, st);
64         }
65 }
66
67
68 LinkedColorsCondition::LinkedColorsCondition(const Colors &c):
69         colors(c)
70 { }
71
72 LinkedColorsCondition *LinkedColorsCondition::clone() const
73 {
74         return new LinkedColorsCondition(colors);
75 }
76
77 void LinkedColorsCondition::add_lines(list<FilterStatement> &st) const
78 {
79         FilterStatement::add_line(st, format("SocketGroup %s", colors.colors));
80 }
81
82
83 void operator>>(const LexicalConverter &conv, LinkedColorsCondition::Colors &colors)
84 {
85         const string &str = conv.get();
86         bool rgb = true;
87         for(string::const_iterator i=str.begin(); (rgb && i!=str.end()); ++i)
88                 rgb = (*i=='R' || *i=='G' || *i=='B');
89         if(str.size()>6 || !rgb)
90                 throw lexical_error(format("conversion of '%s' to LinkedColorsCondition::Colors", str));
91
92         fill(colors.colors, colors.colors+7, '\0');
93         copy(str.begin(), str.end(), colors.colors);
94 }