Browse Source

improve module inspect

pull/1/head
heck 3 years ago
parent
commit
37ac31c8cf
  1. 12
      src/inspect.hh
  2. 56
      src/inspect.hxx

12
src/inspect.hh

@ -1,6 +1,8 @@
#ifndef LIBPEPCXX11_INSPECT_HH
#define LIBPEPCXX11_INSPECT_HH
namespace pEp {
namespace CXX {
struct Inspect {
@ -8,20 +10,28 @@ namespace pEp {
// returns the type of c
template<class T>
static std::string type(T&);
template<class T>
static std::string type(T&&);
// addr ----------------------------------------
// returns the address of c
template<class T>
static std::string addr(T& c);
template<class T>
static std::string addr(T&& c);
// val ----------------------------------------
// returns the value of c (if T is pointer type, the address p is pointing to)
template<class T>
static std::string val(T c, size_t val_len = 30);
static std::string val(T& c, size_t val_len = 30);
template<class T>
static std::string val(T&& c, size_t val_len = 30);
// all ----------------------------------------
template<class T>
static std::string all(T& c, size_t val_len = 30);
template<class T>
static std::string all(T&& c, size_t val_len = 30);
};
} // namespace CXX
} // namespace pEp

56
src/inspect.hxx

@ -6,6 +6,7 @@
#include <pEp/utils.hh>
#include <sstream>
#include <type_traits>
#include <pEp/pEpLog.hh>
namespace pEp {
namespace CXX {
@ -14,27 +15,47 @@ namespace pEp {
template<class T>
std::string Inspect::type(T&)
{
// pEpLog(typeid(T).name());
std::stringstream ss_type;
ss_type << typeid(T).name();
return ss_type.str();
}
template<class T>
std::string Inspect::type(T&&)
{
// pEpLog(typeid(T).name() << "&&");
std::stringstream ss_type;
ss_type << typeid(T).name();
return ss_type.str();
}
// addr ----------------------------------------
//
template<class T>
std::string Inspect::addr(T& c)
{
// pEpLog(typeid(T).name());
std::stringstream ss_addr{};
ss_addr << static_cast<void*>(&c);
return ss_addr.str();
}
template<class T>
std::string Inspect::addr(T&& c)
{
// pEpLog(typeid(T).name() << "&&");
std::stringstream ss_addr{};
ss_addr << &c;
ss_addr << static_cast<void*>(&c);
return ss_addr.str();
}
// val ----------------------------------------
template<class T>
struct val_str_helper {
static std::string str(T c, size_t val_len)
static std::string str(T& c, size_t val_len)
{
// pEpLog(typeid(T).name());
std::stringstream ss_val{};
ss_val << c;
return pEp::Utils::clip(ss_val.str(), val_len);
@ -43,21 +64,26 @@ namespace pEp {
template<class T>
struct val_str_helper<T*> {
static std::string str(T* c, size_t val_len)
static std::string str(T*& c, size_t val_len)
{
// pEpLog(typeid(T).name());
std::stringstream ss_val{};
if (c != nullptr) {
ss_val << c;
} else {
ss_val << "<NULL>";
}
ss_val << static_cast<void*>(c);
return pEp::Utils::clip(ss_val.str(), val_len);
}
};
template<class T>
std::string Inspect::val(T c, size_t val_len)
std::string Inspect::val(T& c, size_t val_len)
{
// pEpLog(typeid(T).name());
return val_str_helper<T>::str(c, val_len);
}
template<class T>
std::string Inspect::val(T&& c, size_t val_len)
{
// pEpLog(typeid(T).name() << "&&");
return val_str_helper<T>::str(c, val_len);
}
@ -65,8 +91,18 @@ namespace pEp {
template<class T>
std::string Inspect::all(T& c, size_t val_len)
{
// pEpLog(typeid(T).name());
std::stringstream ret{};
ret << "{ " << std::setw(10) << type(c) << " | " << addr(c) << " | " << val(c, val_len) << " }";
return ret.str();
}
template<class T>
std::string Inspect::all(T&& c, size_t val_len)
{
// pEpLog(typeid(T).name() << "&&");
std::stringstream ret{};
ret << "{ " << type(c) << " | " << addr(c) << " | " << val(c, val_len) << " }";
ret << "{ " << std::setw(5) << type(c) << " | " << "< temporary >" << " | " << val(c, val_len) << " }";
return ret.str();
}
} // namespace CXX

Loading…
Cancel
Save