mirror of
https://github.com/boostorg/python.git
synced 2026-01-21 17:12:22 +00:00
- Generating code for the improved support of static data members of Boost.Python
[SVN r18193]
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
06 Apr 2003
|
||||
Support for the improved static data members support of Boost.Python.
|
||||
|
||||
05 Apr 2003
|
||||
New option for generating the bindings: --multiple.
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace basic {
|
||||
|
||||
struct C
|
||||
{
|
||||
C(): value(1), const_value(0) {}
|
||||
virtual int f(int x = 10)
|
||||
{
|
||||
return x*2;
|
||||
@@ -16,8 +17,16 @@ struct C
|
||||
const std::string& name() { return _name; }
|
||||
void set_name(const std::string& name) { _name = name; }
|
||||
std::string _name;
|
||||
static int static_value;
|
||||
static const int const_static_value;
|
||||
|
||||
int value;
|
||||
const int const_value;
|
||||
};
|
||||
|
||||
int C::static_value = 3;
|
||||
const int C::const_static_value = 100;
|
||||
|
||||
int call_f(C& c)
|
||||
{
|
||||
return c.f();
|
||||
@@ -28,4 +37,14 @@ int call_f(C& c, int x)
|
||||
return c.f(x);
|
||||
}
|
||||
|
||||
int get_static()
|
||||
{
|
||||
return C::static_value;
|
||||
}
|
||||
|
||||
int get_value(C& c)
|
||||
{
|
||||
return c.value;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,2 +1,5 @@
|
||||
Class('basic::C', 'basic.h')
|
||||
Function('basic::call_f', 'basic.h')
|
||||
Function('basic::get_static', 'basic.h')
|
||||
Function('basic::get_value', 'basic.h')
|
||||
|
||||
|
||||
@@ -243,16 +243,12 @@ class ClassExporter(Exporter):
|
||||
continue
|
||||
name = self.info[var.name].rename or var.name
|
||||
fullname = var.FullName()
|
||||
if var.static:
|
||||
code = '%s->attr("%s") = %s;' % (self.ScopeName(), name, fullname)
|
||||
self.Add('scope', code)
|
||||
if var.type.const:
|
||||
def_ = '.def_readonly'
|
||||
else:
|
||||
if var.type.const:
|
||||
def_ = '.def_readonly'
|
||||
else:
|
||||
def_ = '.def_readwrite'
|
||||
code = '%s("%s", &%s)' % (def_, name, fullname)
|
||||
self.Add('inside', code)
|
||||
def_ = '.def_readwrite'
|
||||
code = '%s("%s", &%s)' % (def_, name, fullname)
|
||||
self.Add('inside', code)
|
||||
|
||||
|
||||
def ExportMethods(self):
|
||||
|
||||
@@ -34,7 +34,7 @@ from policies import *
|
||||
from CppParser import CppParser, CppParserError
|
||||
import time
|
||||
|
||||
__VERSION__ = '0.6.6'
|
||||
__VERSION__ = '0.7.0'
|
||||
|
||||
def RecursiveIncludes(include):
|
||||
'Return a list containg the include dir and all its subdirectories'
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import unittest
|
||||
from basic import *
|
||||
|
||||
class BasicExampleTest(unittest.TestCase):
|
||||
|
||||
def testIt(self):
|
||||
from basic import C, call_f
|
||||
|
||||
# test virtual functions
|
||||
class D(C):
|
||||
def f(self, x=10):
|
||||
return x+1
|
||||
@@ -20,6 +21,37 @@ class BasicExampleTest(unittest.TestCase):
|
||||
self.assertEqual(call_f(c, 4), 8)
|
||||
self.assertEqual(call_f(d), 11)
|
||||
self.assertEqual(call_f(d, 3), 4)
|
||||
|
||||
# test data members
|
||||
def testValue(value):
|
||||
self.assertEqual(c.value, value)
|
||||
self.assertEqual(d.value, value)
|
||||
self.assertEqual(get_value(c), value)
|
||||
self.assertEqual(get_value(d), value)
|
||||
testValue(1)
|
||||
c.value = 30
|
||||
d.value = 30
|
||||
testValue(30)
|
||||
self.assertEqual(c.const_value, 0)
|
||||
self.assertEqual(d.const_value, 0)
|
||||
def set_const_value():
|
||||
c.const_value = 12
|
||||
self.assertRaises(AttributeError, set_const_value)
|
||||
|
||||
# test static data-members
|
||||
def testStatic(value):
|
||||
self.assertEqual(C.static_value, value)
|
||||
self.assertEqual(c.static_value, value)
|
||||
self.assertEqual(D.static_value, value)
|
||||
self.assertEqual(d.static_value, value)
|
||||
self.assertEqual(get_static(), value)
|
||||
testStatic(3)
|
||||
C.static_value = 10
|
||||
testStatic(10)
|
||||
self.assertEqual(C.const_static_value, 100)
|
||||
def set_const_static():
|
||||
C.const_static_value = 1
|
||||
self.assertRaises(AttributeError, set_const_static)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Reference in New Issue
Block a user