Iterator.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * Iterator.h
8  *
9  ****/
10 #pragma once
11 
12 #include "Partition.h"
13 
14 namespace Storage
15 {
16 class Device;
17 
18 class Iterator
19 {
20 public:
21  using iterator_category = std::forward_iterator_tag;
22  using value_type = Partition;
23  using difference_type = std::ptrdiff_t;
24  using pointer = Partition*;
25  using reference = Partition&;
26 
27  Iterator(Device& device) : mSearch{&device, Partition::Type::any, Partition::SubType::any}, mDevice(&device)
28  {
29  next();
30  }
31 
32  Iterator(Device& device, Partition::Type type, uint8_t subtype) : mSearch{&device, type, subtype}, mDevice(&device)
33  {
34  next();
35  }
36 
37  Iterator(Partition::Type type, uint8_t subtype);
38 
39  explicit operator bool() const
40  {
41  return mDevice && mInfo;
42  }
43 
44  Iterator operator++(int)
45  {
46  auto result = *this;
47  next();
48  return result;
49  }
50 
52  {
53  next();
54  return *this;
55  }
56 
57  bool operator==(const Iterator& other) const
58  {
59  return mInfo == other.mInfo;
60  }
61 
62  bool operator!=(const Iterator& other) const
63  {
64  return !operator==(other);
65  }
66 
67  Partition operator*() const
68  {
69  return mDevice && mInfo ? Partition(*mDevice, *mInfo) : Partition{};
70  }
71 
72  Iterator begin()
73  {
74  return mSearch.device ? Iterator(*mSearch.device) : Iterator(mSearch.type, mSearch.subType);
75  }
76 
77  Iterator end()
78  {
79  return Iterator();
80  }
81 
82 private:
83  Iterator()
84  {
85  }
86 
87  void next();
88 
89  struct Search {
90  Device* device;
91  Partition::Type type;
92  uint8_t subType;
93  };
94  Search mSearch{};
95  Device* mDevice{nullptr};
96  const Partition::Info* mInfo{nullptr};
97 };
98 
99 } // namespace Storage
Represents a flash partition.
Definition: Partition.h:85
Partition operator*() const
Definition: Iterator.h:75
std::forward_iterator_tag iterator_category
Definition: Iterator.h:29
Iterator begin()
Definition: Iterator.h:80
Partition * pointer
Definition: Iterator.h:32
Definition: FileDevice.h:25
bool operator==(const Iterator &other) const
Definition: Iterator.h:65
Iterator & operator++()
Definition: Iterator.h:59
Definition: Iterator.h:26
bool operator!=(const Iterator &other) const
Definition: Iterator.h:70
Represents a storage device (e.g. flash memory)
Definition: Components/Storage/src/include/Storage/Device.h:33
Iterator end()
Definition: Iterator.h:85
static constexpr uint8_t any
Definition: Partition.h:99
Partition & reference
Definition: Iterator.h:33
std::ptrdiff_t difference_type
Definition: Iterator.h:31
Type
Definition: Partition.h:88
Partition value_type
Definition: Iterator.h:30