diff --git a/matplotlibcpp.h b/matplotlibcpp.h index d95d46a..85b4bf8 100644 --- a/matplotlibcpp.h +++ b/matplotlibcpp.h @@ -2,9 +2,20 @@ // Python headers must be included before any system headers, since // they define _POSIX_C_SOURCE -#include +#ifdef _WIN32 +# ifdef _DEBUG +# undef _DEBUG +# include +# define _DEBUG +# else +# include +# endif +#else +# include +#endif #include +#include #include #include #include @@ -350,10 +361,12 @@ template <> struct select_npy_type { const static NPY_TYPES type = NPY // Sanity checks; comment them out or change the numpy type below if you're compiling on // a platform where they don't apply -static_assert(sizeof(long long) == 8); -template <> struct select_npy_type { const static NPY_TYPES type = NPY_INT64; }; -static_assert(sizeof(unsigned long long) == 8); -template <> struct select_npy_type { const static NPY_TYPES type = NPY_UINT64; }; +#ifdef __linux__ + static_assert(sizeof(long long) == 8); + template <> struct select_npy_type { const static NPY_TYPES type = NPY_INT64; }; + static_assert(sizeof(unsigned long long) == 8); + template <> struct select_npy_type { const static NPY_TYPES type = NPY_UINT64; }; +#endif template PyObject* get_array(const std::vector& v) @@ -439,7 +452,7 @@ PyObject* get_listlist(const std::vector>& ll) /// /// See: https://matplotlib.org/3.2.1/api/_as_gen/matplotlib.pyplot.plot.html template -bool plot(const std::vector &x, const std::vector &y, const std::map& keywords) +bool plot(const std::vector &x, const std::vector &y, const std::map& keywords) { assert(x.size() == y.size()); @@ -455,10 +468,24 @@ bool plot(const std::vector &x, const std::vector &y, const st PyTuple_SetItem(args, 1, yarray); // construct keyword args - PyObject* kwargs = PyDict_New(); - for(std::map::const_iterator it = keywords.begin(); it != keywords.end(); ++it) - { - PyDict_SetItemString(kwargs, it->first.c_str(), PyString_FromString(it->second.c_str())); + PyObject *kwargs = PyDict_New(); + for (const auto &[key, value] : keywords) { + if (auto py_value = std::any_cast(&value)) { + PyDict_SetItemString(kwargs, key.c_str(), PyInt_FromLong(*py_value)); + } else if (auto py_value = std::any_cast(&value)) { + PyDict_SetItemString(kwargs, key.c_str(), PyLong_FromLong(*py_value)); + } else if (auto py_value = std::any_cast(&value)) { + PyDict_SetItemString(kwargs, key.c_str(), PyLong_FromUnsignedLong(*py_value)); + } else if (auto py_value = std::any_cast(&value)) { + PyDict_SetItemString(kwargs, key.c_str(), PyLong_FromSize_t(*py_value)); + } else if (auto py_value = std::any_cast(&value)) { + PyDict_SetItemString(kwargs, key.c_str(), PyLong_FromDouble(*py_value)); + } else if (auto py_value = std::any_cast(&value)) { + PyDict_SetItemString(kwargs, key.c_str(), PyString_FromString(py_value->c_str())); + } else if (auto py_value = std::any_cast(&value)) { + PyDict_SetItemString(kwargs, key.c_str(), PyString_FromString(*py_value)); + } else + throw std::invalid_argument("Unkown plot arg type"); } PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_plot, args, kwargs); @@ -2877,7 +2904,7 @@ inline bool plot(const std::vector& y, const std::string& format = "") { return plot(y,format); } -inline bool plot(const std::vector& x, const std::vector& y, const std::map& keywords) { +inline bool plot(const std::vector& x, const std::vector& y, const std::map& keywords) { return plot(x,y,keywords); }