Skip to content

Commit

Permalink
add mutable_type typedef to collection and user collection (#718)
Browse files Browse the repository at this point in the history
* add `mutable_type` typedef for collections and user collection

* simplify obtaining mutable type from collection
  • Loading branch information
m-fila authored Dec 19, 2024
1 parent d3f0381 commit 6e9c131
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
1 change: 1 addition & 0 deletions include/podio/UserDataCollection.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class UserDataCollection : public CollectionBase {

public:
using value_type = typename std::vector<BasicType>::value_type;
using mutable_type = value_type;
using const_iterator = typename std::vector<BasicType>::const_iterator;
using iterator = typename std::vector<BasicType>::iterator;
using difference_type = typename std::vector<BasicType>::difference_type;
Expand Down
1 change: 1 addition & 0 deletions python/templates/Collection.h.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ A Collection is identified by an ID.
class {{ class.bare_type }}Collection : public podio::CollectionBase {
public:
using value_type = {{ class.bare_type }};
using mutable_type = Mutable{{ class.bare_type }};
using const_iterator = {{ class.bare_type }}CollectionIterator;
using iterator = {{ class.bare_type }}MutableCollectionIterator;
using difference_type = ptrdiff_t;
Expand Down
15 changes: 7 additions & 8 deletions tests/unittests/std_interoperability.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -749,23 +749,23 @@ TEST_CASE("Collection iterators", "[collection][container][iterator][std]") {
// *r = o
// iterator
DOCUMENTED_STATIC_FAILURE(traits::has_dereference_assignment_v<iterator, CollectionType::value_type>);
STATIC_REQUIRE(traits::has_dereference_assignment_v<iterator, CollectionType::value_type::mutable_type>);
STATIC_REQUIRE(traits::has_dereference_assignment_v<iterator, CollectionType::mutable_type>);
{
auto coll = CollectionType{};
auto item = coll.create(13ull, 0., 0., 0., 0.);
REQUIRE(coll.begin()->cellID() == 13ull);
auto new_item = CollectionType::value_type::mutable_type{42ull, 0., 0., 0., 0.};
auto new_item = CollectionType::mutable_type{42ull, 0., 0., 0., 0.};
*coll.begin() = new_item;
DOCUMENTED_FAILURE(coll.begin()->cellID() == 42ull);
}
// const_iterator
STATIC_REQUIRE(traits::has_dereference_assignment_v<const_iterator, CollectionType::value_type>);
STATIC_REQUIRE(traits::has_dereference_assignment_v<const_iterator, CollectionType::value_type::mutable_type>);
STATIC_REQUIRE(traits::has_dereference_assignment_v<const_iterator, CollectionType::mutable_type>);
{
auto coll = CollectionType{};
auto item = coll.create(13ull, 0., 0., 0., 0.);
REQUIRE(coll.cbegin()->cellID() == 13ull);
auto new_item = CollectionType::value_type::mutable_type{42ull, 0., 0., 0., 0.};
auto new_item = CollectionType::mutable_type{42ull, 0., 0., 0., 0.};
*coll.cbegin() = new_item;
DOCUMENTED_FAILURE(coll.cbegin()->cellID() == 42ull);
new_item.cellID(44ull);
Expand All @@ -792,14 +792,13 @@ TEST_CASE("Collection iterators", "[collection][container][iterator][std]") {
// *r++ = o
// iterator
DOCUMENTED_STATIC_FAILURE(traits::has_dereference_assignment_increment_v<iterator, CollectionType::value_type>);
DOCUMENTED_STATIC_FAILURE(
traits::has_dereference_assignment_increment_v<iterator, CollectionType::value_type::mutable_type>);
DOCUMENTED_STATIC_FAILURE(traits::has_dereference_assignment_increment_v<iterator, CollectionType::mutable_type>);
// TODO add runtime check for assignment validity like in '*r = o' case
// const_iterator
DOCUMENTED_STATIC_FAILURE(
traits::has_dereference_assignment_increment_v<const_iterator, CollectionType::value_type>);
DOCUMENTED_STATIC_FAILURE(
traits::has_dereference_assignment_increment_v<const_iterator, CollectionType::value_type::mutable_type>);
traits::has_dereference_assignment_increment_v<const_iterator, CollectionType::mutable_type>);
// TODO add runtime check for assignment validity like in '*r = o' case

// iterator_category - not strictly necessary but advised
Expand Down Expand Up @@ -852,7 +851,7 @@ TEST_CASE("Collection and std iterator adaptors", "[collection][container][adapt
// insert immutable to not-SubsetCollection
REQUIRE_THROWS_AS(it = CollectionType::value_type{}, std::invalid_argument);
// insert mutable (implicit cast to immutable) to not-SubsetCollection
REQUIRE_THROWS_AS(it = CollectionType::value_type::mutable_type{}, std::invalid_argument);
REQUIRE_THROWS_AS(it = CollectionType::mutable_type{}, std::invalid_argument);
auto subColl = CollectionType{};
subColl.setSubsetCollection(true);
auto subIt = std::back_inserter(subColl);
Expand Down

0 comments on commit 6e9c131

Please sign in to comment.