I've been working on this in other places so it's a little ahead of it's time,
but it still has a few minor issues... heh
here's an example to get started:
x = struct( 12, # struct size
'x,y,z', # variable order
x = bf32,
y = bf32,
z = bf32
)
# working stuff:
d = x()
d2 = x()
#dptr, d2ptr = ref( d, d2 ) # planned for future
dptr = ref( d )
d2ptr = ref( d2 )
deref( dptr, x ).set( 1.0, 1.0, 1.0 )
deref( dptr, x ).x.set( 2.0 )
# type testing (everything passes):
print 'test 1:', ['failed','passed'][type(x) == structType], '( type(x) == structType )'
print 'test 2:', ['failed','passed'][type(d) == x], '( type(d) == x )'
print 'test 3:', ['failed','passed'][type(d.x) == bf32], '( type(d.x) == bf32 )'
print 'test 4:', ['failed','passed'][type(d) == type(d2)], '( type(d) == type(d2) )'
print 'test 5:', ['failed','passed'][type(d) != type(header)], '( type(d) != type(header) )\n'
again, this is for file R/W, not RAM
anyways... the issue it hand is the variable order in structs...
I want to get rid of it, but I'm not sure how... :/
here's the code for struct():
class _STRUCT(object):
import FILE
def __init__(self): pass
def __call__(self, size, order, **names):
#print size, names
class _SUB_STRUCT(object):
_names = names
_order = order #<-- TODO: get rid of this (perhapse with a hack) (we need the raw order)
_addr = None
_size = size
def __init__(this, *values):
#print this._names
this._addr = self.FILE._current.offset # set the address of this struct
write = bool(len(values))
skip = this._size
for vi,name in enumerate(this._order.split(',')):
this.__dict__[name] = this._names[name](values[vi] if write else '')
skip -= this.__dict__[name]._size
self.FILE._current.offset+=skip
self.FILE.DATA._TypesList.append(_SUB_STRUCT)
return _SUB_STRUCT
global struct; struct = _STRUCT()
global structType; structType = type
you may also notice the structType isn't exactly what I want either
that was just something I had to do to get type(x) working... heh
(taken from the NoneType thing)