|
| 1 | +#ifndef _SEQUENCE_H_ |
| 2 | +#define _SEQUENCE_H_ |
| 3 | + |
| 4 | +#include <list> |
| 5 | + |
| 6 | +#include "Value.h" |
| 7 | + |
| 8 | +namespace MafXpathParser |
| 9 | +{ |
| 10 | + |
| 11 | +// A class that represents a xpath Sequence. |
| 12 | +class Sequence |
| 13 | +{ |
| 14 | +public: |
| 15 | + Sequence(); |
| 16 | + explicit Sequence(const std::list<const Value*>& nodes); |
| 17 | + explicit Sequence(const Value* expr); |
| 18 | + Sequence(const Sequence& s); |
| 19 | + /** |
| 20 | + * no virtual destructor since there is a need |
| 21 | + * for this class to be memory efficient. |
| 22 | + * Also the NotOwningSequence dont want this destructor |
| 23 | + * to be called. |
| 24 | + */ |
| 25 | + ~Sequence(); |
| 26 | + |
| 27 | + std::ostream& put(std::ostream& os) const; |
| 28 | + const std::string toString(const std::string& networkManagedElementId="") const; |
| 29 | + bool exists(EvalContext& evalContext) const; |
| 30 | + bool isTrue() const; |
| 31 | + |
| 32 | + /** |
| 33 | + * Methods to access/add values etc |
| 34 | + */ |
| 35 | + |
| 36 | + /** |
| 37 | + * Since the Dns class has a set of values, counting is needed. |
| 38 | + * this should not be too bad since size on list has O(n) |
| 39 | + * complexity according to specs. |
| 40 | + */ |
| 41 | + size_t size() const |
| 42 | + { |
| 43 | + size_t result = 0; |
| 44 | + for (std::list<const Value*>::const_iterator i = _values.begin(); |
| 45 | + i != _values.end(); |
| 46 | + ++i) { |
| 47 | + result += (*i)->size(); |
| 48 | + } |
| 49 | + return result; |
| 50 | + } |
| 51 | + |
| 52 | + void addValue(const Value* n); |
| 53 | + void addValueFront(const Value* n); |
| 54 | + const Value& firstValue() const; |
| 55 | + const Value& secondValue() const; |
| 56 | + std::list<const Value*>& getValues(); |
| 57 | + const std::list<const Value*>& getValues() const; |
| 58 | + void splice(const Sequence& s) |
| 59 | + { |
| 60 | + _values.splice(_values.end(), const_cast<Sequence&>(s)._values); |
| 61 | + } |
| 62 | + struct Destroy { |
| 63 | + void operator ()(const Value* v) |
| 64 | + { |
| 65 | + delete v; |
| 66 | + } |
| 67 | + }; |
| 68 | +private: |
| 69 | + Sequence& operator = (const Sequence&); // Not allowed |
| 70 | + struct NotExist { |
| 71 | + NotExist(EvalContext& evalContext): _evalContext(evalContext) {} |
| 72 | + bool operator() (const Value* v) |
| 73 | + { |
| 74 | + return !v->exists(_evalContext); |
| 75 | + } |
| 76 | + EvalContext& _evalContext; |
| 77 | + }; |
| 78 | + struct Copy { |
| 79 | + Copy(std::list<const Value*>& values) : _values(values) |
| 80 | + { |
| 81 | + ; |
| 82 | + } |
| 83 | + void operator ()(const Value* v) |
| 84 | + { |
| 85 | + _values.push_back(v->clone()); |
| 86 | + } |
| 87 | + std::list<const Value*>& _values; |
| 88 | + }; |
| 89 | +protected: |
| 90 | + /** |
| 91 | + * A sequence is a list of values |
| 92 | + */ |
| 93 | + std::list<const Value*> _values; |
| 94 | +}; |
| 95 | + |
| 96 | +// Class to make sure the destructor does not delete |
| 97 | +// the values in the sequence. |
| 98 | +class NotOwningSequence : public Sequence |
| 99 | +{ |
| 100 | +public: |
| 101 | + ~NotOwningSequence() |
| 102 | + { |
| 103 | + _values.clear(); |
| 104 | + } |
| 105 | +}; |
| 106 | + |
| 107 | +inline |
| 108 | +std::ostream& |
| 109 | +operator<<(std::ostream& os, const Sequence& s) |
| 110 | +{ |
| 111 | + s.put(os); |
| 112 | + return os; |
| 113 | +} |
| 114 | + |
| 115 | +} // namespace XpathParser |
| 116 | + |
| 117 | +#endif |
0 commit comments