#Include reference of owner at creation

1 messages · Page 1 of 1 (latest)

thorny karma
#

In my codebase I end up running a lot in the following pattern:

pub const A = struct {
    child: B,
}

pub const B = struct {
    parent: *A,
}

is there an elegant way to initialize both strucutres (preferably B first and then A) or is my design just bad? at the moment i have something similar to:

pub const B = struct {
    parent: ?*A = null,
}

so that i can initialize b, pass it to a on creation and then set it's parent, but i dont think this is the best way to do it and i also have to deal with the null checks every time.

unborn gorge
#

if the child is contained directly in the parent (i.e. not through a pointer), there is no reason to hold a pointer to the parent in the child, as it can be computed via @fieldParentPtr. if a parent always exists, and the child is held remotely, I'd recommend using a plain pointer type from the child to the parent, initially setting it to undefined, and overriding it as soon as possible.

though, the best case is to redesign your system so as to not need pointers from children to parents

thorny karma
#

I am not quire sure I understand how @fieldParentPtr(comptime field_name: []const u8, field_ptr: *T) anytype works. I am writing an emulator and I have a few interlocked components (for example, I would store the stack on the cpu as a field so i could do cpu.stack... but then i need a reference to the cpu in stack so i can access the mmu which is also stored similarly on the cpu). I am not quire sure what a better design would be in this case

unborn gorge
#

well, if you need access to the whole cpu to access its mmu... just pass the whole cpu to that function. what reason do you have to want to only pass cpu.stack?

thorny karma
#

the function is on cpu.stack so i was looking to access other cpu fields without having to pass the cpu to each function on the stack (by having the cpu pointer as a field in stack)

unborn gorge
#

if you need to access other fields, you likely shouldn't be taking in just the cpu.stack.

thorny karma
#

i am not quite sure what you mean, the function is on the stack class so it takes self: *Stack as parameter, but i also need access to other components. The main issue i have with passing *cpu as a parameter is that i run into situation like the following: on the mmu (by this i mean functions inside the mmu struct whom take *MMU as first parameter) i have a few read/write functions that are used extensivly through the codebase and which had no need for a cpu pointer (so i did not add it to the signature) until recently when i addded a new case to the read function which created a need for the pointer, my solution was to add the pointer as a field on the mmu so that i do not need to modify the signature and then have to go back through the whole source code to add the new parameter, but i run into the issue described above.

unborn gorge
#

the function is on the stack class so it takes self: *Stack as parameter, but i also need access to other components
if you need to access components besides the stack - don't make the function a method on Stack!

thorny karma
#

but it's mainly stack related.?
push16, pop16

#

they both need to access registers (a diffrent struct on the cpu and memory (MMU))

unborn gorge
thorny karma
#

I am not quite sure I understand this, for example, this happens with the MMU struct (memory management unit) as well, i have read write on the MMU which need other fields on the cpu, like the registers struct (mmu is a field in the cpu struct, registers is a field in the cpu struct, both are also structs)

unborn gorge
#

then there too it seems like you put the function in the wrong place. if you need to access more than one part of cpu - don't ask for one part of the cpu!

thorny karma
#

the mmu is a field on the cpu, i dont ask for a part of the cpu, i ask for itself, if i need access to other parts of the cpu i ask for the whole cpu.

unborn gorge
#

you said yourself that you need access to other parts of the cpu - if that's the case, don't make the function a method on the mmu

thorny karma
#

but i also need access to the internals of mmu

unborn gorge
#

if you get the whole cpu, you can transitively access the internals of the mmu