@@ -19,8 +19,10 @@ along with OpenOCPP. If not, see <http://www.gnu.org/licenses/>.
1919#ifndef OPENOCPP_GENERICMESSAGESCONVERTER_H
2020#define OPENOCPP_GENERICMESSAGESCONVERTER_H
2121
22+ #include < memory>
2223#include < string>
2324#include < unordered_map>
25+
2426namespace ocpp
2527{
2628namespace messages
@@ -42,13 +44,13 @@ class GenericMessagesConverter
4244 * @return Pointer to the message converter for the request or nullptr if the converter doesn't exists
4345 */
4446 template <typename RequestType>
45- IMessageConverter<RequestType>* getRequestConverter (const std::string& action) const
47+ std::unique_ptr< IMessageConverter<RequestType>> getRequestConverter (const std::string& action) const
4648 {
47- IMessageConverter<RequestType>* ret = nullptr ;
48- auto it = m_req_converters.find (action);
49+ std::unique_ptr< IMessageConverter<RequestType>> ret;
50+ auto it = m_req_converters.find (action);
4951 if (it != m_req_converters.end ())
5052 {
51- ret = reinterpret_cast <IMessageConverter<RequestType>*>(it->second );
53+ ret. reset ( reinterpret_cast <IMessageConverter<RequestType>*>(it->second )-> clone () );
5254 }
5355 return ret;
5456 }
@@ -59,13 +61,13 @@ class GenericMessagesConverter
5961 * @return Pointer to the message converter for the response or nullptr if the converter doesn't exists
6062 */
6163 template <typename ResponseType>
62- IMessageConverter<ResponseType>* getResponseConverter (const std::string& action) const
64+ std::unique_ptr< IMessageConverter<ResponseType>> getResponseConverter (const std::string& action) const
6365 {
64- IMessageConverter<ResponseType>* ret = nullptr ;
65- auto it = m_resp_converters.find (action);
66+ std::unique_ptr< IMessageConverter<ResponseType>> ret;
67+ auto it = m_resp_converters.find (action);
6668 if (it != m_resp_converters.end ())
6769 {
68- ret = reinterpret_cast <IMessageConverter<ResponseType>*>(it->second );
70+ ret. reset ( reinterpret_cast <IMessageConverter<ResponseType>*>(it->second )-> clone () );
6971 }
7072 return ret;
7173 }
@@ -93,6 +95,34 @@ class GenericMessagesConverter
9395 m_resp_converters[action] = &converter;
9496 }
9597
98+ /* *
99+ * @brief Delete a converter for a request
100+ * @param action Ocpp call action corresponding to the request
101+ */
102+ template <typename RequestType>
103+ void deleteRequestConverter (const std::string& action)
104+ {
105+ auto it = m_req_converters.find (action);
106+ if (it != m_req_converters.end ())
107+ {
108+ delete reinterpret_cast <IMessageConverter<RequestType>*>(it->second );
109+ }
110+ }
111+
112+ /* *
113+ * @brief Delete a converter for a response
114+ * @param action Ocpp call action corresponding to the response
115+ */
116+ template <typename ResponseType>
117+ void deleteResponseConverter (const std::string& action)
118+ {
119+ auto it = m_resp_converters.find (action);
120+ if (it != m_resp_converters.end ())
121+ {
122+ delete reinterpret_cast <IMessageConverter<ResponseType>*>(it->second );
123+ }
124+ }
125+
96126 private:
97127 /* * @brief Request converters */
98128 std::unordered_map<std::string, void *> m_req_converters;
0 commit comments