Skip to content

Commit

Permalink
Clean up device factory
Browse files Browse the repository at this point in the history
  • Loading branch information
uweseimet committed Oct 31, 2023
1 parent 0c65efc commit 1caa227
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 29 deletions.
23 changes: 2 additions & 21 deletions cpp/devices/device_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,13 @@ using namespace std;
using namespace piscsi_util;
using namespace network_util;

DeviceFactory::DeviceFactory()
{
extension_mapping["hd1"] = SCHD;
extension_mapping["hds"] = SCHD;
extension_mapping["hda"] = SCHD;
extension_mapping["hdn"] = SCHD;
extension_mapping["hdi"] = SCHD;
extension_mapping["nhd"] = SCHD;
extension_mapping["hdr"] = SCRM;
extension_mapping["mos"] = SCMO;
extension_mapping["iso"] = SCCD;
extension_mapping["is1"] = SCCD;

device_mapping["bridge"] = SCBR;
device_mapping["daynaport"] = SCDP;
device_mapping["printer"] = SCLP;
device_mapping["services"] = SCHS;
}

PbDeviceType DeviceFactory::GetTypeForFile(const string& filename) const
{
if (const auto& it = extension_mapping.find(GetExtensionLowerCase(filename)); it != extension_mapping.end()) {
if (const auto& it = EXTENSION_MAPPING.find(GetExtensionLowerCase(filename)); it != EXTENSION_MAPPING.end()) {
return it->second;
}

if (const auto& it = device_mapping.find(filename); it != device_mapping.end()) {
if (const auto& it = DEVICE_MAPPING.find(filename); it != DEVICE_MAPPING.end()) {
return it->second;
}

Expand Down
26 changes: 21 additions & 5 deletions cpp/devices/device_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,32 @@ class DeviceFactory

public:

DeviceFactory();
DeviceFactory() = default;
~DeviceFactory() = default;

shared_ptr<PrimaryDevice> CreateDevice(PbDeviceType, int, const string&) const;
PbDeviceType GetTypeForFile(const string&) const;
const auto& GetExtensionMapping() const { return extension_mapping; }
const auto& GetExtensionMapping() const { return EXTENSION_MAPPING; }

private:

unordered_map<string, PbDeviceType, piscsi_util::StringHash, equal_to<>> extension_mapping;

unordered_map<string, PbDeviceType, piscsi_util::StringHash, equal_to<>> device_mapping;
const inline static unordered_map<string, PbDeviceType, piscsi_util::StringHash, equal_to<>> EXTENSION_MAPPING = {
{ "hd1", SCHD },
{ "hds", SCHD },
{ "hda", SCHD },
{ "hdn", SCHD },
{ "hdi", SCHD },
{ "nhd", SCHD },
{ "hdr", SCRM },
{ "mos", SCMO },
{ "is1", SCCD },
{ "iso", SCCD }
};

const inline static unordered_map<string, PbDeviceType, piscsi_util::StringHash, equal_to<>> DEVICE_MAPPING = {
{ "bridge", SCBR },
{ "daynaport", SCDP },
{ "printer", SCLP },
{ "services", SCHS }
};
};
2 changes: 1 addition & 1 deletion cpp/devices/disk.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class Disk : public StorageDevice, private ScsiBlockCommands
virtual int Read(span<uint8_t> , uint64_t);

uint32_t GetSectorSizeInBytes() const;
auto GetSupportedSectorSizes() const { return supported_sector_sizes; }
const auto& GetSupportedSectorSizes() const { return supported_sector_sizes; }
bool SetConfiguredSectorSize(uint32_t);
void FlushCache() override;

Expand Down
2 changes: 0 additions & 2 deletions cpp/test/piscsi_executor_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,6 @@ TEST(PiscsiExecutorTest, Attach)

TEST(PiscsiExecutorTest, Insert)
{
DeviceFactory device_factory;

auto bus = make_shared<MockBus>();
ControllerManager controller_manager;
auto [controller, device] = CreateDevice(SCHD);
Expand Down

0 comments on commit 1caa227

Please sign in to comment.