Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow error handling from StructView::add_path #153

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions include/fixed_containers/struct_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -568,38 +568,51 @@ class StructView
}

template <typename S>
void add_path(S&& instance, const PathNameChain& path)
bool try_add_path(S&& instance, const PathNameChain& path)
{
auto path_properties_map = extract_path_properties_of_filtered<S, 1, 1>(
std::forward<S>(instance), std::optional<FixedSet<PathNameChain, 1>>({path}));
auto [_, was_inserted] = path_properties_.try_emplace(path, path_properties_map.at(path));
assert_or_abort(was_inserted);

const auto itr = path_properties_map.find(path);
if (itr == path_properties_map.cend())
{
return false;
}

path_properties_.emplace(path, itr->second);
return true;
}

template <typename S>
void add_path(const PathNameChain& path)
bool try_add_path(const PathNameChain& path)
{
S instance{};
add_path(instance, path);
return try_add_path(instance, path);
}

template <typename S, typename PathSet>
void add_paths(S&& instance, const PathSet& paths)
bool try_add_paths(S&& instance, const PathSet& paths)
{
auto path_properties_map =
extract_path_properties_of_filtered(std::forward<S>(instance), paths);
if (path_properties_map.size() != paths.size())
{
return false;
}

for (const auto& [path, path_properties] : path_properties_map)
{
auto [_, was_inserted] = path_properties_.try_emplace(path, path_properties);
assert_or_abort(was_inserted);
path_properties_.emplace(path, path_properties);
}

return true;
}

template <typename S, typename PathSet>
void add_paths(const PathSet& paths)
bool try_add_paths(const PathSet& paths)
{
S instance{};
zanneth marked this conversation as resolved.
Show resolved Hide resolved
add_paths(instance, paths);
return try_add_paths(instance, paths);
}

[[nodiscard]] PathProperties at(const PathNameChain& path) const
Expand Down
4 changes: 3 additions & 1 deletion test/struct_view_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ struct FlatSuperStruct1
std::int32_t ignore2{};
std::int32_t retain2{};
std::int16_t ignore3{};
unsigned char bigbuf[1024*1024];
};

struct FlatSubStruct1
Expand Down Expand Up @@ -118,7 +119,8 @@ TEST(StructView, ExtractPathPropertiesOfFlat)
TEST(StructView, StructViewFlat)
{
auto super_struct_view = StructView();
super_struct_view.add_path<FlatSuperStruct1>(path_from_string("retain1"));
bool success = super_struct_view.try_add_path<FlatSuperStruct1>(path_from_string("retain1"));
EXPECT_TRUE(success);
}

TEST(StructView, SubStructViewOfFlat)
Expand Down
Loading