diff options
author | 2015-08-13 21:32:51 +0200 | |
---|---|---|
committer | 2015-08-13 21:35:33 +0200 | |
commit | 3271c77471e299a8e486b246ed5579c80c40d756 (patch) | |
tree | 3995d76c91d6cff91e9f30d9d72e75f285eaa24d | |
parent | 98b1106ee99be2aba9c6b540e7b25bc056bebed8 (diff) |
Changed UINT16 .. UINT64 to generic types in pformat.
@MSVC Users: I checked the MSVC++ documentation prior to committing
this. I also checked on a MSVC2010 installation that "long long" and
"%lld" actually work at least in MSVC2010.
Please let me know if this now works. (nw)
-rw-r--r-- | src/emu/netlist/plib/pstring.c | 6 | ||||
-rw-r--r-- | src/emu/netlist/plib/pstring.h | 158 |
2 files changed, 133 insertions, 31 deletions
diff --git a/src/emu/netlist/plib/pstring.c b/src/emu/netlist/plib/pstring.c index d0a45607244..1c02f026677 100644 --- a/src/emu/netlist/plib/pstring.c +++ b/src/emu/netlist/plib/pstring.c @@ -517,15 +517,16 @@ pformat::pformat(const char *fmt) m_str[sizeof(m_str) - 1] = 0; } -pformat &pformat::update(const char *f, const char *l, ...) +void pformat::format_element(const char *f, const char *l, const char *fmt_spec, ...) { va_list ap; - va_start(ap, l); + va_start(ap, fmt_spec); char fmt[30] = "%"; char search[10] = ""; char buf[1024]; strcat(fmt, f); strcat(fmt, l); + strcat(fmt, fmt_spec); int nl = vsprintf(buf, fmt, ap); m_arg++; int sl = sprintf(search, "%%%d", m_arg); @@ -537,7 +538,6 @@ pformat &pformat::update(const char *f, const char *l, ...) memcpy(p, buf, nl); } va_end(ap); - return *this; } diff --git a/src/emu/netlist/plib/pstring.h b/src/emu/netlist/plib/pstring.h index 3bebcb4cb3f..f59cefb9341 100644 --- a/src/emu/netlist/plib/pstring.h +++ b/src/emu/netlist/plib/pstring.h @@ -379,50 +379,152 @@ private: }; -class pformat +template <typename T> +struct ptype_treats +{ +}; + +template<> +struct ptype_treats<char> +{ + typedef short cast_type; + static const bool is_signed = true; + static const char *size_specifier() { return "h"; } +}; + +template<> +struct ptype_treats<short> +{ + typedef short cast_type; + static const bool is_signed = true; + static const char *size_specifier() { return "h"; } +}; + +template<> +struct ptype_treats<int> +{ + typedef int cast_type; + static const bool is_signed = true; + static const char *size_specifier() { return ""; } +}; + +template<> +struct ptype_treats<long> +{ + typedef long cast_type; + static const bool is_signed = true; + static const char *size_specifier() { return "l"; } +}; + +template<> +struct ptype_treats<long long> +{ + typedef long long cast_type; + static const bool is_signed = true; + static const char *size_specifier() { return "ll"; } +}; + +template<> +struct ptype_treats<unsigned char> +{ + typedef unsigned short cast_type; + static const bool is_signed = false; + static const char *size_specifier() { return "h"; } +}; + +template<> +struct ptype_treats<unsigned short> +{ + typedef unsigned short cast_type; + static const bool is_signed = false; + static const char *size_specifier() { return "h"; } +}; + +template<> +struct ptype_treats<unsigned int> +{ + typedef unsigned int cast_type; + static const bool is_signed = false; + static const char *size_specifier() { return ""; } +}; + +template<> +struct ptype_treats<unsigned long> +{ + typedef unsigned long cast_type; + static const bool is_signed = false; + static const char *size_specifier() { return "l"; } +}; + +template<> +struct ptype_treats<unsigned long long> +{ + typedef unsigned long long cast_type; + static const bool is_signed = false; + static const char *size_specifier() { return "ll"; } +}; + +template <typename P> +class pformat_base { public: - pformat(const pstring &fmt); - pformat(const char *fmt); - operator pstring() const { return m_str; } + virtual ~pformat_base() { } - const char *cstr() { return m_str; } + P &operator ()(const double x, const char *f = "") { format_element(f, "", "g", x); return static_cast<P &>(*this); } + P & e(const double x, const char *f = "") { format_element(f, "", "e", x); return static_cast<P &>(*this); } + P & f(const double x, const char *f = "") { format_element(f, "", "f", x); return static_cast<P &>(*this); } + + P &operator ()(const char *x, const char *f = "") { format_element(f, "", "s", x); return static_cast<P &>(*this); } + P &operator ()(const void *x, const char *f = "") { format_element(f, "", "p", x); return static_cast<P &>(*this); } + P &operator ()(const pstring &x, const char *f = "") { format_element(f, "", "s", x.cstr() ); return static_cast<P &>(*this); } + + template<typename T> + P &operator ()(const T x, const char *f = "") + { + if (ptype_treats<T>::is_signed) + format_element(f, ptype_treats<T>::size_specifier(), "d", (typename ptype_treats<T>::cast_type) x); + else + format_element(f, ptype_treats<T>::size_specifier(), "u", (typename ptype_treats<T>::cast_type) x); + return static_cast<P &>(*this); + } + + template<typename T> + P &x(const T x, const char *f = "") + { + format_element(f, ptype_treats<T>::size_specifier(), "x", x); + return static_cast<P &>(*this); + } - pformat &operator ()(const INT64 x, const char *f = "") { return update(f, I64FMT "d", x); } - pformat &operator ()(const UINT64 x, const char *f = "") { return update(f, I64FMT "u", x); } + template<typename T> + P &o(const T x, const char *f = "") + { + format_element(f, ptype_treats<T>::size_specifier(), "o", x); + return static_cast<P &>(*this); + } - pformat &x (const INT64 x, const char *f = "") { return update(f, I64FMT "x", x); } - pformat &x (const UINT64 x, const char *f = "") { return update(f, I64FMT "x", x); } +protected: - pformat &operator ()(const long x, const char *f = "") { return update(f, "ld", x); } - pformat &operator ()(const unsigned long x, const char *f = "") { return update(f, "lu", x); } + virtual void format_element(const char *f, const char *l, const char *fmt_spec, ...) = 0; +}; - pformat &operator ()(const INT32 x, const char *f = "") { return update(f, "d", x); } - pformat &operator ()(const UINT32 x, const char *f = "") { return update(f, "u", x); } +class pformat : public pformat_base<pformat> +{ +public: + pformat(const pstring &fmt); + pformat(const char *fmt); - pformat &x (const INT32 x, const char *f = "") { return update(f, "x", x); } - pformat &x (const UINT32 x, const char *f = "") { return update(f, "x", x); } + operator pstring() const { return m_str; } - pformat &operator ()(const INT16 x, const char *f = "") { return update(f, "hd", x); } - pformat &operator ()(const UINT16 x, const char *f = "") { return update(f, "hu", x); } + const char *cstr() { return m_str; } -#if !defined(__MINGW32__) && !defined(__MINGW64__) && !defined(EMSCRIPTEN) - //pformat &operator ()(const std::size_t x, const char *f = "") { return update(f, SIZETFMT, x); } -#endif - pformat &operator ()(const double x, const char *f = "") { return update(f, "g", x); } - pformat & e(const double x, const char *f = "") { return update(f, "e", x); } - pformat & f(const double x, const char *f = "") { return update(f, "f", x); } - pformat &operator ()(const char *x, const char *f = "") { return update(f, "s", x); } - pformat &operator ()(const void *x, const char *f = "") { return update(f, "p", x); } - pformat &operator ()(const pstring &x, const char *f = "") { return update(f, "s", x.cstr() ); } +protected: + void format_element(const char *f, const char *l, const char *fmt_spec, ...); private: - pformat &update(const char *f, const char *l, ...); - char m_str[2048]; unsigned m_arg; }; |