forked from chaosct/ofxPython
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathofxPython.h
More file actions
executable file
·190 lines (172 loc) · 5.69 KB
/
Copy pathofxPython.h
File metadata and controls
executable file
·190 lines (172 loc) · 5.69 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#pragma once
#include "ofTypes.h"
extern "C"{
#include <Python.h>
}
#include "ofMain.h"
#include <map>
class ofxPythonMappingValue;
class ofxPythonAttrValue;
class ofxPythonTupleMaker;
class ofxPythonListMaker;
class ofxPythonObject
{
public:
ofxPythonObject method(const string &method_name); //call method without arguments
ofxPythonObject operator ()(); //call objects without arguments
ofxPythonObject operator ()(ofxPythonObject); //call objects 1 argument
ofxPythonObject operator ()(ofxPythonObject, ofxPythonObject); //call objects 2 arguments
ofxPythonObject operator ()(ofxPythonObject, ofxPythonObject, ofxPythonObject); //call objects 3 arguments
ofxPythonObject operator ()(ofxPythonObject, ofxPythonObject, ofxPythonObject, ofxPythonObject); //call objects 4 arguments
ofxPythonAttrValue attr(const string& attribute);
ofxPythonMappingValue operator [](const string& key);
ofxPythonMappingValue operator [](const char * key);
bool isNone() const;
bool isBool() const;
bool isInt() const;
bool isFloat() const;
bool isString() const;
bool isList() const;
bool isTuple() const;
bool isDict() const;
bool asBool( ) const;
long int asInt() const;
double asFloat() const;
string asString() const;
vector<ofxPythonObject> asVector() const;
std::map<ofxPythonObject,ofxPythonObject> asMap() const;
static ofxPythonObject _None();
static ofxPythonObject fromBool(bool);
static ofxPythonObject fromInt(long int);
static ofxPythonObject fromFloat(double);
static ofxPythonObject fromString(const string&);
// use makeTuple like this:
// ofxPythonObject tuple = ofxPythonObject::makeTuple() << obj1 << obj2 << obj3;
static ofxPythonTupleMaker makeTuple();
static ofxPythonListMaker makeList();
static ofxPythonObject makeDict();
operator bool() const;
const string repr();
const string str();
bool isPythonError(){ return pythonError; }
bool pythonError;
protected:
void insert_borrowed(PyObject *);
void insert_owned(PyObject *);
class ofxPythonObjectManaged
{
public:
ofxPythonObjectManaged(PyObject*);
~ofxPythonObjectManaged();
PyObject * obj;
};
ofPtr<ofxPythonObjectManaged> data;
friend class ofxPython;
friend class ofxPythonMappingValue;
friend class ofxPythonAttrValue;
friend class ofxPythonListMaker;
friend class ofxPythonTupleMaker;
friend ofxPythonObject make_object_owned(PyObject * obj, bool);
friend ofxPythonObject make_object_borrowed(PyObject * obj, bool);
};
class ofxPython
{
public:
ofxPython();
virtual ~ofxPython();
int init(); //returns the number of ofxPython instances,
//if it's 1 you should initialize your custom bindings if any
void reset();
void executeScript(const string& path);
void executeString(const string& script);
ofxPythonObject executeStatement(const string& script);
ofxPythonObject evalString(const string& expression);
static ofxPythonObject getObject(const string& name, const string& module);
ofxPythonObject getObject(const string& name);
ofxPythonObject getObjectOrNone(const string& name);
void setObject(const string& name, ofxPythonObject o);
ofxPythonObject getLocals();
void setVirtualEnv(const string & path);
void addPath(const string & path);
bool isPythonError(){ return pythonError; }
protected:
ofxPythonObject locals;
// ofxPythonObject globals;
static unsigned int instances;
bool initialized;
bool pythonError;
friend class ofxPythonObject::ofxPythonObjectManaged;
};
class ofxPythonOperation
{
public:
static PyThreadState * pstate;
static unsigned int instances;
ofxPythonOperation();
~ofxPythonOperation();
};
class ofxPythonObjectLike
{
public:
ofxPythonObject method(const string &method_name); //call method without arguments
ofxPythonObject operator ()(); //call objects without arguments
ofxPythonObject operator ()(ofxPythonObject); //call objects 1 argument
ofxPythonObject operator ()(ofxPythonObject, ofxPythonObject); //call objects 2 arguments
ofxPythonObject operator ()(ofxPythonObject, ofxPythonObject, ofxPythonObject); //call objects 3 arguments
ofxPythonObject operator ()(ofxPythonObject, ofxPythonObject, ofxPythonObject, ofxPythonObject); //call objects 4 arguments
ofxPythonAttrValue attr(const string& attribute);
ofxPythonMappingValue operator [](const string& key);
ofxPythonMappingValue operator [](const char * key);
bool isNone();
bool isBool();
bool isInt();
bool isFloat();
bool isString();
bool isList();
bool isTuple();
bool isDict();
bool asBool();
long int asInt();
double asFloat();
string asString();
vector<ofxPythonObject> asVector();
std::map<ofxPythonObject,ofxPythonObject> asMap();
operator bool();
const string repr();
const string str();
virtual operator ofxPythonObject()=0;
};
class ofxPythonMappingValue: public ofxPythonObjectLike
{
ofxPythonObject object;
string key;
public:
ofxPythonMappingValue(ofxPythonObject o, const string& k);
operator ofxPythonObject();
ofxPythonMappingValue & operator =(ofxPythonObject);
ofxPythonMappingValue & operator =(ofxPythonMappingValue & o);
};
class ofxPythonAttrValue: public ofxPythonObjectLike
{
ofxPythonObject object;
string attribute;
public:
ofxPythonAttrValue(ofxPythonObject o, const string& attr);
operator ofxPythonObject();
ofxPythonAttrValue & operator =(ofxPythonObject);
ofxPythonAttrValue & operator =(ofxPythonAttrValue & o);
};
class ofxPythonTupleMaker: public ofxPythonObjectLike
{
vector<ofxPythonObject> contents;
public:
ofxPythonTupleMaker& operator<<(ofxPythonObject);
operator ofxPythonObject();
};
class ofxPythonListMaker: public ofxPythonObjectLike
{
vector<ofxPythonObject> contents;
public:
ofxPythonListMaker& operator<<(ofxPythonObject);
operator ofxPythonObject();
};