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 : public std::iterator<std::forward_iterator_tag, Partition>
19 {
20 public:
21  Iterator(Device& device, uint8_t partitionIndex);
22 
23  Iterator(Device& device, Partition::Type type, uint8_t subtype) : mSearch{&device, type, subtype}
24  {
25  mDevice = &device;
26  next();
27  }
28 
29  Iterator(Partition::Type type, uint8_t subtype);
30 
31  explicit operator bool() const
32  {
33  return (mDevice != nullptr) && (mPos > beforeStart) && (mPos < afterEnd);
34  }
35 
37  {
38  auto result = *this;
39  next();
40  return result;
41  }
42 
44  {
45  next();
46  return *this;
47  }
48 
49  bool operator==(const Iterator& other) const
50  {
51  return (mDevice == other.mDevice) && (mPos == other.mPos);
52  }
53 
54  bool operator!=(const Iterator& other) const
55  {
56  return !operator==(other);
57  }
58 
59  Partition operator*() const;
60 
61 private:
62  static constexpr int8_t beforeStart{-1};
63  static constexpr int8_t afterEnd{0x7f};
64 
65  bool seek(uint8_t pos);
66  bool next();
67 
68  struct Search {
69  Device* device;
70  Partition::Type type;
71  uint8_t subType;
72  };
73  Search mSearch{};
74  Device* mDevice{nullptr};
75  int8_t mPos{beforeStart};
76 };
77 
78 } // namespace Storage
Iterator(Device &device, Partition::Type type, uint8_t subtype)
Definition: Iterator.h:23
Partition operator*() const
Definition: Iterator.h:18
bool operator!=(const Iterator &other) const
Definition: Iterator.h:54
Iterator(Device &device, uint8_t partitionIndex)
Iterator operator++(int)
Definition: Iterator.h:36
Type
Definition: Partition.h:81
Represents a storage device (e.g. flash memory)
Definition: Components/Storage/src/include/Storage/Device.h:32
bool operator==(const Iterator &other) const
Definition: Iterator.h:49
Represents a flash partition.
Definition: Partition.h:78
Definition: FileDevice.h:23
Iterator & operator++()
Definition: Iterator.h:43