From 4615421aae11ae0cb1979d8f550974297bb91675 Mon Sep 17 00:00:00 2001 From: Juan Miguel Carceller <22276694+jmcarcell@users.noreply.github.com> Date: Wed, 18 Sep 2024 11:59:12 +0200 Subject: [PATCH] Add an static_assert for the GenericParameters and remove the EnableIf (#676) * Add an static_assert for the GenericParameters and remove the EnableIf * Add a static_assert for getParameters * Remove other EnableIfs * Simplify related python code * Add a comment about the indexing * Change the error message --------- Co-authored-by: jmcarcell --- include/podio/Frame.h | 13 ++++++++----- include/podio/GenericParameters.h | 24 ++++++++++++++---------- python/podio/frame.py | 8 +++++--- 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/include/podio/Frame.h b/include/podio/Frame.h index 82a399052..5598ffab6 100644 --- a/include/podio/Frame.h +++ b/include/podio/Frame.h @@ -234,8 +234,9 @@ class Frame { /// is supported by GenericParameters /// @param key The name under which this parameter should be stored /// @param value The value of the parameter. A copy will be put into the Frame - template > + template inline void putParameter(const std::string& key, T value) { + static_assert(podio::isSupportedGenericDataType, "Unsupported parameter type"); m_self->parameters().set(key, std::move(value)); } @@ -246,8 +247,8 @@ class Frame { /// /// @param key The name under which this parameter should be stored /// @param value The value of the parameter. A copy will be put into the Frame - inline void putParameter(const std::string& key, std::string value) { - putParameter(key, std::move(value)); + inline void putParameter(const std::string& key, const char* value) { + putParameter(key, value); } /// Add a vector of strings value the parameters of the Frame. @@ -282,8 +283,9 @@ class Frame { /// @param key The key under which the value is stored /// /// @returns An optional holding the value if it is present - template > + template inline auto getParameter(const std::string& key) const { + static_assert(podio::isSupportedGenericDataType, "Unsupported parameter type"); return m_self->parameters().get(key); } @@ -302,8 +304,9 @@ class Frame { /// @tparam T The desired parameter type /// /// @returns A vector of keys for this parameter type - template > + template inline std::vector getParameterKeys() const { + static_assert(podio::isSupportedGenericDataType, "Unsupported parameter type"); return m_self->parameters().getKeys(); } diff --git a/include/podio/GenericParameters.h b/include/podio/GenericParameters.h index 4f41aad63..7314c5473 100644 --- a/include/podio/GenericParameters.h +++ b/include/podio/GenericParameters.h @@ -81,16 +81,16 @@ class GenericParameters { ~GenericParameters() = default; - template > + template std::optional get(const std::string& key) const; /// Store (a copy of) the passed value under the given key - template > + template void set(const std::string& key, T value); /// Overload for catching const char* setting for string values - void set(const std::string& key, std::string value) { - set(key, std::move(value)); + void set(const std::string& key, const char* value) { + set(key, std::string(value)); } /// Overload for catching initializer list setting of string vector values @@ -113,11 +113,11 @@ class GenericParameters { size_t getN(const std::string& key) const; /// Get all available keys for a given type - template > + template std::vector getKeys() const; /// Get all the available values for a given type - template > + template std::tuple, std::vector>> getKeysAndValues() const; /// erase all elements @@ -202,8 +202,9 @@ class GenericParameters { mutable MutexPtr m_doubleMtx{nullptr}; ///< The mutex guarding the double map }; -template +template std::optional GenericParameters::get(const std::string& key) const { + static_assert(podio::isSupportedGenericDataType, "Unsupported parameter type"); const auto& map = getMap(); auto& mtx = getMutex(); std::lock_guard lock{mtx}; @@ -221,8 +222,9 @@ std::optional GenericParameters::get(const std::string& key) const { } } -template +template void GenericParameters::set(const std::string& key, T value) { + static_assert(podio::isSupportedGenericDataType, "Unsupported parameter type"); auto& map = getMap(); auto& mtx = getMutex(); @@ -248,8 +250,9 @@ size_t GenericParameters::getN(const std::string& key) const { return 0; } -template +template std::vector GenericParameters::getKeys() const { + static_assert(podio::isSupportedGenericDataType, "Unsupported parameter type"); std::vector keys; const auto& map = getMap(); keys.reserve(map.size()); @@ -262,8 +265,9 @@ std::vector GenericParameters::getKeys() const { return keys; } -template +template std::tuple, std::vector>> GenericParameters::getKeysAndValues() const { + static_assert(podio::isSupportedGenericDataType, "Unsupported parameter type"); std::vector> values; std::vector keys; auto& mtx = getMutex(); diff --git a/python/podio/frame.py b/python/podio/frame.py index 2f41db159..4031ce92d 100644 --- a/python/podio/frame.py +++ b/python/podio/frame.py @@ -32,7 +32,7 @@ def _get_cpp_types(type_str): """Get all possible c++ types from the passed py_type string.""" - types = list(filter(lambda t: type_str in t, SUPPORTED_PARAMETER_TYPES)) + types = [t for t in SUPPORTED_PARAMETER_TYPES if type_str in t] if not types: raise ValueError(f"{type_str} cannot be mapped to a valid parameter type") @@ -43,7 +43,7 @@ def _get_cpp_vector_types(type_str): """Get the possible std::vector from the passed py_type string.""" # Gather a list of all types that match the type_str (c++ or python) types = _get_cpp_types(type_str) - return [f"std::vector<{t}>" for t in map(lambda x: x[0], types)] + return [f"std::vector<{t[0]}>" for t in types] def _is_collection_base(thing): @@ -264,7 +264,9 @@ def put_parameter(self, key, value, as_type=None): cpp_types = _get_cpp_types(as_type) if len(cpp_types) == 0: raise ValueError(f"Cannot put a parameter of type {as_type} into a Frame") - self._frame.putParameter[cpp_types[0]](key, value) + # The first [0] gets the tuple, the second [0] gets the actual C++ type + # from SUPPORTED_PARAMETER_TYPES + self._frame.putParameter[cpp_types[0][0]](key, value) # If we have a single integer, a std::string overload kicks in with higher # priority than the template for some reason. So we explicitly select the