netlist: Reduce macro usage and make use of pstring utf8. (nw)

This commit is contained in:
couriersud 2020-06-06 20:41:53 +02:00
parent e3901f419c
commit 8a1ece4a3b
14 changed files with 125 additions and 226 deletions

View File

@ -801,6 +801,16 @@ struct save_helper
{
m_device->save_item(item, (m_prefix + "_" + name).c_str());
}
#if PHAS_INT128
void save_item(INT128 &item, pstring name)
{
auto *p = reinterpret_cast<std::uint64_t *>(&item);
m_device->save_item(p[0], (m_prefix + "_" + name + "_1").c_str());
m_device->save_item(p[1], (m_prefix + "_" + name + "_2").c_str());
}
#endif
private:
device_t *m_device;
pstring m_prefix;

View File

@ -128,15 +128,10 @@ namespace netlist
NETLIB_HANDLERI(updB)
{
const auto cnt(++m_bcd &= 0x07);
#if 1
m_QB[2].push((cnt >> 2) & 1, out_delay3);
m_QB[1].push((cnt >> 1) & 1, out_delay2);
m_QB[0].push(cnt & 1, out_delay);
#else
m_QB[0].push(cnt & 1, out_delay);
m_QB[1].push((cnt >> 1) & 1, out_delay2);
m_QB[2].push((cnt >> 2) & 1, out_delay3);
#endif
}
logic_input_t m_R1;

View File

@ -160,7 +160,7 @@ namespace plib
void mult_vec(VTR & res, const VTV & x) const noexcept
{
// res = A * x
// res = A * x
#if 0
//plib::omp::set_num_threads(4);
plib::omp::for_static(0, constants<std::size_t>::zero(), m_size, [this, &res, &x](std::size_t row)

View File

@ -5,83 +5,39 @@
#ifdef _WIN32
#include "windows.h"
#include "palloc.h"
namespace plib {
CHAR *astring_from_utf8(const char *utf8string)
{
WCHAR *wstring;
int char_count;
CHAR *result;
// convert MAME string (UTF-8) to UTF-16
char_count = MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, nullptr, 0);
wstring = (WCHAR *)alloca(char_count * sizeof(*wstring));
MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, wstring, char_count);
// convert UTF-16 to "ANSI code page" string
char_count = WideCharToMultiByte(CP_ACP, 0, wstring, -1, nullptr, 0, nullptr, nullptr);
result = new CHAR[char_count];
if (result != nullptr)
WideCharToMultiByte(CP_ACP, 0, wstring, -1, result, char_count, nullptr, nullptr);
return result;
}
WCHAR *wstring_from_utf8(const char *utf8string)
{
int char_count;
WCHAR *result;
// convert MAME string (UTF-8) to UTF-16
char_count = MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, nullptr, 0);
result = new WCHAR[char_count];
if (result != nullptr)
MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, result, char_count);
return result;
}
}
#ifdef UNICODE
#define tstring_from_utf8 plib::wstring_from_utf8
#else // !UNICODE
#define tstring_from_utf8 plib::astring_from_utf8
#endif // UNICODE
#else
#include <dlfcn.h>
#endif
namespace plib {
#include <type_traits>
namespace plib
{
using winapi_string = std::conditional<compile_info::unicode::value,
pwstring, pu8string>::type;
dynlib::dynlib(const pstring &libname)
: m_lib(nullptr)
{
#ifdef _WIN32
//fprintf(stderr, "win: loading <%s>\n", libname.c_str());
TCHAR *buffer = tstring_from_utf8(libname.c_str());
if (libname != "")
m_lib = LoadLibrary(buffer);
if (!libname.empty())
m_lib = LoadLibrary(winapi_string(libname).c_str());
else
m_lib = GetModuleHandle(nullptr);
if (m_lib != nullptr)
set_loaded(true);
//else
// fprintf(stderr, "win: library <%s> not found!\n", libname.c_str());
delete [] buffer;
#elif defined(__EMSCRIPTEN__)
//no-op
#else
//printf("loading <%s>\n", libname.c_str());
if (libname != "")
if (!libname.empty())
m_lib = dlopen(libname.c_str(), RTLD_LAZY);
else
m_lib = dlopen(nullptr, RTLD_LAZY);
#endif
if (m_lib != nullptr)
set_loaded(true);
//else
// printf("library <%s> not found: %s\n", libname.c_str(), dlerror());
#endif
}
dynlib::dynlib(const pstring &path, const pstring &libname)
@ -91,33 +47,25 @@ dynlib::dynlib(const pstring &path, const pstring &libname)
plib::unused_var(path);
// printf("win: loading <%s>\n", libname.c_str());
#ifdef _WIN32
TCHAR *buffer = tstring_from_utf8(libname.c_str());
if (libname != "")
m_lib = LoadLibrary(buffer);
if (!libname.empty())
m_lib = LoadLibrary(winapi_string(libname).c_str());
else
m_lib = GetModuleHandle(nullptr);
if (m_lib != nullptr)
set_loaded(true);
else
{
//printf("win: library <%s> not found!\n", libname.c_str());
}
delete [] buffer;
#elif defined(__EMSCRIPTEN__)
//no-op
#else
//printf("loading <%s>\n", libname.c_str());
if (libname != "")
if (!libname.empty())
m_lib = dlopen(libname.c_str(), RTLD_LAZY);
else
m_lib = dlopen(nullptr, RTLD_LAZY);
#endif
if (m_lib != nullptr)
set_loaded(true);
else
{
//printf("library <%s> not found!\n", libname.c_str());
}
#endif
}
dynlib::~dynlib()

View File

@ -3,28 +3,8 @@
#include "pmain.h"
#ifdef _WIN32
#include <windows.h>
#include <cstring>
#include <tchar.h>
#endif
namespace plib {
#ifdef _WIN32
static pstring toutf8(const wchar_t *w)
{
auto wlen = wcslen(w);
int dst_char_count = WideCharToMultiByte(CP_UTF8, 0, w, wlen, nullptr, 0, nullptr, nullptr);
char *buf = new char[dst_char_count + 1];
WideCharToMultiByte(CP_UTF8, 0, w, wlen, buf, dst_char_count, nullptr, nullptr);
buf[dst_char_count] = 0;
auto ret = pstring(buf);
delete [] buf;
return ret;
}
#endif
app::app()
: pout(&std::cout)
, perr(&std::cerr)
@ -32,11 +12,11 @@ namespace plib {
}
int app::main_utfX(int argc, char **argv)
int app::main_utfX(const std::vector<putf8string> &argv)
{
auto r = this->parse(argc, argv);
auto r = this->parse(argv);
if (r != argc)
if (r != argv.size())
{
this->perr("Error parsing {}\n", argv[r]);
this->perr(this->usage_short());
@ -46,22 +26,22 @@ namespace plib {
return this->execute();
}
#ifdef _WIN32
int app::main_utfX(int argc, char *argv[])
{
std::vector<putf8string> arg;
for (std::size_t i = 0; i < static_cast<std::size_t>(argc); i++)
arg.push_back(putf8string(argv[i]));
return main_utfX(arg);
}
int app::main_utfX(int argc, wchar_t *argv[])
{
std::vector<pstring> argv_vectors(argc);
std::vector<char *> utf8_argv(argc);
std::vector<putf8string> arg;
for (std::size_t i = 0; i < static_cast<std::size_t>(argc); i++)
arg.push_back(putf8string(pwstring(argv[i])));
// convert arguments to UTF-8
for (int i = 0; i < argc; i++)
{
argv_vectors[i] = toutf8(argv[i]);
utf8_argv[i] = const_cast<char *>(argv_vectors[i].c_str());
}
// run utf8_main
return main_utfX(argc, utf8_argv.data());
return main_utfX(arg);
}
#endif
} // namespace plib

View File

@ -53,15 +53,14 @@ namespace plib {
template <class C, typename T>
static int mainrun(int argc, T **argv)
{
C application;;
C application;
return application.main_utfX(argc, argv);
}
private:
int main_utfX(int argc, char **argv);
#ifdef _WIN32
int main_utfX(const std::vector<putf8string> &argv);
int main_utfX(int argc, char *argv[]);
int main_utfX(int argc, wchar_t *argv[]);
#endif
};

View File

@ -87,17 +87,17 @@ namespace plib {
}
}
int options::parse(int argc, char **argv)
std::size_t options::parse(const std::vector<putf8string> &argv)
{
check_consistency();
m_app = pstring(argv[0]);
m_app = argv[0];
bool seen_other_args = false;
for (int i=1; i<argc; )
for (std::size_t i=1; i < argv.size(); )
{
pstring arg(argv[i]);
putf8string arg(argv[i]);
option *opt = nullptr;
pstring opt_arg;
putf8string opt_arg;
bool has_equal_arg = false;
if (!seen_other_args && plib::startsWith(arg, "--"))
@ -151,7 +151,7 @@ namespace plib {
else
{
i++;
if (i >= argc)
if (i >= argv.size())
return i - 1;
if (opt->do_parse(pstring(argv[i])) != 0)
return i - 1;
@ -165,7 +165,7 @@ namespace plib {
}
i++;
}
return argc;
return argv.size();
}
pstring options::split_paragraphs(const pstring &text, unsigned width, unsigned indent,

View File

@ -237,7 +237,7 @@ namespace plib {
PCOPYASSIGNMOVE(options, delete)
void register_option(option_base *opt);
int parse(int argc, char **argv);
std::size_t parse(const std::vector<putf8string> &argv);
pstring help(const pstring &description, const pstring &usage,
unsigned width = 72, unsigned indent = 20) const;

View File

@ -95,7 +95,7 @@ namespace plib {
if (this->gptr() == this->egptr())
{
// clang reports sign error - weird
std::size_t bytes = pstring_mem_t_size(m_strm->m_outbuf) - static_cast<std::size_t>(m_strm->m_pos);
std::size_t bytes = m_strm->m_outbuf.size() - static_cast<std::size_t>(m_strm->m_pos);
if (bytes > m_buf.size())
bytes = m_buf.size();
@ -160,7 +160,7 @@ namespace plib {
// vector used as stack because we need to loop through stack
std::vector<input_context> m_stack;
pstring_t<pu8_traits> m_outbuf;
std::string m_outbuf;
std::istream::pos_type m_pos;
state_e m_state;
pstring m_line;

View File

@ -194,7 +194,7 @@ public:
void write(const pstring &s)
{
const auto *const sm = reinterpret_cast<const std::ostream::char_type *>(s.c_str());
const auto sl(static_cast<std::streamsize>(pstring_mem_t_size(s)));
const auto sl(static_cast<std::streamsize>(std::char_traits<std::ostream::char_type>::length(sm)));
write(sl);
m_strm.write(sm, sl);
}
@ -268,19 +268,15 @@ inline void copystream(std::ostream &dest, std::istream &src)
class ifstream : public std::ifstream
{
public:
#ifdef _WIN32
using filename_type = std::conditional<compile_info::win32::value,
pstring_t<pwchar_traits>, pstring_t<putf8_traits>>::type;
template <typename T>
explicit ifstream(const pstring_t<T> name, ios_base::openmode mode = ios_base::in)
: std::ifstream(reinterpret_cast<const wchar_t *>(pstring_t<putf16_traits>(name).c_str()), mode)
: std::ifstream(filename_type(name).c_str(), mode)
{
}
#else
template <typename T>
explicit ifstream(const pstring_t<T> name, ios_base::openmode mode = ios_base::in)
: std::ifstream(pstring_t<putf8_traits>(name).c_str(), mode)
{
}
#endif
};
///
@ -289,19 +285,14 @@ public:
class ofstream : public std::ofstream
{
public:
#ifdef _WIN32
using filename_type = std::conditional<compile_info::win32::value,
pstring_t<pwchar_traits>, pstring_t<putf8_traits>>::type;
template <typename T>
explicit ofstream(const pstring_t<T> name, ios_base::openmode mode = ios_base::in)
: std::ofstream(reinterpret_cast<const wchar_t *>(pstring_t<putf16_traits>(name).c_str()), mode)
: std::ofstream(filename_type(name).c_str(), mode)
{
}
#else
template <typename T>
explicit ofstream(const pstring_t<T> name, ios_base::openmode mode = ios_base::in)
: std::ofstream(pstring_t<putf8_traits>(name).c_str(), mode)
{
}
#endif
};

View File

@ -87,4 +87,5 @@ typename pstring_t<F>::size_type pstring_t<F>::find(code_t search, size_type sta
template struct pstring_t<pu8_traits>;
template struct pstring_t<putf8_traits>;
template struct pstring_t<putf16_traits>;
template struct pstring_t<putf32_traits>;
template struct pstring_t<pwchar_traits>;

View File

@ -102,10 +102,9 @@ public:
}
// mingw treats string constants as char* instead of char[N]
#if !defined(_WIN32) && !defined(_WIN64)
explicit
#endif
pstring_t(const mem_t *string)
template<typename C,
class = typename std::enable_if<std::is_same<C, const mem_t>::value>::type>
pstring_t(const C *string)
: m_str(string)
{
}
@ -211,10 +210,10 @@ public:
// the following are extensions to <string>
private:
// FIXME: remove those
size_type mem_t_size() const noexcept { return m_str.size(); }
private:
string_type m_str;
};
@ -232,11 +231,19 @@ struct pu8_traits
};
// No checking, this may deliver invalid codes
struct putf8_traits
template <std::size_t N, typename CT>
struct putf_traits
{
using mem_t = char;
};
template<typename CT>
struct putf_traits<1, CT>
{
using mem_t = CT;
using code_t = char32_t;
using string_type = std::string;
using string_type = std::basic_string<CT>;
static std::size_t len(const string_type &p) noexcept
{
std::size_t ret = 0;
@ -327,11 +334,12 @@ struct putf8_traits
}
};
struct putf16_traits
template<typename CT>
struct putf_traits<2, CT>
{
using mem_t = char16_t;
using mem_t = CT;
using code_t = char32_t;
using string_type = std::u16string;
using string_type = std::basic_string<CT>;
static std::size_t len(const string_type &p) noexcept
{
std::size_t ret = 0;
@ -392,119 +400,72 @@ struct putf16_traits
}
};
struct pwchar_traits
template<typename CT>
struct putf_traits<4, CT>
{
using mem_t = wchar_t;
using mem_t = CT;
using code_t = char32_t;
using string_type = std::wstring;
using string_type = std::basic_string<CT>;
static std::size_t len(const string_type &p) noexcept
{
if (sizeof(wchar_t) == 2)
{
std::size_t ret = 0;
auto i = p.begin();
while (i != p.end())
{
// FIXME: check that size is equal
auto c = static_cast<uint32_t>(*i++);
if (!((c & 0xd800) == 0xd800)) // NOLINT
ret++;
}
return ret;
}
return p.size();
}
static std::size_t codelen(const mem_t *p) noexcept
{
if (sizeof(wchar_t) == 2)
{
auto c = static_cast<uint16_t>(static_cast<unsigned char>(*p));
return ((c & 0xd800) == 0xd800) ? 2 : 1; // NOLINT
}
plib::unused_var(p);
return 1;
}
static std::size_t codelen(const code_t c) noexcept
{
if (sizeof(wchar_t) == 2)
return ((c & 0xd800) == 0xd800) ? 2 : 1; // NOLINT
plib::unused_var(c);
return 1;
}
static code_t code(const mem_t *p)
{
if (sizeof(wchar_t) == 2)
{
auto c = static_cast<uint32_t>(static_cast<unsigned char>(*p++));
if ((c & 0xd800) == 0xd800) // NOLINT
{
c = (c - 0xd800) << 10; // NOLINT
c += static_cast<uint32_t>(*p) - 0xdc00 + 0x10000; // NOLINT
}
return static_cast<code_t>(c);
}
return static_cast<code_t>(*p);
}
static void encode(code_t c, string_type &s)
{
if (sizeof(wchar_t) == 2)
{
auto cu = static_cast<uint32_t>(c);
if (c > 0xffff) // NOLINT
{ //make a surrogate pair
uint32_t t = ((cu - 0x10000) >> 10) + 0xd800; // NOLINT
cu = (cu & 0x3ff) + 0xdc00; // NOLINT
s += static_cast<mem_t>(t);
s += static_cast<mem_t>(cu);
}
else
s += static_cast<mem_t>(cu);
}
else
s += static_cast<wchar_t>(c);
s += static_cast<mem_t>(c);
}
static const mem_t *nthcode(const mem_t *p, const std::size_t n) noexcept
{
if (sizeof(wchar_t) == 2)
{
std::size_t i = n;
while (i-- > 0)
p += codelen(p);
return p;
}
return p + n;
}
};
using putf8_traits = putf_traits<sizeof(char), char>;
using putf16_traits = putf_traits<sizeof(char16_t), char16_t>;
using putf32_traits = putf_traits<sizeof(char32_t), char32_t>;
using pwchar_traits = putf_traits<sizeof(wchar_t), wchar_t>;
extern template struct pstring_t<pu8_traits>;
extern template struct pstring_t<putf8_traits>;
extern template struct pstring_t<putf16_traits>;
extern template struct pstring_t<putf32_traits>;
extern template struct pstring_t<pwchar_traits>;
#if (PSTRING_USE_STD_STRING)
using pstring = std::string;
static inline pstring::size_type pstring_mem_t_size(const pstring &s) { return s.size(); }
#else
using pstring = pstring_t<putf8_traits>;
template <typename T>
static inline pstring::size_type pstring_mem_t_size(const pstring_t<T> &s) { return s.mem_t_size(); }
#endif
using pu8string = pstring_t<pu8_traits>;
using putf8string = pstring_t<putf8_traits>;
using pu16string = pstring_t<putf16_traits>;
using putf16string = pstring_t<putf16_traits>;
using putf32string = pstring_t<putf32_traits>;
using pwstring = pstring_t<pwchar_traits>;
// custom specialization of std::hash can be injected in namespace std
namespace std
{
template<typename T> struct hash<pstring_t<T>>
template<typename T>
struct hash<pstring_t<T>>
{
using argument_type = pstring_t<T>;
using result_type = std::size_t;

View File

@ -35,6 +35,25 @@
namespace plib
{
//============================================================
// compile time information
//============================================================
struct compile_info
{
#ifdef _WIN32
using win32 = std::integral_constant<bool, true>;
#ifdef UNICODE
using unicode = std::integral_constant<bool, true>;
#else
using unicode = std::integral_constant<bool, false>;
#endif
#else
using win32 = std::integral_constant<bool, false>;
using unicode = std::integral_constant<bool, true>;
#endif
};
template<typename T> struct is_integral : public std::is_integral<T> { };
template<typename T> struct is_signed : public std::is_signed<T> { };
template<typename T> struct is_unsigned : public std::is_unsigned<T> { };

View File

@ -15,13 +15,8 @@ namespace plib
{
namespace util
{
#ifdef _WIN32
static constexpr const char PATH_SEP = '\\';
static constexpr const char *PATH_SEPS = "\\/";
#else
static constexpr const char PATH_SEP = '/';
static constexpr const char *PATH_SEPS = "/";
#endif
static constexpr const char PATH_SEP = compile_info::win32::value ? '\\' : '/';
static constexpr const char *PATH_SEPS = compile_info::win32::value ? "\\/" :"/";
pstring basename(const pstring &filename, const pstring &suffix)
{