#pragma once

#include "IMapSource.h"
#include "fs/Api.h"
#include "tl/optional.h"

namespace fs
{

class FS_API MemoryViewSource final : public IMapSource
{
public:
	~MemoryViewSource() override = default;

	explicit MemoryViewSource(tl::span<const uint8_t> memView);

	MemoryViewSource(MemoryViewSource&&) = default;
	MemoryViewSource& operator=(MemoryViewSource&&) = default;

	tl::optional<Error> getLastError() const override;

	size_t read(tl::span<uint8_t> data) override;

	void seekBeg(uint64_t offset) override;
	void seekRel(int64_t offset) override;

	uint64_t tell() const override;
	uint64_t getSize() const override;

	tl::span<const uint8_t> map(size_t size) override;

	bool isEOS() const override;

	size_t getPreferredBufferSize() const override;

protected:

	tl::span<const uint8_t> m_memView;

private:
	uint64_t m_offset = 0;
	mutable tl::optional<Error> m_optLastError;

};

}

