Skip to content

Commit e2d1483

Browse files
committed
Barely working, just to store the state.
1 parent 8148302 commit e2d1483

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

15-AutoInstantiation/auto_instance.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,30 @@ int main(int argc, char** argv)
6060
{
6161
try {
6262
initializeInterpreter();
63-
executePythonScript("auto_instance.py");
63+
exec_file( str("auto_instance.py"), main_namespace, main_namespace);
64+
object BaseClass = main_namespace["Base"];
65+
PyTypeObject* base_class = reinterpret_cast<PyTypeObject*>(BaseClass.ptr());
66+
list keys = main_namespace.keys();
67+
list items = main_namespace.items();
68+
unsigned int n = len(items);
69+
for (unsigned int i = 0; i<n ; ++i) {
70+
object k = keys[i];
71+
std::string ks = extract<std::string>(k);
72+
std::cout << i << " " << ks ;
73+
object item = items[i];
74+
PyObject* item_ptr = item.ptr();
75+
if ( PyType_Check(item_ptr) != 0 ) {
76+
std::cout << " Type";
77+
PyTypeObject* type_obj = reinterpret_cast<PyTypeObject*>(item_ptr);
78+
if ( ( type_obj != base_class) && ( PyType_IsSubtype( type_obj, base_class) > 0) ) {
79+
object obj = items[i]();
80+
const Base& base_obj = extract<Base>(obj);
81+
std::cout << base_obj.name() << std::endl;
82+
}
83+
}
84+
std::cout << std::endl;
85+
}
86+
6487
// collect all classes derived from Base
6588
// instantiate objects from each class
6689
// call name on each object

15-AutoInstantiation/auto_instance.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,4 @@ class PythonDerivedTwo(Base):
1010
def __init__(self):
1111
Base.__init__(self, "PythonDerivedTwo")
1212

13-
# will be redone in C++
14-
p1 = PythonDerivedOne()
15-
p2 = PythonDerivedTwo()
16-
print p1, p2
1713

build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ set -x
55
cd ${0%%$(basename $0)}
66
mkdir build
77
cd build
8-
cmake .. && make && make test
8+
cmake -DCMAKE_BUILD_TYPE=DEBUG .. && make && make test
99

0 commit comments

Comments
 (0)