fru-device: add offset variant of FRUReader abstraction

The caching FRUReader class is now joined by OffsetFRUReader, both of
which now derive from a common BaseFRUReader class.  OffsetFRUReader is
constructed in terms of an existing BaseFRUReader (probably a FRUReader)
and simply applies a fixed offset to all reads performed on it.

Tested: on an ASRock Rack romed8hm3, fru-device successfully recognizes
and parses the baseboard FRU EEPROM as it did prior to this patch.

Signed-off-by: Zev Weiss <zev@bewilderbeest.net>
Change-Id: I13f3bc48110b8ffdf1d3f135c57ae9f0736a8e53
diff --git a/include/fru_reader.hpp b/include/fru_reader.hpp
index f54d900..4f8b24d 100644
--- a/include/fru_reader.hpp
+++ b/include/fru_reader.hpp
@@ -35,16 +35,25 @@
 using ReadBlockFunc =
     std::function<int64_t(off_t offset, size_t len, uint8_t* outbuf)>;
 
-// A caching wrapper around a ReadBlockFunc
-class FRUReader
+class BaseFRUReader
 {
   public:
-    explicit FRUReader(ReadBlockFunc readFunc) : readFunc(std::move(readFunc))
-    {}
     // The ::read() operation here is analogous to ReadBlockFunc (with the same
     // return value semantics), but is not subject to SMBus block size
     // limitations; it can read as much data as needed in a single call.
-    ssize_t read(off_t start, size_t len, uint8_t* outbuf);
+    virtual ssize_t read(off_t start, size_t len, uint8_t* outbuf) = 0;
+    virtual ~BaseFRUReader() = default;
+};
+
+// A caching wrapper around a ReadBlockFunc
+class FRUReader : public BaseFRUReader
+{
+  public:
+    explicit FRUReader(ReadBlockFunc readFunc) :
+        readFunc(std::move(readFunc)), eof(std::nullopt)
+    {}
+
+    ssize_t read(off_t start, size_t len, uint8_t* outbuf) override;
 
   private:
     static constexpr size_t cacheBlockSize = 32;
@@ -52,7 +61,7 @@
     using CacheBlock = std::array<uint8_t, cacheBlockSize>;
 
     // indexed by block number (byte number / block size)
-    using Cache = std::map<uint32_t, CacheBlock>;
+    using Cache = std::unordered_map<size_t, CacheBlock>;
 
     ReadBlockFunc readFunc;
     Cache cache;
@@ -60,3 +69,18 @@
     // byte offset of the end of the FRU (if readFunc has reported it)
     std::optional<size_t> eof;
 };
+
+// wraps an existing BaseFRUReader and applies a fixed offset to all reads
+class OffsetFRUReader : public BaseFRUReader
+{
+  public:
+    OffsetFRUReader(BaseFRUReader& inner, off_t offset) :
+        inner(inner), offset(offset)
+    {}
+
+    ssize_t read(off_t start, size_t len, uint8_t* outbuf) override;
+
+  private:
+    BaseFRUReader& inner;
+    off_t offset;
+};