Browse Source

* Fixed memory leak in type_helper

master
bergmann 4 years ago
parent
commit
68b49deed3
2 changed files with 26 additions and 5 deletions
  1. +1
    -1
      include/cppcore/misc/type_helper.h
  2. +25
    -4
      include/cppcore/misc/type_helper.inl

+ 1
- 1
include/cppcore/misc/type_helper.h View File

@@ -16,7 +16,7 @@ namespace cppcore
/**
* @brief Get the name of the type.
*/
static inline std::string name();
static inline const std::string& name();
};




+ 25
- 4
include/cppcore/misc/type_helper.inl View File

@@ -1,16 +1,37 @@
#pragma once

#include <memory>

#include "type_helper.h"

namespace cppcore
{

namespace __impl
{

template<typename T>
std::string get_type_name()
{
using ptr_u = std::unique_ptr<char, decltype(&free)>;

int status;
ptr_u name(
abi::__cxa_demangle(typeid(T).name(), 0, 0, &status),
&free);

return std::string(name
? name.get()
: typeid(T).name());
}

}

template<typename T>
std::string type_helper<T>::name()
const std::string& type_helper<T>::name()
{
int status;
auto name = abi::__cxa_demangle(typeid(T).name(), 0, 0, &status);
return std::string(name ? name : typeid(T).name());
static auto value = __impl::get_type_name<T>();
return value;
}

/**


Loading…
Cancel
Save