Skip to content

Commit

Permalink
added boundschecks and better reporting of out of bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
louisponet committed Jan 20, 2023
1 parent eac3409 commit b4a776b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
29 changes: 21 additions & 8 deletions src/component.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ performing checks, i.e. when using [`Groups`](@ref Groups).
"""
function swap_order!(c::AbstractComponent, e1::AbstractEntity, e2::AbstractEntity)
@boundscheck if !in(e1, c)
throw(BoundsError(c, e1))
throw(BoundsError(c, Entity(e1)))
elseif !in(e2, c)
throw(BoundsError(c, e2))
throw(BoundsError(c, Entity(e2)))
end
@inbounds begin
id1, id2 = swap_order!(c.indices, e1.id, e2.id)
Expand Down Expand Up @@ -131,9 +131,16 @@ Component{T}() where {T} = Component(Indices(), T[])
entity_data(c::Component) = c.data

##### BASE Extensions ####
Base.@propagate_inbounds @inline Base.getindex(c::Component, e::AbstractEntity) = c.data[c.indices[e.id]]
Base.@propagate_inbounds @inline Base.getindex(c::Component, i::Integer) = c.data[i]

@inline function Base.getindex(c::Component, e::AbstractEntity)
eid = e.id
@boundscheck if !in(e, c)
throw(BoundsError(c, Entity(e)))
end
return @inbounds c.data[c.indices[eid]]
end

@inline function Base.setindex!(c::Component{T}, v::T, e::AbstractEntity) where {T}
eid = e.id
@boundscheck if !in(e, c)
Expand All @@ -153,7 +160,7 @@ end

function Base.pop!(c::Component, e::AbstractEntity)
@boundscheck if !in(e, c)
throw(BoundsError(c, e))
throw(BoundsError(c, Entity(e)))
end
@inbounds begin
id = c.indices[e.id]
Expand Down Expand Up @@ -242,9 +249,15 @@ entity_data(c::PooledComponent) = c.pool

npools(c::PooledComponent) = length(c.data)

Base.@propagate_inbounds @inline Base.getindex(c::PooledComponent, e::AbstractEntity) = c.data[pool(c, e)]
Base.@propagate_inbounds @inline Base.getindex(c::PooledComponent, i::Integer) = c.data[c.pool[i]]

@inline function Base.getindex(c::PooledComponent, e::AbstractEntity)
@boundscheck if !in(e, c)
throw(BoundsError(c, Entity(e)))
end
return @inbounds c.data[pool(c, e)]
end

Base.@propagate_inbounds @inline function Base.parent(c::PooledComponent, i::Int)
@boundscheck if i > length(c.data)
throw(BoundsError(c, i))
Expand Down Expand Up @@ -285,7 +298,7 @@ end
# set the value of this entity to that of parent
@inline function Base.setindex!(c::PooledComponent, p::AbstractEntity, e::AbstractEntity)
@boundscheck if !in(p, c)
throw(BoundsError(c, p))
throw(BoundsError(c, Entity(p)))
end
@inbounds begin
pg = pool(c, p)
Expand Down Expand Up @@ -320,7 +333,7 @@ end
@inline function Base.setindex!(c::PooledComponent{T}, v::T, x::ApplyToPool) where {T}
e = x.e
@boundscheck if !in(e, c)
throw(BoundsError(c, e))
throw(BoundsError(c, Entity(e)))
end
@inbounds c.data[pool(c, e.id)] = v
return v
Expand Down Expand Up @@ -348,7 +361,7 @@ end

function Base.pop!(c::PooledComponent, e::AbstractEntity)
@boundscheck if !in(e, c)
throw(BoundsError(c, e))
throw(BoundsError(c, Entity(e)))
end

@inbounds begin
Expand Down
2 changes: 2 additions & 0 deletions src/entity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ function Entity(m::AbstractLedger, parent::Entity, datas...)
return e
end

Entity(e::Entity) = e

Base.iterate(e::Entity, state=1) = state > 1 ? nothing : (e, state+1)

const EMPTY_ENTITY = Entity(0)
Expand Down
2 changes: 1 addition & 1 deletion src/ledger.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ stage(m::AbstractLedger, name) = stage(ledger(m), name)
valid_entities(m::AbstractLedger) = filter(x -> x.id != 0, entities(m))
stages(m::AbstractLedger) = ledger(m).stages
groups(m::AbstractLedger) = ledger(m).groups
singleton(m::AbstractLedger, ::Type{T}) where {T} = m[T][1]
singleton(m::AbstractLedger, ::Type{T}) where {T} = EntityState(entity(m[T], 1), m[T])

##### BASE Extensions ####
function Base.show(io::IO, l::AbstractLedger)
Expand Down

0 comments on commit b4a776b

Please sign in to comment.