-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmatplotlib.hpp
127 lines (96 loc) · 2.25 KB
/
cmatplotlib.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#ifndef CMATPLOTLIB_HPP
#define CMATPLOTLIB_HPP
#include <ios>
#include <ostream>
#include <sstream>
#include <string>
#include <type_traits>
#include <vector>
#include <Python.h>
#define GENFUNC(NAME) \
template<typename... T> \
void NAME(T... args) \
{ \
py_execute(std::string(NAMESPACE) + "." + std::string(#NAME) + "(" + ("" + ... + (to_string(args) + ", ")) + ")"); \
}
#define GENKWARG(NAME) \
namespace kwargs { \
struct NAME { \
template<typename T> \
kwarg operator =(const T& v) const \
{ \
return kwarg{#NAME, to_string(v)}; \
} \
}; \
} \
static const kwargs::NAME NAME;
struct kwarg {
std::string key;
std::string value;
};
template<typename T>
inline std::vector<T> to_vector(const T* const v, size_t n)
{
return std::vector(v, v + n);
}
template<typename T>
static std::ostream& operator <<(std::ostream& os, const std::vector<T>& v)
{
if constexpr (std::is_floating_point_v<T>) {
os << std::scientific;
}
os << '[';
for (const T& v_: v) {
os << v_ << ',';
}
return os << ']';
}
template<typename T>
std::string to_string(const T& v)
{
std::ostringstream ss;
ss << v;
return ss.str();
}
std::string to_string(const char v)
{
std::ostringstream ss;
ss << "\"" << v << "\"";
return ss.str();
}
std::string to_string(const char* v)
{
std::ostringstream ss;
ss << "\"" << v << "\"";
return ss.str();
}
std::string to_string(const std::string& v)
{
return "\"" + v + "\"";
}
std::string to_string(const kwarg& v)
{
return "**{'" + v.key + "':" + v.value + "}";
}
namespace cmatplotlib {
#include "genkwarg.hpp"
namespace pyplot {
static std::string NAMESPACE = "matplotlib.pyplot";
static bool initialized = false;
static void py_execute(const std::string& command)
{
if (!initialized) {
Py_Initialize();
PyRun_SimpleString(("import " + NAMESPACE).c_str());
PyRun_SimpleString("from numpy import nan");
initialized = true;
}
int err = PyRun_SimpleString(command.c_str());
if (err != 0) {
exit(err);
}
}
#include "genfunc.hpp"
}
};
#endif