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

Add typeName member to link collections #748

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion include/podio/LinkCollection.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/// buffer creation functionality with the CollectionBufferFactory.
#define PODIO_DECLARE_LINK(FromT, ToT) \
const static auto PODIO_PP_CONCAT(REGISTERED_LINK_, __COUNTER__) = \
podio::detail::registerLinkCollection<FromT, ToT>(podio::detail::linkCollTypeName<FromT, ToT>());
podio::detail::registerLinkCollection<FromT, ToT>(podio::LinkCollection<FromT, ToT>::typeName);

#if PODIO_ENABLE_SIO && __has_include("podio/detail/LinkSIOBlock.h")
#include <podio/detail/LinkSIOBlock.h>
Expand Down
5 changes: 5 additions & 0 deletions include/podio/detail/Link.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
#include "podio/detail/LinkFwd.h"
#include "podio/detail/LinkObj.h"
#include "podio/utilities/MaybeSharedPtr.h"
#include "podio/utilities/StaticConcatenate.h"
#include "podio/utilities/TypeHelpers.h"
#include <string_view>

#ifdef PODIO_JSON_OUTPUT
#include "nlohmann/json.hpp"
Expand Down Expand Up @@ -58,6 +60,9 @@ class LinkT {
using value_type = podio::Link<FromT, ToT>;
using collection_type = podio::LinkCollection<FromT, ToT>;

static constexpr std::string_view typeName =
utils::static_concatenate_v<detail::link_name_prefix, FromT::typeName, detail::link_name_infix, ToT::typeName,
detail::link_name_suffix>;
/// Constructor
LinkT() : m_obj(new LinkObjT{}, podio::utils::MarkOwned) {
}
Expand Down
18 changes: 14 additions & 4 deletions include/podio/detail/LinkCollectionImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
#include "podio/ICollectionProvider.h"
#include "podio/SchemaEvolution.h"
#include "podio/utilities/MaybeSharedPtr.h"
#include "podio/utilities/StaticConcatenate.h"
#include "podio/utilities/TypeHelpers.h"

#include <iterator>
#include <string_view>

#ifdef PODIO_JSON_OUTPUT
#include "nlohmann/json.hpp"
Expand Down Expand Up @@ -204,16 +206,24 @@ class LinkCollection : public podio::CollectionBase {
return m_storage.getCollectionBuffers(m_isSubsetColl);
}

constexpr static std::string_view typeName =
podio::utils::static_concatenate_v<podio::detail::link_coll_name_prefix, FromT::typeName,
podio::detail::link_name_infix, ToT::typeName,
podio::detail::link_name_suffix>;

constexpr static std::string_view valueTypeName = value_type::typeName;
constexpr static std::string_view dataTypeName = "podio::LinkData";

const std::string_view getTypeName() const override {
return podio::detail::linkCollTypeName<FromT, ToT>();
return typeName;
}

const std::string_view getValueTypeName() const override {
return podio::detail::linkTypeName<FromT, ToT>();
return valueTypeName;
}

const std::string_view getDataTypeName() const override {
return "podio::LinkData";
return dataTypeName;
}

bool isSubsetCollection() const override {
Expand Down Expand Up @@ -329,7 +339,7 @@ namespace detail {
template <typename FromT, typename ToT>
podio::CollectionReadBuffers createLinkBuffers(bool subsetColl) {
auto readBuffers = podio::CollectionReadBuffers{};
readBuffers.type = podio::detail::linkCollTypeName<FromT, ToT>();
readBuffers.type = podio::LinkCollection<FromT, ToT>::typeName;
readBuffers.schemaVersion = podio::LinkCollection<FromT, ToT>::schemaVersion;
readBuffers.data = subsetColl ? nullptr : new LinkDataContainer();

Expand Down
28 changes: 5 additions & 23 deletions include/podio/detail/LinkFwd.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,10 @@
namespace podio {
namespace detail {

/// Get the collection type name for a LinkCollection
///
/// @tparam FromT the From type of the link
/// @tparam ToT the To type of the link
/// @returns a type string that is a valid c++ template instantiation
template <typename FromT, typename ToT>
inline const std::string_view linkCollTypeName() {
const static std::string typeName =
std::string("podio::LinkCollection<") + FromT::typeName + "," + ToT::typeName + ">";
return std::string_view{typeName};
}

/// Get the value type name for a LinkCollection
///
/// @tparam FromT the From type of the link
/// @tparam ToT the To type of the link
/// @returns a type string that is a valid c++ template instantiation
template <typename FromT, typename ToT>
inline const std::string_view linkTypeName() {
const static std::string typeName = std::string("podio::Link<") + FromT::typeName + "," + ToT::typeName + ">";
return std::string_view{typeName};
}
constexpr std::string_view link_coll_name_prefix = "podio::LinkCollection<";
constexpr std::string_view link_name_prefix = "podio::LinkCollection<";
constexpr std::string_view link_name_infix = ",";
constexpr std::string_view link_name_suffix = ">";

/// Get an SIO friendly type name for a LinkCollection (necessary for
/// registration in the SIOBlockFactory)
Expand All @@ -44,7 +26,7 @@ namespace detail {
/// types
template <typename FromT, typename ToT>
inline const std::string& linkSIOName() {
static auto n = std::string("LINK_FROM_") + FromT::typeName + "_TO_" + ToT::typeName;
static auto n = std::string("LINK_FROM_") + std::string(FromT::typeName) + "_TO_" + std::string(ToT::typeName);
std::replace(n.begin(), n.end(), ':', '_');
return n;
}
Expand Down
4 changes: 2 additions & 2 deletions include/podio/detail/LinkSIOBlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class LinkSIOBlock : public podio::SIOBlock {
SIOBlock(podio::detail::linkSIOName<FromT, ToT>(),
sio::version::encode_version(LinkCollection<FromT, ToT>::schemaVersion, 0)) {
podio::SIOBlockFactory::instance().registerBlockForCollection(
std::string(podio::detail::linkTypeName<FromT, ToT>()), this);
std::string(podio::LinkCollection<FromT, ToT>::valueTypeName), this);
}

LinkSIOBlock(const std::string& name) :
Expand All @@ -32,7 +32,7 @@ class LinkSIOBlock : public podio::SIOBlock {
auto& bufferFactory = podio::CollectionBufferFactory::instance();
// TODO:
// - Error handling of empty optional
auto maybeBuffers = bufferFactory.createBuffers(std::string(podio::detail::linkCollTypeName<FromT, ToT>()),
auto maybeBuffers = bufferFactory.createBuffers(std::string(podio::LinkCollection<FromT, ToT>::typeName),
sio::version::major_version(version), m_subsetColl);
m_buffers = maybeBuffers.value();

Expand Down
29 changes: 29 additions & 0 deletions include/podio/utilities/StaticConcatenate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef PODIO_UTILITIES_STATICCONCATENATE_H
#define PODIO_UTILITIES_STATICCONCATENATE_H

#include <algorithm>
#include <array>
#include <string_view>
namespace podio::utils {

/// Helper struct to concatenate a set of string_views into a single string_view at compile time
template <const std::string_view&... strs>
struct static_concatenate {
static constexpr auto init_arr() {
constexpr auto total_size = (strs.size() + ... + 1); // reserve space for '\0'
auto arr = std::array<char, total_size>();
auto it = arr.begin();
((it = std::ranges::copy(strs, it).out), ...);
arr.back() = '\0';
return arr;
}
constexpr static auto array = init_arr();
constexpr static auto value = std::string_view(array.data(), array.size() - 1); // skip '\0'
};

/// Variable template for concatenating a set of string_views into a single string_view at compile time
template <const std::string_view&... strs>
inline constexpr std::string_view static_concatenate_v = static_concatenate<strs...>::value;

} // namespace podio::utils
#endif // PODIO_UTILITIES_STATICCONCATENATE_H
6 changes: 3 additions & 3 deletions python/templates/Collection.h.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ public:
// {{ class.bare_type }}Collection({{ class.bare_type }}Vector* data, uint32_t collectionID);
~{{ class.bare_type }}Collection() override;

constexpr static auto typeName = "{{ (class | string ).strip(':') + "Collection" }}";
constexpr static auto valueTypeName = "{{ (class | string ).strip(':') }}";
constexpr static auto dataTypeName = "{{ (class | string ).strip(':') + "Data" }}";
constexpr static std::string_view typeName = "{{ (class | string ).strip(':') + "Collection" }}";
constexpr static std::string_view valueTypeName = "{{ (class | string ).strip(':') }}";
constexpr static std::string_view dataTypeName = "{{ (class | string ).strip(':') + "Data" }}";

void clear() final;

Expand Down
2 changes: 1 addition & 1 deletion python/templates/Interface.h.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public:
return {{ Types[0] }}::makeEmpty();
}

static constexpr auto typeName = "{{ class.full_type }}";
static constexpr std::string_view typeName = "{{ class.full_type }}";

/// check whether the object is actually available
bool isAvailable() const { return m_self->isAvailable(); }
Expand Down
2 changes: 1 addition & 1 deletion python/templates/Object.h.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public:

public:

static constexpr auto typeName = "{{ class.full_type }}";
static constexpr std::string_view typeName = "{{ class.full_type }}";

{{ macros.member_getters(Members, use_get_syntax) }}
{{ macros.single_relation_getters(OneToOneRelations, use_get_syntax) }}
Expand Down
3 changes: 1 addition & 2 deletions tests/unittests/links.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,7 @@ TEST_CASE("LinkCollection subset collection", "[links][subset-colls]") {
// NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)

TEST_CASE("LinkCollection basics", "[links]") {
REQUIRE(podio::detail::linkCollTypeName<ExampleCluster, ExampleHit>() ==
"podio::LinkCollection<ExampleCluster,ExampleHit>");
STATIC_REQUIRE(TestLColl::typeName == "podio::LinkCollection<ExampleHit,ExampleCluster>");

auto links = TestLColl{};
auto link = links.create();
Expand Down
Loading