Skip to content

Commit

Permalink
[Core] Add methods to retrieve available sub-items and handle not fou…
Browse files Browse the repository at this point in the history
…nd errors in `RegistryItem`
  • Loading branch information
loumalouomega committed Dec 17, 2024
1 parent 06eeea3 commit 392eff9
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
12 changes: 12 additions & 0 deletions kratos/includes/registry_item.h
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,18 @@ class KRATOS_API(KRATOS_CORE) RegistryItem
return buffer.str();
}

/**
* @brief Returns the sub items name list available in the registry item (for error messaged).
* @return std::vector<std::string> The sub items name list.
*/
std::vector<std::string> GetSubItemAvailableList() const;

/**
* @brief This method throws an error message for a not found item.
* @param rItemName The name of the item.
*/
void NotFoundError(const std::string& rItemName) const;

///@}
///@name Private classes
///@{
Expand Down
35 changes: 32 additions & 3 deletions kratos/sources/registry_item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,29 @@ namespace Kratos
}
}

std::vector<std::string> RegistryItem::GetSubItemAvailableList() const
{
std::vector<std::string> available_list;
auto it_item_begin = this->cbegin();
auto it_item_end = this->cend();
available_list.reserve(std::distance(it_item_begin, it_item_end));
for (auto it_item = it_item_begin; it_item != it_item_end; ++it_item) {
available_list.push_back((it_item->second)->Name());
}
return available_list;
}

void RegistryItem::NotFoundError(const std::string& rItemName) const
{
const std::vector<std::string> available_list = GetSubItemAvailableList();
std::stringstream available_list_str;
available_list_str << "The RegistryItem " << this->Name() << " does not have an item with name " << rItemName << ". The available objects are: \n";
for (std::string const& item : available_list) {
available_list_str << "\t\t" << item << "\n";
}
KRATOS_ERROR << available_list_str.str() << std::endl;
}

std::string RegistryItem::GetValueString() const
{
return (this->*(this->mGetValueStringMethod))();
Expand Down Expand Up @@ -163,23 +186,29 @@ namespace Kratos
{
SubRegistryItemType& r_map = GetSubRegistryItemMap();
auto iterator = r_map.find(rItemName);
KRATOS_ERROR_IF(iterator == r_map.end()) << "The RegistryItem " << this->Name() << " does not have an item with name " << rItemName << std::endl;
if (iterator == r_map.end()) {
NotFoundError(rItemName);
}
return *(iterator->second);
}

RegistryItem& RegistryItem::GetItem(std::string const& rItemName)
{
SubRegistryItemType& r_map = GetSubRegistryItemMap();
auto iterator = r_map.find(rItemName);
KRATOS_ERROR_IF(iterator == r_map.end()) << "The RegistryItem " << this->Name() << " does not have an item with name " << rItemName << std::endl;
if (iterator == r_map.end()) {
NotFoundError(rItemName);
}
return *(iterator->second);
}

void RegistryItem::RemoveItem(std::string const& rItemName)
{
SubRegistryItemType& r_map = GetSubRegistryItemMap();
auto iterator = r_map.find(rItemName);
KRATOS_ERROR_IF(iterator == r_map.end()) << "The RegistryItem " << this->Name() << " does not have an item with name " << rItemName << std::endl;
if (iterator == r_map.end()) {
NotFoundError(rItemName);
}
r_map.erase(iterator);
}

Expand Down

0 comments on commit 392eff9

Please sign in to comment.