Skip to content
Snippets Groups Projects
Commit 8a63188d authored by Maxence Naud's avatar Maxence Naud
Browse files

Testing std::any for a more standard structure in CParameter before changing...

Testing std::any for a more standard structure in CParameter before changing it for a c++14 compatible option
parent 75a0d5a6
No related branches found
No related tags found
1 merge request!15Remove CParameter memory leak
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include <assert.h> #include <assert.h>
#include <map> #include <map>
#include <vector> #include <vector>
#include <any>
namespace Aidge { namespace Aidge {
...@@ -51,10 +52,7 @@ public: ...@@ -51,10 +52,7 @@ public:
template<class T> T Get(std::string const i_ParamName) const template<class T> T Get(std::string const i_ParamName) const
{ {
assert(m_Params.find(i_ParamName) != m_Params.end()); assert(m_Params.find(i_ParamName) != m_Params.end());
assert(m_Types.find(i_ParamName) != m_Types.end()); return std::any_cast<T>(m_Buffer[m_Params[i_ParamName]]);
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));
} }
///\brief Add a parameter value, identified by its name ///\brief Add a parameter value, identified by its name
...@@ -66,19 +64,13 @@ public: ...@@ -66,19 +64,13 @@ public:
/// internal buffer in a new location (previous value is still in memory at its previous location) /// 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) 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_Params[i_ParamName] = m_Buffer.size(); // Copy pointer offset
m_BeginBuffer = m_Buffer.data(); // Update buffer ptr in case of memory reordering m_Buffer.push_back(std::any(i_Value)); // Black-magic used to add anytype into the vector
*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();
} }
std::string getParamType(std::string const &i_ParamName){ 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(){ std::vector<std::string> getParametersName(){
...@@ -91,23 +83,8 @@ public: ...@@ -91,23 +83,8 @@ public:
private: private:
std::map<std::string, std::size_t> m_Params; // { Param name : offset } 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. ///\brief All parameters values concatenated in raw binary form.
std::vector<uint8_t> m_Buffer = {}; std::vector<std::any> 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;
}; };
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment