Skip to content

Commit

Permalink
Extract method
Browse files Browse the repository at this point in the history
  • Loading branch information
uweseimet committed Jan 24, 2024
1 parent 2820de4 commit 1989454
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 29 deletions.
60 changes: 31 additions & 29 deletions cpp/s2p/s2p_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,7 @@ void S2p::CreateDevices()
int id = -1;
int lun = -1;
bool is_active = false;
const auto &properties = property_handler.GetProperties();
for (const auto& [key, value] : properties) {
for (const auto &properties = property_handler.GetProperties(); const auto& [key, value] : properties) {
if (!key.starts_with("device.")) {
continue;
}
Expand Down Expand Up @@ -316,33 +315,8 @@ void S2p::CreateDevices()
}

const string &k = key_components[2];
if (k == "active") {
// "active" has already been handled above
continue;
}
else if (k == "type") {
device->set_type(ParseDeviceType(value));
}
else if (k == "block_size") {
if (int block_size; !GetAsUnsignedInt(value, block_size)) {
throw parser_exception(fmt::format("Invalid block size: {}", value));
}
else {
assert(device);
device->set_block_size(block_size);
}
}
else if (k == "product_data") {
assert(device);
SetProductData(*device, value);
}
else if (k == "params") {
assert(device);
ParseParameters(*device, value);
}
else {
throw parser_exception(fmt::format("Unknown device definition key: '{}'", k));
}
assert(device);
SetDeviceProperties(*device, k, value);
}

if (command.devices_size()) {
Expand All @@ -364,6 +338,34 @@ void S2p::CreateDevices()
}
}

void S2p::SetDeviceProperties(PbDeviceDefinition &device, const string &key, const string &value)
{
if (key == "active") {
// "active" has already been handled separately
return;
}
else if (key == "type") {
device.set_type(ParseDeviceType(value));
}
else if (key == "block_size") {
if (int block_size; !GetAsUnsignedInt(value, block_size)) {
throw parser_exception(fmt::format("Invalid block size: {}", value));
}
else {
device.set_block_size(block_size);
}
}
else if (key == "product_data") {
SetProductData(device, value);
}
else if (key == "params") {
ParseParameters(device, value);
}
else {
throw parser_exception(fmt::format("Unknown device definition key: '{}'", key));
}
}

PbDeviceType S2p::ParseDeviceType(const string &value)
{
string t;
Expand Down
1 change: 1 addition & 0 deletions cpp/s2p/s2p_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class S2p
bool ExecuteCommand(CommandContext&);
bool HandleDeviceListChange(const CommandContext&, PbOperation) const;

static void SetDeviceProperties(PbDeviceDefinition&, const string&, const string&);
static PbDeviceType ParseDeviceType(const string&);

string access_token;
Expand Down

0 comments on commit 1989454

Please sign in to comment.