diff --git a/src/inspect.hh b/src/inspect.hh new file mode 100644 index 0000000..6daff55 --- /dev/null +++ b/src/inspect.hh @@ -0,0 +1,30 @@ +#ifndef LIBPEPCXX11_INSPECT_HH +#define LIBPEPCXX11_INSPECT_HH + +namespace pEp { + namespace CXX { + struct Inspect { + // type ---------------------------------------- + // returns the type of c + template + static std::string type(T&); + + // addr ---------------------------------------- + // returns the address of c + template + static std::string addr(T& c); + + // val ---------------------------------------- + // returns the value of c (if T is pointer type, the address p is pointing to) + template + static std::string val(T c, size_t val_len = 30); + + // all ---------------------------------------- + template + static std::string all(T& c, size_t val_len = 30); + }; + } // namespace CXX +} // namespace pEp + +#include "inspect.hxx" +#endif diff --git a/src/inspect.hxx b/src/inspect.hxx new file mode 100644 index 0000000..4e4bd88 --- /dev/null +++ b/src/inspect.hxx @@ -0,0 +1,76 @@ +#ifndef LIBPEPCXX11_INSPECT_HXX +#define LIBPEPCXX11_INSPECT_HXX + +#include +#include +#include +#include +#include + +namespace pEp { + namespace CXX { + // type ---------------------------------------- + // same for pointer and value typee + template + std::string Inspect::type(T&) + { + std::stringstream ss_type; + ss_type << typeid(T).name(); + return ss_type.str(); + } + + + // addr ---------------------------------------- + // + template + std::string Inspect::addr(T& c) + { + std::stringstream ss_addr{}; + ss_addr << &c; + return ss_addr.str(); + } + + // val ---------------------------------------- + template + struct val_str_helper { + static std::string str(T c, size_t val_len) + { + std::stringstream ss_val{}; + ss_val << c; + return pEp::Utils::clip(ss_val.str(), val_len); + } + }; + + template + struct val_str_helper { + static std::string str(T* c, size_t val_len) + { + std::stringstream ss_val{}; + if (c != nullptr) { + ss_val << c; + } else { + ss_val << ""; + } + return pEp::Utils::clip(ss_val.str(), val_len); + } + }; + + template + std::string Inspect::val(T c, size_t val_len) + { + return val_str_helper::str(c, val_len); + } + + // all ---------------------------------------- + template + std::string Inspect::all(T& c, size_t val_len) + { + std::stringstream ret{}; + ret << "{ " << type(c) << " | " << addr(c) << " | " << val(c, val_len) << " }"; + return ret.str(); + } + } // namespace CXX +} // namespace pEp + + +#endif \ No newline at end of file