##// END OF EJS Templates
Defines an iterator for a data series...
Alexandre Leroux -
r595:549a850c09e9
parent child
Show More
@@ -0,0 +1,82
1 #ifndef SCIQLOP_DATASERIESITERATOR_H
2 #define SCIQLOP_DATASERIESITERATOR_H
3
4 #include "CoreGlobal.h"
5
6 #include <memory>
7
8 /**
9 * @brief The DataSeriesIteratorValue class represents the current value of a data series iterator.
10 * It offers standard access methods for the data in the series (x-axis, values), but it is up to
11 * each series to define its own implementation of how to retrieve this data, by implementing the
12 * DataSeriesIteratorValue::Impl interface
13 *
14 * @sa DataSeriesIterator
15 */
16 class SCIQLOP_CORE_EXPORT DataSeriesIteratorValue {
17 public:
18 struct Impl {
19 virtual ~Impl() noexcept = default;
20 virtual std::unique_ptr<Impl> clone() const = 0;
21 virtual bool equals(const Impl &other) const = 0;
22 virtual void next() = 0;
23 virtual void prev() = 0;
24 virtual double x() const = 0;
25 virtual double value() const = 0;
26 virtual double value(int componentIndex) const = 0;
27 };
28
29 explicit DataSeriesIteratorValue(std::unique_ptr<Impl> impl);
30 DataSeriesIteratorValue(const DataSeriesIteratorValue &other);
31 DataSeriesIteratorValue(DataSeriesIteratorValue &&other) = default;
32 DataSeriesIteratorValue &operator=(DataSeriesIteratorValue other);
33
34 bool equals(const DataSeriesIteratorValue &other) const;
35
36 /// Advances to the next value
37 void next();
38 /// Moves back to the previous value
39 void prev();
40 /// Gets x-axis data
41 double x() const;
42 /// Gets value data
43 double value() const;
44 /// Gets value data depending on an index
45 double value(int componentIndex) const;
46
47 private:
48 std::unique_ptr<Impl> m_Impl;
49 };
50
51 /**
52 * @brief The DataSeriesIterator class represents an iterator used for data series. It defines all
53 * operators needed for a standard forward iterator
54 * @sa http://www.cplusplus.com/reference/iterator/
55 */
56 class SCIQLOP_CORE_EXPORT DataSeriesIterator {
57 public:
58 using iterator_category = std::forward_iterator_tag;
59 using value_type = const DataSeriesIteratorValue;
60 using difference_type = std::ptrdiff_t;
61 using pointer = value_type *;
62 using reference = value_type &;
63
64 explicit DataSeriesIterator(DataSeriesIteratorValue value);
65 virtual ~DataSeriesIterator() noexcept = default;
66 DataSeriesIterator(const DataSeriesIterator &) = default;
67 DataSeriesIterator(DataSeriesIterator &&) = default;
68 DataSeriesIterator &operator=(const DataSeriesIterator &) = default;
69 DataSeriesIterator &operator=(DataSeriesIterator &&) = default;
70
71 DataSeriesIterator &operator++();
72 DataSeriesIterator &operator--();
73 pointer operator->() const { return &m_CurrentValue; }
74 reference operator*() const { return m_CurrentValue; }
75 bool operator==(const DataSeriesIterator &other) const;
76 bool operator!=(const DataSeriesIterator &other) const;
77
78 private:
79 DataSeriesIteratorValue m_CurrentValue;
80 };
81
82 #endif // SCIQLOP_DATASERIESITERATOR_H
@@ -0,0 +1,75
1 #include "Data/DataSeriesIterator.h"
2
3 DataSeriesIteratorValue::DataSeriesIteratorValue(
4 std::unique_ptr<DataSeriesIteratorValue::Impl> impl)
5 : m_Impl{std::move(impl)}
6 {
7 }
8
9 DataSeriesIteratorValue::DataSeriesIteratorValue(const DataSeriesIteratorValue &other)
10 : m_Impl{other.m_Impl->clone()}
11 {
12 }
13
14 DataSeriesIteratorValue &DataSeriesIteratorValue::operator=(DataSeriesIteratorValue other)
15 {
16 std::swap(m_Impl, other.m_Impl);
17 return *this;
18 }
19
20 bool DataSeriesIteratorValue::equals(const DataSeriesIteratorValue &other) const
21 {
22 return m_Impl->equals(*other.m_Impl);
23 }
24
25 void DataSeriesIteratorValue::next()
26 {
27 m_Impl->next();
28 }
29
30 void DataSeriesIteratorValue::prev()
31 {
32 m_Impl->prev();
33 }
34
35 double DataSeriesIteratorValue::x() const
36 {
37 return m_Impl->x();
38 }
39
40 double DataSeriesIteratorValue::value() const
41 {
42 return m_Impl->value();
43 }
44
45 double DataSeriesIteratorValue::value(int componentIndex) const
46 {
47 return m_Impl->value(componentIndex);
48 }
49
50 DataSeriesIterator::DataSeriesIterator(DataSeriesIteratorValue value)
51 : m_CurrentValue{std::move(value)}
52 {
53 }
54
55 DataSeriesIterator &DataSeriesIterator::operator++()
56 {
57 m_CurrentValue.next();
58 return *this;
59 }
60
61 DataSeriesIterator &DataSeriesIterator::operator--()
62 {
63 m_CurrentValue.prev();
64 return *this;
65 }
66
67 bool DataSeriesIterator::operator==(const DataSeriesIterator &other) const
68 {
69 return m_CurrentValue.equals(other.m_CurrentValue);
70 }
71
72 bool DataSeriesIterator::operator!=(const DataSeriesIterator &other) const
73 {
74 return !(*this == other);
75 }
General Comments 0
You need to be logged in to leave comments. Login now