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;
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 
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 
68  {
69  return mDevice && mInfo ? Partition(*mDevice, *mInfo) : Partition{};
70  }
71 
73  {
74  return mSearch.device ? Iterator(*mSearch.device) : Iterator(mSearch.type, mSearch.subType);
75  }
76 
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 storage device (e.g. flash memory)
Definition: Components/Storage/src/include/Storage/Device.h:34
Definition: Iterator.h:19
std::forward_iterator_tag iterator_category
Definition: Iterator.h:21
std::ptrdiff_t difference_type
Definition: Iterator.h:23
Iterator(Partition::Type type, uint8_t subtype)
bool operator!=(const Iterator &other) const
Definition: Iterator.h:62
Iterator(Device &device)
Definition: Iterator.h:27
Iterator operator++(int)
Definition: Iterator.h:44
Iterator(Device &device, Partition::Type type, uint8_t subtype)
Definition: Iterator.h:32
Partition operator*() const
Definition: Iterator.h:67
Iterator & operator++()
Definition: Iterator.h:51
bool operator==(const Iterator &other) const
Definition: Iterator.h:57
Iterator end()
Definition: Iterator.h:77
Iterator begin()
Definition: Iterator.h:72
Represents a flash partition.
Definition: Partition.h:86
Type
Definition: Partition.h:88
Definition: FileDevice.h:26
Type
Definition: Resource.h:41