2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-23 05:42:30 +00:00

- Support for global variables

- Bug fixes in ClassExporter


[SVN r18518]
This commit is contained in:
Bruno da Silva de Oliveira
2003-05-23 20:37:35 +00:00
parent 19eff7791d
commit 858e1aba67
17 changed files with 178 additions and 16 deletions

View File

@@ -1,3 +1,7 @@
23 May 2003
Support for global variables added.
Various bug fixes.
08 May 2003
Fixed bug where in a certain cases the GCCXMLParser would end up with multiple
declarations of the same class

View File

@@ -0,0 +1,49 @@
<html>
<head>
<!-- Generated by the Spirit (http://spirit.sf.net) QuickDoc -->
<title>Global Variables</title>
<link rel="stylesheet" href="theme/style.css" type="text/css">
<link rel="prev" href="smart_pointers.html">
</head>
<body>
<table width="100%" height="48" border="0" cellspacing="2">
<tr>
<td><img src="theme/c%2B%2Bboost.gif">
</td>
<td width="85%">
<font size="6" face="Verdana, Arial, Helvetica, sans-serif"><b>Global Variables</b></font>
</td>
</tr>
</table>
<br>
<table border="0">
<tr>
<td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td>
<td width="30"><a href="smart_pointers.html"><img src="theme/l_arr.gif" border="0"></a></td>
<td width="20"><img src="theme/r_arr_disabled.gif" border="0"></td>
</tr>
</table>
<p>
To export global variables, use the <tt>Var</tt> construct:</p>
<code><pre>
<span class=identifier>Var</span><span class=special>(</span><span class=string>&quot;myglobal&quot;</span><span class=special>, </span><span class=string>&quot;foo.h&quot;</span><span class=special>)
</span></pre></code>
<p>
Beware of non-const global variables: changes in Python won't reflect in C++!
If you really must change them in Python, you will have to write some accessor
functions, and export those.</p>
<table border="0">
<tr>
<td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td>
<td width="30"><a href="smart_pointers.html"><img src="theme/l_arr.gif" border="0"></a></td>
<td width="20"><img src="theme/r_arr_disabled.gif" border="0"></td>
</tr>
</table>
<br>
<hr size="1"><p class="copyright">Copyright &copy; 2003 Bruno da Silva de Oliveira<br>Copyright &copy; 2002-2003 Joel de Guzman<br><br>
<font size="2">Permission to copy, use, modify, sell and distribute this document
is granted provided this copyright notice appears in all copies. This document
is provided &quot;as is&quot; without express or implied warranty, and with
no claim as to its suitability for any purpose. </font> </p>
</body>
</html>

View File

@@ -57,7 +57,7 @@ this will create a file &quot;<tt>hello.cpp</tt>&quot; in the directory where th
run. </p>
<p>
Pyste supports the following features:</p>
<ul><li>Functions</li><li>Classes</li><li>Class Templates</li><li>Virtual Methods</li><li>Overloading</li><li>Attributes </li><li>Enums (both &quot;free&quot; enums and class enums)</li><li>Nested Classes</li><li>Support for <tt>boost::shared_ptr</tt> and <tt>std::auto_ptr</tt></li></ul><table border="0">
<ul><li>Functions</li><li>Classes</li><li>Class Templates</li><li>Virtual Methods</li><li>Overloading</li><li>Attributes </li><li>Enums (both &quot;free&quot; enums and class enums)</li><li>Nested Classes</li><li>Support for <tt>boost::shared_ptr</tt> and <tt>std::auto_ptr</tt></li><li>Global Variables</li></ul><table border="0">
<tr>
<td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td>
<td width="30"><img src="theme/l_arr_disabled.gif" border="0"></td>

View File

@@ -48,6 +48,7 @@ Pyste supports the following features:
* Enums (both "free" enums and class enums)
* Nested Classes
* Support for [^boost::shared_ptr] and [^std::auto_ptr]
* Global Variables
[page Running Pyste]
@@ -465,3 +466,14 @@ For [^std::auto_ptr]'s, use the function [^use_auto_ptr].
This system is temporary, and in the future the converters will automatically be
exported if needed, without the need to tell Pyste about them explicitly.
[page:1 Global Variables]
To export global variables, use the [^Var] construct:
Var("myglobal", "foo.h")
Beware of non-const global variables: changes in Python won't reflect in C++!
If you really must change them in Python, you will have to write some accessor
functions, and export those.

View File

@@ -4,6 +4,7 @@
<title>Smart Pointers</title>
<link rel="stylesheet" href="theme/style.css" type="text/css">
<link rel="prev" href="exporting_all_declarations_from_a_header.html">
<link rel="next" href="global_variables.html">
</head>
<body>
<table width="100%" height="48" border="0" cellspacing="2">
@@ -20,7 +21,7 @@
<tr>
<td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td>
<td width="30"><a href="exporting_all_declarations_from_a_header.html"><img src="theme/l_arr.gif" border="0"></a></td>
<td width="20"><img src="theme/r_arr_disabled.gif" border="0"></td>
<td width="20"><a href="global_variables.html"><img src="theme/r_arr.gif" border="0"></a></td>
</tr>
</table>
<p>
@@ -61,7 +62,7 @@ exported if needed, without the need to tell Pyste about them explicitly.</p>
<tr>
<td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td>
<td width="30"><a href="exporting_all_declarations_from_a_header.html"><img src="theme/l_arr.gif" border="0"></a></td>
<td width="20"><img src="theme/r_arr_disabled.gif" border="0"></td>
<td width="20"><a href="global_variables.html"><img src="theme/r_arr.gif" border="0"></a></td>
</tr>
</table>
<br>

19
pyste/example/vars.h Normal file
View File

@@ -0,0 +1,19 @@
struct Color
{
Color(int r_ = 0, int g_ = 0, int b_ = 0):
r(r_), g(g_), b(b_)
{}
Color( const Color &c):
r(c.r), g(c.g), b(c.b)
{}
int r;
int g;
int b;
};
const Color black = Color(0, 0, 0);
const Color red = Color(255, 0, 0);
const Color green = Color(0, 255, 0);
const Color blue = Color(0, 0, 255);
Color in_use = black;

1
pyste/example/vars.pyste Normal file
View File

@@ -0,0 +1 @@
AllFromHeader('vars.h')

View File

@@ -65,6 +65,11 @@
<a href="doc/smart_pointers.html">Smart Pointers</a>
</td>
</tr>
<tr>
<td class="toc_cells_L1">
<a href="doc/global_variables.html">Global Variables</a>
</td>
</tr>
</table>
<br>
<hr size="1"><p class="copyright">Copyright &copy; 2003 Bruno da Silva de Oliveira<br>Copyright &copy; 2002-2003 Joel de Guzman<br><br>

View File

@@ -751,7 +751,7 @@ class _VirtualWrapperGenerator(object):
all_methods = [x for x in self.class_.members if IsVirtual(x)]
for base in self.bases:
base_methods = [x.Copy() for x in self.bases.members if IsVirtual(x)]
base_methods = [x.Copy() for x in base if IsVirtual(x)]
for base_method in base_methods:
base_method.class_ = self.class_.FullName()
all_methods.append(base_method)

View File

@@ -2,6 +2,7 @@ from Exporter import Exporter
from ClassExporter import ClassExporter
from FunctionExporter import FunctionExporter
from EnumExporter import EnumExporter
from VarExporter import VarExporter
from infos import *
from declarations import *
import os.path
@@ -47,6 +48,7 @@ class HeaderExporter(Exporter):
Class : ClassExporter,
Enumeration : EnumExporter,
Function : FunctionExporter,
Variable : VarExporter,
}
exporter_class = dispatch_table.get(type(decl))

31
pyste/src/VarExporter.py Normal file
View File

@@ -0,0 +1,31 @@
from Exporter import Exporter
from settings import *
#==============================================================================
# VarExporter
#==============================================================================
class VarExporter(Exporter):
'''Exports a global variable.
'''
def __init__(self, info):
Exporter.__init__(self, info)
def Export(self, codeunit, exported_names):
if self.info.exclude: return
decl = self.GetDeclaration(self.info.name)
if not decl.type.const:
msg = '---> Warning: The global variable "%s" is non-const:\n' \
' changes in Python will not reflect in C++.'
print msg % self.info.name
print
rename = self.info.rename or self.info.name
code = self.INDENT + namespaces.python
code += 'scope().attr("%s") = %s;\n' % (rename, self.info.name)
codeunit.Write('module', code)
def Order(self):
return self.info.name

View File

@@ -243,8 +243,8 @@ class Method(Function):
self.name,
self.class_,
self.result,
self.params[:],
self.visib,
self.parameters[:],
self.visibility,
self.virtual,
self.abstract,
self.static,

View File

@@ -10,11 +10,11 @@ from declarations import *
# FunctionWrapper
#==============================================================================
class FunctionWrapper(object):
'''Holds information about a wrapper for a function or a method. It is in 2
parts: the name of the Wrapper, and its code. The code is placed in the
declaration section of the module, while the name is used to def' the
function or method (with the pyste namespace prepend to it). If code is None,
the name is left unchanged.
'''Holds information about a wrapper for a function or a method. It is
divided in 2 parts: the name of the Wrapper, and its code. The code is
placed in the declaration section of the module, while the name is used to
def' the function or method (with the pyste namespace prepend to it). If
code is None, the name is left unchanged.
'''
def __init__(self, name, code=None):

View File

@@ -6,6 +6,7 @@ from FunctionExporter import FunctionExporter
from IncludeExporter import IncludeExporter
from EnumExporter import EnumExporter
from HeaderExporter import HeaderExporter
from VarExporter import VarExporter
from exporterutils import FunctionWrapper
from utils import makeid
@@ -86,7 +87,7 @@ class IncludeInfo(DeclarationInfo):
exporter = IncludeExporter(InfoWrapper(self))
exporters.exporters.append(exporter)
#==============================================================================
# templates
#==============================================================================
@@ -148,6 +149,19 @@ class HeaderInfo(DeclarationInfo):
exporters.exporters.append(exporter)
#==============================================================================
# VarInfo
#==============================================================================
class VarInfo(DeclarationInfo):
def __init__(self, name, include):
DeclarationInfo.__init__(self)
self._Attribute('name', name)
self._Attribute('include', include)
exporter = VarExporter(InfoWrapper(self))
exporters.exporters.append(exporter)
#==============================================================================
# InfoWrapper
#==============================================================================

View File

@@ -2,9 +2,12 @@
Pyste version %s
Usage:
pyste [options] --module=<name> interface-files
pyste [options] interface-files
where options are:
--module=<name> the name of the module that will be generated.
Defaults to the first interface filename, without
the extension.
-I <path> add an include path
-D <symbol> define symbol
--multiple create various cpps, instead of only one
@@ -34,7 +37,7 @@ from policies import *
from CppParser import CppParser, CppParserError
import time
__VERSION__ = '0.7.9'
__VERSION__ = '0.8.0'
def RecursiveIncludes(include):
'Return a list containg the include dir and all its subdirectories'
@@ -104,8 +107,10 @@ def ParseArguments():
print 'Unknown option:', opt
Usage()
if not files or not module:
Usage()
if not files:
Usage()
if not module:
module = os.path.splitext(files[0])[0]
if not out:
out = module
if not multiple:
@@ -123,6 +128,7 @@ def CreateContext():
context['Template'] = infos.ClassTemplateInfo
context['Enum'] = infos.EnumInfo
context['AllFromHeader'] = infos.HeaderInfo
context['Var'] = infos.VarInfo
# functions
context['rename'] = infos.rename
context['set_policy'] = infos.set_policy

View File

@@ -0,0 +1,17 @@
import unittest
import vars
class VarsTest(unittest.TestCase):
def testIt(self):
def testColor(c, r, g, b):
self.assertEqual(c.r, r)
self.assertEqual(c.g, g)
self.assertEqual(c.b, b)
testColor(vars.black, 0, 0, 0)
testColor(vars.red, 255, 0, 0)
testColor(vars.green, 0, 255, 0)
testColor(vars.blue, 0, 0, 255)

View File

@@ -13,3 +13,4 @@ call nt_build_pyste.bat virtual2 %1
call nt_build_pyste.bat wrappertest %1
call nt_build_pyste.bat opaque %1
call nt_build_pyste.bat inherit %1
call nt_build_pyste.bat vars %1