diff --git a/sandboxed_api/var_reg.h b/sandboxed_api/var_reg.h index b7774f9..bc46e6f 100644 --- a/sandboxed_api/var_reg.h +++ b/sandboxed_api/var_reg.h @@ -15,10 +15,12 @@ #ifndef SANDBOXED_API_VAR_REG_H_ #define SANDBOXED_API_VAR_REG_H_ -#include #include +#include #include +#include "absl/strings/str_cat.h" +#include "absl/strings/str_format.h" #include "sandboxed_api/var_abstract.h" namespace sapi::v { @@ -32,9 +34,11 @@ class Callable : public Var { // Get pointer to the stored data. virtual const void* GetDataPtr() = 0; + // Set internal data from ptr. virtual void SetDataFromPtr(const void* ptr, size_t max_sz) = 0; - // Get data from inernal ptr. + + // Get data from internal ptr. void GetDataFromPtr(void* ptr, size_t max_sz) { size_t min_sz = std::min(GetSize(), max_sz); memcpy(ptr, GetDataPtr(), min_sz); @@ -48,8 +52,8 @@ class Callable : public Var { template class Reg : public Callable { public: - static_assert(std::is_integral() || std::is_floating_point() || - std::is_pointer() || std::is_enum(), + static_assert(std::is_integral_v || std::is_floating_point_v || + std::is_pointer_v || std::is_enum_v, "Only register-sized types are allowed as template argument " "for class Reg."); @@ -73,109 +77,59 @@ class Reg : public Callable { size_t GetSize() const override { return sizeof(T); } - Type GetType() const override { - if constexpr (std::is_integral() || std::is_enum()) { - return Type::kInt; - } - if constexpr (std::is_floating_point()) { - return Type::kFloat; - } - if constexpr (std::is_pointer()) { - return Type::kPointer; - } - // Not reached - } + Type GetType() const override; - std::string GetTypeString() const override { - if constexpr (std::is_integral() || std::is_enum()) { - return "Integer"; - } - if constexpr (std::is_floating_point()) { - return "Floating-point"; - } - if constexpr (std::is_pointer()) { - return "Pointer"; - } - // Not reached - } + std::string GetTypeString() const override; - std::string ToString() const override { return ValToString(); } + std::string ToString() const override; protected: // The stored value. T val_; - - private: - std::string ValToString() const { - char buf[32]; - bool signd = true; - if (std::is_integral::value || std::is_enum::value) { - if (std::is_integral::value) { - signd = std::is_signed::value; - } - - switch (sizeof(T)) { - case 1: - if (signd) { - snprintf(buf, sizeof(buf), "%" PRId8, - *(reinterpret_cast(&val_))); - } else { - snprintf(buf, sizeof(buf), "%" PRIu8, - *(reinterpret_cast(&val_))); - } - break; - case 2: - if (signd) { - snprintf(buf, sizeof(buf), "%" PRId16, - *(reinterpret_cast(&val_))); - } else { - snprintf(buf, sizeof(buf), "%" PRIu16, - *(reinterpret_cast(&val_))); - } - break; - case 4: - if (signd) { - snprintf(buf, sizeof(buf), "%" PRId32, - *(reinterpret_cast(&val_))); - } else { - snprintf(buf, sizeof(buf), "%" PRIu32, - *(reinterpret_cast(&val_))); - } - break; - case 8: - if (signd) { - snprintf(buf, sizeof(buf), "%" PRId64, - *(reinterpret_cast(&val_))); - } else { - snprintf(buf, sizeof(buf), "%" PRIu64, - *(reinterpret_cast(&val_))); - } - break; - default: - LOG(FATAL) << "Incorrect type"; - break; - } - } else if (std::is_floating_point::value) { - if (std::is_same::value) { - snprintf(buf, sizeof(buf), "%.10f", - *(reinterpret_cast(&val_))); - } else if (std::is_same::value) { - snprintf(buf, sizeof(buf), "%.10lf", - *(reinterpret_cast(&val_))); - } else if (std::is_same::value) { - snprintf(buf, sizeof(buf), "%.10Lf", - *(reinterpret_cast(&val_))); - } - } else if (std::is_pointer::value) { - snprintf(buf, sizeof(buf), "%p", *reinterpret_cast(&val_)); - } else { - LOG(FATAL) << "Incorrect type"; - } - - return std::string(buf); - } }; +template +Type Reg::GetType() const { + if constexpr (std::is_integral_v || std::is_enum_v) { + return Type::kInt; + } + if constexpr (std::is_floating_point_v) { + return Type::kFloat; + } + if constexpr (std::is_pointer_v) { + return Type::kPointer; + } + // Not reached +} + +template +std::string Reg::GetTypeString() const { + if constexpr (std::is_integral_v || std::is_enum_v) { + return "Integer"; + } + if constexpr (std::is_floating_point_v) { + return "Floating-point"; + } + if constexpr (std::is_pointer_v) { + return "Pointer"; + } + // Not reached +} + +template +std::string Reg::ToString() const { + if constexpr (std::is_integral_v || std::is_enum_v) { + return absl::StrCat(val_); + } + if constexpr (std::is_floating_point_v) { + return absl::StrFormat("%.10f", val_); + } + if constexpr (std::is_pointer::value) { + return absl::StrFormat("%p", val_); + } + // Not reached. +} + } // namespace sapi::v #endif // SANDBOXED_API_VAR_REG_H_