From 8a63188d628c2be8bf0b095e766e4301f2b11dcf Mon Sep 17 00:00:00 2001 From: NAUD Maxence <maxence.naud@cea.fr> Date: Thu, 24 Aug 2023 07:57:20 +0000 Subject: [PATCH] Testing std::any for a more standard structure in CParameter before changing it for a c++14 compatible option --- include/aidge/utils/CParameter.hpp | 35 +++++------------------------- 1 file changed, 6 insertions(+), 29 deletions(-) diff --git a/include/aidge/utils/CParameter.hpp b/include/aidge/utils/CParameter.hpp index 0f4c74ab8..fdf812b5e 100644 --- a/include/aidge/utils/CParameter.hpp +++ b/include/aidge/utils/CParameter.hpp @@ -15,6 +15,7 @@ #include <assert.h> #include <map> #include <vector> +#include <any> namespace Aidge { @@ -51,10 +52,7 @@ public: template<class T> T Get(std::string const i_ParamName) const { assert(m_Params.find(i_ParamName) != m_Params.end()); - assert(m_Types.find(i_ParamName) != m_Types.end()); - assert(m_Params.at(i_ParamName) <= m_OffSet); - assert(typeid(T).name() == m_Types.at(i_ParamName)); - return *reinterpret_cast<T *>(m_BeginBuffer + m_Params.at(i_ParamName)); + return std::any_cast<T>(m_Buffer[m_Params[i_ParamName]]); } ///\brief Add a parameter value, identified by its name @@ -66,19 +64,13 @@ public: /// internal buffer in a new location (previous value is still in memory at its previous location) template<class T> void Add(std::string const &i_ParamName, T const &i_Value) { - m_Buffer.resize(m_Buffer.size() + (sizeof(T) / sizeof(uint8_t))); - m_BeginBuffer = m_Buffer.data(); // Update buffer ptr in case of memory reordering - *reinterpret_cast<T *>(m_BeginBuffer + m_OffSet) - = i_Value; // Black-magic used to add anytype into the vector - m_Params[i_ParamName] = m_OffSet; // Copy pointer offset - m_OffSet += sizeof(T); // Increment offset - - m_Types[i_ParamName] = typeid(i_Value).name(); + m_Params[i_ParamName] = m_Buffer.size(); // Copy pointer offset + m_Buffer.push_back(std::any(i_Value)); // Black-magic used to add anytype into the vector } std::string getParamType(std::string const &i_ParamName){ - return m_Types[i_ParamName]; + return m_Buffer[m_Params[i_ParamName]].type().name(); } std::vector<std::string> getParametersName(){ @@ -91,23 +83,8 @@ public: private: std::map<std::string, std::size_t> m_Params; // { Param name : offset } - ///\brief Map to check type error - /* Note : i tried this : `std::map<std::string, std::type_info const *> mTypes;` - but looks like the type_ingo object was destroyed. - I am not a hugde fan of storing a string and making string comparison. - Maybe we can use a custom enum type (or is there a standard solution ?) - */ - std::map<std::string, std::string> m_Types; - ///\brief All parameters values concatenated in raw binary form. - std::vector<uint8_t> m_Buffer = {}; - - ///\brief Starting address of the buffer - uint8_t *m_BeginBuffer = m_Buffer.data(); - - ///\brief Offset, in number of uint8_t, of the next parameter to write - std::size_t m_OffSet = 0; - + std::vector<std::any> m_Buffer = {}; }; } -- GitLab