[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Brainstorming on recursive class definitions

Hi group,

so I'm having a problem that I'd like to solve *nicely*. I know plenty
of ways to solve it, but am curious if there's a solution that allows me
to write the solution in a way that is most comfortable for the user.

I'm trying to map registers of a processor. So assume you have a n bit
address space, the processor might have duplicate units of identical
functionality mapped at different places in memory. For example, assume
there's a GPIO unit that has registers FOO, BAR and KOO and two GPIO
ports GPIOA and GPIOB. I'd like to write code along the lines of this:

class GpioMap(BaseRegisterMap):
	FOO = 0x0
	BAR = 0x4
	KOO = 0x8

class CPURegisterMap(BaseRegisterMap):
	GPIOA = GpioMap(0x10000)
	GPIOB = GpioMap(0x20000)

cpu = CPURegisterMap(0x80000000)

assert(cpu.addr == 0x80000000)
assert(cpu.GPIOA.addr == 0x80000000 + 0x10000)
assert(cpu.GPIOB.addr == 0x80000000 + 0x20000)
assert(cpu.GPIOA.FOO.addr == 0x80000000 + 0x10000 + 0x0)
assert(cpu.GPIOA.KOO.addr == 0x80000000 + 0x10000 + 0x8)
assert(cpu.GPIOB.BAR.addr == 0x80000000 + 0x20000 + 0x4)

So, obviously, FOO, BAR and KOO are of type "int" without any "addr"
property, so there would need to be some magic there. Additionally,
through some way the instanciation of GpioMap() would need the knowledge
of its parent base, which I'm not sure is even possible. Maybe (that's
what I'm currently trying to get right) the __getattribute__ would
propagate the information about the accumulated parent's base address to
the child during lookup.

Anyways, I'm looking for your ideas on how to solve such a thing
"nicely". Note that "BaseRegisterMap" is allowed to do dirty things as
long as the definition code has a clean look & feel.


>> Wo hattest Du das Beben nochmal GENAU vorhergesagt?
> Zumindest nicht ?ffentlich!
Ah, der neueste und bis heute genialste Streich unsere gro?en
Kosmologen: Die Geheim-Vorhersage.
 - Karl Kaos ?ber R?diger Thomas in dsa <hidbv3$om2$1 at speranza.aioe.org>