I want to make a map that can store pointers to any type. Is it possible?
For example:
import "core:fmt"
CHUNK_SIZE :: 100
Position :: struct {
x, y : int
}
Center :: struct {
cx, cy: int
}
Component :: union {
Position,
Center
}
main :: proc() {
pos_type : typeid = typeid_of(Position)
center_type : typeid = typeid_of(Center)
types := new(map[typeid]^[CHUNK_SIZE]Component)
types[typeid_of(Position)] = new([CHUNK_SIZE]Component)
types[typeid_of(Center)] = new([CHUNK_SIZE]Component)
positions := types[pos_type]
centers := types[center_type]
positions[0] = Position { 10, 20 }
centers[0] = Center { 5, 7 }
free(types[pos_type])
free(types[center_type])
free(types)
}
It works, but it will waste a memory.
If I could to use something like ^any instead of Component…
Or maybe point what exactly union type should be used when allocate with new…
But from the other perspective if not to make big components and keep them small it will be faster to allocate all chunk of unions instead of separate allocation of each component and storing the pointer in the map…