1 """Type classes and a modest collection of standard types."""
4 from bgenOutput
import *
9 """Define the various things you can do with a C type.
11 Most methods are intended to be extended or overridden.
14 def __init__(self
, typeName
, fmt
):
15 """Call with the C name and getargs format for the type.
17 Example: int = Type("int", "i")
19 self
.typeName
= typeName
22 def declare(self
, name
):
23 """Declare a variable of the type with a given name.
25 Example: int.declare('spam') prints "int spam;"
27 Output("%s %s;", self
.typeName
, name
)
30 return self
.getargsFormat(), self
.getargsArgs()
32 def getargsFormat(self
):
33 """Return the format for this type for use with [new]getargs().
35 Example: int.getargsFormat() returns the string "i".
39 def getargsArgs(self
, name
):
40 """Return an argument for use with [new]getargs().
42 Example: int.getargsArgs("spam") returns the string "&spam".
46 def getargsCheck(self
, name
):
47 """Perform any needed post-[new]getargs() checks.
49 This is type-dependent; the default does not check for errors.
50 An example would be a check for a maximum string length."""
52 def passInput(self
, name
):
53 """Return an argument for passing a variable into a call.
55 Example: int.passInput("spam") returns the string "spam".
59 def passOutput(self
, name
):
60 """Return an argument for returning a variable out of a call.
62 Example: int.passOutput("spam") returns the string "&spam".
66 def errorCheck(self
, name
):
67 """Check for an error returned in the variable.
69 This is type-dependent; the default does not check for errors.
70 An example would be a check for a NULL pointer.
71 If an error is found, the generated routine should
72 raise an exception and return NULL.
74 XXX There should be a way to add error clean-up code.
76 Output("/* XXX no err check for %s %s */", self
.typeName
, name
)
79 return self
.mkvalueFormat(), self
.mkvalueArgs()
81 def mkvalueFormat(self
):
82 """Return the format for this type for use with mkvalue().
84 This is normally the same as getargsFormat() but it is
85 a separate function to allow future divergence.
87 return self
.getargsFormat()
89 def mkvalueArgs(self
, name
):
90 """Return an argument for use with mkvalue().
92 Example: int.mkvalueArgs("spam") returns the string "spam".
96 def cleanup(self
, name
):
97 """Clean up if necessary.
99 This is normally empty; it may deallocate buffers etc.
103 class ByAddressType(Type
):
104 "Simple type that is also passed by address for input"
106 def passInput(self
, name
):
111 # Sometimes it's useful to define a type that's only usable as input or output parameter
113 class InputOnlyMixIn
:
115 "Mix-in class to boobytrap passOutput"
117 def passOutput(self
, name
):
118 raise RuntimeError, "this type can only be used for input parameters"
120 class InputOnlyType(InputOnlyMixIn
, Type
):
122 "Same as Type, but only usable for input parameters -- passOutput is boobytrapped"
124 class OutputOnlyMixIn
:
126 "Mix-in class to boobytrap passInput"
128 def passInput(self
, name
):
129 raise RuntimeError, "this type can only be used for output parameters"
131 class OutputOnlyType(OutputOnlyMixIn
, Type
):
133 "Same as Type, but only usable for output parameters -- passInput is boobytrapped"
136 # A modest collection of standard C types.
138 char
= Type("char", "c")
139 short
= Type("short", "h")
140 unsigned_short
= Type("unsigned short", "H")
141 int = Type("int", "i")
142 long = Type("long", "l")
143 unsigned_long
= Type("unsigned long", "l")
144 float = Type("float", "f")
145 double
= Type("double", "d")
148 # The most common use of character pointers is a null-terminated string.
149 # For input, this is easy. For output, and for other uses of char *,
150 # see the module bgenBuffer.
151 stringptr
= InputOnlyType("char*", "s")
154 # Some Python related types.
155 objectptr
= Type("PyObject*", "O")
156 stringobjectptr
= Type("PyStringObject*", "S")
160 class FakeType(InputOnlyType
):
162 """A type that is not represented in the Python version of the interface.
164 Instantiate with a value to pass in the call.
167 def __init__(self
, substitute
):
168 self
.substitute
= substitute
169 self
.typeName
= None # Don't show this argument in __doc__ string
171 def declare(self
, name
):
174 def getargsFormat(self
):
177 def getargsArgs(self
, name
):
180 def passInput(self
, name
):
181 return self
.substitute
184 class OpaqueType(Type
):
186 """A type represented by an opaque object type, always passed by address.
188 Instantiate with the type name and the names of the new and convert procs.
189 If fewer than three arguments are passed, the second argument is used
190 to derive the new and convert procs by appending _New and _Convert; it
191 defaults to the first argument.
194 def __init__(self
, name
, arg
= None, extra
= None):
197 # Two arguments (name, usetype) or one (name)
199 self
.new
= arg
+ '_New'
200 self
.convert
= arg
+ '_Convert'
202 # Three arguments (name, new, convert)
206 def getargsFormat(self
):
209 def getargsArgs(self
, name
):
210 return "%s, &%s" % (self
.convert
, name
)
212 def passInput(self
, name
):
215 def mkvalueFormat(self
):
218 def mkvalueArgs(self
, name
):
219 return "%s, &%s" % (self
.new
, name
)
222 class OpaqueByValueType(OpaqueType
):
224 """A type represented by an opaque object type, on input passed BY VALUE.
226 Instantiate with the type name, and optionally an object type name whose
227 New/Convert functions will be used.
230 def passInput(self
, name
):
233 def mkvalueArgs(self
, name
):
234 return "%s, %s" % (self
.new
, name
)
237 class OpaqueArrayType(OpaqueByValueType
):
239 """A type represented by an opaque object type, with ARRAY passing semantics.
241 Instantiate with the type name, and optional an object type name whose
242 New/Convert functions will be used.
245 def getargsArgs(self
, name
):
246 return "%s, %s" % (self
.convert
, name
)
248 def passOutput(self
, name
):