-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyObject.cpp
More file actions
108 lines (88 loc) · 3.24 KB
/
PyObject.cpp
File metadata and controls
108 lines (88 loc) · 3.24 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
/*
* PyObject.cpp
* Author: Kent D. Lee
* (c) 2013
* Created on: Feb 3, 2013
*
* License:
* Please read the LICENSE file in this distribution for details regarding
* the licensing of this code. This code is freely available for educational
* use. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.
*
* Description:
* See the associated header file for a description of the purpose of this
* class. Implementation details are provided here. Read below for
* any specific details.
*
*/
#include "PyObject.h"
#include "PyException.h"
#include "PyStr.h"
#include <iostream>
#include "PyType.h"
#include <sstream>
using namespace std;
ostream& operator <<(ostream &os, PyObject &t) {
return os << t.toString();
}
PyObject* PyObject::callMethod(string name, vector<PyObject*>* args) {
// This is where the magic happens. The declaration below declares mbr
// as a member function pointer to a method that takes a pointer to a
// vector of PyObject pointers as its argument and returns a PyObject
// pointer. All magic methods and method calls in CoCo adhere to this
// strict function call type.
PyObject* (PyObject::*mbr)(vector<PyObject*>*);
if (dict.find(name) == dict.end()) {
// If we lookup the magic method and don't find it, raise an exception.
throw new PyException(PYILLEGALOPERATIONEXCEPTION, "TypeError: '"+ getType()->toString() + "' object has no attribute '" + name + "'");
}
// Set the mbr pointer to the function's address.
mbr = dict[name];
// Here is how to call the function. First, mbr is a method of
// the current class, whether a sub-class or not. Sub-classes will
// automatically work with this code. So, we dereference this to get
// into the object. Then we dereference mbr to get to the method's code
// and finally pass the args to the method. This is very slick since
// it works on any subclass that defines a magic method. Because all
// magic methods are implemented as virtual functions, this works means
// if a base class defines a magic method, subclasses automatically pick
// up that functionality.
return (this->*mbr)(args);
}
PyObject::PyObject() {
dict["__str__"] = (PyObject* (PyObject::*)(vector<PyObject*>*)) (&PyObject::__str__);
dict["__type__"] = (PyObject* (PyObject::*)(vector<PyObject*>*)) (&PyObject::__type__);
}
PyObject::~PyObject() {
}
PyType* PyObject::getType() {
return NULL;
}
string PyObject::toString() {
return "PyObject()";
}
void PyObject::incRef() {
refCount++;
}
void PyObject::decRef() {
refCount--;
}
int PyObject::getRefCount() const {
return refCount;
}
PyObject* PyObject::__str__(vector<PyObject*>* args) {
ostringstream msg;
if (args->size() != 0) {
msg << "TypeError: expected 0 arguments, got " << args->size();
throw new PyException(PYWRONGARGCOUNTEXCEPTION,msg.str());
}
return new PyStr(toString());
}
PyObject* PyObject::__type__(vector<PyObject*>* args) {
ostringstream msg;
if (args->size() != 0) {
msg << "TypeError: expected 0 arguments, got " << args->size();
throw new PyException(PYWRONGARGCOUNTEXCEPTION,msg.str());
}
return (PyObject*)this->getType();
}