]> git.tdb.fi Git - poefilter.git/commitdiff
Tighten some parameters and return values
authorMikko Rasa <tdb@tdb.fi>
Fri, 17 Aug 2018 16:56:59 +0000 (19:56 +0300)
committerMikko Rasa <tdb@tdb.fi>
Fri, 17 Aug 2018 16:56:59 +0000 (19:56 +0300)
source/choicecondition.h
source/condition.cpp
source/condition.h
source/rangecondition.h

index c2f7f0e66875bf465721574ff4cf244af3605ca4..9792f64c42f012963933b794ca09cba09acee6c7 100644 (file)
@@ -21,7 +21,7 @@ public:
 
        virtual ChoiceCondition<T> *clone() const;
        virtual bool can_merge(const Condition &, const CompoundCondition &) const;
-       virtual ChoiceCondition<T> *merge(const std::vector<Condition *> &, const CompoundCondition &) const;
+       virtual ChoiceCondition<T> *merge(const std::vector<const Condition *> &, const CompoundCondition &) const;
        virtual void add_lines(std::list<FilterStatement> &) const;
 };
 
@@ -68,14 +68,14 @@ bool ChoiceCondition<T>::can_merge(const Condition &other, const CompoundConditi
 }
 
 template<typename T>
-ChoiceCondition<T> *ChoiceCondition<T>::merge(const std::vector<Condition *> &conditions, const CompoundCondition &parent) const
+ChoiceCondition<T> *ChoiceCondition<T>::merge(const std::vector<const Condition *> &conditions, const CompoundCondition &parent) const
 {
        if(dynamic_cast<const OrCondition *>(&parent) && !conditions.empty())
        {
                ChoiceCondition<T> *result = new ChoiceCondition<T>(std::vector<Type>());
-               for(std::vector<Condition *>::const_iterator i=conditions.begin(); i!=conditions.end(); ++i)
+               for(std::vector<const Condition *>::const_iterator i=conditions.begin(); i!=conditions.end(); ++i)
                {
-                       const std::vector<Type> &v = static_cast<ChoiceCondition<T> *>(*i)->values;
+                       const std::vector<Type> &v = static_cast<const ChoiceCondition<T> *>(*i)->values;
                        for(typename std::vector<Type>::const_iterator j=v.begin(); j!=v.end(); ++j)
                                if(Msp::find(result->values, *j)==result->values.end())
                                        result->values.push_back(*j);
index 4d00b6c7a75a3c183aca2367fb474deb04615108..1ff316c361f76f51d6f0c13db000cd21a4b621f1 100644 (file)
@@ -18,14 +18,16 @@ void CompoundCondition::clone_to(CompoundCondition &other) const
 
 void CompoundCondition::add(Condition *cond)
 {
+       if(!cond)
+               throw invalid_argument("CompoundCondition::add");
        conditions.push_back(cond);
 }
 
-const Condition *CompoundCondition::get(unsigned i) const
+const Condition &CompoundCondition::get(unsigned i) const
 {
        if(i>=conditions.size())
                throw out_of_range("CompoundCondition::get");
-       return conditions[i];
+       return *conditions[i];
 }
 
 Condition *CompoundCondition::flatten() const
@@ -40,7 +42,10 @@ Condition *CompoundCondition::flatten() const
                merge = conditions.front()->can_merge(**i, *this);
 
        if(merge)
-               return conditions.front()->merge(conditions, *this);
+       {
+               vector<const Condition *> merge_conds(conditions.begin(), conditions.end());
+               return conditions.front()->merge(merge_conds, *this);
+       }
        else
        {
                Condition *result = 0;
@@ -97,10 +102,9 @@ Condition *CompoundCondition::dispatch_flatten(Condition *cond1, Condition *cond
 
 Condition *CompoundCondition::merge_two(Condition *cond1, Condition *cond2, bool del) const
 {
-       vector<Condition *> parts;
-       parts.reserve(2);
-       parts.push_back(cond1);
-       parts.push_back(cond2);
+       vector<const Condition *> parts(2);
+       parts[0] = cond1;
+       parts[1] = cond2;
        Condition *result = cond1->merge(parts, *this);
 
        if(del)
@@ -193,7 +197,7 @@ Condition *AndCondition::flatten(OrCondition *cond1, Condition *cond2) const
        OrCondition *result = new OrCondition;
        unsigned count = cond1->count();
        for(unsigned i=0; i<count; ++i)
-               if(Condition *sub = dispatch_flatten(cond1->get(i)->clone(), (i+1<count ? cond2->clone() : cond2)))
+               if(Condition *sub = dispatch_flatten(cond1->get(i).clone(), (i+1<count ? cond2->clone() : cond2)))
                        result->add(sub);
        delete cond1;
 
@@ -216,7 +220,7 @@ Condition *AndCondition::flatten(OrCondition *cond1, OrCondition *cond2) const
        OrCondition *result = new OrCondition;
        for(unsigned i=0; i<count1; ++i)
                for(unsigned j=0; j<count2; ++j)
-                       if(Condition *sub = dispatch_flatten(cond1->get(i)->clone(), cond2->get(j)->clone()))
+                       if(Condition *sub = dispatch_flatten(cond1->get(i).clone(), cond2->get(j).clone()))
                                result->add(sub);
 
        delete cond1;
index 163c9c828f6d7499f5be57442e8fac5bfbb87486..bbabf22e5f27d98c8de47b0b4a58622a910dff63 100644 (file)
@@ -20,7 +20,7 @@ public:
        virtual Condition *clone() const = 0;
        virtual Condition *flatten() const { return clone(); }
        virtual bool can_merge(const Condition &, const CompoundCondition &) const { return false; }
-       virtual Condition *merge(const std::vector<Condition *> &, const CompoundCondition &) const { return 0; }
+       virtual Condition *merge(const std::vector<const Condition *> &, const CompoundCondition &) const { return 0; }
        virtual bool is_viable() const { return true; }
        virtual void add_lines(std::list<FilterStatement> &) const = 0;
 };
@@ -43,7 +43,7 @@ protected:
 public:
        void add(Condition *);
        unsigned count() const { return conditions.size(); }
-       const Condition *get(unsigned) const;
+       const Condition &get(unsigned) const;
 
        virtual Condition *flatten() const;
 protected:
index ad54e0a0582f6e89cdd3b1834cc06a88aa334e70..a72fec2d1475db075baffbeb1ed46aa3979480e0 100644 (file)
@@ -21,7 +21,7 @@ public:
 
        virtual RangeCondition<T> *clone() const;
        virtual bool can_merge(const Condition &, const CompoundCondition &) const;
-       virtual RangeCondition<T> *merge(const std::vector<Condition *> &, const CompoundCondition &) const;
+       virtual RangeCondition<T> *merge(const std::vector<const Condition *> &, const CompoundCondition &) const;
        virtual bool is_viable() const { return min<=max; }
        virtual void add_lines(std::list<FilterStatement> &) const;
 };
@@ -149,7 +149,7 @@ bool RangeCondition<T>::can_merge(const Condition &other, const CompoundConditio
 }
 
 template<typename T>
-RangeCondition<T> *RangeCondition<T>::merge(const std::vector<Condition *> &conditions, const CompoundCondition &parent) const
+RangeCondition<T> *RangeCondition<T>::merge(const std::vector<const Condition *> &conditions, const CompoundCondition &parent) const
 {
        if(conditions.empty())
                return 0;
@@ -160,9 +160,9 @@ RangeCondition<T> *RangeCondition<T>::merge(const std::vector<Condition *> &cond
                result = new RangeCondition<T>(Traits::get_min(), Traits::get_max());
        else
                result = new RangeCondition<T>(Traits::get_max(), Traits::get_min());
-       for(std::vector<Condition *>::const_iterator i=conditions.begin(); i!=conditions.end(); ++i)
+       for(std::vector<const Condition *>::const_iterator i=conditions.begin(); i!=conditions.end(); ++i)
        {
-               const RangeCondition<T> *c = static_cast<RangeCondition<T> *>(*i);
+               const RangeCondition<T> *c = static_cast<const RangeCondition<T> *>(*i);
                if(intersect)
                {
                        result->min = std::max(result->min, c->min);